You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.0 KiB

5 months ago
from fastapi import FastAPI
from fastapi.responses import StreamingResponse, PlainTextResponse
import time
import socket
from openai import OpenAI
app = FastAPI()
5 months ago
# 阿里云中用来调用 deepseek v3 的密钥
5 months ago
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",
)
5 months ago
5 months ago
# 获取本机所有 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
5 months ago
5 months ago
# 模拟逐字生成数据的函数SSE 格式)
def generate_data(text):
for char in text:
time.sleep(0.05) # 模拟延迟
5 months ago
# yield f"data: {char}\n\n".encode("utf-8") # 按照 SSE 格式返回数据
5 months ago
yield f"{char}".encode("utf-8")
5 months ago
5 months ago
# 根路由,返回提示信息
@app.get("/")
def root():
return PlainTextResponse("Hello ApiStream")
5 months ago
5 months ago
# 流式返回数据SSE
5 months ago
'''
http://10.10.21.20:8000/stream?course_name=%E9%97%B0%E5%9C%9F
'''
5 months ago
@app.get("/stream")
5 months ago
def stream_data(course_name: str):
if not course_name:
return PlainTextResponse("请提供课程名称,例course_name=三角形面积")
5 months ago
# 调用阿里云 API 获取文本内容
completion = client.chat.completions.create(
model=MODEL_NAME,
messages=[
5 months ago
{'role': 'system', 'content': '你是一个教学经验丰富的基础教育教师'},
{'role': 'user', 'content': '帮我设计一下' + course_name + '的课件提纲用markdown格式返回。'}
5 months ago
],
5 months ago
timeout=6000,
# stream=True,
5 months ago
)
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", # 禁用缓存
5 months ago
"Connection": "keep-alive", # 保持连接
5 months ago
"Access-Control-Allow-Origin": "*", # 允许跨域
5 months ago
"X-Accel-Buffering": "no" # 禁用 Nginx 缓冲(如果有代理)
5 months ago
}
)
5 months ago
5 months ago
# 运行应用
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 地址
5 months ago
uvicorn.run(app, host="0.0.0.0", port=8000)