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

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

1. 引入

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

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

2. 抓包过程

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

  1. 首先,启动wireshark

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

from langchain_openai import ChatOpenAI
from langchain.schema import HumanMessagellm = 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: chunkedc5
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.mrgr.cn/news/64423.html

相关文章:

  • Python使用K-means实现文本聚类
  • 解决pytorch问题:received an invalid combination of arguments - got
  • 【设计模式系列】总览
  • 【MacOS实操】如何基于SSH连接远程linux服务器
  • qt QBrush详解
  • 【AIGC】2024-arXiv-Lumiere:视频生成的时空扩散模型
  • next项目app router 中layout命名规范
  • ViT面试知识点
  • Google Guava 发布订阅模式/生产消费者模式 使用详情
  • SpringMVC的执行流程以及运行原理
  • 单链表OJ题(3):合并两个有序链表、链表分割、链表的回文结构
  • Oracle视频基础1.4.2练习
  • FFmpeg 4.3 音视频-多路H265监控录放C++开发十. 多线程控制帧率。
  • 大学新生入门编程的最佳选择:为什么我推荐Python?
  • RSI是指在5G通信技术中用于标识小区的特定参数
  • Spring框架中的AOP是什么?如何使用AOP实现切面编程和拦截器功能?
  • 3.2链路聚合
  • P3-2.【结构化程序设计】第二节——知识要点:多分支选择语句
  • 2024年系统架构师---下午题目真题
  • php开发实战分析(8):优化MySQL分页查询与数量统计,提升数据库性能
  • sql在hive和阿里云maxComputer的区别
  • 合并区间 leetcode56
  • 【云备份项目】json以及jsoncpp库的使用
  • 在Zetero中调用腾讯云API的输入密钥的问题
  • 快速上手 Windows 命令:简化你的工作流程
  • tauri开发中如果取消了默认的菜单项,复制黏贴撤销等功能也就没有了,解决办法