Skip to content

Commit

Permalink
Add Hybrid RAG chat with Huberman CLI example
Browse files Browse the repository at this point in the history
  • Loading branch information
rileytomasek committed Nov 5, 2023
1 parent ee47cac commit dfbb6c3
Show file tree
Hide file tree
Showing 6 changed files with 32,901 additions and 1 deletion.
35 changes: 35 additions & 0 deletions example/answer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { chatModel, chunkDatastore } from './tools.js';
import type { Prompt } from '../src/prompt/index.js';
import { Msg } from '../src/prompt/index.js';

const system = Msg.system(`
You are a helpful and accurate Q&A bot.
You answer the user's QUESTION, based only on the PROVIDED_CONTEXT.
Your answer should be at most 100 words long.
You use Markdown footnotes to cite the chunkId of the PROVIDED_CONTEXT that
you used for each part of your answer. (Eg: [^1])
`);

function userMsg(query: string, chunks: unknown[]) {
return Msg.user(`
QUESTION: ${query}
PROVIDED_CONTEXT:
\`\`\`json
${JSON.stringify(chunks, null, 2)}
\`\`\`
`);
}

export async function generateAnswer(
query: string,
history: Prompt.Msg[]
): Promise<Prompt.Msg> {
const results = await chunkDatastore.query({ query, topK: 5 });
const chunks = results.docs.map((doc) => doc.metadata);
const messages = [system, ...history.slice(-5), userMsg(query, chunks)];
const { message } = await chatModel.run({
messages,
handleUpdate: (c) => process.stdout.write(c),
});
return message;
}
24 changes: 24 additions & 0 deletions example/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'dotenv/config.js';
import readline from 'readline';
import { generateAnswer } from './answer.js';
import type { Prompt } from '../src/prompt/types.js';

const history: Prompt.Msg[] = [];
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

function askQuestion(): Promise<string> {
return new Promise((resolve) => rl.question('> ', resolve));
}

console.log('Welcome to Huberman chat. Enter your question to begin.');

while (true) {
const query = await askQuestion();
process.stdout.write('Huberman: ');
const answer = await generateAnswer(query, history);
process.stdout.write('\n\n');
history.push(answer);
}
Loading

0 comments on commit dfbb6c3

Please sign in to comment.