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

网站备案所需资料小程序专区

网站备案所需资料,小程序专区,南京本地网站有哪些,自建网站如何被百度收录LangSmith可以记录LangChain程序对LLM的调用,但它需要登陆LangSmith网站才能看到。有什么办法在本地就能看到详细的信息,以方便调试LangChain编写的程序吗? 使用LangChain提供的set_debug(True) 在Python代码中只需要导入set_debug这个方法…

LangSmith可以记录LangChain程序对LLM的调用,但它需要登陆LangSmith网站才能看到。有什么办法在本地就能看到详细的信息,以方便调试LangChain编写的程序吗?

使用LangChain提供的set_debug(True)

在Python代码中只需要导入set_debug这个方法,再调用即可。参考代码如下

from langchain_ollama import ChatOllama  
from langchain.globals import set_debug  set_debug(True)  
llm = ChatOllama(model="llama3:8b")  
response = llm.invoke("What are you?")

这段代码访问了本地通过Ollama部署的llama3大模型,提了一个简单的问题就结束了。执行后我们可以在日志中看到如下的日志

[llm/start] [llm:ChatOllama] Entering LLM run with input:
{"prompts": ["Human: What are you?"]
}
[llm/end] [llm:ChatOllama] [8.92s] Exiting LLM run with output:
{"generations": [[{"text": "I am LLaMA, an AI assistant developed by Meta AI that can understand and respond to human input in a conversational manner. ... What would you like to talk about?",..."type": "ChatGeneration","message": {"lc": 1,"type": "constructor","id": ["langchain","schema","messages","AIMessage"],"kwargs": {..."usage_metadata": {"input_tokens": 14,"output_tokens": 116,"total_tokens": 130},"tool_calls": [],"invalid_tool_calls": []}...
}

这里我们能看到LangChain代码内部运行产生的输入输出数据。在输出数据中,能看到大模型返回的信息、消耗的token等信息。如果使用了LangSmith的话,你会发现这些信息和LangSmith里记录的信息差不多(不过LangSmith里还额外记录了很多元数据)。

有的时候你可能想查看更多的信息,比如程序和后端大模型API服务的http请求响应信息,需要怎么做呢?

启用全局的Debug日志

在代码里启用全局的Debug日志,代码如下

from langchain_ollama import ChatOllama  
import logging  
logging.basicConfig(level=logging.DEBUG)  llm = ChatOllama(model="llama3:8b")  
response = llm.invoke("What are you?")

这时运行程序会看到下面的日志

DEBUG:httpcore.connection:connect_tcp.started host='127.0.0.1' port=11434 local_address=None timeout=None socket_options=None
DEBUG:httpcore.connection:connect_tcp.complete return_value=<httpcore._backends.sync.SyncStream object at 0x11876b520>
DEBUG:httpcore.http11:send_request_headers.started request=<Request [b'POST']>
DEBUG:httpcore.http11:send_request_headers.complete
DEBUG:httpcore.http11:send_request_body.started request=<Request [b'POST']>
DEBUG:httpcore.http11:send_request_body.complete
DEBUG:httpcore.http11:receive_response_headers.started request=<Request [b'POST']>
DEBUG:httpcore.http11:receive_response_headers.complete return_value=(b'HTTP/1.1', 200, b'OK', [(b'Content-Type', b'application/x-ndjson'), (b'Date', b'Thu, 01 May 2025 05:59:24 GMT'), (b'Transfer-Encoding', b'chunked')])
INFO:httpx:HTTP Request: POST http://127.0.0.1:11434/api/chat "HTTP/1.1 200 OK"
DEBUG:httpcore.http11:receive_response_body.started request=<Request [b'POST']>
DEBUG:httpcore.http11:receive_response_body.complete
DEBUG:httpcore.http11:response_closed.started
DEBUG:httpcore.http11:response_closed.complete

现在能看到http请求和响应的信息了,但也只是一点点而已,只有后端服务的地址、端口、header、HTTP状态码。还能有更多的信息吗?机缘巧合下,我发现对程序稍加修改就能看到更多的信息。

from langchain_openai import ChatOpenAI  
import logging  
logging.basicConfig(level=logging.DEBUG)  llm = ChatOpenAI(  model="llama3:8b",  base_url="http://localhost:11434/v1",  api_key="123456"  
)  
response = llm.invoke("What are you?")

这里把对ChatOllama的使用改为ChatOpenAI,日志中就会多出一些openai的日志

DEBUG:openai._base_client:Request options: {'method': 'post', 'url': '/chat/completions', 'files': None, 'idempotency_key': 'stainless-python-retry-fe5f895d-c0da-4216-a273-b16f967433d3', 'json_data': {'messages': [{'content': 'What are you?', 'role': 'user'}], 'model': 'llama3:8b', 'stream': False}}
DEBUG:openai._base_client:Sending HTTP Request: POST http://localhost:11434/v1/chat/completions

这里我们能看到和后端大模型服务交互的地址和HTTP request body(也就是上面json_data的值),但还是缺少HTTP response的详细信息。好在ChatOpenAI这个构造函数允许我们传递http_client,于是我们就可以对http客户端做一些修改

from langchain_openai import ChatOpenAI  
import logging  
logging.basicConfig(level=logging.DEBUG)  import httpx  
def log_request(request):  print(f"Request: {request.method} {request.url}")  print("Headers:", request.headers)  print("Body:", request.content.decode())  def log_response(response):  response.read()  print(f"Response: {response.status_code}")  print("Headers:", response.headers)  print("Body:", response.text)  client = httpx.Client(  event_hooks={  "request": [log_request],  "response": [log_response],  }  
)  llm = ChatOpenAI(  model="llama3:8b",  base_url="http://localhost:11434/v1",  api_key="123456",  http_client=client  
)  
response = llm.invoke("What are you?")

给httpx.Client设置请求和响应的hook,让它在发送请求和收到响应后打印请求和相应信息。现在再运行代码,会看到更详细的日志

Request: POST http://localhost:11434/v1/chat/completions
Headers: Headers({'host': 'localhost:11434', 'accept-encoding': 'gzip, deflate, zstd', 'connection': 'keep-alive', 'accept': 'application/json', 'content-type': 'application/json', 'user-agent': 'OpenAI/Python 1.76.0', 'x-stainless-lang': 'python', 'x-stainless-package-version': '1.76.0', 'x-stainless-os': 'MacOS', 'x-stainless-arch': 'arm64', 'x-stainless-runtime': 'CPython', 'x-stainless-runtime-version': '3.10.16', 'authorization': '[secure]', 'x-stainless-async': 'false', 'x-stainless-retry-count': '0', 'content-length': '91'})
Body: {"messages":[{"content":"What are you?","role":"user"}],"model":"llama3:8b","stream":false}...Response: 200
Headers: Headers({'content-type': 'application/json', 'date': 'Thu, 01 May 2025 06:29:08 GMT', 'content-length': '1262'})
Body: {"id":"chatcmpl-96","object":"chat.completion","created":1746080948,"model":"llama3:8b","system_fingerprint":"fp_ollama","choices":[{"index":0,"message":{"role":"assistant","content":"I am LLaMA, an AI assistant developed by Meta AI ... when needed."},"finish_reason":"stop"}],"usage":{"prompt_tokens":14,"completion_tokens":184,"total_tokens":198}}

这下程序和后端大模型API服务的http请求响应信息都完整的打印出来了。这里引申出一个问题

必须使用ChatOpenAI才能得到http请求和响应信息吗?

当前的 ChatOpenAI(版本0.3.14)支持我们传入http_client参数,所以我们才有机会通过hook来打印http信息。我查看了下面几个chat model,发现它们都不支持http_client的传入,所以这个方案必须要使用ChatOpenAI。

  • ChatTongyi
  • ChatBaichuan
  • ChatCoze
  • ChatOllama

但这同时也带来一个使用的限制:后端大模型服务必须要兼容OpenAI的API规范(否则无法使用ChatOpenAI和他们通信)。

好了,文章到此结束,希望能给你带来一些帮助。

http://www.dtcms.com/a/510947.html

相关文章:

  • 网站空间多少钱一年网站开发项目进度表
  • 上海网站建设公司价格免费域名空间国外
  • 南京工大建设工程技术有限公司网站wordpress采集微博
  • 丹阳做网站的哪个网站做视频有收益
  • 广州网站建设排名一览表摄影师如何做网站
  • 全球最好的域名注册公司长春seo优化
  • 天津市建设工程质量安全监督管理总队网站免费的网站域名查询app
  • 番禺建设银行网站信誉好的购物网站建设
  • 微网站的特点服务器 多wordpress
  • 石家庄开发网站建设网站免费优化平台
  • 网站建设背景需要写些什么网站建设软硬件平台有哪些
  • 个人网站的留言板怎么做综合门户网站什么意思
  • 东莞市国外网站建设报价眉山市网站建设
  • 四川省建设厅中心网站定制开发小程序的公司
  • 如何使用开源程序做网站移动通信网站建设
  • 好看 大气的网站seo网站建设优化
  • 中国制造网国际站网站建设专家排名
  • 网站建设有关图片手机购物平台
  • 建筑设计方案网站企业设计公司
  • 成都网站建设优点网站开发流程分为哪几个阶段
  • 网站开发地图wordpress主题 ipcme
  • 中文域名网站建设网销怎么找客户
  • 相城区住房建设局网站wordpress 移动端网页
  • 我有虚拟服务器怎么快速做网站阿里巴巴网站建设教程视频
  • 信誉比较好的网上做任务的网站建设通小程序
  • pw域名网站商标注册查询网官网
  • 大兴建设网站设计一个商务网站
  • 比如做百度知道 .html,这些都是我们不可控制的网站!html网站设计实例代码
  • 云虚拟主机和网站建设襄州区住房和城乡建设局网站
  • 网站开发一个多少钱啊荣盛科技网站建设