Skip to content

Commit

Permalink
feat: support custom system component
Browse files Browse the repository at this point in the history
  • Loading branch information
rxliuli committed Nov 7, 2024
1 parent efcdea3 commit bce2cad
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
5 changes: 3 additions & 2 deletions packages/plugin-translator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"novachat",
"novachat-plugin"
],
"version": "0.2.0",
"version": "0.3.2",
"license": "MIT",
"type": "module",
"files": [
Expand All @@ -13,7 +13,8 @@
"scripts": {
"setup": "pnpm build",
"build": "novachat",
"dev": "pnpm build --watch"
"dev": "pnpm build --watch",
"prepublishOnly": "pnpm build"
},
"sideEffects": false,
"devDependencies": {
Expand Down
13 changes: 6 additions & 7 deletions packages/plugin-translator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ import { last } from 'lodash-es'
import { franc } from 'franc-min'
import { configuration } from './plugin.json'

const SYSTEM_MESSAGE = `
You are a professional, authentic machine translation engine. Translate the following source text to {{to}}, Output translation directly without any additional text.
`.trim()

export async function activate() {
await novachat.model.registerBot({
id: 'translator',
name: 'Translator',
async *stream(
query: novachat.QueryRequest,
): AsyncGenerator<novachat.QueryChunkResponse> {
const systemPrompt = await novachat.setting.get('translator.systemPrompt')
const lastMessage = last(query.messages)
if (!lastMessage) {
throw new Error('No last message')
}
const defaultModel = await novachat.model.getDefault()
const defaultModel =
((await novachat.setting.get('translator.model')) as string) ??
(await novachat.model.getDefault())?.id
if (!defaultModel) {
throw new Error('No default model')
}
Expand All @@ -32,7 +31,7 @@ export async function activate() {
messages: [
{
role: 'system',
content: SYSTEM_MESSAGE.replace(
content: systemPrompt.replace(
'{{to}}',
translateConfig.enumDescriptions[
translateConfig.enum.indexOf(toLanguage)
Expand All @@ -44,7 +43,7 @@ export async function activate() {
content: lastMessage.content,
},
],
model: defaultModel.id,
model: defaultModel,
})
for await (const it of stream) {
yield it
Expand Down
11 changes: 11 additions & 0 deletions packages/plugin-translator/src/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@
"Ilokano",
"Қазақ"
]
},
"translator.systemPrompt": {
"type": "string",
"description": "System prompt",
"default": "You are a professional, authentic machine translation engine. Translate the following source text to {{to}}, Output translation directly without any additional text.",
"format": "markdown"
},
"translator.model": {
"type": "string",
"description": "Model",
"format": "model"
}
}
}
Expand Down

0 comments on commit bce2cad

Please sign in to comment.