ChatCloudflareWorkersAI 통합

ChatCloudflareWorkersAI 통합

LangChain JavaScript로 ChatCloudflareWorkersAI 채팅 모델과 통합하는 방법을 안내할게요.

출처: 문서

본문

Workers AI를 사용하면 Cloudflare 네트워크에서 머신러닝 모델을 자신의 코드로 실행할 수 있어요.

이 문서는 ChatCloudflareWorkersAI 채팅 모델을 시작하는 데 도움을 줘요. 모든 ChatCloudflareWorkersAI 기능과 구성에 대한 자세한 문서는 API 레퍼런스를 참고하세요.

개요

통합 세부 정보

클래스 패키지 Serializable PY 지원 Downloads Version
ChatCloudflareWorkersAI @langchain/cloudflare NPM - Downloads NPM - Version

모델 기능

아래 표 헤더의 링크에서 특정 기능을 사용하는 방법에 대한 가이드를 확인할 수 있어요.

Tool calling Structured output Image input Audio input Video input Token-level streaming Token usage Logprobs

설정

Cloudflare Workers AI 모델에 접근하려면 Cloudflare 계정을 만들고 API 키를 받은 뒤 @langchain/cloudflare 통합 패키지를 설치해야 해요.

자격 증명

Cloudflare Workers AI에서 Cloudflare에 가입하고 API 키를 생성하세요. 완료되면 환경에 자격 증명을 설정하세요:

export CLOUDFLARE_ACCOUNT_ID="your-account-id"
export CLOUDFLARE_API_TOKEN="your-api-token"

Cloudflare Worker 내에서 바인딩을 전달하는 것은 아직 지원되지 않아요.

설치

LangChain ChatCloudflareWorkersAI 통합은 @langchain/cloudflare 패키지에 있어요:

```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}} npm install @langchain/cloudflare @langchain/core ```
yarn add @langchain/cloudflare @langchain/core
pnpm add @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", // 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."

API 레퍼런스

모든 ChatCloudflareWorkersAI 기능과 구성에 대한 자세한 문서는 API 레퍼런스를 참고하세요.


[Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers. [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/javascript/integrations/chat/cloudflare_workersai.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).

더 알아보기