diff --git a/AI/WxMini/Start.py b/AI/WxMini/Start.py index 9b5bcef5..bd6d68f5 100644 --- a/AI/WxMini/Start.py +++ b/AI/WxMini/Start.py @@ -247,23 +247,6 @@ async def reply(person_id: str = Form(...), "success": False } - if '生成图片' in prompt or '生图' in prompt: - success, key = generate_image(prompt) - if success: - image_url = f"{OSS_PREFIX}{key}" - image_width, image_height = getImgWidthHeight(image_url) - # 记录聊天数据到 MySQL - await save_chat_to_mysql(app.state.mysql_pool, person_id, prompt, - key, '', 0, input_type=2, output_type=4, - input_image_type=0, image_width=image_width, image_height=image_height) - # 返回数据 - return { - "success": True, - "url": None, - "duration": 0, # 返回大模型的回复时长 - "response": key, # 返回大模型的回复 - } - # 从连接池中获取一个连接 connection = milvus_pool.get_connection() @@ -315,14 +298,14 @@ async def reply(person_id: str = Form(...), # 拼接交互提示词 if '天气' in prompt or '降温' in prompt or '气温' in prompt or '下雨' in prompt or '下雪' in prompt or '风' in prompt: weather_info = await get_weather('长春') - history_prompt += f"天气信息: {weather_info}\n" + history_prompt = f"天气信息: {weather_info}\n" logger.info(f"历史交互提示词: {history_prompt}") # NBA与CBA result = await get_news(client, prompt) if result is not None: - history_prompt += result + history_prompt = result print("新闻返回了下面的内容:" + result) # 调用大模型,将历史交互作为提示词 diff --git a/AI/WxMini/Utils/NewsUtil.py b/AI/WxMini/Utils/NewsUtil.py index 66e83a90..65ded7ad 100644 --- a/AI/WxMini/Utils/NewsUtil.py +++ b/AI/WxMini/Utils/NewsUtil.py @@ -6,19 +6,22 @@ import requests from bs4 import BeautifulSoup from fastapi import HTTPException from WxMini.Milvus.Config.MulvusConfig import * + # 配置日志 logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") logger = logging.getLogger(__name__) + # 分词与关键词提取 def extract_keywords(user_input): # 使用 jieba 进行分词 words = jieba.lcut(user_input) # 过滤掉无意义的词(如标点符号、停用词等) - stop_words = ['的', '了', '吗', '呢', '是', '在', '啊', '呀', '怎么', '怎么样', '?', ',', '。'] + stop_words = ['的', '了', '吗', '呢', '是', '在', '啊', '呀', '怎么', '怎么样', '今天','今日', '?', ',', '。'] keywords = [word for word in words if word not in stop_words] return keywords + # 搜索新浪新闻 def search_sina_news(keyword='新闻', time='d', page=1): results = [] @@ -49,6 +52,7 @@ def search_sina_news(keyword='新闻', time='d', page=1): logger.error(f"请求新浪新闻时出错: {e}") return results + # 根据关键词调用搜索函数 def search_based_on_keywords(keywords): if not keywords: @@ -56,6 +60,7 @@ def search_based_on_keywords(keywords): # 使用第一个关键词进行搜索 return search_sina_news(keyword=keywords[0], time='d', page=1) + # 使用AI整理搜索结果 async def format_results_with_ai(client, results): if not results: @@ -78,6 +83,7 @@ async def format_results_with_ai(client, results): logger.error("大模型调用超时") raise HTTPException(status_code=500, detail="大模型调用超时") + # 主函数 async def get_news(client, user_input): keywords = extract_keywords(user_input) @@ -87,4 +93,3 @@ async def get_news(client, user_input): return formatted_response else: return "未找到相关新闻。" - diff --git a/AI/WxMini/安装.txt b/AI/WxMini/安装.txt index 211e27bb..50721070 100644 --- a/AI/WxMini/安装.txt +++ b/AI/WxMini/安装.txt @@ -4,4 +4,22 @@ https://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/Tencent_AILab_ChineseE # 显示已安装包的版本号 pip show gensim pip show jieba -pip show pymilvus \ No newline at end of file +pip show pymilvus + + + if '生成图片' in prompt or '生图' in prompt: + success, key = generate_image(prompt) + if success: + image_url = f"{OSS_PREFIX}{key}" + image_width, image_height = getImgWidthHeight(image_url) + # 记录聊天数据到 MySQL + await save_chat_to_mysql(app.state.mysql_pool, person_id, prompt, + key, '', 0, input_type=2, output_type=4, + input_image_type=0, image_width=image_width, image_height=image_height) + # 返回数据 + return { + "success": True, + "url": None, + "duration": 0, # 返回大模型的回复时长 + "response": key, # 返回大模型的回复 + } \ No newline at end of file