Skip to content

Commit

Permalink
Adds Cloudflare Workers AI LLM and chat model (langchain-ai#2923)
Browse files Browse the repository at this point in the history
* Adds Cloudflare Workers AI LLM and chat model

* Formatting
  • Loading branch information
jacoblee93 authored Oct 18, 2023
1 parent 38eee64 commit c5d5e86
Show file tree
Hide file tree
Showing 20 changed files with 480 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
sidebar_label: Cloudflare Workers AI
---

# ChatCloudflareWorkersAI

:::info
Workers AI is currently in Open Beta and is not recommended for production data and traffic, and limits + access are subject to change
:::

Workers AI allows you to run machine learning models, on the Cloudflare network, from your own code.

## Usage

import CodeBlock from "@theme/CodeBlock";
import Example from "@examples/models/chat/integration_cloudflare_workersai.ts";

<CodeBlock language="typescript">{Example}</CodeBlock>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Cloudflare Workers AI

:::info
Workers AI is currently in Open Beta and is not recommended for production data and traffic, and limits + access are subject to change
:::

Workers AI allows you to run machine learning models, on the Cloudflare network, from your own code.

## Usage

import CodeBlock from "@theme/CodeBlock";
import Example from "@examples/models/llm/cloudflare_workersai.ts";

<CodeBlock language="typescript">{Example}</CodeBlock>
2 changes: 2 additions & 0 deletions environment_tests/test-exports-bun/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * from "langchain/llms/base";
export * from "langchain/llms/openai";
export * from "langchain/llms/ai21";
export * from "langchain/llms/aleph_alpha";
export * from "langchain/llms/cloudflare_workersai";
export * from "langchain/llms/ollama";
export * from "langchain/llms/fireworks";
export * from "langchain/llms/yandex";
Expand All @@ -37,6 +38,7 @@ export * from "langchain/document_transformers/openai_functions";
export * from "langchain/chat_models/base";
export * from "langchain/chat_models/openai";
export * from "langchain/chat_models/anthropic";
export * from "langchain/chat_models/cloudflare_workersai";
export * from "langchain/chat_models/fireworks";
export * from "langchain/chat_models/baiduwenxin";
export * from "langchain/chat_models/ollama";
Expand Down
2 changes: 2 additions & 0 deletions environment_tests/test-exports-cf/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * from "langchain/llms/base";
export * from "langchain/llms/openai";
export * from "langchain/llms/ai21";
export * from "langchain/llms/aleph_alpha";
export * from "langchain/llms/cloudflare_workersai";
export * from "langchain/llms/ollama";
export * from "langchain/llms/fireworks";
export * from "langchain/llms/yandex";
Expand All @@ -37,6 +38,7 @@ export * from "langchain/document_transformers/openai_functions";
export * from "langchain/chat_models/base";
export * from "langchain/chat_models/openai";
export * from "langchain/chat_models/anthropic";
export * from "langchain/chat_models/cloudflare_workersai";
export * from "langchain/chat_models/fireworks";
export * from "langchain/chat_models/baiduwenxin";
export * from "langchain/chat_models/ollama";
Expand Down
2 changes: 2 additions & 0 deletions environment_tests/test-exports-cjs/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const llms_base = require("langchain/llms/base");
const llms_openai = require("langchain/llms/openai");
const llms_ai21 = require("langchain/llms/ai21");
const llms_aleph_alpha = require("langchain/llms/aleph_alpha");
const llms_cloudflare_workersai = require("langchain/llms/cloudflare_workersai");
const llms_ollama = require("langchain/llms/ollama");
const llms_fireworks = require("langchain/llms/fireworks");
const llms_yandex = require("langchain/llms/yandex");
Expand All @@ -37,6 +38,7 @@ const document_transformers_openai_functions = require("langchain/document_trans
const chat_models_base = require("langchain/chat_models/base");
const chat_models_openai = require("langchain/chat_models/openai");
const chat_models_anthropic = require("langchain/chat_models/anthropic");
const chat_models_cloudflare_workersai = require("langchain/chat_models/cloudflare_workersai");
const chat_models_fireworks = require("langchain/chat_models/fireworks");
const chat_models_baiduwenxin = require("langchain/chat_models/baiduwenxin");
const chat_models_ollama = require("langchain/chat_models/ollama");
Expand Down
2 changes: 2 additions & 0 deletions environment_tests/test-exports-esbuild/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as llms_base from "langchain/llms/base";
import * as llms_openai from "langchain/llms/openai";
import * as llms_ai21 from "langchain/llms/ai21";
import * as llms_aleph_alpha from "langchain/llms/aleph_alpha";
import * as llms_cloudflare_workersai from "langchain/llms/cloudflare_workersai";
import * as llms_ollama from "langchain/llms/ollama";
import * as llms_fireworks from "langchain/llms/fireworks";
import * as llms_yandex from "langchain/llms/yandex";
Expand All @@ -37,6 +38,7 @@ import * as document_transformers_openai_functions from "langchain/document_tran
import * as chat_models_base from "langchain/chat_models/base";
import * as chat_models_openai from "langchain/chat_models/openai";
import * as chat_models_anthropic from "langchain/chat_models/anthropic";
import * as chat_models_cloudflare_workersai from "langchain/chat_models/cloudflare_workersai";
import * as chat_models_fireworks from "langchain/chat_models/fireworks";
import * as chat_models_baiduwenxin from "langchain/chat_models/baiduwenxin";
import * as chat_models_ollama from "langchain/chat_models/ollama";
Expand Down
2 changes: 2 additions & 0 deletions environment_tests/test-exports-esm/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as llms_base from "langchain/llms/base";
import * as llms_openai from "langchain/llms/openai";
import * as llms_ai21 from "langchain/llms/ai21";
import * as llms_aleph_alpha from "langchain/llms/aleph_alpha";
import * as llms_cloudflare_workersai from "langchain/llms/cloudflare_workersai";
import * as llms_ollama from "langchain/llms/ollama";
import * as llms_fireworks from "langchain/llms/fireworks";
import * as llms_yandex from "langchain/llms/yandex";
Expand All @@ -37,6 +38,7 @@ import * as document_transformers_openai_functions from "langchain/document_tran
import * as chat_models_base from "langchain/chat_models/base";
import * as chat_models_openai from "langchain/chat_models/openai";
import * as chat_models_anthropic from "langchain/chat_models/anthropic";
import * as chat_models_cloudflare_workersai from "langchain/chat_models/cloudflare_workersai";
import * as chat_models_fireworks from "langchain/chat_models/fireworks";
import * as chat_models_baiduwenxin from "langchain/chat_models/baiduwenxin";
import * as chat_models_ollama from "langchain/chat_models/ollama";
Expand Down
2 changes: 2 additions & 0 deletions environment_tests/test-exports-vercel/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * from "langchain/llms/base";
export * from "langchain/llms/openai";
export * from "langchain/llms/ai21";
export * from "langchain/llms/aleph_alpha";
export * from "langchain/llms/cloudflare_workersai";
export * from "langchain/llms/ollama";
export * from "langchain/llms/fireworks";
export * from "langchain/llms/yandex";
Expand All @@ -37,6 +38,7 @@ export * from "langchain/document_transformers/openai_functions";
export * from "langchain/chat_models/base";
export * from "langchain/chat_models/openai";
export * from "langchain/chat_models/anthropic";
export * from "langchain/chat_models/cloudflare_workersai";
export * from "langchain/chat_models/fireworks";
export * from "langchain/chat_models/baiduwenxin";
export * from "langchain/chat_models/ollama";
Expand Down
2 changes: 2 additions & 0 deletions environment_tests/test-exports-vite/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * from "langchain/llms/base";
export * from "langchain/llms/openai";
export * from "langchain/llms/ai21";
export * from "langchain/llms/aleph_alpha";
export * from "langchain/llms/cloudflare_workersai";
export * from "langchain/llms/ollama";
export * from "langchain/llms/fireworks";
export * from "langchain/llms/yandex";
Expand All @@ -37,6 +38,7 @@ export * from "langchain/document_transformers/openai_functions";
export * from "langchain/chat_models/base";
export * from "langchain/chat_models/openai";
export * from "langchain/chat_models/anthropic";
export * from "langchain/chat_models/cloudflare_workersai";
export * from "langchain/chat_models/fireworks";
export * from "langchain/chat_models/baiduwenxin";
export * from "langchain/chat_models/ollama";
Expand Down
25 changes: 25 additions & 0 deletions examples/src/models/chat/integration_cloudflare_workersai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ChatCloudflareWorkersAI } from "langchain/chat_models/cloudflare_workersai";

const model = new ChatCloudflareWorkersAI({
model: "@cf/meta/llama-2-7b-chat-int8", // Default value
cloudflareAccountId: process.env.CLOUDFLARE_ACCOUNT_ID,
cloudflareApiToken: process.env.CLOUDFLARE_API_TOKEN,
});

const response = await model.invoke([
["system", "You are a helpful assistant that translates English to German."],
["human", `Translate "I love programming".`],
]);

console.log(response);

/*
AIMessage {
content: `Sure! Here's the translation of "I love programming" into German:\n` +
'\n' +
'"Ich liebe Programmieren."\n' +
'\n' +
'In this sentence, "Ich" means "I," "liebe" means "love," and "Programmieren" means "programming."',
additional_kwargs: {}
}
*/
23 changes: 23 additions & 0 deletions examples/src/models/llm/cloudflare_workersai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { CloudflareWorkersAI } from "langchain/llms/cloudflare_workersai";

const model = new CloudflareWorkersAI({
model: "@cf/meta/llama-2-7b-chat-int8", // Default value
cloudflareAccountId: process.env.CLOUDFLARE_ACCOUNT_ID,
cloudflareApiToken: process.env.CLOUDFLARE_API_TOKEN,
});

const response = await model.invoke(
`Translate "I love programming" into German.`
);

console.log(response);

/*
Here are a few options:
1. "Ich liebe Programmieren" - This is the most common way to say "I love programming" in German. "Liebe" means "love" in German, and "Programmieren" means "programming".
2. "Programmieren macht mir Spaß" - This means "Programming makes me happy". This is a more casual way to express your love for programming in German.
3. "Ich bin ein großer Fan von Programmieren" - This means "I'm a big fan of programming". This is a more formal way to express your love for programming in German.
4. "Programmieren ist mein Hobby" - This means "Programming is my hobby". This is a more casual way to express your love for programming in German.
5. "Ich liebe es, Programme zu schreiben" - This means "I love writing programs". This is a more formal way to express your love for programming in German.
*/
6 changes: 6 additions & 0 deletions langchain/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ llms/ai21.d.ts
llms/aleph_alpha.cjs
llms/aleph_alpha.js
llms/aleph_alpha.d.ts
llms/cloudflare_workersai.cjs
llms/cloudflare_workersai.js
llms/cloudflare_workersai.d.ts
llms/cohere.cjs
llms/cohere.js
llms/cohere.d.ts
Expand Down Expand Up @@ -433,6 +436,9 @@ chat_models/anthropic.d.ts
chat_models/bedrock.cjs
chat_models/bedrock.js
chat_models/bedrock.d.ts
chat_models/cloudflare_workersai.cjs
chat_models/cloudflare_workersai.js
chat_models/cloudflare_workersai.d.ts
chat_models/googlevertexai.cjs
chat_models/googlevertexai.js
chat_models/googlevertexai.d.ts
Expand Down
16 changes: 16 additions & 0 deletions langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@
"llms/aleph_alpha.cjs",
"llms/aleph_alpha.js",
"llms/aleph_alpha.d.ts",
"llms/cloudflare_workersai.cjs",
"llms/cloudflare_workersai.js",
"llms/cloudflare_workersai.d.ts",
"llms/cohere.cjs",
"llms/cohere.js",
"llms/cohere.d.ts",
Expand Down Expand Up @@ -445,6 +448,9 @@
"chat_models/bedrock.cjs",
"chat_models/bedrock.js",
"chat_models/bedrock.d.ts",
"chat_models/cloudflare_workersai.cjs",
"chat_models/cloudflare_workersai.js",
"chat_models/cloudflare_workersai.d.ts",
"chat_models/googlevertexai.cjs",
"chat_models/googlevertexai.js",
"chat_models/googlevertexai.d.ts",
Expand Down Expand Up @@ -1492,6 +1498,11 @@
"import": "./llms/aleph_alpha.js",
"require": "./llms/aleph_alpha.cjs"
},
"./llms/cloudflare_workersai": {
"types": "./llms/cloudflare_workersai.d.ts",
"import": "./llms/cloudflare_workersai.js",
"require": "./llms/cloudflare_workersai.cjs"
},
"./llms/cohere": {
"types": "./llms/cohere.d.ts",
"import": "./llms/cohere.js",
Expand Down Expand Up @@ -2012,6 +2023,11 @@
"import": "./chat_models/bedrock.js",
"require": "./chat_models/bedrock.cjs"
},
"./chat_models/cloudflare_workersai": {
"types": "./chat_models/cloudflare_workersai.d.ts",
"import": "./chat_models/cloudflare_workersai.js",
"require": "./chat_models/cloudflare_workersai.cjs"
},
"./chat_models/googlevertexai": {
"types": "./chat_models/googlevertexai.d.ts",
"import": "./chat_models/googlevertexai.js",
Expand Down
2 changes: 2 additions & 0 deletions langchain/scripts/create-entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const entrypoints = {
"llms/openai": "llms/openai",
"llms/ai21": "llms/ai21",
"llms/aleph_alpha": "llms/aleph_alpha",
"llms/cloudflare_workersai": "llms/cloudflare_workersai",
"llms/cohere": "llms/cohere",
"llms/hf": "llms/hf",
"llms/raycast": "llms/raycast",
Expand Down Expand Up @@ -174,6 +175,7 @@ const entrypoints = {
"chat_models/portkey": "chat_models/portkey",
"chat_models/anthropic": "chat_models/anthropic",
"chat_models/bedrock": "chat_models/bedrock",
"chat_models/cloudflare_workersai": "chat_models/cloudflare_workersai",
"chat_models/googlevertexai": "chat_models/googlevertexai/index",
"chat_models/googlevertexai/web": "chat_models/googlevertexai/web",
"chat_models/googlepalm": "chat_models/googlepalm",
Expand Down
Loading

0 comments on commit c5d5e86

Please sign in to comment.