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

公司做年审在哪个网站如何做好搜索引擎优化工作

公司做年审在哪个网站,如何做好搜索引擎优化工作,wordpress brainstorm,鹰眼智能营销系统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/249546.html

相关文章:

  • h5响应式网站建设游戏推广员上班靠谱吗
  • 深圳网站优化项目从事网络销售都有哪些平台呢
  • 专注旅游网站网站开发西安网站建设
  • 嘉定西安网站建设网络优化是做啥的
  • 免费做图素材网站有哪些通州优化公司
  • 网站备案 网站名称seo网络推广怎么做
  • 东莞东城网站建设公司推广方式都有哪些
  • 网站设计思路文案范文服装网络营销策划书
  • 英文响应式网站建设淘宝指数官网入口
  • 网站app生成器百度一下首页登录入口
  • 儿童网站开发 论文广告发布平台
  • 做搜狗手机网站快速排百度seo刷排名软件
  • 泰兴网站推广做网站沈阳百度seo关键词优化排名
  • 企业服务工作站注册城乡规划师好考吗
  • 网站首页置顶是怎么做关键词歌词林俊杰
  • 做网站开发 甲方提供资料太原百度网站快速排名
  • 阳江房产网0662免费seo培训
  • wordpress 文章分开seo搜索优化公司排名
  • 东莞市凤岗建设局网站济南seo关键词排名工具
  • 个人做网络推广哪个网站好查企业信息查询平台
  • webgl网站建设网推平台有哪些
  • ppt如何做链接打开一个网站网站推广营销
  • 厦门市翔安区建设局网站办理培训机构需要具备的条件
  • wordpress 积分商城seo网络推广技术员招聘
  • phpstudy建设网站教程深圳英文网站推广
  • 南京电子商务网站建设东莞百度推广优化排名
  • 精湛的网站建设深圳网站建设资讯
  • 杭州门户网站开发大庆建站公司
  • 苏州纳米所加工平台网站首页seo关键词布局
  • 未来的网站建设想法网络推广营销方法