remove /chat/fastchat API endpoint (#2506)
This commit is contained in:
parent
3b28f40c6a
commit
c179230ce0
|
|
@ -13,7 +13,6 @@ from fastapi import Body
|
|||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from starlette.responses import RedirectResponse
|
||||
from server.chat.chat import chat
|
||||
from server.chat.openai_chat import openai_chat
|
||||
from server.chat.search_engine_chat import search_engine_chat
|
||||
from server.chat.completion import completion
|
||||
from server.chat.feedback import chat_feedback
|
||||
|
|
@ -59,11 +58,6 @@ def mount_app_routes(app: FastAPI, run_mode: str = None):
|
|||
summary="swagger 文档")(document)
|
||||
|
||||
# Tag: Chat
|
||||
app.post("/chat/fastchat",
|
||||
tags=["Chat"],
|
||||
summary="与llm模型对话(直接与fastchat api对话)",
|
||||
)(openai_chat)
|
||||
|
||||
app.post("/chat/chat",
|
||||
tags=["Chat"],
|
||||
summary="与llm模型对话(通过LLMChain)",
|
||||
|
|
|
|||
|
|
@ -1,58 +0,0 @@
|
|||
from fastapi.responses import StreamingResponse
|
||||
from typing import List, Optional
|
||||
import openai
|
||||
from configs import LLM_MODELS, logger, log_verbose
|
||||
from server.utils import get_model_worker_config, fschat_openai_api_address
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class OpenAiMessage(BaseModel):
|
||||
role: str = "user"
|
||||
content: str = "hello"
|
||||
|
||||
|
||||
class OpenAiChatMsgIn(BaseModel):
|
||||
model: str = LLM_MODELS[0]
|
||||
messages: List[OpenAiMessage]
|
||||
temperature: float = 0.7
|
||||
n: int = 1
|
||||
max_tokens: Optional[int] = None
|
||||
stop: List[str] = []
|
||||
stream: bool = False
|
||||
presence_penalty: int = 0
|
||||
frequency_penalty: int = 0
|
||||
|
||||
|
||||
async def openai_chat(msg: OpenAiChatMsgIn):
|
||||
config = get_model_worker_config(msg.model)
|
||||
openai.api_key = config.get("api_key", "EMPTY")
|
||||
print(f"{openai.api_key=}")
|
||||
openai.api_base = config.get("api_base_url", fschat_openai_api_address())
|
||||
print(f"{openai.api_base=}")
|
||||
print(msg)
|
||||
|
||||
async def get_response(msg):
|
||||
data = msg.dict()
|
||||
|
||||
try:
|
||||
response = await openai.ChatCompletion.acreate(**data)
|
||||
if msg.stream:
|
||||
async for data in response:
|
||||
if choices := data.choices:
|
||||
if chunk := choices[0].get("delta", {}).get("content"):
|
||||
print(chunk, end="", flush=True)
|
||||
yield chunk
|
||||
else:
|
||||
if response.choices:
|
||||
answer = response.choices[0].message.content
|
||||
print(answer)
|
||||
yield(answer)
|
||||
except Exception as e:
|
||||
msg = f"获取ChatCompletion时出错:{e}"
|
||||
logger.error(f'{e.__class__.__name__}: {msg}',
|
||||
exc_info=e if log_verbose else None)
|
||||
|
||||
return StreamingResponse(
|
||||
get_response(msg),
|
||||
media_type='text/event-stream',
|
||||
)
|
||||
|
|
@ -48,18 +48,6 @@ data = {
|
|||
}
|
||||
|
||||
|
||||
def test_chat_fastchat(api="/chat/fastchat"):
|
||||
url = f"{api_base_url}{api}"
|
||||
data2 = {
|
||||
"stream": True,
|
||||
"messages": data["history"] + [{"role": "user", "content": "推荐一部科幻电影"}]
|
||||
}
|
||||
dump_input(data2, api)
|
||||
response = requests.post(url, headers=headers, json=data2, stream=True)
|
||||
dump_output(response, api)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_chat_chat(api="/chat/chat"):
|
||||
url = f"{api_base_url}{api}"
|
||||
dump_input(data, api)
|
||||
|
|
|
|||
|
|
@ -259,38 +259,6 @@ class ApiRequest:
|
|||
return self._get_response_value(response, value_func=lambda r: r.text)
|
||||
|
||||
# 对话相关操作
|
||||
|
||||
def chat_fastchat(
|
||||
self,
|
||||
messages: List[Dict],
|
||||
stream: bool = True,
|
||||
model: str = LLM_MODELS[0],
|
||||
temperature: float = TEMPERATURE,
|
||||
max_tokens: int = None,
|
||||
**kwargs: Any,
|
||||
):
|
||||
'''
|
||||
对应api.py/chat/fastchat接口
|
||||
'''
|
||||
data = {
|
||||
"messages": messages,
|
||||
"stream": stream,
|
||||
"model": model,
|
||||
"temperature": temperature,
|
||||
"max_tokens": max_tokens,
|
||||
}
|
||||
|
||||
# print(f"received input message:")
|
||||
# pprint(data)
|
||||
|
||||
response = self.post(
|
||||
"/chat/fastchat",
|
||||
json=data,
|
||||
stream=True,
|
||||
**kwargs,
|
||||
)
|
||||
return self._httpx_stream2generator(response)
|
||||
|
||||
def chat_chat(
|
||||
self,
|
||||
query: str,
|
||||
|
|
@ -1058,10 +1026,6 @@ if __name__ == "__main__":
|
|||
api = ApiRequest()
|
||||
aapi = AsyncApiRequest()
|
||||
|
||||
# print(api.chat_fastchat(
|
||||
# messages=[{"role": "user", "content": "hello"}]
|
||||
# ))
|
||||
|
||||
# with api.chat_chat("你好") as r:
|
||||
# for t in r.iter_text(None):
|
||||
# print(t)
|
||||
|
|
|
|||
Loading…
Reference in New Issue