-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
375e3ae
commit 8532294
Showing
19 changed files
with
140 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./src/rag-chat"; | ||
export * from "./src/services/history"; | ||
export * from "./src/error"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { PreferredRegions } from "./types"; | ||
|
||
export const DEFAULT_CHAT_SESSION_ID = "upstash-rag-chat-session"; | ||
export const DEFAULT_CHAT_RATELIMIT_SESSION_ID = "upstash-rag-chat-ratelimit-session"; | ||
|
||
export const RATELIMIT_ERROR_MESSAGE = "ERR:USER_RATELIMITED"; | ||
|
||
export const DEFAULT_VECTOR_DB_NAME = "upstash-rag-chat-vector"; | ||
export const DEFAULT_REDIS_DB_NAME = "upstash-rag-chat-redis"; | ||
export const PREFERRED_REGION: PreferredRegions = "us-east-1"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./ratelimit"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
export class InternalUpstashError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = "InternalUpstashError"; | ||
this.name = "InternalError"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
export class UpstashModelError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = "UpstashModelError"; | ||
this.name = "ModelError"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { RATELIMIT_ERROR_MESSAGE } from "../constants"; | ||
|
||
type RatelimitResponse = { | ||
error: typeof RATELIMIT_ERROR_MESSAGE; | ||
resetTime?: number; | ||
}; | ||
|
||
export class RatelimitUpstashError extends Error { | ||
constructor(message: string, cause: RatelimitResponse) { | ||
super(message); | ||
this.name = "RatelimitError"; | ||
this.cause = cause; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { Ratelimit } from "@upstash/sdk"; | ||
|
||
export class RateLimitService { | ||
private ratelimit?: Ratelimit; | ||
|
||
constructor(ratelimit?: Ratelimit) { | ||
this.ratelimit = ratelimit; | ||
} | ||
|
||
async checkLimit(sessionId: string): Promise<{ success: boolean; resetTime?: number }> { | ||
if (!this.ratelimit) { | ||
// If no ratelimit object is provided, always allow the operation. | ||
return { success: true }; | ||
} | ||
|
||
const result = await this.ratelimit.limit(sessionId); | ||
if (!result.success) { | ||
return { success: false, resetTime: result.reset }; | ||
} | ||
return { success: true }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,30 @@ | ||
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base"; | ||
import type { PromptTemplate } from "@langchain/core/prompts"; | ||
import type { Index, Ratelimit, Redis } from "@upstash/sdk"; | ||
|
||
export type PreferredRegions = "eu-west-1" | "us-east-1"; | ||
|
||
export type ChatOptions = { | ||
stream: boolean; | ||
sessionId?: string; | ||
includeHistory?: number; | ||
similarityThreshold?: number; | ||
ratelimitSessionId?: string; | ||
}; | ||
|
||
export type PrepareChatResult = { | ||
question: string; | ||
facts: string; | ||
}; | ||
|
||
type RAGChatConfigCommon = { | ||
model?: BaseLanguageModelInterface; | ||
template?: PromptTemplate; | ||
region?: PreferredRegions; | ||
ratelimit?: Ratelimit; | ||
}; | ||
|
||
export type RAGChatConfig = { | ||
vector?: string | Index; | ||
redis?: string | Redis; | ||
} & RAGChatConfigCommon; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters