Langchain-Chatchat的流式响应api写法
·
Langchain-Chatchat的流式接口如何访问呢?
参考网上有说明用websocket接口的示例。但是看当下最新的github里的api.py里,已经没有用websocket了。而是用了http的流式协议。
看Langchain-Chatchat的api文档,没有看到调用端的代码。
摸索了好一阵,发现是这样的,python3.10的requests包就支持,只需要设置stream=True。后面再按流式方式接收响应,就可以达到流式API调用的效果。
下面是实现代码,希望对大家有参考。而部署和启动Langchain-Chatchat的api(v0.2.3),请参考其它的文章。
########### 下面是参考代码 ###########
import time
import requests
if name == ‘main’:
url = 'http://192.168.0.102:7861/chat/chat'
response = requests.post(url,
stream=True,
headers={
'Authorization': '',
'Content-Type': 'application/json'
},
json={
'query': "如何飞行?",
'max_tokens': 5,
'temperature': 0.5
},
)
if response.status_code == 200:
for line in response.iter_lines():
if line:
print(line.decode("utf-8"))
time.sleep(1)
更多推荐
所有评论(0)