ChatCloudflareWorkersAI
这将帮助您开始使用 CloudflareWorkersAI 聊天模型。有关所有 ChatCloudflareWorkersAI 功能和配置的详细文档,请查阅API 参考。
概述
集成详情
类别 | 包 | 本地 | 可序列化 | JS 支持 | 包下载量 | 最新包版本 |
---|---|---|---|---|---|---|
ChatCloudflareWorkersAI | langchain-cloudflare | ✅ | ❌ | ❌ |
模型特性
工具调用 | 结构化输出 | JSON 模式 | 图片输入 | 音频输入 | 视频输入 | 逐令牌流式传输 | 原生异步 | 令牌使用量 | 对数概率 |
---|---|---|---|---|---|---|---|---|---|
✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ |
设置
要访问 CloudflareWorkersAI 模型,您需要创建一个 CloudflareWorkersAI 账户,获取 API 密钥,并安装 `langchain-cloudflare` 集成包。
凭证
前往 https://www.cloudflare.com/developer-platform/products/workers-ai/ 注册 CloudflareWorkersAI 并生成 API 密钥。完成后,设置 CF_AI_API_KEY 环境变量和 CF_ACCOUNT_ID 环境变量
import getpass
import os
if not os.getenv("CF_AI_API_KEY"):
os.environ["CF_AI_API_KEY"] = getpass.getpass(
"Enter your CloudflareWorkersAI API key: "
)
if not os.getenv("CF_ACCOUNT_ID"):
os.environ["CF_ACCOUNT_ID"] = getpass.getpass(
"Enter your CloudflareWorkersAI account ID: "
)
如果您希望对模型调用进行自动化追踪,也可以通过取消注释下方内容来设置您的 LangSmith API 密钥。
# os.environ["LANGSMITH_TRACING"] = "true"
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
安装
LangChain CloudflareWorkersAI 集成位于 `langchain-cloudflare` 包中
%pip install -qU langchain-cloudflare
实例化
现在我们可以实例化模型对象并生成聊天补全
- 使用相关参数更新模型实例化。
from langchain_cloudflare.chat_models import ChatCloudflareWorkersAI
llm = ChatCloudflareWorkersAI(
model="@cf/meta/llama-3.3-70b-instruct-fp8-fast",
temperature=0,
max_tokens=1024,
# other params...
)
调用
messages = [
(
"system",
"You are a helpful assistant that translates English to French. Translate the user sentence.",
),
("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
ai_msg
AIMessage(content="J'adore la programmation.", additional_kwargs={}, response_metadata={'token_usage': {'prompt_tokens': 37, 'completion_tokens': 9, 'total_tokens': 46}, 'model_name': '@cf/meta/llama-3.3-70b-instruct-fp8-fast'}, id='run-995d1970-b6be-49f3-99ae-af4cdba02304-0', usage_metadata={'input_tokens': 37, 'output_tokens': 9, 'total_tokens': 46})
print(ai_msg.content)
J'adore la programmation.
链式调用
我们可以像这样将模型与提示模板链式连接起来
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate(
[
(
"system",
"You are a helpful assistant that translates {input_language} to {output_language}.",
),
("human", "{input}"),
]
)
chain = prompt | llm
chain.invoke(
{
"input_language": "English",
"output_language": "German",
"input": "I love programming.",
}
)
API 参考:ChatPromptTemplate
AIMessage(content='Ich liebe das Programmieren.', additional_kwargs={}, response_metadata={'token_usage': {'prompt_tokens': 32, 'completion_tokens': 7, 'total_tokens': 39}, 'model_name': '@cf/meta/llama-3.3-70b-instruct-fp8-fast'}, id='run-d1b677bc-194e-4473-90f1-aa65e8e46d50-0', usage_metadata={'input_tokens': 32, 'output_tokens': 7, 'total_tokens': 39})
结构化输出
json_schema = {
"title": "joke",
"description": "Joke to tell user.",
"type": "object",
"properties": {
"setup": {
"type": "string",
"description": "The setup of the joke",
},
"punchline": {
"type": "string",
"description": "The punchline to the joke",
},
"rating": {
"type": "integer",
"description": "How funny the joke is, from 1 to 10",
"default": None,
},
},
"required": ["setup", "punchline"],
}
structured_llm = llm.with_structured_output(json_schema)
structured_llm.invoke("Tell me a joke about cats")
{'setup': 'Why did the cat join a band?',
'punchline': 'Because it wanted to be the purr-cussionist',
'rating': '8'}
绑定工具
from typing import List
from langchain_core.tools import tool
@tool
def validate_user(user_id: int, addresses: List[str]) -> bool:
"""Validate user using historical addresses.
Args:
user_id (int): the user ID.
addresses (List[str]): Previous addresses as a list of strings.
"""
return True
llm_with_tools = llm.bind_tools([validate_user])
result = llm_with_tools.invoke(
"Could you validate user 123? They previously lived at "
"123 Fake St in Boston MA and 234 Pretend Boulevard in "
"Houston TX."
)
result.tool_calls
API 参考:tool
[{'name': 'validate_user',
'args': {'user_id': '123',
'addresses': '["123 Fake St in Boston MA", "234 Pretend Boulevard in Houston TX"]'},
'id': '31ec7d6a-9ce5-471b-be64-8ea0492d1387',
'type': 'tool_call'}]
API 参考
https://developers.cloudflare.com/workers-ai/ https://developers.cloudflare.com/agents/