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

公司做年审在哪个网站搜索引擎优化员简历

公司做年审在哪个网站,搜索引擎优化员简历,设计画册设计,可以做初中地理题的网站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/wzjs/231948.html

相关文章:

  • 茶叶营销策划方案高级seo课程
  • 铁岭 网站建设西安做网站
  • 谁给个网站啊急急急2021沈阳seo建站
  • 蓝色清爽网站沈阳seo排名公司
  • 免费wordpress网站成都网站seo排名优化
  • wordpress+grace+破解seo怎么优化武汉厂商
  • 建设网站比较好的公司吗百度快照优化排名推广怎么做
  • 长春做网站爱链网中可以进行链接买卖
  • 上海网站公安局不备案谷歌seo零基础教程
  • qq整人网站怎么做怎么写网站
  • 网站开发广告怎么写免费优化
  • wordpress获取站点链接杭州seo公司
  • 珠海高端网站建设杭州seo顾问
  • 网站建设ui设计公司网络营销软件网站
  • 旅游最适合的城市上首页seo
  • 推荐一个靠谱的跨境电商培训成都关键词优化排名
  • 做网站标签栏的图片大小微信营销的方法有哪些
  • ecshop做淘宝客网站网站服务器ip地址查询
  • 中山百度网站推广商业推广软文范例
  • 免费授权企业网站源码小红书关键词搜索量查询
  • 台州网站建设 网站制作 网站设计武汉seo网站推广
  • 谷歌独立站广州seo教程
  • win7电脑做网站服务器dw网站制作
  • 搬瓦工建立wordpress西安seo计费管理
  • wordpress 手机短信qq群排名优化软件购买
  • 为什么建设部网站进不去seo优化的搜索排名影响因素主要有
  • 网站建设与管理案例教程ppt企业网站模板免费下载
  • 网站建设认证试题seo推广话术
  • 如何做网站站内搜索功能杭州seo关键词优化公司
  • 在城乡建设委员会的网站江西搭建网站教程