mirror of
https://github.com/csunny/DB-GPT.git
synced 2025-09-03 18:17:45 +00:00
feat: add Client and API v2 (#1316)
# Description 1. Provide /api/v2 for DB-GPT 2. Add DBGPT Python Client for Chat, Flow, App, Knowledge, Including: - Chat - Create - Update - Delete - Get - List 3. Add examples in `examples/client/` 4. Add API Reference document # How Has This Been Tested? ## Test Chat Normal ### Curl 1. set `API_KEYS=dbgpt` in `.env` 2. `python dbgpt/app/dbgpt_server.py` 3. test with curl ``` DBGPT_API_KEY=dbgpt curl -X POST "http://localhost:5000/api/v2/chat/completions" \ -H "Authorization: Bearer $DBGPT_API_KEY" \ -H "accept: application/json" \ -H "Content-Type: application/json" \ -d "{\"messages\":\"Hello\",\"model\":\"chatgpt_proxyllm\"}" ``` ``` data: {"id": "chatcmpl-ab5fd180-e699-11ee-8388-acde48001122", "model": "chatgpt_proxyllm", "choices": [{"index": 0, "delta": {"role": "assistant", "content": "Hello"}}]} data: {"id": "chatcmpl-ab5fd180-e699-11ee-8388-acde48001122", "model": "chatgpt_proxyllm", "choices": [{"index": 0, "delta": {"role": "assistant", "content": "!"}}]} data: {"id": "chatcmpl-ab5fd180-e699-11ee-8388-acde48001122", "model": "chatgpt_proxyllm", "choices": [{"index": 0, "delta": {"role": "assistant", "content": " How"}}]} data: {"id": "chatcmpl-ab5fd180-e699-11ee-8388-acde48001122", "model": "chatgpt_proxyllm", "choices": [{"index": 0, "delta": {"role": "assistant", "content": " can"}}]} data: {"id": "chatcmpl-ab5fd180-e699-11ee-8388-acde48001122", "model": "chatgpt_proxyllm", "choices": [{"index": 0, "delta": {"role": "assistant", "content": " I"}}]} data: {"id": "chatcmpl-ab5fd180-e699-11ee-8388-acde48001122", "model": "chatgpt_proxyllm", "choices": [{"index": 0, "delta": {"role": "assistant", "content": " assist"}}]} data: {"id": "chatcmpl-ab5fd180-e699-11ee-8388-acde48001122", "model": "chatgpt_proxyllm", "choices": [{"index": 0, "delta": {"role": "assistant", "content": " you"}}]} data: {"id": "chatcmpl-ab5fd180-e699-11ee-8388-acde48001122", "model": "chatgpt_proxyllm", "choices": [{"index": 0, "delta": {"role": "assistant", "content": " today"}}]} data: {"id": "chatcmpl-ab5fd180-e699-11ee-8388-acde48001122", "model": "chatgpt_proxyllm", "choices": [{"index": 0, "delta": {"role": "assistant", "content": "?"}}]} data: [DONE] ``` ### Python ```python from dbgpt.client import Client DBGPT_API_KEY = "dbgpt" client = Client(api_key=DBGPT_API_KEY) # stream async for data in client.chat_stream( model="chatgpt_proxyllm", messages="hello", ): print(data) # no stream await client.chat(model="chatgpt_proxyllm", messages="hello") ``` ## Test Chat App ### Curl test with curl ``` DBGPT_API_KEY=dbgpt APP_CODE={YOUR_APP_CODE} curl -X POST "http://localhost:5000/api/v2/chat/completions" \ -H "Authorization: Bearer $DBGPT_API_KEY" \ -H "accept: application/json" \ -H "Content-Type: application/json" \ -d "{\"messages\":\"Hello\",\"model\":\"chatgpt_proxyllm\", \"chat_mode\": \"chat_app\", \"chat_param\": \"$APP_CODE\"}" ``` ### Python ```python from dbgpt.client import Client DBGPT_API_KEY = "dbgpt" client = Client(api_key=DBGPT_API_KEY) APP_CODE="{YOUR_APP_CODE}" async for data in client.chat_stream( model="chatgpt_proxyllm", messages="hello", chat_mode="chat_app", chat_param=APP_CODE ): print(data) ``` # Snapshots: Include snapshots for easier review. # Checklist: - [x] My code follows the style guidelines of this project - [x] I have already rebased the commits and make the commit message conform to the project standard. - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation - [x] Any dependent changes have been merged and published in downstream modules
This commit is contained in:
1
dbgpt/core/schema/__init__.py
Normal file
1
dbgpt/core/schema/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Module for common schemas."""
|
116
dbgpt/core/schema/api.py
Normal file
116
dbgpt/core/schema/api.py
Normal file
@@ -0,0 +1,116 @@
|
||||
"""API schema module."""
|
||||
|
||||
import time
|
||||
import uuid
|
||||
from typing import Any, Generic, List, Literal, Optional, TypeVar
|
||||
|
||||
from dbgpt._private.pydantic import BaseModel, Field
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class Result(BaseModel, Generic[T]):
|
||||
"""Common result entity for API response."""
|
||||
|
||||
success: bool = Field(
|
||||
..., description="Whether it is successful, True: success, False: failure"
|
||||
)
|
||||
err_code: str | None = Field(None, description="Error code")
|
||||
err_msg: str | None = Field(None, description="Error message")
|
||||
data: T | None = Field(None, description="Return data")
|
||||
|
||||
@staticmethod
|
||||
def succ(data: T) -> "Result[T]":
|
||||
"""Build a successful result entity.
|
||||
|
||||
Args:
|
||||
data (T): Return data
|
||||
|
||||
Returns:
|
||||
Result[T]: Result entity
|
||||
"""
|
||||
return Result(success=True, err_code=None, err_msg=None, data=data)
|
||||
|
||||
@staticmethod
|
||||
def failed(msg: str, err_code: Optional[str] = "E000X") -> "Result[Any]":
|
||||
"""Build a failed result entity.
|
||||
|
||||
Args:
|
||||
msg (str): Error message
|
||||
err_code (Optional[str], optional): Error code. Defaults to "E000X".
|
||||
"""
|
||||
return Result(success=False, err_code=err_code, err_msg=msg, data=None)
|
||||
|
||||
|
||||
class DeltaMessage(BaseModel):
|
||||
"""Delta message entity for chat completion response."""
|
||||
|
||||
role: Optional[str] = None
|
||||
content: Optional[str] = None
|
||||
|
||||
|
||||
class ChatCompletionResponseStreamChoice(BaseModel):
|
||||
"""Chat completion response choice entity."""
|
||||
|
||||
index: int = Field(..., description="Choice index")
|
||||
delta: DeltaMessage = Field(..., description="Delta message")
|
||||
finish_reason: Optional[Literal["stop", "length"]] = Field(
|
||||
None, description="Finish reason"
|
||||
)
|
||||
|
||||
|
||||
class ChatCompletionStreamResponse(BaseModel):
|
||||
"""Chat completion response stream entity."""
|
||||
|
||||
id: str = Field(
|
||||
default_factory=lambda: f"chatcmpl-{str(uuid.uuid1())}", description="Stream ID"
|
||||
)
|
||||
created: int = Field(
|
||||
default_factory=lambda: int(time.time()), description="Created time"
|
||||
)
|
||||
model: str = Field(..., description="Model name")
|
||||
choices: List[ChatCompletionResponseStreamChoice] = Field(
|
||||
..., description="Chat completion response choices"
|
||||
)
|
||||
|
||||
|
||||
class ChatMessage(BaseModel):
|
||||
"""Chat message entity."""
|
||||
|
||||
role: str = Field(..., description="Role of the message")
|
||||
content: str = Field(..., description="Content of the message")
|
||||
|
||||
|
||||
class UsageInfo(BaseModel):
|
||||
"""Usage info entity."""
|
||||
|
||||
prompt_tokens: int = Field(0, description="Prompt tokens")
|
||||
total_tokens: int = Field(0, description="Total tokens")
|
||||
completion_tokens: Optional[int] = Field(0, description="Completion tokens")
|
||||
|
||||
|
||||
class ChatCompletionResponseChoice(BaseModel):
|
||||
"""Chat completion response choice entity."""
|
||||
|
||||
index: int = Field(..., description="Choice index")
|
||||
message: ChatMessage = Field(..., description="Chat message")
|
||||
finish_reason: Optional[Literal["stop", "length"]] = Field(
|
||||
None, description="Finish reason"
|
||||
)
|
||||
|
||||
|
||||
class ChatCompletionResponse(BaseModel):
|
||||
"""Chat completion response entity."""
|
||||
|
||||
id: str = Field(
|
||||
default_factory=lambda: f"chatcmpl-{str(uuid.uuid1())}", description="Stream ID"
|
||||
)
|
||||
object: str = "chat.completion"
|
||||
created: int = Field(
|
||||
default_factory=lambda: int(time.time()), description="Created time"
|
||||
)
|
||||
model: str = Field(..., description="Model name")
|
||||
choices: List[ChatCompletionResponseChoice] = Field(
|
||||
..., description="Chat completion response choices"
|
||||
)
|
||||
usage: UsageInfo = Field(..., description="Usage info")
|
Reference in New Issue
Block a user