无法通过Flask API发送OpenAI流响应。

3

用于在langchain中流式传输OpenAI响应

我使用这个https://python.langchain.com/en/latest/modules/models/chat/examples/streaming.html

它将响应流式传输到我的终端。我想使用Flask API将流式响应发送回用户

请任何人给予建议

我甚至尝试了自定义同步处理程序,但没有成功

class MyCustomSyncHandler(BaseCallbackHandler):
    def __iter__(self):
        for elem in self._datastructure:
            if elem.visible:
                yield elem.value
    def on_llm_new_token(self, token: str, **kwargs):
        yield b'data: {{"stream": '+ token+ '}}\n\n'
   

1个回答

1

我没有使用LangChain;只是使用内置的requests模块。然而,对于生产项目,请考虑使用Flask-SSE。

def open_ai_stream(prompt):
    url = config["open_ai_url"] #I am reading this from a config value 
#Value is this https://api.openai.com/v1/chat/completions
    session = requests.Session()
    payload = json.dumps({
        "model": "gpt-3.5-turbo",
            "messages": [
             {
            "role": "user",
            "content": prompt
            }
        ],
        "temperature": 0,
        "stream": True
        })
    headers = {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer '+ config['open_ai_key']
    }
    with session.post(url, headers=headers, data=payload) as resp:
        for line in resp.iter_lines():
            if line:
                yield f'data: %s\n\n' % line.decode('utf-8')

这是 Flask 路由:

@app.route("/llm-resp", methods=["POST"])
def stream_response():
    prompt = request.get_json(force = True)["prompt"]
    return Response(stream_with_context(open_ai_stream(query_text)),
                    mimetype="text/event-stream")

请查看测试结果(如果这是一个正常响应,那它在Postman上看起来会非常不同): {{link1:Postman快照}}
编辑:这里有一种稍微更好的方法,但需要添加openai库的依赖。
def chat_gpt_helper(prompt):
    """
    This function returns the response from OpenAI's Gpt3.5 turbo model using the completions API
    """
    try:
        resp = ''
        openai.api_key = os.getenv('OPEN_API_KEY')
        for chunk in openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=[{
                "role": "user",
                "content":prompt
            }],
            stream=True,
        ):
            content = chunk["choices"][0].get("delta", {}).get("content")
            if content is not None:
                    print(content, end='')
                    resp += content
                    yield f'data: %s\n\n' % resp

    except Exception as e:
        print(e)
        return str(e)

其余部分保持不变。 在此处检查完整代码here

编辑2:

如果有人无法在Postman中以块的形式查看响应,请确保您的Postman已经更新,因为在撰写本文时,这似乎是一个新功能。相关新闻发布


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接