Skip to content

Commit

Permalink
feat: add ollama plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rxliuli committed Oct 6, 2024
1 parent 2a1f0f6 commit 35ff148
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 89 deletions.
1 change: 1 addition & 0 deletions packages/plugin-ollama/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
publish
3 changes: 3 additions & 0 deletions packages/plugin-ollama/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @novachat/plugin-openai

NovaChat OpenAI Plugin.
33 changes: 33 additions & 0 deletions packages/plugin-ollama/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@novachat/plugin-ollama",
"private": true,
"keywords": [
"novachat",
"novachat-plugin"
],
"version": "0.1.0",
"license": "MIT",
"type": "module",
"files": [
"publish"
],
"scripts": {
"setup": "pnpm build",
"build": "tsup",
"dev": "pnpm build --watch"
},
"sideEffects": false,
"devDependencies": {
"ollama": "^0.5.9",
"tsup": "^8.3.0",
"typescript": "^5.3.3",
"vitest": "^1.2.2"
},
"dependencies": {
"@novachat/plugin": "workspace:*"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
}
}
39 changes: 39 additions & 0 deletions packages/plugin-ollama/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as novachat from '@novachat/plugin'
import ollama from 'ollama/browser'

function convertMessages(messages: novachat.QueryRequest['messages']) {
return messages.map((it) => ({
role: it.role,
content: it.content,
images: it.attachments?.map((it) => it.url),
}))
}

export async function activate() {
const list = await ollama.list()
await novachat.model.registerProvider({
name: 'Ollama',
models: list.models.map((it) => ({ id: it.name, name: it.name })),
async invoke(query) {
const r = await ollama.chat({
model: query.model,
messages: convertMessages(query.messages),
})
return {
content: r.message.content,
}
},
async *stream(query) {
const r = await ollama.chat({
model: query.model,
messages: convertMessages(query.messages),
stream: true,
})
for await (const it of r) {
yield {
content: it.message.content,
}
}
},
})
}
7 changes: 7 additions & 0 deletions packages/plugin-ollama/src/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": "novachat.ollama",
"name": "Ollama",
"description": "Ollama Provider",
"version": "0.1.0",
"author": "NovaChat"
}
16 changes: 16 additions & 0 deletions packages/plugin-ollama/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "ESNext",
"lib": ["ESNext"],
"outDir": "./dist",
"skipLibCheck": true,
"esModuleInterop": true,
"strict": true,
"module": "ESNext",
"moduleResolution": "bundler",
"sourceMap": true,
"declaration": true,
"declarationMap": true
},
"include": ["src"]
}
34 changes: 34 additions & 0 deletions packages/plugin-ollama/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import JSZip from 'jszip'
import { defineConfig } from 'tsup'
import manifest from './src/plugin.json'
import { mkdir, readFile, writeFile } from 'fs/promises'

