From ffdb664760db77e72ed12d023c5b80abc8476c77 Mon Sep 17 00:00:00 2001 From: HuangHai <10402852@qq.com> Date: Wed, 5 Mar 2025 08:06:02 +0800 Subject: [PATCH] 'commit' --- AI/AiService/TestChat.py | 24 +++++++++++ AI/AiService/TestStream.py | 4 +- AI/AiService/TestZhongHe.py | 86 +++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 AI/AiService/TestChat.py create mode 100644 AI/AiService/TestZhongHe.py diff --git a/AI/AiService/TestChat.py b/AI/AiService/TestChat.py new file mode 100644 index 00000000..0965fe49 --- /dev/null +++ b/AI/AiService/TestChat.py @@ -0,0 +1,24 @@ +from openai import OpenAI + +# 阿里云中用来调用 deepseek r1 的密钥 +MODEL_API_KEY = "sk-01d13a39e09844038322108ecdbd1bbc" +MODEL_NAME = "deepseek-v3" + +# 初始化 OpenAI 客户端 +client = OpenAI( + api_key=MODEL_API_KEY, + base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", +) + +# 调用语言模型 +completion = client.chat.completions.create( + model=MODEL_NAME, + messages=[ + {'role': 'system', 'content': 'You are a helpful assistant.'}, + {'role': 'user', 'content': '你是谁?'} + ], +) + +# 提取并打印文本内容 +response_content = completion.choices[0].message.content +print("返回的文本内容:", response_content) \ No newline at end of file diff --git a/AI/AiService/TestStream.py b/AI/AiService/TestStream.py index 6567ad17..8d2c6b52 100644 --- a/AI/AiService/TestStream.py +++ b/AI/AiService/TestStream.py @@ -48,6 +48,7 @@ def stream_data(): # 运行应用 if __name__ == "__main__": + import uvicorn # 获取本机所有 IPv4 地址 @@ -62,4 +63,5 @@ if __name__ == "__main__": print(f"http://{ip}:8000") # 启动 FastAPI 应用,绑定到所有 IP 地址 - uvicorn.run(app, host="0.0.0.0", port=8000) \ No newline at end of file + uvicorn.run(app, host="0.0.0.0", port=8000) + diff --git a/AI/AiService/TestZhongHe.py b/AI/AiService/TestZhongHe.py new file mode 100644 index 00000000..90ee4a60 --- /dev/null +++ b/AI/AiService/TestZhongHe.py @@ -0,0 +1,86 @@ +from fastapi import FastAPI +from fastapi.responses import StreamingResponse, PlainTextResponse +import time +import socket +from openai import OpenAI + +app = FastAPI() + +# 阿里云中用来调用 deepseek r1 的密钥 +MODEL_API_KEY = "sk-01d13a39e09844038322108ecdbd1bbc" +MODEL_NAME = "deepseek-v3" + +# 初始化 OpenAI 客户端 +client = OpenAI( + api_key=MODEL_API_KEY, + base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", +) + +# 获取本机所有 IPv4 地址 +def get_local_ips(): + ips = [] + hostname = socket.gethostname() + try: + # 获取所有 IP 地址 + addrs = socket.getaddrinfo(hostname, None, family=socket.AF_INET) # 只获取 IPv4 地址 + for addr in addrs: + ip = addr[4][0] + if ip not in ips: + ips.append(ip) + except Exception as e: + print(f"获取 IP 地址失败: {e}") + return ips + +# 模拟逐字生成数据的函数(SSE 格式) +def generate_data(text): + for char in text: + time.sleep(0.05) # 模拟延迟 + #yield f"data: {char}\n\n".encode("utf-8") # 按照 SSE 格式返回数据 + yield f"{char}".encode("utf-8") +# 根路由,返回提示信息 +@app.get("/") +def root(): + return PlainTextResponse("Hello ApiStream") + +# 流式返回数据(SSE) +@app.get("/stream") +def stream_data(): + # 调用阿里云 API 获取文本内容 + completion = client.chat.completions.create( + model=MODEL_NAME, + messages=[ + {'role': 'system', 'content': 'You are a helpful assistant.'}, + {'role': 'user', 'content': '你是谁?'} + ], + ) + text_content = completion.choices[0].message.content + + # 返回流式响应 + return StreamingResponse( + generate_data(text_content), + media_type="text/event-stream", # 使用 text/event-stream + headers={ + "Cache-Control": "no-cache", # 禁用缓存 + "Connection": "keep-alive", # 保持连接 + "Access-Control-Allow-Origin": "*", # 允许跨域 + "X-Accel-Buffering": "no" # 禁用 Nginx 缓冲(如果有代理) + } + ) + +# 运行应用 +if __name__ == "__main__": + import uvicorn + + # 获取本机所有 IPv4 地址 + ips = get_local_ips() + if not ips: + print("无法获取本机 IP 地址,使用默认地址 127.0.0.1") + ips = ["127.0.0.1"] + + # 打印所有 IP 地址 + print("服务将在以下 IP 地址上运行:") + for ip in ips: + print(f"http://{ip}:8000") + + # 启动 FastAPI 应用,绑定到所有 IP 地址 + uvicorn.run(app, host="0.0.0.0", port=8000) \ No newline at end of file