title |
---|
Hono |
Create a new Hono application with cloudflare-workers
template and install @upstash/rag-chat
package.
npm create hono@latest my-app
cd my-app
npm install @upstash/rag-chat
Create a Redis database using Upstash Console or Upstash CLI and copy the UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
into your .dev.vars
file.
UPSTASH_REDIS_REST_URL=<YOUR_URL>
UPSTASH_REDIS_REST_TOKEN=<YOUR_TOKEN>
Create a Vector index using Upstash Console or Upstash CLI and copy the UPSTASH_VECTOR_REST_URL
and UPSTASH_VECTOR_REST_TOKEN
into your .dev.vars
file.
UPSTASH_VECTOR_REST_URL=<YOUR_URL>
UPSTASH_VECTOR_REST_TOKEN=<YOUR_TOKEN>
Navigate to QStash Console and copy the QSTASH_TOKEN
into your .dev.vars
file.
QSTASH_TOKEN=<YOUR_TOKEN>
Note that we are using the @upstash/redis/cloudflare
package and setting cache
to false
in the Index
constructor to be able to work in Cloudflare Workers, these are not related to Hono.
Update /src/index.ts
:
import { Hono } from 'hono'
import { RAGChat, upstash } from "@upstash/rag-chat";
import { Index } from "@upstash/vector";
import { Redis } from "@upstash/redis/cloudflare";
const app = new Hono<{
Bindings: {
UPSTASH_REDIS_REST_URL: string;
UPSTASH_REDIS_REST_TOKEN: string;
UPSTASH_VECTOR_REST_URL: string;
UPSTASH_VECTOR_REST_TOKEN: string;
QSTASH_TOKEN: string;
};
}>();
app.get('/', async (c) => {
const ragChat = new RAGChat({
redis: new Redis({
token: c.env.UPSTASH_REDIS_REST_TOKEN,
url: c.env.UPSTASH_REDIS_REST_URL
}),
vector: new Index({
token: c.env.UPSTASH_VECTOR_REST_TOKEN,
url: c.env.UPSTASH_VECTOR_REST_URL,
cache: false
}),
model: upstash("meta-llama/Meta-Llama-3-8B-Instruct", {
apiKey: c.env.QSTASH_TOKEN
}),
});
const response = await ragChat.chat("What is the speed of light?");
return c.text(response.output)
})
export default app
Run the Hono application locally:
npm run dev
Visit http://localhost:8787
For deployment, use the wrangler
CLI to securely set environment variables. Run the following command for each secret:
npx wrangler secret put SECRET_NAME
Replace SECRET_NAME
with the actual name of each environment variable (e.g., UPSTASH_REDIS_REST_URL
).
Deploy the Hono application:
npm run deploy