This commit is contained in:
2025-08-31 08:44:03 +08:00
parent 09a5aa166e
commit 6b241749ae
3 changed files with 29 additions and 118 deletions

View File

@@ -1,7 +1,9 @@
from openai import OpenAI, AsyncOpenAI
from Config.Config import *
import sys import sys
from openai import AsyncOpenAI
from Config.Config import *
# 保留原有的prompt定义 # 保留原有的prompt定义
# prompt=""" | # prompt=""" |
@@ -46,6 +48,7 @@ async def get_xueban_response_async(query_text: str, stream: bool = True):
绝不: 绝不:
- 长篇大论,叽叽歪歪 - 长篇大论,叽叽歪歪
- 长时间严肃对话 - 长时间严肃对话
- 每次回答不要太长控制在3分钟以内
""" """
# 打开文件读取知识内容 # 打开文件读取知识内容
f = open(r"D:\dsWork\dsProject\dsLightRag\static\YunXiao.txt", "r", encoding="utf-8") f = open(r"D:\dsWork\dsProject\dsLightRag\static\YunXiao.txt", "r", encoding="utf-8")
@@ -87,117 +90,3 @@ async def get_xueban_response_async(query_text: str, stream: bool = True):
except Exception as e: except Exception as e:
print(f"大模型请求异常: {str(e)}", file=sys.stderr) print(f"大模型请求异常: {str(e)}", file=sys.stderr)
yield f"处理请求时发生异常: {str(e)}" yield f"处理请求时发生异常: {str(e)}"
# 同步获取大模型响应
def get_xueban_response(query_text: str, stream: bool = True):
"""
获取学伴角色的大模型响应
@param query_text: 查询文本
@param stream: 是否使用流式输出
@return: 完整响应文本
"""
client = OpenAI(
api_key=LLM_API_KEY,
base_url=LLM_BASE_URL,
)
# 创建请求
completion = client.chat.completions.create(
model=LLM_MODEL_NAME,
messages=[
{'role': 'system', 'content': prompt.strip()},
{'role': 'user', 'content': query_text}
],
stream=stream
)
full_response = []
if stream:
for chunk in completion:
# 提取当前块的内容
if chunk.choices and chunk.choices[0].delta and chunk.choices[0].delta.content:
content = chunk.choices[0].delta.content
full_response.append(content)
# 实时输出内容,不换行
print(content, end='', flush=True)
else:
# 非流式处理
full_response.append(completion.choices[0].message.content)
return ''.join(full_response)
# 测试用例 main 函数
def main():
"""
测试学伴工具接口的主函数
"""
print("===== 测试学伴工具接口 =====")
# 测试同步接口
test_sync_interface()
# 测试异步接口
import asyncio
print("\n测试异步接口...")
asyncio.run(test_async_interface())
print("\n===== 测试完成 =====")
def test_sync_interface():
"""测试同步接口"""
print("\n测试同步接口...")
# 测试问题
questions = [
"你是谁?",
"讲个冷笑话",
"你男朋友是做什么的?"
]
for question in questions:
print(f"\n问题: {question}")
try:
# 调用同步接口获取响应
print("获取学伴响应中...")
response = get_xueban_response(question, stream=False)
print(f"学伴响应: {response}")
# 简单验证响应
assert response.strip(), "响应内容为空"
print("✅ 同步接口测试通过")
except Exception as e:
print(f"❌ 同步接口测试失败: {str(e)}")
async def test_async_interface():
"""测试异步接口"""
# 测试问题
questions = [
"你是谁?",
"讲个冷笑话",
"你男朋友是做什么的?"
]
for question in questions:
print(f"\n问题: {question}")
try:
# 调用异步接口获取响应
print("获取学伴响应中...")
response_generator = get_xueban_response_async(question, stream=False)
response = ""
async for chunk in response_generator:
response += chunk
print(f"学伴响应: {response}")
# 简单验证响应
assert response.strip(), "响应内容为空"
print("✅ 异步接口测试通过")
except Exception as e:
print(f"❌ 异步接口测试失败: {str(e)}")
if __name__ == "__main__":
main()

