wireshark抓包查看langchain的ChatOpenAI接口发送和接收的数据

news/2024/11/3 3:11:41 标签: wireshark, langchain, 测试工具

1. 引入

当我们用vllm部署一个大模型,就可以调用langchain的ChatOpenAI()接口来访问大模型(具体过程参考[1]),这也是langchain的Agent的基础接口使用方式。

那么问题来了,这个接口是使用哪种方式与大模型进行通信的呢?

2. 抓包过程

我们可以通过抓包这个过程来看一看:

  1. 首先,启动wireshark

  2. 运行如下python代码,与大模型进行通信

from langchain_openai import ChatOpenAI
from langchain.schema import HumanMessage

llm = ChatOpenAI(
    streaming=True,
    verbose=True,
    openai_api_key="none",
    openai_api_base='http://10.11.12.13:4000', 
    model_name="aaa-gpt" 
)
output = llm([HumanMessage(content="你好")])
print(output.content) # 你好!很高兴为你提供帮助。有什么问题或需要什么信息呢?
  1. 使用wireshark搜索目的地址为大模型服务IP的内容

搜索:ip.dst eq 10.11.12.13 and tcp
在这里插入图片描述
可以看到,ChatOpenAI接口,往目的地址,POST了一个HTTP请求到/chat/completions,并收到回复。

要知道具体POST了什么内容,可以鼠标右键,选“追踪流”:
在这里插入图片描述
就能看到
(1)接口发送的内容如下

POST /chat/completions HTTP/1.1
Host: 10.11.12.13:4000
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Accept: application/json
Content-Type: application/json
User-Agent: OpenAI/Python 1.51.2
X-Stainless-Lang: python
X-Stainless-Package-Version: 1.51.2
X-Stainless-OS: Windows
X-Stainless-Arch: other:amd64
X-Stainless-Runtime: CPython
X-Stainless-Runtime-Version: 3.12.7
Authorization: Bearer none
X-Stainless-Async: false
x-stainless-retry-count: 0
Content-Length: 122

{"messages": [{"content": "\u4f60\u597d", "role": "user"}], "model": "aa-gpt", "n": 1, "stream": true, "temperature": 0.7}

这里有两个关键点:

  • \u4f60\u597d:是中文“你好”的unicode
  • Authorization: Bearer none,这里的none就是python代码中的openai_api_key

可以看到,openai_api_key,是被放在POST请求的header中发送的。

(2)接收/大模型回复的内容如下

HTTP/1.1 200 OK
date: Tue, 15 Oct 2024 06:39:28 GMT
server: uvicorn
x-litellm-call-id: 1111c2cf-011c-4111-b110-c31111113e11
x-litellm-model-id: 15111111da111111c2618be6ed1111113a15111111db0101111114c111111725
x-litellm-version: 1.43.4
x-litellm-key-tpm-limit: None
x-litellm-key-rpm-limit: None
llm_provider-date: Tue, 15 Oct 2024 06:39:28 GMT
llm_provider-server: uvicorn
llm_provider-content-type: text/event-stream; charset=utf-8
llm_provider-transfer-encoding: chunked
content-type: text/event-stream; charset=utf-8
transfer-encoding: chunked

c5
data: {"id":"chat-111111111111111111111111111","choices":[{"index":0,"delta":{"content":"......","role":"assistant"}}],"created":1728974369,"model":"tq-gpt","object":"chat.completion.chunk"}
af
data: {"id":"chat-111111111111111111111111111","choices":[{"index":0,"delta":{"content":"..."}}],"created":1728974369,"model":"tq-gpt","object":"chat.completion.chunk"}
b5
data: {"id":"chat-111111111111111111111111111","choices":[{"index":0,"delta":{"content":"........."}}],"created":1728974369,"model":"tq-gpt","object":"chat.completion.chunk"}
af
data: {"id":"chat-111111111111111111111111111","choices":[{"index":0,"delta":{"content":"..."}}],"created":1728974369,"model":"tq-gpt","object":"chat.completion.chunk"}
b2
data: {"id":"chat-111111111111111111111111111","choices":[{"index":0,"delta":{"content":"......"}}],"created":1728974369,"model":"tq-gpt","object":"chat.completion.chunk"}
b2
data: {"id":"chat-111111111111111111111111111","choices":[{"index":0,"delta":{"content":"......"}}],"created":1728974369,"model":"tq-gpt","object":"chat.completion.chunk"}
af
data: {"id":"chat-111111111111111111111111111","choices":[{"index":0,"delta":{"content":"..."}}],"created":1728974369,"model":"tq-gpt","object":"chat.completion.chunk"}
b5
data: {"id":"chat-111111111111111111111111111","choices":[{"index":0,"delta":{"content":"........."}}],"created":1728974369,"model":"tq-gpt","object":"chat.completion.chunk"}
b2
data: {"id":"chat-111111111111111111111111111","choices":[{"index":0,"delta":{"content":"......"}}],"created":1728974369,"model":"tq-gpt","object":"chat.completion.chunk"}
b2
data: {"id":"chat-111111111111111111111111111","choices":[{"index":0,"delta":{"content":"......"}}],"created":1728974369,"model":"tq-gpt","object":"chat.completion.chunk"}
b2
data: {"id":"chat-111111111111111111111111111","choices":[{"index":0,"delta":{"content":"......"}}],"created":1728974369,"model":"tq-gpt","object":"chat.completion.chunk"}
b2
data: {"id":"chat-111111111111111111111111111","choices":[{"index":0,"delta":{"content":"......"}}],"created":1728974369,"model":"tq-gpt","object":"chat.completion.chunk"}
af
data: {"id":"chat-111111111111111111111111111","choices":[{"index":0,"delta":{"content":"..."}}],"created":1728974369,"model":"tq-gpt","object":"chat.completion.chunk"}
af
data: {"id":"chat-111111111111111111111111111","choices":[{"index":0,"delta":{"content":"..."}}],"created":1728974369,"model":"tq-gpt","object":"chat.completion.chunk"}
af
data: {"id":"chat-111111111111111111111111111","choices":[{"index":0,"delta":{"content":"..."}}],"created":1728974369,"model":"tq-gpt","object":"chat.completion.chunk"}
ac
data: {"id":"chat-111111111111111111111111111","choices":[{"index":0,"delta":{"content":""}}],"created":1728974369,"model":"tq-gpt","object":"chat.completion.chunk"}
b7
data: {"id":"chat-111111111111111111111111111","choices":[{"finish_reason":"stop","index":0,"delta":{}}],"created":1728974369,"model":"tq-gpt","object":"chat.completion.chunk"}
e
data: [DONE]
0

