from fastapi import FastAPI, Request
from fastapi.responses import StreamingResponse
from langchain_openai import ChatOpenAI
import json
from fastapi.staticfiles import StaticFiles
app = FastAPI()
# 配置 OpenAI API
url2 = r"http://10.0.101.250:7996/v1"
model_name = "Qwen2-72B-Instruct-feisuan"
openai_api_key = "bfe2acf258a744aeaa79b71e8c905386"
# 初始化模型
llm = ChatOpenAI(openai_api_key=openai_api_key,
openai_api_base=url2,
model_name=model_name,
max_retries=1,
streaming=True,
temperature=0.1,
max_tokens=5000)
async def event_generator(sql_query):
prompt = f'''{sql_query}'''
# 调用模型并获取流式响应
async for chunk in llm.astream(prompt):
# 假设每个chunk是一个字典,包含'id'和'text'
print(chunk.content)
json_=json.dumps({'text':chunk.content})
yield f"data: {json_}\n\n"
@app.get("/annotate_sql/")
async def annotate_sql(request: Request, sql_query: str):
return StreamingResponse(event_generator(sql_query), media_type="text/event-stream")
app.mount("/static", StaticFiles(directory="static"), name="static")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Database Table Annotator</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
textarea {
width: 100%;
height: 100px;
margin-bottom: 10px;
}
#output {
width: 100%;
height: 300px;
border: 1px solid #ccc;
padding: 10px;
overflow-y: scroll;
white-space: pre-wrap;
}
</style>
</head>
<body>
<h1>Database Table Annotator</h1>
<textarea id="sqlQuery" placeholder="Enter your SQL query here..."></textarea>
<button onclick="startAnnotating()">Annotate</button>
<div id="output"></div>
<script>
function startAnnotating() {
const sqlQuery = document.getElementById('sqlQuery').value;
if (!sqlQuery) {
alert('Please enter a SQL query.');
return;
}
const outputDiv = document.getElementById('output');
outputDiv.innerHTML = ''; // 清空之前的输出
const eventSource = new EventSource(`/annotate_sql/?sql_query=${encodeURIComponent(sqlQuery)}`);
eventSource.onmessage = function(event) {
const data = JSON.parse(event.data);
const text = data.text || '';
outputDiv.innerHTML += text.replace(/\n/g, '<br>'); // 将换行符转换为 <br>
outputDiv.scrollTop = outputDiv.scrollHeight; // 自动滚动到底部
};
eventSource.onerror = function() {
console.error('EventSource failed.');
eventSource.close();
};
}
</script>
</body>
</html>
文档目录:
├── main.py
└── static
└── index.html