本页还没有译文
本页 18 段还没译成한국어,下面显示的是英文原文。CloudflareWorkersAIEmbeddings integration
还没翻译译自英文(docs.langchain.com)本页 18 段都还没译
This will help you get started with Cloudflare Workers AI embedding models using LangChain. For detailed documentation on CloudflareWorkersAIEmbeddings features and configuration options, please refer to the API reference.
Overview
Integration details
| Class | Package | Local | Py support | Downloads | Version |
|---|---|---|---|---|---|
CloudflareWorkersAIEmbeddings | @langchain/cloudflare | ❌ | ❌ |
Setup
To access Cloudflare embedding models you'll need to create a Cloudflare account and install the @langchain/cloudflare integration package. This integration is made to run in a Cloudflare worker and accept a binding.
Follow the official docs to set up your worker.
Your wrangler.toml file should look similar to this:
name = "langchain-test"
main = "worker.js"
compatibility_date = "2024-01-10"
[[vectorize]]
binding = "VECTORIZE_INDEX"
index_name = "langchain-test"
[ai]
binding = "AI"Credentials
If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"Installation
The LangChain CloudflareWorkersAIEmbeddings integration lives in the @langchain/cloudflare package:
npmyarnpnpm
npm install @langchain/cloudflare @langchain/coreyarn add @langchain/cloudflare @langchain/corepnpm add @langchain/cloudflare @langchain/coreUsage
Below is an example worker that uses Workers AI embeddings with a Cloudflare Vectorize vectorstore.
// @ts-nocheck
import type {
VectorizeIndex,
Fetcher,
Request,
} from "@cloudflare/workers-types";
import {
CloudflareVectorizeStore,
CloudflareWorkersAIEmbeddings,
} from "@langchain/cloudflare";
export interface Env {
VECTORIZE_INDEX: VectorizeIndex;
AI: Fetcher;
}
export default {
async fetch(request: Request, env: Env) {
const { pathname } = new URL(request.url);
const embeddings = new CloudflareWorkersAIEmbeddings({
binding: env.AI,
model: "@cf/baai/bge-small-en-v1.5",
});
const store = new CloudflareVectorizeStore(embeddings, {
index: env.VECTORIZE_INDEX,
});
if (pathname === "/") {
const results = await store.similaritySearch("hello", 5);
return Response.json(results);
} else if (pathname === "/load") {
// Upsertion by id is supported
await store.addDocuments(
[
{
pageContent: "hello",
metadata: {},
},
{
pageContent: "world",
metadata: {},
},
{
pageContent: "hi",
metadata: {},
},
],
{ ids: ["id1", "id2", "id3"] }
);
return Response.json({ success: true });
} else if (pathname === "/clear") {
await store.delete({ ids: ["id1", "id2", "id3"] });
return Response.json({ success: true });
}
return Response.json({ error: "Not Found" }, { status: 404 });
},
};API reference
For detailed documentation of all CloudflareWorkersAIEmbeddings features and configurations head to the API reference.