3. 测试POST

把上面分析得到的内容,POST的header配置,写为如下的POST请求:

curl http://10.11.12.13:4000/chat/completions \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer none" \
    -d '{"model": "aa-gpt","messages": [{"role":"user","content":"你好"}]}'

到linux上测试,得到的结果也和上面接口的一致。

4. 总结

通过wireshark抓包,目的IP地址过滤,追踪流,就能看到langchain的ChatOpenAI发送的POST请求细节内容,并能依此构造一个POST请求来模拟该接口的通信。

参考

  1. https://blog.csdn.net/ybdesire/article/details/140691972

http://www.niftyadmin.cn/n/5735928.html

相关文章

Data+AI━━人群圈选,你被圈中了吗?

DataAI━━人群圈选,你被圈中了吗? 前言我们是否正在失去选择的自主权?AI人群圈选流程AI人群圈选技术发展趋势 前言 智能时代的清晨,打开手机,各类APP精准推送的信息扑面而来。菜市场买过一次三文鱼,生鲜A…

软件测试学习笔记丨Flask操作数据库-对象与数据模型

本文转自测试人社区,原文链接:https://ceshiren.com/t/topic/23440 对象与数据模型 数据模型:是数据特征的抽象,抽象层次上描述了系统的静态特征、动态行为和约束条件,为数据库系统的信息表示与操作提供一个抽象的框架…

探秘机器学习算法:智慧背后的代码逻辑

1、 线性回归 线性回归是预测连续变量的一种简单而有效的方法。其数学模型假设因变量 y 与自变量 x 之间存在线性关系,用公式表示为: ​ Python代码实现 import numpy as np from sklearn.linear_model import LinearRegression import matplotlib.…

T-Mobile股票分析:T-Mobile的股价还能继续上涨吗?

猛兽财经核心观点: (1)今年以来T-Mobile股价已经上涨42%。 (2)强劲的第三季度业绩:盈利超过预期,提高了全年指引,扩大了客户群和合作伙伴关系。 (3)猛兽财经对…

初识二叉树( 二)

初识二叉树 二 实现链式结构二叉树前中后序遍历遍历规则代码实现 结点个数以及高度等层序遍历判断是否为完全二叉树 实现链式结构二叉树 ⽤链表来表示⼀棵二叉树,即用链来指示元素的逻辑关系。通常的方法是链表中每个结点由三个域组成,数据域和左右指针…

大型语言模型(LLM)的小型化研究进展

2024年,大型语言模型(LLM)的小型化研究取得了显著进展,主要采用以下几种方法实现: 模型融合:通过将多个模型或检查点合并为一个单一模型,减少资源消耗并提升整体性能。例如,《WARM: …

基于springboot的社区团购系统设计与实现

一、项目背景 网络交易(Electronic Commerce):是指实现整个贸易过程中各阶段的贸易活动的电子化。网络交易是一种多技术的集合体。其业务可包括:信息交换、售后服务、销售、电子支付、运输、组建虚拟企业、公司和贸易伙伴可以共同…

软考:大数据架构设计

大数据总结 大数据处理系统的特征 1、鲁棒性和容错性 2、低延迟读取和更新能力 3、横向扩容 4、通用性 5、延展性 6、即席查询能力 7、最少维护能力 8、可调试性 Lambda架构 批处理层 存储数据集和生成Batch View 管理主数据集,原始的,不可变的&…