AI创想

标题: LangChain 76 LangSmith 从入门到精通一 [打印本页]

作者: a纯情杜蕾斯    时间: 5 天前
标题: LangChain 76 LangSmith 从入门到精通一
作者:AI架构师易筋
LangChain系列文章
(, 下载次数: 1)


LangChain使得原型化LLM应用程序和代理变得容易。然而,将LLM应用程序交付到生产环境可能会出乎意料地困难。您将不得不对提示、链条和其他组件进行迭代,以构建高质量的产品。
LangSmith使得调试、测试和持续改进LLM应用程序变得容易。
这在什么时候可能会派上用场?当您想要:
1. 先决条件

创建一个LangSmith账户并生成一个API密钥(见左下角)。通过查看文档来熟悉平台。
请注意,LangSmith目前处于封闭测试阶段;我们正在逐步向更多用户推出。但是,您可以在网站上填写表格以获得加快访问速度。
现在,让我们开始吧!
2. 日志运行到LangSmith

首先,配置您的环境变量,告诉LangChain记录跟踪。这可以通过将LANGCHAIN_TRACING_V2环境变量设置为true来完成。您可以通过设置LANGCHAIN_PROJECT环境变量(如果未设置,运行将被记录到默认项目)来告诉LangChain要记录到哪个项目。如果该项目不存在,这将自动为您创建该项目。您还必须设置LANGCHAIN_ENDPOINT和LANGCHAIN_API_KEY环境变量。
有关设置跟踪的其他方法的更多信息,请参考LangSmith文档。
注意:您还可以在Python中使用上下文管理器来记录跟踪。
  1. from langchain_core.tracers.context import tracing_v2_enabled
  2. with tracing_v2_enabled(project_name="My Project"):
  3.     agent.run("How many people live in canada as of 2023?")
复制代码
然而,在这个例子中,我们将使用环境变量。
  1. %pip install --upgrade --quiet  langchain langsmith langchainhub --quiet
  2. %pip install --upgrade --quiet  langchain-openai tiktoken pandas duckduckgo-search --quiet
复制代码
  1. import os
  2. from uuid import uuid4
  3. unique_id = uuid4().hex[0:8]
  4. os.environ["LANGCHAIN_TRACING_V2"]="true"
  5. os.environ["LANGCHAIN_PROJECT"]=f"Tracing Walkthrough - {unique_id}"
  6. os.environ["LANGCHAIN_ENDPOINT"]="https://api.smith.langchain.com"
  7. os.environ["LANGCHAIN_API_KEY"]="<YOUR-API-KEY>"# Update to your API key# Used by the agent in this tutorial
  8. os.environ["OPENAI_API_KEY"]="<YOUR-OPENAI-API-KEY>"
复制代码
创建langsmith客户端以与API交互
  1. from langsmith import Client
  2. client = Client()
复制代码
创建一个LangChain组件并将运行记录到平台上。在这个例子中,我们将创建一个ReAct风格的代理,可以访问一个通用搜索工具(DuckDuckGo)。代理的提示可以在这里的Hub中查看。
  1. from langchain import hub
  2. from langchain.agents import AgentExecutor
  3. from langchain.agents.format_scratchpad import format_to_openai_function_messages
  4. from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser
  5. from langchain_community.tools import DuckDuckGoSearchResults
  6. from langchain_openai import ChatOpenAI
  7. # Fetches the latest version of this prompt
  8. prompt = hub.pull("wfh/langsmith-agent-prompt:5d466cbc")
  9. llm = ChatOpenAI(
  10.     model="gpt-3.5-turbo-16k",
  11.     temperature=0,)
  12. tools =[
  13.     DuckDuckGoSearchResults(
  14.         name="duck_duck_go"),# General internet search using DuckDuckGo]
  15. llm_with_tools = llm.bind_functions(tools)
  16. runnable_agent =({"input":lambda x: x["input"],"agent_scratchpad":lambda x: format_to_openai_function_messages(
  17.             x["intermediate_steps"]),}| prompt
  18.     | llm_with_tools
  19.     | OpenAIFunctionsAgentOutputParser())
  20. agent_executor = AgentExecutor(
  21.     agent=runnable_agent, tools=tools, handle_parsing_errors=True)
复制代码
我们正在同时在多个输入上运行代理,以减少延迟。运行日志会在后台记录到LangSmith,因此执行延迟不受影响。
  1. inputs =["What is LangChain?","What's LangSmith?","When was Llama-v2 released?","What is the langsmith cookbook?","When did langchain first announce the hub?",]
  2. results = agent_executor.batch([{"input": x}for x in inputs], return_exceptions=True)print(results[:2])
复制代码
  1. [{'input':'What is LangChain?',
  2.   'output':'I\'m sorry, but I couldn\'t find any information about "LangChain". Could you please provide more context or clarify your question?'},
  3. {'input': "What's LangSmith?",
  4.   'output': 'I\'m sorry, but I couldn\'t find any information about "LangSmith". It could be a company, a product, or a person. Can you provide more context or details about what you are referring to?'}]
复制代码
假设你已经成功设置好了你的环境,你的代理追踪应该会显示在应用程序的“项目”部分。恭喜!

(, 下载次数: 1)

看起来代理并没有有效地使用这些工具。让我们评估一下,这样我们就有一个基准。
代码

https://github.com/zgpeace/pets-name-langchain/tree/develop
参考

https://python.langchain.com/docs/langsmith/walkthrough

原文地址:https://blog.csdn.net/zgpeace/article/details/136002662




欢迎光临 AI创想 (https://llms-ai.com/) Powered by Discuz! X3.4