-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cloudflare worker full example (#1521)
- Loading branch information
Showing
12 changed files
with
469 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@llamaindex/env": patch | ||
--- | ||
|
||
fix: async local storage on cloudflare worker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { AsyncLocalStorage } from "node:async_hooks"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.