两个非常好用的流式输出工具fastapi和langchain

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值