title |
---|
Node.js |
Initialize the project and install the required packages:
npm init es6
npm install dotenv
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 .env
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 .env
file.
UPSTASH_VECTOR_REST_URL=<YOUR_URL>
UPSTASH_VECTOR_REST_TOKEN=<YOUR_TOKEN>
Navigate to QStash Console and copy the QSTASH_TOKEN
into your .env
file.
QSTASH_TOKEN=<YOUR_TOKEN>
Create server.ts
:
import { RAGChat, upstash } from "@upstash/rag-chat";
import dotenv from "dotenv";
import { createServer } from "node:http";
dotenv.config();
const server = createServer(async (_, result) => {
const ragChat = new RAGChat({
model: upstash("meta-llama/Meta-Llama-3-8B-Instruct"),
});
await ragChat.context.add({
type: "text",
data: "Paris, the capital of France, is renowned for its iconic landmark, the Eiffel Tower, which was completed in 1889 and stands at 330 meters tall.",
});
// 👇 slight delay to allow for vector indexing
await sleep(3000);
const response = await ragChat.chat(
"What year was the construction of the Eiffel Tower completed, and what is its height?"
);
result.writeHead(200, { "Content-Type": "text/plain" });
result.write(response.output);
result.end();
});
server.listen(8080, () => {
console.log("Server listening on http://localhost:8080");
});
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
npx tsx server.ts
Visit http://localhost:8080