Skip to content

Commit

Permalink
chore: update readme and organize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ogzhanolguncu committed May 6, 2024
1 parent cf9af20 commit 6aaaa32
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 10 deletions.
65 changes: 64 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,64 @@
# SOON
# Upstash RAG Chat SDK

The `@upstash/rag-chat` SDK simplifies RAG (retrieval-augmented generation) chat development.

Features:

- Creates a Redis instance for your chat history, fully configurable.
- Creates a Index instance for your knowledge base.
- Integrates with Next.js using streams and is compatible with other frameworks.
- Leverages LangChain, Vercel AI SDK, and Upstash products.

```typescript
import { ChatOpenAI } from "@langchain/openai";
import { PromptTemplate } from "@langchain/core/prompts";

// Basic usage
const ragchat = await RAGChat.initialize({
email: "YOUR_UPSTASH_EMAIL",
token: "YOUR_UPSTASH_TOKEN,
model: new ChatOpenAI({
modelName: "gpt-3.5-turbo",
streaming: true,
verbose,
temperature: 0,
apiKey: "YOUR_OPEN_AI_KEY_HERE",
}),
});
await ragchat.chat("Say Hello To My Little Friend", { stream: true });

// Advance
const ragchat = await RAGChat.initialize({
email: process.env.UPSTASH_EMAIL!,
token: process.env.UPSTASH_TOKEN!,
model: new ChatOpenAI({
modelName: "gpt-3.5-turbo",
streaming: true,
verbose,
temperature: 0,
apiKey: "YOUR_OPEN_AI_KEY_HERE",
}) // Or your can pass any other Langchain compatible LLMs like Gemini, Anthropic, etc...
vector: new Index(),
redis: new Redis(),
ratelimit: new Ratelimit({
redis,
limiter: Ratelimit.tokenBucket(10, "1d", 10),
prefix: "@upstash/rag-chat-ratelimit",
}),
region: "us-east-1", // Default to "us-east-1"
template: PromptTemplate.fromTemplate(
"Use this history {chat_history} and this context {context}"
),
});

await ragchat.chat("Say Hello To My Little Friend", {
stream: true,
includeHistory: 5,
ratelimitSessionId: "user-ip",
sessionId: "chat-session-id",
similarityThreshold: 0.8,
topK: 10,
});
```

In the future, you'll be able to pass your own custom LangChain flows as callbacks, providing better control and flexibility.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@upstash/rag-chat",
"version": "0.0.15-alpha",
"version": "0.0.20-alpha",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
Expand Down Expand Up @@ -51,7 +51,7 @@
"@langchain/community": "^0.0.50",
"@langchain/core": "^0.1.58",
"@langchain/openai": "^0.0.28",
"@upstash/sdk": "0.0.26-alpha",
"@upstash/sdk": "0.0.30-alpha",
"ai": "^3.0.35"
}
}
4 changes: 2 additions & 2 deletions src/client-factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe("ClientFactory Tests", () => {

await upstash.deleteRedisDatabase("test-rag-chat-client-factor-redis");
},
{ timeout: 20_000 }
{ timeout: 30_000 }
);

test(
Expand All @@ -36,6 +36,6 @@ describe("ClientFactory Tests", () => {

await upstash.deleteVectorIndex("test-rag-chat-client-factor-vector");
},
{ timeout: 20_000 }
{ timeout: 30_000 }
);
});
6 changes: 3 additions & 3 deletions src/clients/redis/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("Redis Client", () => {

await upstashSDK.deleteRedisDatabase(DEFAULT_REDIS_DB_NAME);
},
{ timeout: 20_000 }
{ timeout: 30_000 }
);

test(
Expand All @@ -38,7 +38,7 @@ describe("Redis Client", () => {

await upstashSDK.deleteRedisDatabase("test-name");
},
{ timeout: 20_000 }
{ timeout: 30_000 }
);

test(
Expand All @@ -61,6 +61,6 @@ describe("Redis Client", () => {

await upstashSDK.deleteRedisDatabase(dbName);
},
{ timeout: 10_000 }
{ timeout: 30_000 }
);
});
2 changes: 2 additions & 0 deletions src/clients/redis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { CreateCommandPayload, Upstash } from "@upstash/sdk";
import { Redis } from "@upstash/sdk";
import type { PreferredRegions } from "../../types";
import { DEFAULT_REDIS_DB_NAME } from "../../constants";
import { delay } from "../../utils";

export const DEFAULT_REDIS_CONFIG: CreateCommandPayload = {
name: DEFAULT_REDIS_DB_NAME,
Expand Down Expand Up @@ -76,6 +77,7 @@ export class RedisClient {
name: redisDbName ?? DEFAULT_REDIS_CONFIG.name,
region: this.region ?? DEFAULT_REDIS_CONFIG.region,
});
await delay();

if (redisDatabase?.database_name) {
this.redisClient = await this.upstashSDK.newRedisClient(redisDatabase.database_name);
Expand Down
2 changes: 1 addition & 1 deletion src/clients/vector/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("Vector Client", () => {
const vectorInstance = await upstashSDK.createVectorIndex({
...DEFAULT_VECTOR_CONFIG,
name: indexName,
type: "free",
type: "payg",
});
const existingVectorClient = await upstashSDK.newVectorClient(vectorInstance.name);

Expand Down
5 changes: 4 additions & 1 deletion src/clients/vector/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ export class VectorClient {
});
} catch (error) {
const error_ = error as Error;
if (error_.message === "free plan is the only available option for free accounts") {
if (
error_.message.includes("free plan is the only available option for free accounts") ||
error_.message.includes("payg and fixed are only available options for paid accounts")
) {
index = await this.upstashSDK.getOrCreateVectorIndex({
...DEFAULT_VECTOR_CONFIG,
name: indexName ?? DEFAULT_VECTOR_CONFIG.name,
Expand Down
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ export function appendDefaultsIfNeeded(options: ChatOptions) {
ratelimitSessionId: options.ratelimitSessionId ?? DEFAULT_CHAT_RATELIMIT_SESSION_ID,
} satisfies ChatOptions;
}

const DEFAULT_DELAY = 20_000;
export function delay(ms = DEFAULT_DELAY): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

0 comments on commit 6aaaa32

Please sign in to comment.