From 105b0e59ee23fdd6dd8c53e9bc41fa039526964c Mon Sep 17 00:00:00 2001 From: ogzhanolguncu Date: Mon, 27 May 2024 16:20:06 +0300 Subject: [PATCH] feat: allow users to initialize without being explicit --- README.md | 26 ++++++++++++++++++++++---- src/config.ts | 21 +++++++++++++++++---- src/rag-chat.ts | 8 ++++---- src/types.ts | 2 +- 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 3b478d3..6d5339c 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,29 @@ Features: - Leverages LangChain, Vercel AI SDK, and Upstash products. - Allows you to add various data types into your Vector store. +## Installation + +```sh +pnpm add @upstash/vector @uptash/index @upstash/rag-chat + +bun add @upstash/vector @uptash/index @upstash/rag-chat + +npm i @upstash/vector @uptash/index @upstash/rag-chat +``` + ### Basic Usage of Initilization and `chat()` -Most basic usage relies on in-memory chat history instead of Redis. +If you are planning to use the most basic version of our SDK, make sure you have those files in your `.env`. + +```sh +UPSTASH_VECTOR_REST_URL="XXXXX" +UPSTASH_VECTOR_REST_TOKEN="XXXXX" + +UPSTASH_REDIS_REST_URL="XXXXX" +UPSTASH_REDIS_REST_TOKEN="XXXXX" +``` + +Now, you are all set. Required Redis and Vector instances will be created for you. ```typescript import { ChatOpenAI } from "@langchain/openai"; @@ -26,10 +46,8 @@ const ragChat = new RAGChat({ streaming: true, verbose: false, temperature: 0, - apiKey: process.env.OPENAI_API_KEY, + apiKey: "XXXXX", }), - vector: new Index(), - redis: new Redis(), }); await ragchat.chat("Say Hello To My Little Friend", { stream: true }); ``` diff --git a/src/config.ts b/src/config.ts index 729ace7..11818d0 100644 --- a/src/config.ts +++ b/src/config.ts @@ -2,8 +2,8 @@ import type { BaseLanguageModelInterface } from "@langchain/core/language_models import type { PromptTemplate } from "@langchain/core/prompts"; import type { RAGChatConfig } from "./types"; import type { Ratelimit } from "@upstash/ratelimit"; -import type { Redis } from "@upstash/redis"; -import type { Index } from "@upstash/vector"; +import { Redis } from "@upstash/redis"; +import { Index } from "@upstash/vector"; export class Config { public readonly vector: Index; @@ -14,8 +14,8 @@ export class Config { public readonly prompt?: PromptTemplate; constructor(config: RAGChatConfig) { - this.vector = config.vector; - this.redis = config.redis; + this.vector = config.vector ?? Index.fromEnv(); + this.redis = config.redis ?? initializeRedis(); this.ratelimit = config.ratelimit; @@ -23,3 +23,16 @@ export class Config { this.prompt = config.prompt; } } + +/** + * Attempts to create a Redis instance using environment variables. + * If the required environment variables are not found, it catches the error + * and returns undefined, allowing RAG CHAT to fall back to using an in-memory database. + */ +const initializeRedis = () => { + try { + return Redis.fromEnv(); + } catch { + return; + } +}; diff --git a/src/rag-chat.ts b/src/rag-chat.ts index 5a25d38..1450aa4 100644 --- a/src/rag-chat.ts +++ b/src/rag-chat.ts @@ -6,10 +6,10 @@ import { QA_PROMPT_TEMPLATE } from "./prompts"; import { UpstashModelError } from "./error/model"; import { RatelimitUpstashError } from "./error/ratelimit"; -import type { Config } from "./config"; +import { Config } from "./config"; import { RAGChatBase } from "./rag-chat-base"; import { RateLimitService } from "./ratelimit"; -import type { ChatOptions } from "./types"; +import type { ChatOptions, RAGChatConfig } from "./types"; import { appendDefaultsIfNeeded } from "./utils"; import type { AddContextOptions, AddContextPayload } from "./database"; import { Database } from "./database"; @@ -18,8 +18,8 @@ import { History } from "./history"; export class RAGChat extends RAGChatBase { #ratelimitService: RateLimitService; - constructor(config: Config) { - const { vector: index, redis } = config; + constructor(config: RAGChatConfig) { + const { vector: index, redis } = new Config(config); const historyService = new History(redis); const vectorService = new Database(index); diff --git a/src/types.ts b/src/types.ts index 2435ea8..3cd5399 100644 --- a/src/types.ts +++ b/src/types.ts @@ -100,6 +100,6 @@ type RAGChatConfigCommon = { /**Config needed to initialize RAG Chat SDK */ export type RAGChatConfig = { - vector: Index; + vector?: Index; redis?: Redis; } & RAGChatConfigCommon;