Skip to content

Commit

Permalink
feat(client): Modify api address
Browse files Browse the repository at this point in the history
  • Loading branch information
fangyinc committed Mar 21, 2024
1 parent 01ea5f8 commit ab3e8e5
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 96 deletions.
4 changes: 4 additions & 0 deletions dbgpt/client/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
"""This module is the client of the dbgpt package."""

from .client import Client, ClientException # noqa: F401

__ALL__ = ["Client", "ClientException"]
2 changes: 1 addition & 1 deletion dbgpt/client/app.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
28 changes: 16 additions & 12 deletions dbgpt/client/client.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -59,7 +60,7 @@ def __init__(
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.
Expand All @@ -73,20 +74,25 @@ def __init__(
--------
.. 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(
Expand Down Expand Up @@ -135,9 +141,9 @@ async def chat(
--------
.. 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?")
Expand Down Expand Up @@ -210,9 +216,9 @@ async def chat_stream(
--------
.. 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?")
Expand Down Expand Up @@ -257,7 +263,6 @@ async def chat_stream(
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):
Expand All @@ -274,7 +279,6 @@ async def get(self, path: str, *args):
)
return response
finally:

await self._http_client.aclose()

async def post(self, path: str, args):
Expand Down
2 changes: 1 addition & 1 deletion dbgpt/client/flow.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion dbgpt/client/knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions docs/docs/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import TabItem from '@theme/TabItem';
<TabItem value="python">

```python
from dbgpt.client.client import Client
from dbgpt.client import Client

DBGPT_API_KEY = "dbgpt"
APP_ID="{YOUR_APP_ID}"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/api/chat.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import TabItem from '@theme/TabItem';
<TabItem value="python">

```python
from dbgpt.client.client import Client
from dbgpt.client import Client

DBGPT_API_KEY = "dbgpt"

Expand Down Expand Up @@ -104,7 +104,7 @@ data: [DONE]
<TabItem value="python">

```python
from dbgpt.client.client import Client
from dbgpt.client import Client

DBGPT_API_KEY = "dbgpt"
client = Client(api_key=DBGPT_API_KEY)
Expand Down
8 changes: 4 additions & 4 deletions docs/docs/api/flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ curl -X POST "http://localhost:5000/api/v2/chat/completions" \
<TabItem value="python">

```python
from dbgpt.client.client import Client
from dbgpt.client import Client

DBGPT_API_KEY = "dbgpt"
FLOW_ID="{YOUR_FLOW_ID}"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/api/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions docs/docs/api/knowledge.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ curl -X POST "http://localhost:5000/api/v2/chat/completions" \
<TabItem value="python">

```python
from dbgpt.client.client import Client
from dbgpt.client import Client

DBGPT_API_KEY = "dbgpt"
SPACE_NAME="{YOUR_SPACE_NAME}"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
Empty file removed examples/client/__init__.py
Empty file.
31 changes: 13 additions & 18 deletions examples/client/app_crud_example.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import asyncio
"""Client: Simple App CRUD example.
from dbgpt.client.app import list_app
from dbgpt.client.client import Client
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")
"""
Client: Simple App CRUD example
This example demonstrates how to use the dbgpt client to get, list apps.
Example:
.. code-block:: python
import asyncio

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"
)
"""
from dbgpt.client import Client
from dbgpt.client.app import list_app


async def main():
Expand Down
15 changes: 7 additions & 8 deletions examples/client/client_chat_example.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import asyncio

from dbgpt.client.client import Client
"""Client: Simple Chat example.
"""
Client: Simple Chat example
This example demonstrates how to use the dbgpt client to chat with the chatgpt model.
This example demonstrates how to use the dbgpt client to chat with the chatgpt model.
Example:
Example:
.. code-block:: python
DBGPT_API_KEY = "dbgpt"
Expand Down Expand Up @@ -53,6 +48,10 @@
print(data.dict())
"""

import asyncio

from dbgpt.client import Client


async def main():
# initialize client
Expand Down
Loading

0 comments on commit ab3e8e5

Please sign in to comment.