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

python中使用websocket调用、获取、保存大模型API

笔者最近在测试星火大模型的时候,他们是使用websocket 来建立对话,而且星火大模型开放的测试代码,质量上不咋地(20231030记录),还需要对websocket有一定的了解,才适合自己微调。

安装:

pip install websocket
pip install websocket-client

文章目录

  • 1 常见的websocket获取数据的方法
    • 1.1 第一种使用create_connection链接
    • 1.2 第二种:WebSocketApp + run_forever的方式
  • 2 针对`run_forever`内容保存
    • 2.1 通过定义global变量来保存内容
    • 2.2 通过`CallbackToIterator()`来返回


1 常见的websocket获取数据的方法

参考【python: websocket获取实时数据的几种常见链接方式】常见的两种。

1.1 第一种使用create_connection链接

需要pip install websocket-client (此方法不建议使用,链接不稳定,容易断,并且连接很耗时)

import time
from websocket import create_connectionurl = 'wss://i.cg.net/wi/ws'
while True:  # 一直链接,直到连接上就退出循环time.sleep(2)try:ws = create_connection(url)print(ws)breakexcept Exception as e:print('连接异常:', e)continue
while True:  # 连接上,退出第一个循环之后,此循环用于一直获取数据ws.send('{"event":"subscribe", "channel":"btc_usdt.ticker"}')response = ws.recv()print(response)

1.2 第二种:WebSocketApp + run_forever的方式

import websocketdef on_message(ws, message):  # 服务器有数据更新时,主动推送过来的数据print(message)def on_error(ws, error):  # 程序报错时,就会触发on_error事件print(error)def on_close(ws):print("Connection closed ……")def on_open(ws):  # 连接到服务器之后就会触发on_open事件,这里用于send数据req = '{"event":"subscribe", "channel":"btc_usdt.deep"}'print(req)ws.send(req)if __name__ == "__main__":websocket.enableTrace(True)ws = websocket.WebSocketApp("wss://i.cg.net/wi/ws",on_message=on_message,on_error=on_error,on_close=on_close)ws.on_open = on_openws.run_forever(ping_timeout=30)

第二种方式里面,run_forever其实是流式返回内容,大概可以看,流式输出的样例:


{"code":0,"sid":"5ebc0d6833b54909b4a51fbe75a5051a","status":0}
### error: 'content'{"code":0,"fileRefer":"{\"43816997a7a44a299d0bfb7c360c5838\":[2,0,1]}","sid":"5ebc0d6833b54909b4a51fbe75a5051a","status":99}
### error: 'content'{"code":0,"content":"橘","sid":"5ebc0d6833b54909b4a51fbe75a5051a","status":1}橘{"code":0,"content":"子。","sid":"5ebc0d6833b54909b4a51fbe75a5051a","status":1}子。{"code":0,"content":"","sid":"5ebc0d6833b54909b4a51fbe75a5051a","status":2}
### closed ###

那么run_forever流式输出,正常的内容如何保存呢,进入下一章


2 针对run_forever内容保存

2.1 通过定义global变量来保存内容

参考【将Websocket数据保存到Pandas】
来看一下,文中的案例:

import jsonimport pandas as pd
import websocketdf = pd.DataFrame(columns=['foreignNotional', 'grossValue', 'homeNotional', 'price', 'side','size', 'symbol', 'tickDirection', 'timestamp', 'trdMatchID'])def on_message(ws, message):msg = json.loads(message)print(msg)global df# `ignore_index=True` has to be provided, otherwise you'll get# "Can only append a Series if ignore_index=True or if the Series has a name" errorsdf = df.append(msg, ignore_index=True)def on_error(ws, error):print(error)def on_close(ws):print("### closed ###")def on_open(ws):returnif __name__ == "__main__":ws = websocket.WebSocketApp("wss://www.bitmex.com/realtime?subscribe=trade:XBTUSD",on_open=on_open, on_message=on_message, on_error=on_error, on_close=on_close)ws.run_forever()

其中global df是在定义全局变量df,可以在函数中把流式数据拿出来,还是很不错的

