Documentation Index
Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
Use this file to discover all available pages before exploring further.
Workers AI 允许您在自己的代码中运行 Cloudflare 网络上的机器学习模型。
本文将帮助您开始使用 ChatCloudflareWorkersAI 聊天模型。有关 ChatCloudflareWorkersAI 所有功能和配置的详细文档,请参阅 API 参考。
集成详情
模型特性
下表标题中的链接提供了如何使用特定功能的指南。
要访问 Cloudflare Workers AI 模型,您需要创建一个 Cloudflare 账户、获取 API 密钥,并安装 @langchain/cloudflare 集成包。
请访问此页面注册 Cloudflare 并生成 API 密钥。完成后,请记下您的 CLOUDFLARE_ACCOUNT_ID 和 CLOUDFLARE_API_TOKEN。
目前尚不支持在 Cloudflare Worker 内部传递绑定。
LangChain ChatCloudflareWorkersAI 集成位于 @langchain/cloudflare 包中:
npm install @langchain/cloudflare @langchain/core
实例化
现在我们可以实例化模型对象并生成聊天补全:
// @lc-docs-hide-cell
// @ts-expect-error Deno is not recognized
const CLOUDFLARE_ACCOUNT_ID = Deno.env.get("CLOUDFLARE_ACCOUNT_ID");
// @ts-expect-error Deno is not recognized
const CLOUDFLARE_API_TOKEN = Deno.env.get("CLOUDFLARE_API_TOKEN");
import { ChatCloudflareWorkersAI } from "@langchain/cloudflare";
const llm = new ChatCloudflareWorkersAI({
model: "@cf/meta/llama-2-7b-chat-int8", // 默认值
cloudflareAccountId: CLOUDFLARE_ACCOUNT_ID,
cloudflareApiToken: CLOUDFLARE_API_TOKEN,
// 传递自定义基础 URL 以使用 Cloudflare AI Gateway
// baseUrl: `https://gateway.ai.cloudflare.com/v1/{YOUR_ACCOUNT_ID}/{GATEWAY_NAME}/workers-ai/`,
});
const aiMsg = await llm.invoke([
[
"system",
"You are a helpful assistant that translates English to French. Translate the user sentence.",
],
["human", "I love programming."],
])
aiMsg
AIMessage {
lc_serializable: true,
lc_kwargs: {
content: 'I can help with that! The translation of "I love programming" in French is:\n' +
"\n" +
`"J'adore le programmati`... 4 more characters,
tool_calls: [],
invalid_tool_calls: [],
additional_kwargs: {},
response_metadata: {}
},
lc_namespace: [ "langchain_core", "messages" ],
content: 'I can help with that! The translation of "I love programming" in French is:\n' +
"\n" +
`"J'adore le programmati`... 4 more characters,
name: undefined,
additional_kwargs: {},
response_metadata: {},
tool_calls: [],
invalid_tool_calls: []
}
console.log(aiMsg.content)
I can help with that! The translation of "I love programming" in French is:
"J'adore le programmation."
API 参考
有关 ChatCloudflareWorkersAI 所有功能和配置的详细文档,请参阅 API 参考。