当前位置: 首页 > news >正文

How can I stream a response from LangChain‘s OpenAI using Flask API?

题意:怎样在 Flask API 中使用 LangChain 的 OpenAI 模型流式传输响应

问题背景:

I am using Python Flask app for chat over data. In the console I am getting streamable response directly from the OpenAI since I can enable streming with a flag streaming=True.

我正在使用 Python Flask 应用程序进行数据聊天。在控制台中,我直接从 OpenAI 获取流式响应,因为我可以通过设置 `streaming=True` 来启用流式传输。

The problem is, that I can't "forward" the stream or "show" the stream than in my API call.

问题是,我无法在 API 调用中“转发”或“显示”这个流式响应。

Code for the processing OpenAI and chain is:

处理 OpenAI 和链的代码如下:

def askQuestion(self, collection_id, question):collection_name = "collection-" + str(collection_id)self.llm = ChatOpenAI(model_name=self.model_name, temperature=self.temperature, openai_api_key=os.environ.get('OPENAI_API_KEY'), streaming=True, callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]))self.memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True,  output_key='answer')chroma_Vectorstore = Chroma(collection_name=collection_name, embedding_function=self.embeddingsOpenAi, client=self.chroma_client)self.chain = ConversationalRetrievalChain.from_llm(self.llm, chroma_Vectorstore.as_retriever(similarity_search_with_score=True),return_source_documents=True,verbose=VERBOSE, memory=self.memory)result = self.chain({"question": question})res_dict = {"answer": result["answer"],}res_dict["source_documents"] = []for source in result["source_documents"]:res_dict["source_documents"].append({"page_content": source.page_content,"metadata":  source.metadata})return res_dict

and the API route code:        以及 API 路由的代码:

@app.route("/collection/<int:collection_id>/ask_question", methods=["POST"])
def ask_question(collection_id):question = request.form["question"]# response_generator = document_thread.askQuestion(collection_id, question)# return jsonify(response_generator)def stream(question):completion = document_thread.askQuestion(collection_id, question)for line in completion['answer']:yield line

I am testing my endpoint with curl and I am passing flag -N to curl, so I should get the streamable response, if it is possible.

我正在使用 curl 测试我的端点,并传递了 `-N` 标志,因此如果可能的话,我应该能得到流式响应。

When I make API call first the endpoint is waiting to process the data (I can see in my terminal in VS code the streamable answer) and when finished, I get everything displayed in one go.

当我发起 API 调用时,端点首先等待处理数据(我可以在 VS Code 的终端中看到流式的回答),处理完成后,所有内容一次性显示出来。

问题解决:

With the usage of threading and callback we can have a streaming response from flask API.

通过使用 `threading` 和 `callback`,我们可以在 Flask API 中实现流式响应。

In flask API, you may create a queue to register tokens through langchain's callback.

在 Flask API 中,可以创建一个队列,通过 LangChain 的回调函数来注册令牌。

class StreamingHandler(BaseCallbackHandler):...def on_llm_new_token(self, token: str, **kwargs) -> None:self.queue.put(token)

You may get tokens from the same queue in your flask route.

你可以在 Flask 路由中从同一个队列获取令牌。

from flask import Response, stream_with_context
import threading @app.route(....):
def stream_output():q = Queue()def generate(rq: Queue):...# add your logic to prevent while loop# to run indefinitely  while( ...):yield rq.get()callback_fn = StreamingHandler(q)threading.Thread(target= askQuestion, args=(collection_id, question, callback_fn))return Response(stream_with_context(generate(q))

In your langchain's ChatOpenAI add the above custom callback StreamingHandler.

在你的 LangChain 的 `ChatOpenAI` 中添加上述自定义回调 `StreamingHandler`。

self.llm = ChatOpenAI(model_name=self.model_name, temperature=self.temperature, openai_api_key=os.environ.get('OPENAI_API_KEY'), streaming=True, callback=[callback_fn,]
)

For reference:        参考如下

  • https://python.langchain.com/en/latest/modules/callbacks/getting_started.html#creating-a-custom-handler
  • Streaming Contents — Flask Documentation (2.3.x)

http://www.lryc.cn/news/443892.html

相关文章:

  • 什么是慢充优惠话费充值api?如何选择平台
  • 【MySQL 03】表的操作
  • 3、论文阅读:EnYOLO:一种基于图像增强的水下目标区域自适应实时检测框架
  • MYSQL面试知识点手册
  • 排序算法的分析和应用
  • iptables限制网速
  • ALSA ubuntu 编译
  • 【学习笔记】SSL/TLS证书安全机制之证书透明
  • 网络编程问题解答
  • 【开源免费】基于SpringBoot+Vue.JS服装商城系统(JAVA毕业设计)
  • C语言字符串学习
  • 当你在Linux系统中使用MySQL命令行工具查询数据库时,如果中文显示为问号(?)或其他乱码,简单解决办法。(2)
  • API网关之Fizz Gateway
  • pgvector docker版安装;稀疏向量使用;psycopg2 python连接使用
  • C#命令行参数解析库System.CommandLine介绍
  • CCF CSP题解:密码(key)(202409-1)
  • RuntimeError: Maximum Recursion Depth Exceeded - 递归深度超限的完美解决方案
  • Linux1-ls,cd,pwd
  • 【高级编程】XML DOM4J解析XML文件(含案例)
  • 查看VSFTPD配置的服务器路径和linux系统有哪些用户
  • JavaEE: 创造无限连接——网络编程中的套接字
  • 记K8s组件harbor和kuboard故障恢复
  • c++ return {};
  • 【设计模式-适配】
  • 深度学习02-pytorch-08-自动微分模块
  • 使用Python实现深度学习模型:智能宠物监控与管理
  • 【HTTPS】对称加密和非对称加密
  • MySQL中的LIMIT与ORDER BY关键字详解
  • Java 编码系列:集合框架(List、Set、Map 及其常用实现类)
  • Go进阶概览 -【7.2 泛型的使用与实现分析】