export default defineConfig({
entry: ['src/index.ts'],
format: ['esm'],
bundle: true,
splitting: false,
sourcemap: 'inline',
esbuildOptions: (options) => {
options.platform = 'browser'
},
plugins: [
{
name: 'plugin-novachat',
async buildEnd() {
const zip = new JSZip()
zip.file('plugin.json', JSON.stringify(manifest))
zip.file('index.js', await readFile('dist/index.js'))
await mkdir('publish', { recursive: true })
await writeFile(
'publish/plugin.zip',
await zip.generateAsync({ type: 'nodebuffer' }),
)
await writeFile(
'publish/plugin.json',
JSON.stringify(manifest, null, 2),
)
},
},
],
})
1 change: 1 addition & 0 deletions packages/plugin-translator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"devDependencies": {
"tsup": "^8.3.0",
"typescript": "^5.3.3",
"@langchain/textsplitters": "^0.1.0",
"vitest": "^1.2.2"
},
"dependencies": {
Expand Down
27 changes: 15 additions & 12 deletions packages/plugin-translator/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import * as novachat from '@novachat/plugin'
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.'
const USER_MESSAGE = `
Translate the following source text to {{to}}, Output translation directly without any additional text.
Source Text: {{text}}
Translated Text:
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() {
Expand All @@ -30,16 +26,23 @@ export async function activate() {
(await novachat.setting.get('translator.localLanguage')) ?? 'eng'
const language = franc(lastMessage.content)
const toLanguage = language === localLanguage ? 'eng' : localLanguage
const translateConfig =
configuration.properties['translator.localLanguage']
const stream = novachat.model.stream({
messages: [
{ role: 'system', content: SYSTEM_MESSAGE },
{
role: 'user',
content: USER_MESSAGE.replace('{{to}}', toLanguage).replace(
'{{text}}',
lastMessage.content,
role: 'system',
content: SYSTEM_MESSAGE.replace(
'{{to}}',
translateConfig.enumDescriptions[
translateConfig.enum.indexOf(toLanguage)
],
),
},
{
role: 'user',
content: lastMessage.content,
},
],
model: defaultModel.id,
})
Expand Down
152 changes: 76 additions & 76 deletions packages/plugin-translator/src/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,88 +96,88 @@
"kaz"
],
"enumDescriptions": [
"Mandarin Chinese",
"Spanish",
"简体中文",
"Español",
"English",
"Russian",
"Standard Arabic",
"Bengali",
"Hindi",
"Portuguese",
"Indonesian",
"Japanese",
"French",
"German",
"Javanese (Javanese)",
"Javanese (Latin)",
"Korean",
"Telugu",
"Vietnamese",
"Marathi",
"Italian",
"Tamil",
"Turkish",
"Urdu",
"Gujarati",
"Polish",
"Ukrainian",
"Kannada",
"Maithili",
"Malayalam",
"Iranian Persian",
"Burmese",
"Swahili (individual language)",
"Sundanese",
"Romanian",
"Panjabi",
"Bhojpuri",
"Amharic",
"Русский",
"العربية الفصحى",
"বাংলা",
"हिन्दी",
"Português",
"Bahasa Indonesia",
"日本語",
"Français",
"Deutsch",
"ꦧꦱꦗꦮ",
"Basa Jawa",
"한국어",
"తెలుగు",
"Tiếng Việt",
"मराठी",
"Italiano",
"தமிழ்",
"Türkçe",
"اردو",
"ગુજરાતી",
"Polski",
"Українська",
"ಕನ್ನಡ",
"मैथिली",
"മലയാളം",
"فارسی",
"မြန်မာဘာသာ",
"Kiswahili",
"Basa Sunda",
"Română",
"ਪੰਜਾਬੀ",
"भोजपुरी",
"አማርኛ",
"Hausa",
"Nigerian Fulfulde",
"Bosnian (Cyrillic)",
"Bosnian (Latin)",
"Croatian",
"Dutch",
"Serbian (Cyrillic)",
"Serbian (Latin)",
"Thai",
"Central Kurdish",
"Yoruba",
"Northern Uzbek (Cyrillic)",
"Northern Uzbek (Latin)",
"Malay (individual language) (Arabic)",
"Malay (individual language) (Latin)",
"Fulfulde",
"Босански",
"Bosanski",
"Hrvatski",
"Nederlands",
"Српски",
"Srpski",
"ไทย",
"کوردیی ناوەندی",
"Yorùbá",
"Ўзбек",
"O'zbek",
"بهاس ملايو",
"Bahasa Melayu",
"Igbo",
"Nepali (individual language)",
"नेपाली",
"Cebuano",
"Saraiki",
"سرائیکی",
"Tagalog",
"Hungarian",
"North Azerbaijani (Cyrillic)",
"North Azerbaijani (Latin)",
"Sinhala",
"Komi-Permyak",
"Modern Greek (1453-)",
"Czech",
"Magahi",
"Rundi",
"Belarusian",
"Plateau Malagasy",
"Chimborazo Highland Quichua",
"Madurese",
"Nyanja",
"Yongbei Zhuang",
"Northern Pashto",
"Magyar",
"Азәрбајҹан",
"Azərbaycan",
"සිංහල",
"Коми-Пермяцкӧй",
"Ελληνικά",
"Čeština",
"मगही",
"Ikirundi",
"Беларуская",
"Malagasy",
"Kichwa",
"Madhura",
"Chichewa",
"Vahcuengh",
"پښتو",
"Kinyarwanda",
"Zulu",
"Bulgarian",
"Swedish",
"Lingala",
"Somali",
"Southern Qiandong Miao",
"Hmong Njua",
"Iloko",
"Kazakh"
"isiZulu",
"Български",
"Svenska",
"Lingála",
"Soomaali",
"Hmong Daw",
"Hmoob Njua",
"Ilokano",
"Қазақ"
]
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-vertex-anthropic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"@anthropic-ai/vertex-sdk": "^0.4.2",
"@langchain/anthropic": "^0.3.3",
"google-auth-library": "^9.14.1",
"openai": "^4.65.0",
"jose": "^5.9.3",
"tsup": "^8.3.0",
"typescript": "^5.3.3",
Expand Down

0 comments on commit 35ff148

Please sign in to comment.