|
|
|
@ -1,3 +1,5 @@
|
|
|
|
|
import asyncio
|
|
|
|
|
import json
|
|
|
|
|
import logging
|
|
|
|
|
import os
|
|
|
|
|
import subprocess
|
|
|
|
@ -11,8 +13,11 @@ from logging.handlers import RotatingFileHandler
|
|
|
|
|
import fastapi
|
|
|
|
|
import uvicorn
|
|
|
|
|
from fastapi import FastAPI, HTTPException
|
|
|
|
|
from openai import AsyncOpenAI
|
|
|
|
|
from sse_starlette import EventSourceResponse
|
|
|
|
|
from starlette.staticfiles import StaticFiles
|
|
|
|
|
|
|
|
|
|
from Config import Config
|
|
|
|
|
from Util.ALiYunUtil import ALiYunUtil
|
|
|
|
|
from Util.SearchUtil import *
|
|
|
|
|
|
|
|
|
@ -123,16 +128,54 @@ async def rag(request: fastapi.Request):
|
|
|
|
|
|
|
|
|
|
return {"data": "没有在知识库中找到相关的信息,无法回答此问题。"}
|
|
|
|
|
|
|
|
|
|
@app.post("/api/helloWorld", response_model=None)
|
|
|
|
|
async def helloWorld(request: fastapi.Request):
|
|
|
|
|
async def generate_hello_world():
|
|
|
|
|
message = "Hello,World,"
|
|
|
|
|
for char in message:
|
|
|
|
|
yield f"data: {char}\n\n"
|
|
|
|
|
import asyncio
|
|
|
|
|
await asyncio.sleep(0.5)
|
|
|
|
|
|
|
|
|
|
return StreamingResponse(generate_hello_world(), media_type="text/event-stream")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/rag_stream", response_model=None)
|
|
|
|
|
async def rag_stream(request: fastapi.Request):
|
|
|
|
|
data = await request.json()
|
|
|
|
|
query = data.get('query', '')
|
|
|
|
|
query_tags = data.get('tags', [])
|
|
|
|
|
|
|
|
|
|
# 调用es进行混合搜索
|
|
|
|
|
search_results = queryByEs(query, query_tags, logger)
|
|
|
|
|
|
|
|
|
|
# 流式调用大模型
|
|
|
|
|
# 获取StreamingResponse对象
|
|
|
|
|
return callLLM(request, query, search_results, logger, True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 与用户交流聊天
|
|
|
|
|
@app.post("/api/helloWorld")
|
|
|
|
|
async def reply():
|
|
|
|
|
# 初始化异步 OpenAI 客户端
|
|
|
|
|
client = AsyncOpenAI(
|
|
|
|
|
api_key=Config.MODEL_API_KEY,
|
|
|
|
|
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
async def generate_response_stream():
|
|
|
|
|
try:
|
|
|
|
|
# 流式调用大模型
|
|
|
|
|
stream = await client.chat.completions.create(
|
|
|
|
|
model=Config.MODEL_NAME,
|
|
|
|
|
messages=[
|
|
|
|
|
{"role": "system",
|
|
|
|
|
"content": "你是聊天人的好朋友,你认识深刻,知识渊博,不要使用哎呀这样的语气词。聊天的回复内容不要超过150字。"},
|
|
|
|
|
{"role": "user", "content": "你是谁?"}
|
|
|
|
|
],
|
|
|
|
|
max_tokens=4000,
|
|
|
|
|
stream=True # 启用流式模式
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 流式返回模型生成的回复
|
|
|
|
|
async for chunk in stream:
|
|
|
|
|
if chunk.choices[0].delta.content:
|
|
|
|
|
yield f"data: {json.dumps({'reply': chunk.choices[0].delta.content}, ensure_ascii=False)}\n\n"
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
yield f"data: {json.dumps({'error': str(e)})}\n\n"
|
|
|
|
|
|
|
|
|
|
return EventSourceResponse(generate_response_stream())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|