| title | Anthropic |
|---|
Anthropic is a language model provider. Check out Anthropic API for more information about their models and pricing.
Initialize the project and install the required packages:
npm init es6
npm install dotenv
npm install @upstash/rag-chatCreate 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>Create an Anthropic account and get an API key from Anthropic Console -> Settings -> API keys. Set your Anthropic API key as an environment variable:
ANTHROPIC_API_KEY=<YOUR_API_KEY>Initialize RAGChat with the Anthropic model:
import { RAGChat, anthropic } from "@upstash/rag-chat";
import "dotenv/config";
export const ragChat = new RAGChat({
model: anthropic("claude-3-5-sonnet-20240620",{apiKey: process.env.ANTHROPIC_API_KEY}),
});Add context to the RAG Chat:
await ragChat.context.add("The speed of light is approximately 299,792,458 meters per second.");Chat with the RAG Chat:
const response = await ragChat.chat("What is the speed of light?");
console.log(response);Run the project:
npx tsx index.ts