mirror of
https://github.com/csunny/DB-GPT.git
synced 2025-09-08 04:23:35 +00:00
feat: Add dbgpt client and add api v2
This commit is contained in:
0
examples/client/__init__.py
Normal file
0
examples/client/__init__.py
Normal file
35
examples/client/app_crud_example.py
Normal file
35
examples/client/app_crud_example.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import asyncio
|
||||
|
||||
from dbgpt.client.app import list_app
|
||||
from dbgpt.client.client import Client
|
||||
|
||||
"""
|
||||
Client: Simple App CRUD example
|
||||
|
||||
This example demonstrates how to use the dbgpt client to get, list apps.
|
||||
Example:
|
||||
.. code-block:: python
|
||||
|
||||
DBGPT_API_KEY = "dbgpt"
|
||||
client = Client(api_key=DBGPT_API_KEY)
|
||||
# 1. List all apps
|
||||
res = await list_app(client)
|
||||
# 2. Get an app
|
||||
res = await get_app(
|
||||
client, app_id="bf1c7561-13fc-4fe0-bf5d-c22e724766a8"
|
||||
)
|
||||
|
||||
"""
|
||||
|
||||
|
||||
async def main():
|
||||
# initialize client
|
||||
|
||||
DBGPT_API_KEY = "dbgpt"
|
||||
client = Client(api_key=DBGPT_API_KEY)
|
||||
res = await list_app(client)
|
||||
print(res.json())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
73
examples/client/client_chat_example.py
Normal file
73
examples/client/client_chat_example.py
Normal file
@@ -0,0 +1,73 @@
|
||||
import asyncio
|
||||
|
||||
from dbgpt.client.client import Client
|
||||
|
||||
"""
|
||||
Client: Simple Chat example
|
||||
|
||||
This example demonstrates how to use the dbgpt client to chat with the chatgpt model.
|
||||
|
||||
Example:
|
||||
.. code-block:: python
|
||||
|
||||
DBGPT_API_KEY = "dbgpt"
|
||||
# chat with stream
|
||||
client = Client(api_key=DBGPT_API_KEY)
|
||||
|
||||
# 1. chat normal
|
||||
async for data in client.chat_stream(
|
||||
model="chatgpt_proxyllm",
|
||||
messages="hello",
|
||||
):
|
||||
print(data.dict())
|
||||
|
||||
# chat with no stream
|
||||
res = await client.chat(model="chatgpt_proxyllm", messages="Hello?")
|
||||
print(res.json())
|
||||
|
||||
# 2. chat with app
|
||||
async for data in client.chat_stream(
|
||||
model="chatgpt_proxyllm",
|
||||
chat_mode="chat_app",
|
||||
chat_param="${app_code}",
|
||||
messages="hello",
|
||||
):
|
||||
print(data.dict())
|
||||
|
||||
# 3. chat with knowledge
|
||||
async for data in client.chat_stream(
|
||||
model="chatgpt_proxyllm",
|
||||
chat_mode="chat_knowledge",
|
||||
chat_param="${space_name}",
|
||||
messages="hello",
|
||||
):
|
||||
print(data.dict())
|
||||
|
||||
# 4. chat with flow
|
||||
async for data in client.chat_stream(
|
||||
model="chatgpt_proxyllm",
|
||||
chat_mode="chat_flow",
|
||||
chat_param="${flow_id}",
|
||||
messages="hello",
|
||||
):
|
||||
print(data.dict())
|
||||
"""
|
||||
|
||||
|
||||
async def main():
|
||||
# initialize client
|
||||
DBGPT_API_KEY = "dbgpt"
|
||||
client = Client(api_key=DBGPT_API_KEY)
|
||||
|
||||
async for data in client.chat_stream(
|
||||
model="chatgpt_proxyllm",
|
||||
messages="hello",
|
||||
):
|
||||
print(data)
|
||||
|
||||
# res = await client.chat(model="chatgpt_proxyllm" ,messages="hello")
|
||||
# print(res)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
48
examples/client/flow_crud_example.py
Normal file
48
examples/client/flow_crud_example.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import asyncio
|
||||
|
||||
from dbgpt.client.app import list_app
|
||||
from dbgpt.client.client import Client
|
||||
from dbgpt.client.flow import list_flow
|
||||
|
||||
"""
|
||||
Client: Simple Flow CRUD example
|
||||
|
||||
This example demonstrates how to use the dbgpt client to create, get, update, and delete flows.
|
||||
Example:
|
||||
.. code-block:: python
|
||||
|
||||
DBGPT_API_KEY = "dbgpt"
|
||||
client = Client(api_key=DBGPT_API_KEY)
|
||||
# 1. Create a flow
|
||||
res = await create_flow(
|
||||
client,
|
||||
FlowPanel(name="test_flow", desc="for client flow", owner="dbgpt"),
|
||||
)
|
||||
# 2. Update a flow
|
||||
res = await update_flow(
|
||||
client,
|
||||
FlowPanel(name="test_flow", desc="for client flow333", owner="dbgpt"),
|
||||
)
|
||||
# 3. Delete a flow
|
||||
res = await delete_flow(
|
||||
client, flow_id="bf1c7561-13fc-4fe0-bf5d-c22e724766a8"
|
||||
)
|
||||
# 4. Get a flow
|
||||
res = await get_flow(client, flow_id="bf1c7561-13fc-4fe0-bf5d-c22e724766a8")
|
||||
# 5. List all flows
|
||||
res = await list_flow(client)
|
||||
|
||||
"""
|
||||
|
||||
|
||||
async def main():
|
||||
# initialize client
|
||||
|
||||
DBGPT_API_KEY = "dbgpt"
|
||||
client = Client(api_key=DBGPT_API_KEY)
|
||||
res = await list_flow(client)
|
||||
print(res.json())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
109
examples/client/knowledge_crud_example.py
Normal file
109
examples/client/knowledge_crud_example.py
Normal file
@@ -0,0 +1,109 @@
|
||||
import asyncio
|
||||
|
||||
from dbgpt.client.client import Client
|
||||
from dbgpt.client.knowledge import list_space
|
||||
|
||||
"""Client: Simple Knowledge CRUD example
|
||||
|
||||
This example demonstrates how to use the dbgpt client to create, get, update, and delete knowledge spaces and documents.
|
||||
Example:
|
||||
.. code-block:: python
|
||||
|
||||
DBGPT_API_KEY = "dbgpt"
|
||||
client = Client(api_key=DBGPT_API_KEY)
|
||||
# 1. Create a space
|
||||
res = await create_space(
|
||||
client,
|
||||
SpaceModel(
|
||||
name="test_space",
|
||||
vector_type="Chroma",
|
||||
desc="for client space",
|
||||
owner="dbgpt",
|
||||
),
|
||||
)
|
||||
# 2. Update a space
|
||||
res = await update_space(
|
||||
client,
|
||||
SpaceModel(
|
||||
name="test_space",
|
||||
vector_type="Chroma",
|
||||
desc="for client space333",
|
||||
owner="dbgpt",
|
||||
),
|
||||
)
|
||||
# 3. Delete a space
|
||||
res = await delete_space(client, space_id="37")
|
||||
# 4. Get a space
|
||||
res = await get_space(client, space_id="5")
|
||||
# 5. List all spaces
|
||||
res = await list_space(client)
|
||||
# 6. Create a document
|
||||
res = await create_document(
|
||||
client,
|
||||
DocumentModel(
|
||||
space_id="5",
|
||||
doc_name="test_doc",
|
||||
doc_type="TEXT",
|
||||
doc_content="test content",
|
||||
doc_source="",
|
||||
),
|
||||
)
|
||||
# 7. Sync a document
|
||||
res = await sync_document(
|
||||
client,
|
||||
sync_model=SyncModel(
|
||||
doc_id="153",
|
||||
space_id="40",
|
||||
model_name="text2vec",
|
||||
chunk_parameters=ChunkParameters(chunk_strategy="Automatic"),
|
||||
),
|
||||
)
|
||||
# 8. Get a document
|
||||
res = await get_document(client, "52")
|
||||
# 9. List all documents
|
||||
res = await list_document(client)
|
||||
# 10. Delete a document
|
||||
res = await delete_document(client, "150")
|
||||
"""
|
||||
|
||||
|
||||
async def main():
|
||||
|
||||
DBGPT_API_KEY = "dbgpt"
|
||||
client = Client(api_key=DBGPT_API_KEY)
|
||||
|
||||
# list all spaces
|
||||
res = await list_space(client)
|
||||
print(res)
|
||||
|
||||
# get space
|
||||
# res = await get_space(client, space_id='5')
|
||||
|
||||
# create space
|
||||
# res = await create_space(client, SpaceModel(name="test_space", vector_type="Chroma", desc="for client space", owner="dbgpt"))
|
||||
|
||||
# update space
|
||||
# res = await update_space(client, SpaceModel(name="test_space", vector_type="Chroma", desc="for client space333", owner="dbgpt"))
|
||||
|
||||
# delete space
|
||||
# res = await delete_space(client, space_id='37')
|
||||
|
||||
# list all documents
|
||||
# res = await list_document(client)
|
||||
|
||||
# get document
|
||||
# res = await get_document(client, "52")
|
||||
|
||||
# delete document
|
||||
# res = await delete_document(client, "150")
|
||||
|
||||
# create document
|
||||
# res = await create_document(client, DocumentModel(space_id="5", doc_name="test_doc", doc_type="test", doc_content="test content"
|
||||
# , doc_file=('your_file_name', open('{your_file_path}', 'rb'))))
|
||||
|
||||
# sync document
|
||||
# res = await sync_document(client, sync_model=SyncModel(doc_id="153", space_id="40", model_name="text2vec", chunk_parameters=ChunkParameters(chunk_strategy="Automatic")))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
Reference in New Issue
Block a user