2.2 通过CallbackToIterator()来返回

在开源项目中ChuanhuChatGPT,看到了使用的方式spark.py,个人还没有尝试,只是贴在这里。

贴一下这个函数:

class CallbackToIterator:def __init__(self):self.queue = deque()self.cond = Condition()self.finished = Falsedef callback(self, result):with self.cond:self.queue.append(result)self.cond.notify()  # Wake up the generator.def __iter__(self):return selfdef __next__(self):with self.cond:# Wait for a value to be added to the queue.while not self.queue and not self.finished:self.cond.wait()if not self.queue:raise StopIteration()return self.queue.popleft()def finish(self):with self.cond:self.finished = Trueself.cond.notify()  # Wake up the generator if it's waiting.# 主函数截取
def get_answer_stream_iter(self):wsParam = Ws_Param(self.appid, self.api_key, self.api_secret, self.spark_url)websocket.enableTrace(False)wsUrl = wsParam.create_url()ws = websocket.WebSocketApp(wsUrl,on_message=self.on_message,on_error=self.on_error,on_close=self.on_close,on_open=self.on_open,)ws.appid = self.appidws.domain = self.domain# Initialize the CallbackToIteratorws.iterator = CallbackToIterator()# Start the WebSocket connection in a separate threadthread.start_new_thread(ws.run_forever, (), {"sslopt": {"cert_reqs": ssl.CERT_NONE}})# Iterate over the CallbackToIterator instanceanswer = ""total_tokens = 0for message in ws.iterator:data = json.loads(message)code = data["header"]["code"]if code != 0:ws.close()raise Exception(f"请求错误: {code}, {data}")else:choices = data["payload"]["choices"]status = choices["status"]content = choices["text"][0]["content"]if "usage" in data["payload"]:total_tokens = data["payload"]["usage"]["text"]["total_tokens"]answer += contentif status == 2:ws.iterator.finish()  # Finish the iterator when the status is 2ws.close()yield answer, total_tokens

截取了部分代码,这里先是定义ws.iterator = CallbackToIterator()然后通过迭代从for message in ws.iterator:拿出数据,看上去也是可行的

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

相关文章:

  • Linux的账号管理
  • 优优嗨聚集团:医保新政来袭,乙类OTC、保健品或将退出医保舞台,影响几何?
  • ubuntu安装pandora-gpt
  • PHP校验身份证号是否正确
  • [C++ ]:5.类和对象中(运算符重载补充)+ 类和对象下(初始化列表)
  • D. Doremy‘s Connecting Plan Codeforces Round 906 (Div. 2)
  • Prometheus+Grafana
  • CoCa论文笔记
  • uniapp 微信小程ios端键盘弹起后导致页面无法滚动
  • 三维模型优势在哪里?如何提升产品自身商业价值?
  • WheatA 轻量级生态数据软件
  • 2127. 参加会议的最多员工数 : 啥是内向/外向基环树(拓扑排序)
  • Qt入门日记1
  • SpringBoot_第七章(读写分离)
  • linux下mysql-8.2.0集群部署(python版本要在2.7以上)
  • 40 深度学习(四):卷积神经网络|深度可分离卷积|colab和kaggle的基础使用
  • Spring Boot面向切面加注解
  • uniapp小程序授权统一处理
  • 光学仿真|优化汽车内部照明体验
  • Spring XML使用CASE WHEN处理SELECT字段
  • 关于C#中使用多线程的讨论
  • 工程机械数字孪生可视化平台建设,推动大型装备智能化数字化转型升级
  • Linux 网络流量监控利器 iftop命令详解及实战
  • protected by SourceGuardian and requires a SourceGuardian loader ‘ixed.8解决方案
  • KWin、libdrm、DRM从上到下全过程 —— drmModeAddFBxxx(14)
  • 2023-macOS下安装anaconda,终端自动会出现(base)字样,如何取消
  • Nginx搭载负载均衡及前端项目部署
  • 深度学习——炼丹
  • Matlab中的app设计
  • 曾经遇到过的无法解释的问题