Skip to content

Commit

Permalink
chore: move functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ogzhanolguncu committed Apr 30, 2024
1 parent ddfa01f commit 0be5029
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { formatChatHistory, formatFacts, sanitizeQuestion } from "./utils";
const SIMILARITY_THRESHOLD = 0.5;

type CustomInputValues = { chat_history?: BaseMessage[]; question: string; context: string };
type ChatOptions = { stream: boolean; sessionId: string; includeHistory?: number };

type RAGChatConfigCommon = {
model: BaseLanguageModelInterface;
Expand All @@ -42,26 +43,34 @@ export type RAGChatConfig = (
) &
RAGChatConfigCommon;

type PrepareChatResult = {
question: string;
facts: string;
};

export class RAGChat {
private sdkClient: Upstash;
private config?: RAGChatConfig;

//CLIENTS
private vectorClient?: Index;
private redisClient?: Redis;
private ratelimiterClient?: Ratelimit;

constructor(email: string, token: string, config?: RAGChatConfig) {
this.sdkClient = new Upstash({ email, token, ...config?.umbrellaConfig });
this.config = config;

this.initializeClients().catch((error: unknown) => {
console.error("Failed to initialize clients:", error);
});
}

private async getFactsFromVector(
question: string,
similarityThreshold = SIMILARITY_THRESHOLD
): Promise<string> {
if (!this.vectorClient)
throw new InternalUpstashError("vectorClient is missing in getFactsFromVector");
throw new InternalUpstashError("Vector client is missing in getFactsFromVector");

const index = this.vectorClient;
const result = await index.query<{ value: string }>({
Expand All @@ -85,32 +94,23 @@ export class RAGChat {
return formatFacts(facts);
}

chat = async (
input: string,
chatOptions: { stream: boolean; sessionId: string; includeHistory?: number }
) => {
await this.initializeClients();

private async prepareChat(input: string): Promise<PrepareChatResult> {
const question = sanitizeQuestion(input);
const facts = await this.getFactsFromVector(question);
return { question, facts };
}

const { stream, sessionId, includeHistory } = chatOptions;

if (stream) {
return this.chainCallStreaming(question, facts, sessionId, includeHistory);
}
async chat(input: string, options: ChatOptions) {
const { question, facts } = await this.prepareChat(input);

return this.chainCall({ sessionId, includeHistory }, question, facts);
};
return options.stream
? this.streamingChainCall(question, facts, options)
: this.chainCall(options, question, facts);
}

private chainCallStreaming = (
question: string,
facts: string,
sessionId: string,
includeHistory?: number
) => {
private streamingChainCall = (question: string, facts: string, chatOptions: ChatOptions) => {
const { stream, handlers } = LangChainStream();
void this.chainCall({ sessionId, includeHistory }, question, facts, [handlers]);
void this.chainCall(chatOptions, question, facts, [handlers]);
return new StreamingTextResponse(stream, {});
};

Expand Down

0 comments on commit 0be5029

Please sign in to comment.