From e742bfca49f19b5f44389a9e0b7bcf3c888b3b2e Mon Sep 17 00:00:00 2001 From: HuangHai <10402852@qq.com> Date: Sun, 29 Jun 2025 21:07:29 +0800 Subject: [PATCH] 'commit' --- dsRag/Start.py | 63 +++++++++++++++--- dsRag/Util/ALiYunUtil.py | 8 +-- dsRag/Util/SearchUtil.py | 1 + .../__pycache__/ALiYunUtil.cpython-310.pyc | Bin 1927 -> 1911 bytes .../__pycache__/SearchUtil.cpython-310.pyc | Bin 5143 -> 5143 bytes 5 files changed, 58 insertions(+), 14 deletions(-) diff --git a/dsRag/Start.py b/dsRag/Start.py index 4547f2e1..6facaf81 100644 --- a/dsRag/Start.py +++ b/dsRag/Start.py @@ -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__": diff --git a/dsRag/Util/ALiYunUtil.py b/dsRag/Util/ALiYunUtil.py index c186db61..0b912280 100644 --- a/dsRag/Util/ALiYunUtil.py +++ b/dsRag/Util/ALiYunUtil.py @@ -27,7 +27,7 @@ class ALiYunUtil: except Exception as e: return f"发生错误: {str(e)}" - async def chat_stream(self, prompt, model=None): + def chat_stream(self, prompt, model=None): """ 与阿里云大模型流式对话 :param prompt: 用户输入的问题 @@ -35,15 +35,15 @@ class ALiYunUtil: :return: 异步生成器,返回模型流式响应 """ try: - stream = await self.client.chat.completions.create( + stream = self.client.chat.completions.create( model=model or self.model_name, messages=[ {'role': 'user', 'content': prompt} ], stream=True ) - async for chunk in stream: + for chunk in stream: if chunk.choices[0].delta.content: yield chunk.choices[0].delta.content except Exception as e: - yield f"发生错误: {str(e)}" \ No newline at end of file + yield f"发生错误: {str(e)}" diff --git a/dsRag/Util/SearchUtil.py b/dsRag/Util/SearchUtil.py index dc4284e1..2052d5bf 100644 --- a/dsRag/Util/SearchUtil.py +++ b/dsRag/Util/SearchUtil.py @@ -170,6 +170,7 @@ def callLLM(request, query, search_results, logger,streamBack=False): async def generate(): async for chunk in aliyun_util.chat_stream(prompt): yield f"data: {chunk}\n\n" + # data: 发生错误: object Stream can't be used in 'await' expression return StreamingResponse(generate(), media_type="text/event-stream") else: # 一次性返回 diff --git a/dsRag/Util/__pycache__/ALiYunUtil.cpython-310.pyc b/dsRag/Util/__pycache__/ALiYunUtil.cpython-310.pyc index 676bd1617203c00c1d39b3e0d3ccc15ea2e0a77a..c56fb91877cd85000f7e2d2cf7d6b62593368821 100644 GIT binary patch delta 331 zcmZqY|IWvo&&$ij00bv25;IE1s+RQkcVJ;I$RX9T=P*p8!9Vc>LXiLf delta 364 zcmey)*UrzI&&$ij00b^-i5WjO@`f@qMoms;E@2dye1ln;RS+l>HHk%zQDm|_iwCbW z69Yr>9H3YuQ!cpnV$WY75$WXx2HK!!8NB|@t2_mF`geF@NC{T)|K?K-%Ss?2cYjQ?uUUrcTNR%5+ nOipCe;O6-)z|O}gz{u3VF?kAG02|mog~_kjOo8F4z#a|&7k5T( diff --git a/dsRag/Util/__pycache__/SearchUtil.cpython-310.pyc b/dsRag/Util/__pycache__/SearchUtil.cpython-310.pyc index e8e5b99d85b595fadc6366ff2168d3be139179ab..72d3998c22d001a281ec4da87338a88b5ad7511a 100644 GIT binary patch delta 34 ocmbQPF