feat(client): Modify api address

This commit is contained in:
Fangyin Cheng 2024-03-21 09:58:32 +08:00
parent 01ea5f8064
commit ab3e8e54a1
15 changed files with 98 additions and 97 deletions

View File

@ -1 +1,5 @@
"""This module is the client of the dbgpt package.""" """This module is the client of the dbgpt package."""
from .client import Client, ClientException # noqa: F401
__ALL__ = ["Client", "ClientException"]

View File

@ -1,7 +1,7 @@
"""App Client API.""" """App Client API."""
from typing import List 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.client.schemas import AppModel
from dbgpt.serve.core import Result from dbgpt.serve.core import Result

View File

@ -1,5 +1,6 @@
"""This module contains the client for the DB-GPT API.""" """This module contains the client for the DB-GPT API."""
import json import json
import os
from typing import Any, AsyncGenerator, List, Optional, Union from typing import Any, AsyncGenerator, List, Optional, Union
from urllib.parse import urlparse from urllib.parse import urlparse
@ -50,7 +51,7 @@ class Client:
def __init__( def __init__(
self, self,
api_base: str = "http://localhost:5000", api_base: Optional[str] = None,
api_key: Optional[str] = None, api_key: Optional[str] = None,
version: str = "v2", version: str = "v2",
timeout: Optional[httpx._types.TimeoutTypes] = 120, timeout: Optional[httpx._types.TimeoutTypes] = 120,
@ -59,7 +60,7 @@ class Client:
Args: Args:
api_base: Optional[str], a full URL for the DB-GPT API. 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. api_key: Optional[str], The dbgpt api key to use for authentication.
Defaults to None. Defaults to None.
timeout: Optional[httpx._types.TimeoutTypes]: The timeout to use. timeout: Optional[httpx._types.TimeoutTypes]: The timeout to use.
@ -73,20 +74,25 @@ class Client:
-------- --------
.. code-block:: python .. 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" DBGPT_API_KEY = "dbgpt"
client = Client(api_base=DBGPT_API_BASE, api_key=DBGPT_API_KEY) client = Client(api_base=DBGPT_API_BASE, api_key=DBGPT_API_KEY)
client.chat(model="chatgpt_proxyllm", messages="Hello?") 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): if api_base and is_valid_url(api_base):
self._api_url = api_base.rstrip("/") self._api_url = api_base
else: else:
raise ValueError(f"api url {api_base} does not exist or is not accessible.") raise ValueError(f"api url {api_base} does not exist or is not accessible.")
self._api_key = api_key self._api_key = api_key
self._version = version self._version = version
self._api_url = api_base + CLIENT_API_PATH + "/" + version
self._timeout = timeout self._timeout = timeout
headers = {"Authorization": f"Bearer {self._api_key}"} if self._api_key else {} headers = {"Authorization": f"Bearer {self._api_key}"} if self._api_key else {}
self._http_client = httpx.AsyncClient( self._http_client = httpx.AsyncClient(
@ -135,9 +141,9 @@ class Client:
-------- --------
.. code-block:: python .. 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" DBGPT_API_KEY = "dbgpt"
client = Client(api_base=DBGPT_API_BASE, api_key=DBGPT_API_KEY) client = Client(api_base=DBGPT_API_BASE, api_key=DBGPT_API_KEY)
res = await client.chat(model="chatgpt_proxyllm", messages="Hello?") res = await client.chat(model="chatgpt_proxyllm", messages="Hello?")
@ -210,9 +216,9 @@ class Client:
-------- --------
.. code-block:: python .. 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" DBGPT_API_KEY = "dbgpt"
client = Client(api_base=DBGPT_API_BASE, api_key=DBGPT_API_KEY) client = Client(api_base=DBGPT_API_BASE, api_key=DBGPT_API_KEY)
res = await client.chat_stream(model="chatgpt_proxyllm", messages="Hello?") res = await client.chat_stream(model="chatgpt_proxyllm", messages="Hello?")
@ -257,7 +263,6 @@ class Client:
error = await response.aread() error = await response.aread()
yield json.loads(error) yield json.loads(error)
except Exception as e: except Exception as e:
yield f"data:[SERVER_ERROR]{str(e)}\n\n" yield f"data:[SERVER_ERROR]{str(e)}\n\n"
async def get(self, path: str, *args): async def get(self, path: str, *args):
@ -274,7 +279,6 @@ class Client:
) )
return response return response
finally: finally:
await self._http_client.aclose() await self._http_client.aclose()
async def post(self, path: str, args): async def post(self, path: str, args):

View File

@ -1,7 +1,7 @@
"""this module contains the flow client functions.""" """this module contains the flow client functions."""
from typing import List 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.core.awel.flow.flow_factory import FlowPanel
from dbgpt.serve.core import Result from dbgpt.serve.core import Result

View File

@ -2,7 +2,7 @@
import json import json
from typing import List 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.client.schemas import DocumentModel, SpaceModel, SyncModel
from dbgpt.serve.core import Result from dbgpt.serve.core import Result

View File

@ -42,7 +42,7 @@ import TabItem from '@theme/TabItem';
<TabItem value="python"> <TabItem value="python">
```python ```python
from dbgpt.client.client import Client from dbgpt.client import Client
DBGPT_API_KEY = "dbgpt" DBGPT_API_KEY = "dbgpt"
APP_ID="{YOUR_APP_ID}" APP_ID="{YOUR_APP_ID}"
@ -94,7 +94,7 @@ curl -X GET "http://localhost:5000/api/v2/serve/apps/$APP_ID" -H "Authorization:
```python ```python
from dbgpt.client.client import Client from dbgpt.client import Client
from dbgpt.client.app import get_app from dbgpt.client.app import get_app
DBGPT_API_KEY = "dbgpt" DBGPT_API_KEY = "dbgpt"
@ -146,7 +146,7 @@ curl -X GET 'http://localhost:5000/api/v2/serve/apps' -H "Authorization: Bearer
```python ```python
from dbgpt.client.client import Client from dbgpt.client import Client
from dbgpt.client.app import list_app from dbgpt.client.app import list_app
DBGPT_API_KEY = "dbgpt" DBGPT_API_KEY = "dbgpt"

View File

@ -42,7 +42,7 @@ import TabItem from '@theme/TabItem';
<TabItem value="python"> <TabItem value="python">
```python ```python
from dbgpt.client.client import Client from dbgpt.client import Client
DBGPT_API_KEY = "dbgpt" DBGPT_API_KEY = "dbgpt"
@ -104,7 +104,7 @@ data: [DONE]
<TabItem value="python"> <TabItem value="python">
```python ```python
from dbgpt.client.client import Client from dbgpt.client import Client
DBGPT_API_KEY = "dbgpt" DBGPT_API_KEY = "dbgpt"
client = Client(api_key=DBGPT_API_KEY) client = Client(api_key=DBGPT_API_KEY)

View File

@ -42,7 +42,7 @@ curl -X POST "http://localhost:5000/api/v2/chat/completions" \
<TabItem value="python"> <TabItem value="python">
```python ```python
from dbgpt.client.client import Client from dbgpt.client import Client
DBGPT_API_KEY = "dbgpt" DBGPT_API_KEY = "dbgpt"
FLOW_ID="{YOUR_FLOW_ID}" FLOW_ID="{YOUR_FLOW_ID}"
@ -115,7 +115,7 @@ FLOW_ID={YOUR_FLOW_ID}
```python ```python
from dbgpt.client.client import Client from dbgpt.client import Client
from dbgpt.client.flow import delete_flow from dbgpt.client.flow import delete_flow
DBGPT_API_KEY = "dbgpt" DBGPT_API_KEY = "dbgpt"
@ -168,7 +168,7 @@ curl -X GET "http://localhost:5000/api/v2/serve/awel/flows/$FLOW_ID" -H "Authori
```python ```python
from dbgpt.client.client import Client from dbgpt.client import Client
from dbgpt.client.knowledge import get_flow from dbgpt.client.knowledge import get_flow
DBGPT_API_KEY = "dbgpt" DBGPT_API_KEY = "dbgpt"
@ -222,7 +222,7 @@ curl -X GET "http://localhost:5000/api/v2/serve/awel/flows" -H "Authorization: B
```python ```python
from dbgpt.client.client import Client from dbgpt.client import Client
from dbgpt.client.flow import list_flow from dbgpt.client.flow import list_flow
DBGPT_API_KEY = "dbgpt" DBGPT_API_KEY = "dbgpt"

View File

@ -21,7 +21,7 @@ Example with the DB-GPT API curl command:
Example with the DB-GPT Client Python package: Example with the DB-GPT Client Python package:
```python ```python
from dbgpt.client.client import Client from dbgpt.client import Client
DBGPT_API_KEY = "dbgpt" DBGPT_API_KEY = "dbgpt"
client = Client(api_key=DBGPT_API_KEY) client = Client(api_key=DBGPT_API_KEY)

View File

@ -41,7 +41,7 @@ curl -X POST "http://localhost:5000/api/v2/chat/completions" \
<TabItem value="python"> <TabItem value="python">
```python ```python
from dbgpt.client.client import Client from dbgpt.client import Client
DBGPT_API_KEY = "dbgpt" DBGPT_API_KEY = "dbgpt"
SPACE_NAME="{YOUR_SPACE_NAME}" SPACE_NAME="{YOUR_SPACE_NAME}"
@ -345,7 +345,7 @@ POST /api/v2/serve/knowledge/spaces
```python ```python
from dbgpt.client.client import Client from dbgpt.client import Client
from dbgpt.client.knowledge import create_space from dbgpt.client.knowledge import create_space
from dbgpt.client.schemas import SpaceModel from dbgpt.client.schemas import SpaceModel
@ -422,7 +422,7 @@ PUT /api/v2/serve/knowledge/spaces
```python ```python
from dbgpt.client.client import Client from dbgpt.client import Client
from dbgpt.client.knowledge import update_space from dbgpt.client.knowledge import update_space
from dbgpt.client.schemas import SpaceModel from dbgpt.client.schemas import SpaceModel
@ -504,7 +504,7 @@ DELETE /api/v2/serve/knowledge/spaces
```python ```python
from dbgpt.client.client import Client from dbgpt.client import Client
from dbgpt.client.knowledge import delete_space from dbgpt.client.knowledge import delete_space
DBGPT_API_KEY = "dbgpt" DBGPT_API_KEY = "dbgpt"
@ -556,7 +556,7 @@ curl -X GET "http://localhost:5000/api/v2/serve/knowledge/spaces/$SPACE_ID" -H "
```python ```python
from dbgpt.client.client import Client from dbgpt.client import Client
from dbgpt.client.knowledge import get_space from dbgpt.client.knowledge import get_space
DBGPT_API_KEY = "dbgpt" DBGPT_API_KEY = "dbgpt"
@ -608,7 +608,7 @@ curl -X GET 'http://localhost:5000/api/v2/serve/knowledge/spaces' -H "Authorizat
```python ```python
from dbgpt.client.client import Client from dbgpt.client import Client
from dbgpt.client.knowledge import list_space from dbgpt.client.knowledge import list_space
DBGPT_API_KEY = "dbgpt" DBGPT_API_KEY = "dbgpt"

View File

@ -1,13 +1,7 @@
import asyncio """Client: Simple App CRUD example.
from dbgpt.client.app import list_app This example demonstrates how to use the dbgpt client to get, list apps.
from dbgpt.client.client import Client Example:
"""
Client: Simple App CRUD example
This example demonstrates how to use the dbgpt client to get, list apps.
Example:
.. code-block:: python .. code-block:: python
DBGPT_API_KEY = "dbgpt" DBGPT_API_KEY = "dbgpt"
@ -15,11 +9,12 @@ Client: Simple App CRUD example
# 1. List all apps # 1. List all apps
res = await list_app(client) res = await list_app(client)
# 2. Get an app # 2. Get an app
res = await get_app( res = await get_app(client, app_id="bf1c7561-13fc-4fe0-bf5d-c22e724766a8")
client, app_id="bf1c7561-13fc-4fe0-bf5d-c22e724766a8"
)
""" """
import asyncio
from dbgpt.client import Client
from dbgpt.client.app import list_app
async def main(): async def main():

View File

@ -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.
""" Example:
Client: Simple Chat example
This example demonstrates how to use the dbgpt client to chat with the chatgpt model.
Example:
.. code-block:: python .. code-block:: python
DBGPT_API_KEY = "dbgpt" DBGPT_API_KEY = "dbgpt"
@ -53,6 +48,10 @@ Client: Simple Chat example
print(data.dict()) print(data.dict())
""" """
import asyncio
from dbgpt.client import Client
async def main(): async def main():
# initialize client # initialize client

View File

@ -1,13 +1,9 @@
import asyncio """Client: Simple Flow CRUD example
from dbgpt.client.client import Client This example demonstrates how to use the dbgpt client to create, get, update, and
from dbgpt.client.flow import list_flow delete flows.
""" Example:
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 .. code-block:: python
DBGPT_API_KEY = "dbgpt" DBGPT_API_KEY = "dbgpt"
@ -23,15 +19,17 @@ Client: Simple Flow CRUD example
FlowPanel(name="test_flow", desc="for client flow333", owner="dbgpt"), FlowPanel(name="test_flow", desc="for client flow333", owner="dbgpt"),
) )
# 3. Delete a flow # 3. Delete a flow
res = await delete_flow( res = await delete_flow(client, flow_id="bf1c7561-13fc-4fe0-bf5d-c22e724766a8")
client, flow_id="bf1c7561-13fc-4fe0-bf5d-c22e724766a8"
)
# 4. Get a flow # 4. Get a flow
res = await get_flow(client, flow_id="bf1c7561-13fc-4fe0-bf5d-c22e724766a8") res = await get_flow(client, flow_id="bf1c7561-13fc-4fe0-bf5d-c22e724766a8")
# 5. List all flows # 5. List all flows
res = await list_flow(client) res = await list_flow(client)
""" """
import asyncio
from dbgpt.client import Client
from dbgpt.client.flow import list_flow
async def main(): async def main():

View File

@ -1,13 +1,9 @@
import asyncio """Client: Simple Knowledge CRUD example.
from dbgpt.client.client import Client This example demonstrates how to use the dbgpt client to create, get, update, and
from dbgpt.client.knowledge import create_space delete knowledge spaces and documents.
from dbgpt.client.schemas import SpaceModel
"""Client: Simple Knowledge CRUD example Example:
This example demonstrates how to use the dbgpt client to create, get, update, and delete knowledge spaces and documents.
Example:
.. code-block:: python .. code-block:: python
DBGPT_API_KEY = "dbgpt" DBGPT_API_KEY = "dbgpt"
@ -66,6 +62,11 @@ from dbgpt.client.schemas import SpaceModel
# 10. Delete a document # 10. Delete a document
res = await delete_document(client, "150") 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(): async def main():