View File

@@ -272,10 +272,32 @@
<!-- 示例3 --> <!-- 示例3 -->
<div class="bg-white dark:bg-gray-800 rounded-xl overflow-hidden shadow-lg transition-all duration-300 hover:shadow-xl group"> <div class="bg-white dark:bg-gray-800 rounded-xl overflow-hidden shadow-lg transition-all duration-300 hover:shadow-xl group">
<div class="relative overflow-hidden"> <div class="relative overflow-hidden">
<img src="https://picsum.photos/id/20/600/600" alt="示例图像" class="w-full aspect-square object-cover transition-transform duration-500 group-hover:scale-110"> <img src="https://pic2.zhimg.com/v2-e0e9ae420ce9536b9d5a31308c843483_1440w.jpg" alt="示例图像" class="w-full aspect-square object-cover transition-transform duration-500 group-hover:scale-110">
<div class="absolute inset-0 bg-gradient-to-t from-black/70 to-transparent opacity-0 group-hover:opacity-100 transition-opacity duration-300 flex items-end"> <div class="absolute inset-0 bg-gradient-to-t from-black/70 to-transparent opacity-0 group-hover:opacity-100 transition-opacity duration-300 flex items-end">
<div class="p-4 text-white"> <div class="p-4 text-white">
<p class="font-medium text-sm truncate">未来城市,赛博朋克风格</p> <p class="font-medium text-sm truncate">一张写有「山高水长,风清月明」的水墨画,搭配山川、竹林和飞鸟,文字清晰自然,风格一致</p>
</div>
</div>
</div>
</div>
<!-- 示例4 -->
<div class="bg-white dark:bg-gray-800 rounded-xl overflow-hidden shadow-lg transition-all duration-300 hover:shadow-xl group">
<div class="relative overflow-hidden">
<img src="https://pic1.zhimg.com/v2-f964d7744d4bcf808003ce64bd913482_1440w.jpg" alt="示例图像" class="w-full aspect-square object-cover transition-transform duration-500 group-hover:scale-110">
<div class="absolute inset-0 bg-gradient-to-t from-black/70 to-transparent opacity-0 group-hover:opacity-100 transition-opacity duration-300 flex items-end">
<div class="p-4 text-white">
<p class="font-medium text-sm truncate">一位身着淡雅水粉色交领襦裙的年轻女子背对镜头而坐,俯身专注地手持毛笔在素白宣纸上书写“通義千問”四个遒劲汉字。古色古香的室内陈设典雅考究,案头错落摆放着青瓷茶盏与鎏金香炉,一缕熏香轻盈升腾;柔和光线洒落肩头,勾勒出她衣裙的柔美质感与专注神情,仿佛凝固了一段宁静温润的旧时光。</p>
</div>
</div>
</div>
</div>
<!-- 示例5 -->
<div class="bg-white dark:bg-gray-800 rounded-xl overflow-hidden shadow-lg transition-all duration-300 hover:shadow-xl group">
<div class="relative overflow-hidden">
<img src="https://pica.zhimg.com/v2-982a735ace0bbc83ad7ed67c700a183c_1440w.jpg" alt="示例图像" class="w-full aspect-square object-cover transition-transform duration-500 group-hover:scale-110">
<div class="absolute inset-0 bg-gradient-to-t from-black/70 to-transparent opacity-0 group-hover:opacity-100 transition-opacity duration-300 flex items-end">
<div class="p-4 text-white">
<p class="font-medium text-sm truncate">一个咖啡店门口有一个黑板,上面写着 AI 咖啡2元一杯旁边有个霓虹灯写着开源中国旁边有个海报海报上面是一个中国美女海报下方写着 Gitee AI。</p>
</div> </div>
</div> </div>
</div> </div>