mirror of
https://github.com/csunny/DB-GPT.git
synced 2025-09-05 02:51:07 +00:00
feat(core): Support multi round conversation operator (#986)
This commit is contained in:
@@ -16,7 +16,6 @@ from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import StreamingResponse, JSONResponse
|
||||
from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBearer
|
||||
|
||||
from pydantic import BaseSettings
|
||||
|
||||
from fastchat.protocol.openai_api_protocol import (
|
||||
ChatCompletionResponse,
|
||||
@@ -42,6 +41,7 @@ from dbgpt.model.parameter import ModelAPIServerParameters, WorkerType
|
||||
from dbgpt.model.cluster import ModelRegistry
|
||||
from dbgpt.model.cluster.manager_base import WorkerManager, WorkerManagerFactory
|
||||
from dbgpt.util.utils import setup_logging
|
||||
from dbgpt._private.pydantic import BaseModel
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -52,7 +52,7 @@ class APIServerException(Exception):
|
||||
self.message = message
|
||||
|
||||
|
||||
class APISettings(BaseSettings):
|
||||
class APISettings(BaseModel):
|
||||
api_keys: Optional[List[str]] = None
|
||||
|
||||
|
||||
|
@@ -24,6 +24,7 @@ from dbgpt.core.interface.llm import ModelMetadata, LLMClient
|
||||
from dbgpt.core.interface.llm import ModelOutput, ModelRequest
|
||||
from dbgpt.model.cluster.client import DefaultLLMClient
|
||||
from dbgpt.model.cluster import WorkerManagerFactory
|
||||
from dbgpt._private.pydantic import model_to_json
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import httpx
|
||||
@@ -175,6 +176,9 @@ class OpenAILLMClient(LLMClient):
|
||||
async def generate(self, request: ModelRequest) -> ModelOutput:
|
||||
messages = request.to_openai_messages()
|
||||
payload = self._build_request(request)
|
||||
logger.info(
|
||||
f"Send request to openai, payload: {payload}\n\n messages:\n{messages}"
|
||||
)
|
||||
try:
|
||||
chat_completion = await self.client.chat.completions.create(
|
||||
messages=messages, **payload
|
||||
@@ -193,6 +197,9 @@ class OpenAILLMClient(LLMClient):
|
||||
) -> AsyncIterator[ModelOutput]:
|
||||
messages = request.to_openai_messages()
|
||||
payload = self._build_request(request, True)
|
||||
logger.info(
|
||||
f"Send request to openai, payload: {payload}\n\n messages:\n{messages}"
|
||||
)
|
||||
try:
|
||||
chat_completion = await self.client.chat.completions.create(
|
||||
messages=messages, **payload
|
||||
@@ -321,7 +328,7 @@ async def _to_openai_stream(
|
||||
chunk = ChatCompletionStreamResponse(
|
||||
id=id, choices=[choice_data], model=model or ""
|
||||
)
|
||||
yield f"data: {chunk.json(exclude_unset=True, ensure_ascii=False)}\n\n"
|
||||
yield f"data: {model_to_json(chunk, exclude_unset=True, ensure_ascii=False)}\n\n"
|
||||
|
||||
previous_text = ""
|
||||
finish_stream_events = []
|
||||
@@ -356,7 +363,7 @@ async def _to_openai_stream(
|
||||
if model_output.finish_reason is not None:
|
||||
finish_stream_events.append(chunk)
|
||||
continue
|
||||
yield f"data: {chunk.json(exclude_unset=True, ensure_ascii=False)}\n\n"
|
||||
yield f"data: {model_to_json(chunk, exclude_unset=True, ensure_ascii=False)}\n\n"
|
||||
for finish_chunk in finish_stream_events:
|
||||
yield f"data: {finish_chunk.json(exclude_none=True, ensure_ascii=False)}\n\n"
|
||||
yield f"data: {model_to_json(finish_chunk, exclude_none=True, ensure_ascii=False)}\n\n"
|
||||
yield "data: [DONE]\n\n"
|
||||
|
Reference in New Issue
Block a user