-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Hybrid RAG chat with Huberman CLI example
- Loading branch information
1 parent
ee47cac
commit dfbb6c3
Showing
6 changed files
with
32,901 additions
and
1 deletion.
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 |
---|---|---|
@@ -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; | ||
} |
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,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); | ||
} |
Oops, something went wrong.