-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathconfig.ts
More file actions
75 lines (61 loc) · 2.68 KB
/
config.ts
File metadata and controls
75 lines (61 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
import type { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
import { Index } from "@upstash/vector";
import { DEFAULT_PROMPT } from "./constants";
import { upstash, openai } from "./models";
import type { RAGChatConfig, UpstashDict, OpenAIChatLanguageModel } from "./types";
import type { CustomPrompt } from "./rag-chat";
export class Config {
public readonly vector?: Index;
public readonly redis?: Redis;
public readonly ratelimit?: Ratelimit;
public readonly ratelimitSessionId?: string;
public readonly model?: BaseLanguageModelInterface | OpenAIChatLanguageModel;
public readonly prompt: CustomPrompt;
public readonly streaming?: boolean;
public readonly namespace?: string;
public readonly metadata?: UpstashDict | undefined;
public readonly sessionId?: string | undefined;
public readonly debug?: boolean;
constructor(config?: RAGChatConfig) {
this.redis = config?.redis ?? initializeRedis();
this.ratelimit = config?.ratelimit;
this.ratelimitSessionId = config?.ratelimitSessionId;
this.streaming = config?.streaming;
this.namespace = config?.namespace;
this.metadata = config?.metadata;
this.sessionId = config?.sessionId;
this.model = config?.model ?? initializeModel();
this.prompt = config?.promptFn ?? DEFAULT_PROMPT;
this.vector = config?.vector ?? Index.fromEnv();
this.debug = config?.debug;
}
}
/**
* 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 = () => {
const environment = typeof process === "undefined" ? ({} as Record<string, string>) : process.env;
return environment.UPSTASH_REDIS_REST_URL && environment.UPSTASH_REDIS_REST_TOKEN
? Redis.fromEnv()
: undefined;
};
/**
* Attempts to create a model instance using environment variables.
* It first looks for QStash LLM tokens, if not present, looks for OpenAI tokens. If both of them are missing returns undefined.
*/
const initializeModel = () => {
const qstashToken = process.env.QSTASH_TOKEN;
const openAIToken = process.env.OPENAI_API_KEY;
const organization = process.env.OPENAI_ORGANIZATION;
if (qstashToken) return upstash("meta-llama/Meta-Llama-3-8B-Instruct", { apiKey: qstashToken });
if (openAIToken) {
return openai("gpt-4o", { apiKey: openAIToken, organization });
}
throw new Error(
"[RagChat Error]: Unable to connect to model. Pass one of OPENAI_API_KEY or QSTASH_TOKEN environment variables."
);
};