diff --git a/dbgpt/client/__init__.py b/dbgpt/client/__init__.py index be187a027..7ddf7e9eb 100644 --- a/dbgpt/client/__init__.py +++ b/dbgpt/client/__init__.py @@ -1 +1,5 @@ """This module is the client of the dbgpt package.""" + +from .client import Client, ClientException # noqa: F401 + +__ALL__ = ["Client", "ClientException"] diff --git a/dbgpt/client/app.py b/dbgpt/client/app.py index 594576cb0..f90a9906b 100644 --- a/dbgpt/client/app.py +++ b/dbgpt/client/app.py @@ -1,7 +1,7 @@ """App Client API.""" from typing import List -from dbgpt.client.client import Client, ClientException +from dbgpt.client import Client, ClientException from dbgpt.client.schemas import AppModel from dbgpt.serve.core import Result diff --git a/dbgpt/client/client.py b/dbgpt/client/client.py index b2c7f40ad..60d46ba9c 100644 --- a/dbgpt/client/client.py +++ b/dbgpt/client/client.py @@ -1,5 +1,6 @@ """This module contains the client for the DB-GPT API.""" import json +import os from typing import Any, AsyncGenerator, List, Optional, Union from urllib.parse import urlparse @@ -50,7 +51,7 @@ class Client: def __init__( self, - api_base: str = "http://localhost:5000", + api_base: Optional[str] = None, api_key: Optional[str] = None, version: str = "v2", timeout: Optional[httpx._types.TimeoutTypes] = 120, @@ -59,7 +60,7 @@ class Client: Args: api_base: Optional[str], a full URL for the DB-GPT API. - Defaults to the `http://localhost:5000`. + Defaults to the `http://localhost:5000/api/v2`. api_key: Optional[str], The dbgpt api key to use for authentication. Defaults to None. timeout: Optional[httpx._types.TimeoutTypes]: The timeout to use. @@ -73,20 +74,25 @@ class Client: -------- .. code-block:: python - from dbgpt.client.client import Client + from dbgpt.client import Client - DBGPT_API_BASE = "http://localhost:5000" + DBGPT_API_BASE = "http://localhost:5000/api/v2" DBGPT_API_KEY = "dbgpt" client = Client(api_base=DBGPT_API_BASE, api_key=DBGPT_API_KEY) client.chat(model="chatgpt_proxyllm", messages="Hello?") """ + if not api_base: + api_base = os.getenv( + "DBGPT_API_BASE", f"http://localhost:5000/{CLIENT_API_PATH}/{version}" + ) + if not api_key: + api_key = os.getenv("DBGPT_API_KEY") if api_base and is_valid_url(api_base): - self._api_url = api_base.rstrip("/") + self._api_url = api_base else: raise ValueError(f"api url {api_base} does not exist or is not accessible.") self._api_key = api_key self._version = version - self._api_url = api_base + CLIENT_API_PATH + "/" + version self._timeout = timeout headers = {"Authorization": f"Bearer {self._api_key}"} if self._api_key else {} self._http_client = httpx.AsyncClient( @@ -135,9 +141,9 @@ class Client: -------- .. code-block:: python - from dbgpt.client.client import Client + from dbgpt.client import Client - DBGPT_API_BASE = "http://localhost:5000" + DBGPT_API_BASE = "http://localhost:5000/api/v2" DBGPT_API_KEY = "dbgpt" client = Client(api_base=DBGPT_API_BASE, api_key=DBGPT_API_KEY) res = await client.chat(model="chatgpt_proxyllm", messages="Hello?") @@ -210,9 +216,9 @@ class Client: -------- .. code-block:: python - from dbgpt.client.client import Client + from dbgpt.client import Client - DBGPT_API_BASE = "http://localhost:5000" + DBGPT_API_BASE = "http://localhost:5000/api/v2" DBGPT_API_KEY = "dbgpt" client = Client(api_base=DBGPT_API_BASE, api_key=DBGPT_API_KEY) res = await client.chat_stream(model="chatgpt_proxyllm", messages="Hello?") @@ -257,7 +263,6 @@ class Client: error = await response.aread() yield json.loads(error) except Exception as e: - yield f"data:[SERVER_ERROR]{str(e)}\n\n" async def get(self, path: str, *args): @@ -274,7 +279,6 @@ class Client: ) return response finally: - await self._http_client.aclose() async def post(self, path: str, args): diff --git a/dbgpt/client/flow.py b/dbgpt/client/flow.py index 489b99ae2..eda995da6 100644 --- a/dbgpt/client/flow.py +++ b/dbgpt/client/flow.py @@ -1,7 +1,7 @@ """this module contains the flow client functions.""" from typing import List -from dbgpt.client.client import Client, ClientException +from dbgpt.client import Client, ClientException from dbgpt.core.awel.flow.flow_factory import FlowPanel from dbgpt.serve.core import Result diff --git a/dbgpt/client/knowledge.py b/dbgpt/client/knowledge.py index 5ed1cdd71..323b51fcb 100644 --- a/dbgpt/client/knowledge.py +++ b/dbgpt/client/knowledge.py @@ -2,7 +2,7 @@ import json from typing import List -from dbgpt.client.client import Client, ClientException +from dbgpt.client import Client, ClientException from dbgpt.client.schemas import DocumentModel, SpaceModel, SyncModel from dbgpt.serve.core import Result diff --git a/docs/docs/api/app.md b/docs/docs/api/app.md index 3d7a9953b..013b4e297 100644 --- a/docs/docs/api/app.md +++ b/docs/docs/api/app.md @@ -42,7 +42,7 @@ import TabItem from '@theme/TabItem'; ```python -from dbgpt.client.client import Client +from dbgpt.client import Client DBGPT_API_KEY = "dbgpt" APP_ID="{YOUR_APP_ID}" @@ -94,7 +94,7 @@ curl -X GET "http://localhost:5000/api/v2/serve/apps/$APP_ID" -H "Authorization: ```python -from dbgpt.client.client import Client +from dbgpt.client import Client from dbgpt.client.app import get_app DBGPT_API_KEY = "dbgpt" @@ -146,7 +146,7 @@ curl -X GET 'http://localhost:5000/api/v2/serve/apps' -H "Authorization: Bearer ```python -from dbgpt.client.client import Client +from dbgpt.client import Client from dbgpt.client.app import list_app DBGPT_API_KEY = "dbgpt" diff --git a/docs/docs/api/chat.md b/docs/docs/api/chat.md index 55284a28e..6cf3d2621 100644 --- a/docs/docs/api/chat.md +++ b/docs/docs/api/chat.md @@ -42,7 +42,7 @@ import TabItem from '@theme/TabItem'; ```python -from dbgpt.client.client import Client +from dbgpt.client import Client DBGPT_API_KEY = "dbgpt" @@ -104,7 +104,7 @@ data: [DONE] ```python -from dbgpt.client.client import Client +from dbgpt.client import Client DBGPT_API_KEY = "dbgpt" client = Client(api_key=DBGPT_API_KEY) diff --git a/docs/docs/api/flow.md b/docs/docs/api/flow.md index ae649839b..7e63e93cf 100644 --- a/docs/docs/api/flow.md +++ b/docs/docs/api/flow.md @@ -42,7 +42,7 @@ curl -X POST "http://localhost:5000/api/v2/chat/completions" \ ```python -from dbgpt.client.client import Client +from dbgpt.client import Client DBGPT_API_KEY = "dbgpt" FLOW_ID="{YOUR_FLOW_ID}" @@ -115,7 +115,7 @@ FLOW_ID={YOUR_FLOW_ID} ```python -from dbgpt.client.client import Client +from dbgpt.client import Client from dbgpt.client.flow import delete_flow DBGPT_API_KEY = "dbgpt" @@ -168,7 +168,7 @@ curl -X GET "http://localhost:5000/api/v2/serve/awel/flows/$FLOW_ID" -H "Authori ```python -from dbgpt.client.client import Client +from dbgpt.client import Client from dbgpt.client.knowledge import get_flow DBGPT_API_KEY = "dbgpt" @@ -222,7 +222,7 @@ curl -X GET "http://localhost:5000/api/v2/serve/awel/flows" -H "Authorization: B ```python -from dbgpt.client.client import Client +from dbgpt.client import Client from dbgpt.client.flow import list_flow DBGPT_API_KEY = "dbgpt" diff --git a/docs/docs/api/introduction.md b/docs/docs/api/introduction.md index d195b7c56..6354982ae 100644 --- a/docs/docs/api/introduction.md +++ b/docs/docs/api/introduction.md @@ -21,7 +21,7 @@ Example with the DB-GPT API curl command: Example with the DB-GPT Client Python package: ```python - from dbgpt.client.client import Client + from dbgpt.client import Client DBGPT_API_KEY = "dbgpt" client = Client(api_key=DBGPT_API_KEY) diff --git a/docs/docs/api/knowledge.md b/docs/docs/api/knowledge.md index 688add674..b19be1478 100644 --- a/docs/docs/api/knowledge.md +++ b/docs/docs/api/knowledge.md @@ -41,7 +41,7 @@ curl -X POST "http://localhost:5000/api/v2/chat/completions" \ ```python -from dbgpt.client.client import Client +from dbgpt.client import Client DBGPT_API_KEY = "dbgpt" SPACE_NAME="{YOUR_SPACE_NAME}" @@ -345,7 +345,7 @@ POST /api/v2/serve/knowledge/spaces ```python -from dbgpt.client.client import Client +from dbgpt.client import Client from dbgpt.client.knowledge import create_space from dbgpt.client.schemas import SpaceModel @@ -422,7 +422,7 @@ PUT /api/v2/serve/knowledge/spaces ```python -from dbgpt.client.client import Client +from dbgpt.client import Client from dbgpt.client.knowledge import update_space from dbgpt.client.schemas import SpaceModel @@ -504,7 +504,7 @@ DELETE /api/v2/serve/knowledge/spaces ```python -from dbgpt.client.client import Client +from dbgpt.client import Client from dbgpt.client.knowledge import delete_space DBGPT_API_KEY = "dbgpt" @@ -556,7 +556,7 @@ curl -X GET "http://localhost:5000/api/v2/serve/knowledge/spaces/$SPACE_ID" -H " ```python -from dbgpt.client.client import Client +from dbgpt.client import Client from dbgpt.client.knowledge import get_space DBGPT_API_KEY = "dbgpt" @@ -608,7 +608,7 @@ curl -X GET 'http://localhost:5000/api/v2/serve/knowledge/spaces' -H "Authorizat ```python -from dbgpt.client.client import Client +from dbgpt.client import Client from dbgpt.client.knowledge import list_space DBGPT_API_KEY = "dbgpt" diff --git a/examples/client/__init__.py b/examples/client/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/examples/client/app_crud_example.py b/examples/client/app_crud_example.py index 2f7e9bae5..000607841 100644 --- a/examples/client/app_crud_example.py +++ b/examples/client/app_crud_example.py @@ -1,25 +1,20 @@ +"""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") +""" import asyncio +from dbgpt.client import Client 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(): diff --git a/examples/client/client_chat_example.py b/examples/client/client_chat_example.py index 5ae7009f8..266ef1bc7 100644 --- a/examples/client/client_chat_example.py +++ b/examples/client/client_chat_example.py @@ -1,13 +1,8 @@ -import asyncio +"""Client: Simple Chat example. -from dbgpt.client.client import Client +This example demonstrates how to use the dbgpt client to chat with the chatgpt model. -""" -Client: Simple Chat example - - This example demonstrates how to use the dbgpt client to chat with the chatgpt model. - - Example: +Example: .. code-block:: python DBGPT_API_KEY = "dbgpt" @@ -53,6 +48,10 @@ Client: Simple Chat example print(data.dict()) """ +import asyncio + +from dbgpt.client import Client + async def main(): # initialize client diff --git a/examples/client/flow_crud_example.py b/examples/client/flow_crud_example.py index 29dc95f87..7b83a00a0 100644 --- a/examples/client/flow_crud_example.py +++ b/examples/client/flow_crud_example.py @@ -1,38 +1,36 @@ +"""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) + +""" import asyncio -from dbgpt.client.client import Client +from dbgpt.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 diff --git a/examples/client/knowledge_crud_example.py b/examples/client/knowledge_crud_example.py index b00d01e53..f3533bea2 100644 --- a/examples/client/knowledge_crud_example.py +++ b/examples/client/knowledge_crud_example.py @@ -1,13 +1,9 @@ -import asyncio +"""Client: Simple Knowledge CRUD example. -from dbgpt.client.client import Client -from dbgpt.client.knowledge import create_space -from dbgpt.client.schemas import SpaceModel +This example demonstrates how to use the dbgpt client to create, get, update, and +delete knowledge spaces and documents. -"""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: +Example: .. code-block:: python DBGPT_API_KEY = "dbgpt" @@ -66,6 +62,11 @@ from dbgpt.client.schemas import SpaceModel # 10. Delete a document res = await delete_document(client, "150") """ +import asyncio + +from dbgpt.client import Client +from dbgpt.client.knowledge import create_space +from dbgpt.client.schemas import SpaceModel async def main():