Together AI is a language model provider. Check out Together AI API for more information about their models and pricing.

Install RAG Chat SDK

Initialize the project and install the required packages:

npm init es6
npm install dotenv
npm install @upstash/rag-chat

Setup Upstash Redis

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.

.env
UPSTASH_REDIS_REST_URL=<YOUR_URL>
UPSTASH_REDIS_REST_TOKEN=<YOUR_TOKEN>

Setup Upstash Vector

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.

.env
UPSTASH_VECTOR_REST_URL=<YOUR_URL>
UPSTASH_VECTOR_REST_TOKEN=<YOUR_TOKEN>

Setup Together AI

Create a Together AI account and get an API key from Together AI API -> Settings -> API KEYS. Set your Together AI API key as an environment variable:

.env
TOGETHER_AI_KEY=<YOUR_API_KEY>

Setup the Project

Initialize RAGChat with the Together AI model:

index.ts
import { RAGChat, togetherai } from "@upstash/rag-chat";
import "dotenv/config";

export const ragChat = new RAGChat({
  model: togetherai("meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo",{apiKey: process.env.TOGETHER_AI_KEY}),
});

Add context to the RAG Chat:

index.ts
await ragChat.context.add("The speed of light is approximately 299,792,458 meters per second.");

Chat with the RAG Chat:

index.ts
const response = await ragChat.chat("What is the speed of light?");
console.log(response);

Run

Run the project:

npx tsx index.ts