Skip to main content

ChatCloudflareWorkersAI

Workers AI 允许你在 Cloudflare 网络上通过你自己的代码运行机器学习模型。

这将帮助你快速开始使用 Cloudflare Workers AI 的聊天模型。如需了解 ChatCloudflareWorkersAI 所有功能和配置的详细文档,请前往 API 参考

概览

集成详情

本地可序列化PY 支持包下载量最新包
ChatCloudflareWorkersAI@langchain/cloudflareNPM - 下载量NPM - 版本

模型功能

有关如何使用特定功能的指南,请参见下表表头中的链接。

工具调用结构化输出JSON 模式图像输入音频输入视频输入逐 token 流式传输token 使用量logprobs

准备工作

要访问 Cloudflare Workers AI 模型,你需要创建一个 Cloudflare 账号,获取 API 密钥,并安装 @langchain/cloudflare 集成包。

凭证

请前往此页面 注册 Cloudflare 并生成一个 API 密钥。完成之后,请记录下你的 CLOUDFLARE_ACCOUNT_IDCLOUDFLARE_API_TOKEN

在 Cloudflare Worker 中传递绑定目前尚未支持。

安装

LangChain ChatCloudflareWorkersAI 集成位于 @langchain/cloudflare 包中:

:::提示 请参阅安装集成包的一般说明部分。 :::

yarn add @langchain/cloudflare @langchain/core

实例化

现在我们可以实例化我们的模型对象并生成聊天补全:

import { ChatCloudflareWorkersAI } from "@langchain/cloudflare";

const llm = new ChatCloudflareWorkersAI({
model: "@cf/meta/llama-2-7b-chat-int8", // Default value
cloudflareAccountId: CLOUDFLARE_ACCOUNT_ID,
cloudflareApiToken: CLOUDFLARE_API_TOKEN,
// Pass a custom base URL to use 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."

链式调用

我们可以像这样将模型与提示模板链式调用

import { ChatPromptTemplate } from "@langchain/core/prompts";

const prompt = ChatPromptTemplate.fromMessages([
[
"system",
"You are a helpful assistant that translates {input_language} to {output_language}.",
],
["human", "{input}"],
]);

const chain = prompt.pipe(llm);
await chain.invoke({
input_language: "English",
output_language: "German",
input: "I love programming.",
});
AIMessage {
lc_serializable: true,
lc_kwargs: {
content: "Das Programmieren ist für mich sehr Valent sein!",
tool_calls: [],
invalid_tool_calls: [],
additional_kwargs: {},
response_metadata: {}
},
lc_namespace: [ "langchain_core", "messages" ],
content: "Das Programmieren ist für mich sehr Valent sein!",
name: undefined,
additional_kwargs: {},
response_metadata: {},
tool_calls: [],
invalid_tool_calls: []
}

API 参考文档

如需详细了解所有 ChatCloudflareWorkersAI 功能和配置,请访问 API 参考文档:https://api.js.langchain.com/classes/langchain_cloudflare.ChatCloudflareWorkersAI.html


Was this page helpful?


You can also leave detailed feedback on GitHub.