이 문서는 CloudflareWorkersAI chat models 시작하는 데 도움을 드립니다. ChatCloudflareWorkersAI의 모든 기능과 구성에 대한 자세한 문서는 API reference를 참조하세요.

Overview

Integration details

ClassPackageLocalSerializableJS supportDownloadsVersion
ChatCloudflareWorkersAIlangchain-cloudflarePyPI - DownloadsPyPI - Version

Model features

Tool callingStructured outputJSON modeImage inputAudio inputVideo inputToken-level streamingNative asyncToken usageLogprobs

Setup

CloudflareWorkersAI 모델에 액세스하려면 CloudflareWorkersAI 계정을 생성하고 API 키를 받은 다음 langchain-cloudflare integration package를 설치해야 합니다.

Credentials

www.cloudflare.com/developer-platform/products/workers-ai/로 이동하여 CloudflareWorkersAI에 가입하고 API 키를 생성하세요. 완료한 후 CF_AI_API_KEY environment variable과 CF_ACCOUNT_ID environment variable을 설정하세요:
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: ")

Installation

LangChain CloudflareWorkersAI integration은 langchain-cloudflare package에 있습니다:
pip install -qU langchain-cloudflare

Instantiation

이제 모델 객체를 인스턴스화하고 chat completion을 생성할 수 있습니다:
  • 관련 매개변수로 모델 인스턴스화를 업데이트하세요.
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...
)

Invocation

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.

Structured Outputs

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'}

Bind tools

from typing import List

from langchain.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
[{'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 reference

developers.cloudflare.com/workers-ai/ developers.cloudflare.com/agents/
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.
I