Skip to content

Commit

Permalink
feat: add cloudflare worker full example (#1521)
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 authored Nov 22, 2024
1 parent 819af45 commit 969365c
Show file tree
Hide file tree
Showing 12 changed files with 469 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/strong-mice-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@llamaindex/env": patch
---

fix: async local storage on cloudflare worker
31 changes: 31 additions & 0 deletions apps/next/src/content/docs/llamaindex/setup/cloudflare.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,33 @@ Then, you need create `.dev.vars` and add LLM api keys for the local development

<Callout type="warn">Do not commit the api key to git repository.</Callout>

## Integrating with Hono

```ts
import { Hono } from "hono";

type Bindings = {
OPENAI_API_KEY: string;
};

const app = new Hono<{
Bindings: Bindings;
}>();

app.post("/llm", async (c) => {
const { setEnvs } = await import("@llamaindex/env");
setEnvs(c.env);

// ...

return new Response('Hello, world!');
})

export default {
fetch: app.fetch,
};
```

## Difference between Node.js and Cloudflare Worker

In Cloudflare Worker and similar serverless JS environment, you need to be aware of the following differences:
Expand All @@ -46,3 +73,7 @@ In Cloudflare Worker and similar serverless JS environment, you need to be aware
- Some of LlamaIndex.TS modules are not available in Cloudflare Worker, for example `SimpleDirectoryReader` (requires `node:fs`), Some multimodal API that relies on [`onnxruntime-node`](https://www.npmjs.com/package/onnxruntime-node)(we might port to HTTP based module in the future).
- `@llamaindex/core` is designed to work in all JavaScript environment, including Cloudflare Worker. If you find any issue, please report to us.
- `@llamaindex/env` is a JS environment binding module, which polyfill some Node.js/Modern Web API (for example, we have a memory based `fs` module, and Crypto API polyfill). It is designed to work in all JavaScript environment, including Cloudflare Worker.

## Known issues

- `llamaindex` not work perfectly in Cloudflare Worker, bundle size will be larger than 1MB, which is the limit of Cloudflare Worker. You will need import submodule instead of the whole `llamaindex` module.
172 changes: 172 additions & 0 deletions e2e/examples/cloudflare-hono/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
\*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
\*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

\*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

\*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.cache
.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

.cache/

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp
.cache

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.\*

# wrangler project

.dev.vars
.wrangler/
20 changes: 20 additions & 0 deletions e2e/examples/cloudflare-hono/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@llamaindex/cloudflare-hono",
"version": "0.0.0",
"private": true,
"scripts": {
"deploy": "wrangler deploy",
"build": "wrangler deploy --dry-run --outdir dist",
"dev": "wrangler dev",
"start": "wrangler dev",
"cf-typegen": "wrangler types"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20241112.0",
"typescript": "^5.5.2",
"wrangler": "^3.89.0"
},
"dependencies": {
"hono": "^4.6.11"
}
}
91 changes: 91 additions & 0 deletions e2e/examples/cloudflare-hono/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Hono } from "hono";

type Bindings = {
OPENAI_API_KEY: string;
PINECONE_API_KEY: string;
};

const app = new Hono<{
Bindings: Bindings;
}>();

app.post("/llm", async (c) => {
//#region init envs
const { setEnvs } = await import("@llamaindex/env");
setEnvs(c.env);
//#endregion

const { message } = await c.req.json();

const { extractText } = await import("@llamaindex/core/utils");

const {
QueryEngineTool,
serviceContextFromDefaults,
VectorStoreIndex,
OpenAIAgent,
Settings,
OpenAI,
OpenAIEmbedding,
} = await import("llamaindex");

const { PineconeVectorStore } = await import(
"llamaindex/vector-store/PineconeVectorStore"
);

const llm = new OpenAI({
model: "gpt-4o-mini",
apiKey: c.env.OPENAI_API_KEY,
});

Settings.embedModel = new OpenAIEmbedding({
model: "text-embedding-3-small",
apiKey: c.env.OPENAI_API_KEY,
});

const serviceContext = serviceContextFromDefaults({
llm,
chunkSize: 8191,
chunkOverlap: 0,
});

const store = new PineconeVectorStore({
namespace: "8xolsn4ulEQGdhnhP76yCzfLHdOZ",
});

const index = await VectorStoreIndex.fromVectorStore(store, serviceContext);

const retriever = index.asRetriever({
similarityTopK: 3,
});

// Create a query engine
const queryEngine = index.asQueryEngine({
retriever,
});

const tools = [
new QueryEngineTool({
queryEngine: queryEngine,
metadata: {
name: "business_info_tool",
description:
"This tool can answer questions based " +
"on business information. Return not found if you" +
" can't find the answer in the documents.",
},
}),
];

const agent = new OpenAIAgent({ tools });

const response = await agent.chat({
message: message,
});

return new Response(extractText(response.message.content));
});

export default {
fetch: app.fetch,
};
39 changes: 39 additions & 0 deletions e2e/examples/cloudflare-hono/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"target": "es2021",
/* Specify a set of bundled library declaration files that describe the target runtime environment. */
"lib": ["es2021", "DOM.AsyncIterable"],
/* Specify what JSX code is generated. */
"jsx": "react-jsx",
/* Specify what module code is generated. */
"module": "es2022",
/* Specify how TypeScript looks up a file from a given module specifier. */
"moduleResolution": "Bundler",
/* Specify type package names to be included without being referenced in a source file. */
"types": ["@cloudflare/workers-types/2023-07-01"],
/* Enable importing .json files */
"resolveJsonModule": true,
/* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
"allowJs": true,
/* Enable error reporting in type-checked JavaScript files. */
"checkJs": false,
/* Disable emitting files from a compilation. */
"noEmit": true,
/* Ensure that each file can be safely transpiled without relying on other imports. */
"isolatedModules": true,
/* Allow 'import x from y' when a module doesn't have a default export. */
"allowSyntheticDefaultImports": true,
/* Ensure that casing is correct in imports. */
"forceConsistentCasingInFileNames": true,
/* Enable all strict type-checking options. */
"strict": true,
/* Skip type checking all .d.ts files. */
"skipLibCheck": true
},
"exclude": ["test"],
"include": ["vitest.config.mts", "worker-configuration.d.ts", "src/**/*.ts"]
}
4 changes: 4 additions & 0 deletions e2e/examples/cloudflare-hono/worker-configuration.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Generated by Wrangler by running `wrangler types`

// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface Env {}
7 changes: 7 additions & 0 deletions e2e/examples/cloudflare-hono/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name = "llamaindex-cloudflare-hono-example"
main = "src/index.ts"
compatibility_date = "2024-11-12"
compatibility_flags = ["nodejs_als"]

[observability]
enabled = true
1 change: 1 addition & 0 deletions packages/env/src/als/index.workerd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { AsyncLocalStorage } from "node:async_hooks";
2 changes: 1 addition & 1 deletion packages/env/src/index.workerd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
import { INTERNAL_ENV } from "./utils/index.js";

export * from "./als/index.non-node.js";
export * from "./als/index.workerd.js";
export { NotSupportCurrentRuntimeClass } from "./utils/shared.js";

export * from "./node-polyfill.js";
Expand Down
Loading

0 comments on commit 969365c

Please sign in to comment.