Skip to content

Commit

Permalink
feat: allow users to initialize without being explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
ogzhanolguncu committed May 27, 2024
1 parent b66cb8a commit 105b0e5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 });
```
Expand Down
21 changes: 17 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,12 +14,25 @@ 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;

this.model = config.model;
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;
}
};
8 changes: 4 additions & 4 deletions src/rag-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ type RAGChatConfigCommon = {

/**Config needed to initialize RAG Chat SDK */
export type RAGChatConfig = {
vector: Index;
vector?: Index;
redis?: Redis;
} & RAGChatConfigCommon;

0 comments on commit 105b0e5

Please sign in to comment.