import { ChatOpenAI } from"@langchain/openai";import { createHeaders, PORTKEY_GATEWAY_URL} from"portkey-ai"constPORTKEY_API_KEY="..."constPROVIDER_API_KEY="..."// 添加所使用的 AI 提供者的 API 密钥constportkeyConf= { baseURL:PORTKEY_GATEWAY_URL, defaultHeaders:createHeaders({apiKey:PORTKEY_API_KEY, provider:"openai"})}constchatModel=newChatOpenAI({ apiKey:PROVIDER_API_KEY, configuration: portkeyConf});awaitchatModel.invoke("What is the meaning of life, universe and everything?")
响应
调用及相应的提示也将在 Portkey 日志标签中可见。
使用虚拟密钥管理多个模型
Portkey 支持 虚拟密钥,这是一种安全地存储和管理 API 密钥的简单方法。让我们尝试使用虚拟密钥进行 LLM 调用。
1. 在您的 Portkey 账户中创建一个虚拟密钥并复制 ID
让我们尝试为 Mistral 创建一个新的虚拟密钥,如下所示:
2. 在 Portkey Headers 中使用虚拟密钥
virtualKey 参数设置所使用的 AI 提供者的身份验证和提供者。在我们的案例中,我们使用的是 Mistral 虚拟密钥。
请注意,apiKey 可以留空,因为该身份验证将不会被使用。
Portkey AI 网关将对 Mistral 的 API 请求进行身份验证,并以 OpenAI 格式返回响应,供您使用。
AI 网关扩展了 Langchain 的 ChatOpenAI 类,使其成为调用任何提供者和任何模型的单一接口。
AIMessage {
lc_serializable: true,
lc_kwargs: {
content: `The phrase "the meaning of life, universe, and everything" is a reference to Douglas Adams' science fiction series, "The Hitchhiker's Guide to the Galaxy." In the series, a supercomputer called Deep Thought was asked to calculate the Answer to the Ultimate Question of Life, the Universe, and Everything. After much deliberation, Deep Thought revealed that the answer was simply the number 42.\n` +
'\n' +
'In the context of the series, the number 42 is meant to highlight the absurdity and unknowability of the ultimate meaning of life and the universe. It is a humorous and satirical take on the deep philosophical questions that have puzzled humanity for centuries.\n' +
'\n' +
'Ultimately, the meaning of life, universe, and everything is a complex and deeply personal question that each individual must grapple with and find their own answer to. It may be different for each person and can encompass a wide range of beliefs, values, and experiences.',
tool_calls: [],
invalid_tool_calls: [],
additional_kwargs: { function_call: undefined, tool_calls: undefined },
response_metadata: {}
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: `The phrase "the meaning of life, universe, and everything" is a reference to Douglas Adams' science fiction series, "The Hitchhiker's Guide to the Galaxy." In the series, a supercomputer called Deep Thought was asked to calculate the Answer to the Ultimate Question of Life, the Universe, and Everything. After much deliberation, Deep Thought revealed that the answer was simply the number 42.\n` +
'\n' +
'In the context of the series, the number 42 is meant to highlight the absurdity and unknowability of the ultimate meaning of life and the universe. It is a humorous and satirical take on the deep philosophical questions that have puzzled humanity for centuries.\n' +
'\n' +
'Ultimately, the meaning of life, universe, and everything is a complex and deeply personal question that each individual must grapple with and find their own answer to. It may be different for each person and can encompass a wide range of beliefs, values, and experiences.',
name: undefined,
additional_kwargs: { function_call: undefined, tool_calls: undefined },
response_metadata: {
tokenUsage: { completionTokens: 186, promptTokens: 18, totalTokens: 204 },
finish_reason: 'stop'
},
tool_calls: [],
invalid_tool_calls: []
}
import { ChatOpenAI } from "@langchain/openai";
import { createHeaders, PORTKEY_GATEWAY_URL} from "portkey-ai"
const PORTKEY_API_KEY = "..."
const MISTRAL_VK = "..." // 添加我们刚创建的 Mistral 虚拟密钥
const portkeyConf = {
baseURL: PORTKEY_GATEWAY_URL,
defaultHeaders: createHeaders({apiKey: PORTKEY_API_KEY, virtualKey: MISTRAL_VK})
}
const chatModel = new ChatOpenAI({
apiKey: "X",
configuration: portkeyConf,
model: "mistral-large-latest"
});
await chatModel.invoke("What is the meaning of life, universe and everything?")
const portkeyConf = {
baseURL: PORTKEY_GATEWAY_URL,
defaultHeaders: createHeaders({apiKey: PORTKEY_API_KEY, config: config})
}
const chatModel = new ChatOpenAI({
apiKey: "X",
configuration: portkeyConf
});
const res = await chatModel.invoke("What is the meaning of life, universe and everything?")