Skip to content

Commit

Permalink
feat: Add knowledge base content support
Browse files Browse the repository at this point in the history
RezaRahemtola committed Aug 7, 2024
1 parent c8ce919 commit 236397e
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/inference.ts
Original file line number Diff line number Diff line change
@@ -24,13 +24,15 @@ export class LlamaCppApiEngine {
* @param {Message[]} messages - The sequence of messages to generate an answer for
* @param {Model} model - The model to use for inference
* @param {Persona} persona - The persona to use for inference
* @param {string[]} knowledgeSearchResults - Knowledge bases content to help the model answer correctly
* @param {string} targetUser - The user to generate the answer for, if different from Message[-1].role
* @param {boolean} debug - Whether to print debug information
*/
async *generateAnswer(
messages: Message[],
model: Model,
persona: Persona,
knowledgeSearchResults: string[] = [],
targetUser: string | null = null,
debug: boolean = false
): AsyncGenerator<{ content: string; stopped: boolean }> {
@@ -41,7 +43,13 @@ export class LlamaCppApiEngine {
);

// Prepare the prompt
const prompt = this.preparePrompt(messages, model, persona, targetUser);
const prompt = this.preparePrompt(
messages,
model,
persona,
targetUser,
knowledgeSearchResults
);

if (debug) {
console.log(
@@ -141,7 +149,8 @@ export class LlamaCppApiEngine {
model: Model,
persona: Persona,
// Allow caller to specify a target user, if different from Message[-1].role
targetUser: string | null = null
targetUser: string | null = null,
knowledgeSearchResults: string[]
): string {
const maxTokens = model.maxTokens;
const promptFormat = model.promptFormat;
@@ -160,7 +169,10 @@ export class LlamaCppApiEngine {
description = description.replace(/\{\{model\}\}/g, model.name);

// Prepare our system prompt
let systemPrompt = '';
let systemPrompt =
knowledgeSearchResults.length > 0
? `You have access to the following information to help you: ${knowledgeSearchResults.join(promptFormat.lineSeparator)}`
: '';
systemPrompt += `${promptFormat.userPrepend}system${promptFormat.userAppend}`;
systemPrompt += `${description}`;
systemPrompt += `${promptFormat.stopSequence}${promptFormat.logStart}`;

0 comments on commit 236397e

Please sign in to comment.