from langchain_openai import ChatOpenAIfrom portkey_ai import createHeaders, PORTKEY_GATEWAY_URLPORTKEY_API_KEY ="..."PROVIDER_API_KEY ="..."# 添加所使用的 AI 提供商的 API 密钥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?")
响应
调用及相应的提示也将显示在 Portkey 日志标签中。
使用虚拟密钥进行多模型操作
Portkey 支持 虚拟密钥,这是一种在安全的保险库中存储和管理 API 密钥的简单方法。让我们尝试使用虚拟密钥进行 LLM 调用。
1. 在您的 Portkey 账户中创建虚拟密钥并复制 ID
让我们尝试为 Mistral 创建一个新的虚拟密钥,如下所示
2. 在 Portkey 头部中使用虚拟密钥
virtual_key 参数设置所使用的 AI 提供者的身份验证和提供者。在我们的案例中,我们使用的是 Mistral 虚拟密钥。
请注意,api_key 可以留空,因为该身份验证将不被使用。
Portkey AI 网关将对 Mistral 的 API 请求进行身份验证,并以 OpenAI 格式返回响应供您使用。
AI 网关扩展了 Langchain 的 ChatOpenAI 类,使其成为调用任何提供者和任何模型的单一接口。
AIMessage(content='The meaning of life, universe, and everything is a question that has puzzled humanity for centuries. In the book "The Hitchhiker\'s Guide to the Galaxy" by Douglas Adams, the answer to this ultimate question is humorously given as "42." This has since become a popular meme and cultural reference.\n\nIn a more philosophical sense, the meaning of life, universe, and everything is subjective and can vary greatly from person to person. Some may find meaning in religious beliefs, others in personal relationships, achievements, or experiences. Ultimately, it is up to each individual to find their own meaning and purpose in life.', response_metadata={'token_usage': {'completion_tokens': 124, 'prompt_tokens': 18, 'total_tokens': 142}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_c2295e73ad', 'finish_reason': 'stop', 'logprobs': None}, id='run-d2b790be-589b-4c72-add8-a36e098ab277-0')
from langchain_openai import ChatOpenAI
from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL
PORTKEY_API_KEY = "..."
VIRTUAL_KEY = "..." # Mistral's virtual key we copied above
portkey_headers = createHeaders(api_key=PORTKEY_API_KEY,virtual_key=VIRTUAL_KEY)
llm = ChatOpenAI(api_key="X", base_url=PORTKEY_GATEWAY_URL, default_headers=portkey_headers, model="mistral-large-latest")
llm.invoke("What is the meaning of life, universe and everything?")
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?")
"""
Libraries:
pip install langchain langchain-openai langchainhub portkey-ai
"""
import os
from langchain import hub
from langchain.agents import AgentExecutor, create_openai_tools_agent, tool
from langchain_openai import ChatOpenAI
from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders
prompt = hub.pull("hwchase17/openai-tools-agent")
portkey_headers = createHeaders(
api_key=os.getenv("PORTKEY_API_KEY"),
config="your config id",
trace_id="uuid-uuid-uuid-uuid",
)
@tool
def add(first_int: int, second_int: int) -> int:
return first_int + second_int
@tool
def multiply(first_int: int, second_int: int) -> int:
return first_int * second_int
@tool
def exponentiate(base: int, exponent: int) -> int:
return base**exponent
tools = [multiply, add, exponentiate]
model = ChatOpenAI(
api_key="ignore", # type: ignore
base_url=PORTKEY_GATEWAY_URL,
default_headers=portkey_headers,
temperature=0,
)
# 构建 OpenAI 工具代理
agent = create_openai_tools_agent(model, tools, prompt) # type: ignore
# 通过传入代理和工具创建代理执行器
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True) # type: ignore
agent_executor.invoke(
{
"input": "Take 3 to the fifth power and multiply that by the sum of twelve and three, then square the whole result"
}
)