Skip to content

Commit

Permalink
system prompt enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed Nov 5, 2024
1 parent a8dad58 commit 2d2406b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
14 changes: 2 additions & 12 deletions src/services/assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Attachment from '../models/attachment'
import Message from '../models/message'
import LlmFactory from '../llms/llm'
import { store } from './store'
import { countryCodeToName } from './i18n'
import { availablePlugins } from '../plugins/plugins'
import Generator, { GenerationOpts } from './generator'

Expand Down Expand Up @@ -87,14 +86,11 @@ export default class extends Generator {
// we need a chat
if (this.chat === null) {

// system instructions
const systemPrompt = opts.systemInstructions || this.config.instructions.default

// initialize the chat
this.chat = new Chat()
this.chat.docrepo = opts.docrepo
this.chat.setEngineModel(opts.engine, opts.model)
this.chat.addMessage(new Message('system', this.getLocalizedInstructions(systemPrompt)))
this.chat.addMessage(new Message('system', this.getSystemInstructions(opts.systemInstructions)))

// save
if (opts.save) {
Expand Down Expand Up @@ -169,7 +165,7 @@ export default class extends Generator {

// build messages
const messages = [
new Message('system', this.getLocalizedInstructions(this.config.instructions.titling)),
new Message('system', this.getSystemInstructions(this.config.instructions.titling)),
this.chat.messages[1],
this.chat.messages[2],
new Message('user', this.config.instructions.titling_user)
Expand Down Expand Up @@ -201,10 +197,4 @@ export default class extends Generator {

}

getLocalizedInstructions(instructions: string) {
const instr = instructions
if (!this.config.general.language) return instr
return instr + ' Always answer in ' + countryCodeToName(this.config.general.language) + '.'
}

}
26 changes: 25 additions & 1 deletion src/services/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { LlmEngine, LlmCompletionOpts, LlmChunk } from 'multi-llm-ts'
import { Configuration } from 'types/config.d'
import { DocRepoQueryResponseItem } from 'types/rag.d'
import { countryCodeToName } from './i18n'
import Attachment from '../models/attachment'
import Message from '../models/message'

Expand Down Expand Up @@ -128,7 +129,10 @@ export default class {
getConversation(messages: Message[]): Message[] {
const conversationLength = this.config.llm.conversationLength
const chatMessages = messages.filter((msg) => msg.role !== 'system')
const conversation = [messages[0], ...chatMessages.slice(-conversationLength*2, -1)]
const conversation = [
new Message('system', this.patchSystemInstructions(messages[0].content)),
...chatMessages.slice(-conversationLength*2, -1)
]
for (const message of conversation) {
if (message.attachment && !message.attachment.contents) {
message.attachment.loadContents()
Expand All @@ -137,4 +141,24 @@ export default class {
return conversation
}

getSystemInstructions(instructions?: string) {

// default
let instr = instructions || this.config.instructions.default

// language. asking the LLM to talk in the user language confuses them more than often!
if (this.config.general.language) instr += ' Always answer in ' + countryCodeToName(this.config.general.language) + '.'
//else instr += ' Always reply in the user language unless expicitely asked to do otherwise.'

// add date and time
instr += ' Current date and time is ' + new Date().toLocaleString() + '.'

// done
return instr
}

patchSystemInstructions(instructions: string) {
return instructions.replace(/Current date and time is [^.]+/, 'Current date and time is ' + new Date().toLocaleString())
}

}

0 comments on commit 2d2406b

Please sign in to comment.