本文主要是介绍LangChain 17 LangSmith调试、测试、评估和监视基于任何LLM框架构建的链和智能代理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
LangChain系列文章
- LangChain 实现给动物取名字,
- LangChain 2模块化prompt template并用streamlit生成网站 实现给动物取名字
- LangChain 3使用Agent访问Wikipedia和llm-math计算狗的平均年龄
- LangChain 4用向量数据库Faiss存储,读取YouTube的视频文本搜索Indexes for information retrieve
- LangChain 5易速鲜花内部问答系统
- LangChain 6根据图片生成推广文案HuggingFace中的image-caption模型
- LangChain 7 文本模型TextLangChain和聊天模型ChatLangChain
- LangChain 8 模型Model I/O:输入提示、调用模型、解析输出
- LangChain 9 模型Model I/O 聊天提示词ChatPromptTemplate, 少量样本提示词FewShotPrompt
- LangChain 10思维链Chain of Thought一步一步的思考 think step by step
- LangChain 11实现思维树Implementing the Tree of Thoughts in LangChain’s Chain
- LangChain 12调用模型HuggingFace中的Llama2和Google Flan t5
- LangChain 13输出解析Output Parsers 自动修复解析器
- LangChain 14 SequencialChain链接不同的组件
- LangChain 15根据问题自动路由Router Chain确定用户的意图
- LangChain 16 通过Memory记住历史对话的内容
1. LangSmith
LangSmith是一个用于构建生产级LLM应用程序的平台。
它让您可以调试、测试、评估和监视基于任何LLM框架构建的链和智能代理,并与LangChain无缝集成,LangChain是构建LLM的开源框架的首选。
LangChain使得原型化LLM应用程序和代理变得容易。然而,将LLM应用程序交付到生产环境可能会出乎意料地困难。您可能需要对提示prompt、chains链条和tools其他组件进行大量定制和迭代,以创建高质量的产品。
为了帮助这一过程,我们推出了LangSmith,这是一个统一的平台,用于调试、测试和监控您的LLM应用程序。
这在什么时候会派上用场呢?当您想要:
- 快速调试新的链条、代理或一套工具
- 可视化组件(链条、LLM、检索器等)的关系和使用方式
- 评估单个组件的不同提示和LLM
- 多次运行给定的链条,以确保它始终达到质量标准
- 捕获使用痕迹,并使用LLM或分析管道生成见解时
LangSmith是由LangChain开发的,LangChain是开源LangChain框架背后的公司。
如果您已经使用LangChain,您可以通过以下几个步骤连接到LangSmith:
- 使用支持的登录方法创建LangSmith账户。
- 通过转到设置页面创建API密钥。
- 为您的目标环境和编程语言安装最新版本的LangChain。
export LANGCHAIN_TRACING_V2=true
export LANGCHAIN_ENDPOINT=https://api.smith.langchain.com
export LANGCHAIN_API_KEY=<your-api-key>
export LANGCHAIN_PROJECT=<your-project> # if not specified, defaults to "default"# The below examples use the OpenAI API, so you will need
export OPENAI_API_KEY=<your-openai-api-key>
2. Agent 搜索实现
创建一个LangChain组件并将运行记录到平台上。在这个例子中,我们将创建一个ReAct风格的代理,可以访问一个通用搜索工具(DuckDuckGo)。代理的提示可以在这里的Hub中查看。
安装libs
%pip install openai tiktoken pandas duckduckgo-search
Agents/chat_agents_search.py
这段代码使用了 Langchain 库来构建一个复杂的问答系统,它结合了多个工具和大型语言模型(GPT-3.5),并使用 LangSmith Client 进行管理。以下是对每一行代码的注释解释:
# 导入 Langchain 库中的 OpenAI 模块,用于与 OpenAI 语言模型进行交互。
from langchain.llms import OpenAI # 导入 Langchain 库中的 PromptTemplate 模块,用于创建和管理提示模板。
from langchain.prompts import PromptTemplate # 导入 Langchain 库中的 LLMChain 模块,它允许构建基于大型语言模型的处理链。
from langchain.chains import LLMChain # 导入 dotenv 库,用于从 .env 文件加载环境变量,这对于管理敏感数据如 API 密钥很有用。
from dotenv import load_dotenv # 导入 Langchain 库中的 ChatOpenAI 类,用于创建和管理 OpenAI 聊天模型的实例。
from langchain.chat_models import ChatOpenAI# 调用 dotenv 库的 load_dotenv 函数来加载 .env 文件中的环境变量。
load_dotenv() # 这部分被注释掉,原本用于创建一个 ChatOpenAI 实例并执行一个简单的查询。
# llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
# response = llm.invoke("how many letters in the word educa?")
# print("educa count > ")
# print(response)# 设置一些环境变量,包括唯一的项目 ID 和 Langchain API 的相关设置。
import os
from uuid import uuid4
unique_id = uuid4().hex[0:8]
os.environ["LANGCHAIN_PROJECT"] = f"Tracing Walkthrough - {unique_id}"# 初始化 LangSmith 客户端。
from langsmith import Client
client = Client()# 导入 Langchain 的其他必要模块和工具。
from langchain import hub
from langchain.agents import AgentExecutor
from langchain.agents.format_scratchpad import format_to_openai_function_messages
from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser
from langchain.tools import DuckDuckGoSearchResults
from langchain.tools.render import format_tool_to_openai_function# 从 Langchain Hub 拉取最新版本的提示。
prompt = hub.pull("wfh/langsmith-agent-prompt:latest")
print(prompt)# 创建 ChatOpenAI 实例。
llm = ChatOpenAI(model="gpt-3.5-turbo-16k", temperature=0)# 定义工具列表,这里使用了 DuckDuckGo 作为搜索工具。
tools = [DuckDuckGoSearchResults(name="duck_duck_go")]# 将 ChatOpenAI 实例绑定到工具上。
llm_with_tools = llm.bind(functions=[format_tool_to_openai_function(t) for t in tools])# 创建可运行的代理,它结合了提示、模型、工具和输出解析器。
runnable_agent = ({"input": lambda x: x["input"],"agent_scratchpad": lambda x: format_to_openai_function_messages(x["intermediate_steps"]),}| prompt| llm_with_tools| OpenAIFunctionsAgentOutputParser()
)# 创建代理执行器,用于执行代理并管理工具。
agent_executor = AgentExecutor(agent=runnable_agent, tools=tools, handle_parsing_errors=True
)# 定义一系列输入问题。
inputs = ["What is LangChain?","What's LangSmith?","When was Llama-v2 released?","What is the langsmith cookbook?","When did langchain first announce the hub?",
]# 批量执行输入问题,并返回结果。
results = agent_executor.batch([{"input": x} for x in inputs], return_exceptions=True)
print(results)
输出结果
$ python Agents/chat_agents_search.py
input_variables=['agent_scratchpad', 'input'] input_types={'agent_scratchpad': typing.List[typing.Union[langchain.schema.messages.AIMessage, langchain.schema.messages.HumanMessage, langchain.schema.messages.ChatMessage, langchain.schema.messages.SystemMessage, langchain.schema.messages.FunctionMessage, langchain.schema.messages.ToolMessage]]} messages=[SystemMessagePromptTemplate(prompt=PromptTemplate(input_variables=[], template='You are a helpful assistant.')), HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['input'], template='{input}')), MessagesPlaceholder(variable_name='agent_scratchpad')][{'input': 'What is LangChain?', 'output': 'I\'m sorry, but I couldn\'t find any information about "LangChain". Could you please provide more context or clarify your question?'}, {'input': 'What is the langsmith cookbook?', 'output': 'The Langsmith Cookbook is a collection of recipes and cooking techniques created by Langsmith, a fictional character. It is a comprehensive guide that covers a wide range of cuisines and dishes. The cookbook includes step-by-step instructions, ingredient lists, and tips for successful cooking. Whether you are a beginner or an experienced cook, the Langsmith Cookbook can help you enhance your culinary skills and create delicious meals.'}
在 LangSmith 中可以看到OpenAI 调用了搜索引擎 duck_duck_go
代码
https://github.com/zgpeace/pets-name-langchain/tree/develop
参考
- https://python.langchain.com/docs/langsmith/walkthrough
- https://docs.smith.langchain.com/
这篇关于LangChain 17 LangSmith调试、测试、评估和监视基于任何LLM框架构建的链和智能代理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!