-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
577 additions
and
5 deletions.
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
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,44 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { updateText } from '../lib' | ||
import path from 'path' | ||
import { initTempPath } from '@liuli-util/test' | ||
import { readFile, writeFile } from 'fs/promises' | ||
|
||
const tempPath = initTempPath(__filename) | ||
|
||
it('updateText', async () => { | ||
await writeFile( | ||
path.resolve(tempPath, 'package.json'), | ||
JSON.stringify({ | ||
name: 'novachat.template', | ||
}), | ||
) | ||
await updateText({ | ||
rootPath: tempPath, | ||
rules: [ | ||
{ | ||
path: 'package.json', | ||
replaces: [['novachat.template', 'novachat-plugin-test']], | ||
}, | ||
], | ||
}) | ||
expect( | ||
JSON.parse(await readFile(path.resolve(tempPath, 'package.json'), 'utf-8')), | ||
).toEqual({ | ||
name: 'novachat-plugin-test', | ||
}) | ||
}) | ||
|
||
it("don't exist file", async () => { | ||
await expect( | ||
updateText({ | ||
rootPath: tempPath, | ||
rules: [ | ||
{ | ||
path: 'not-exist-file', | ||
replaces: [['novachat.template', 'novachat-plugin-test']], | ||
}, | ||
], | ||
}), | ||
).rejects.toThrow() | ||
}) |
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
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,21 @@ | ||
import path from 'path' | ||
import { readFile, writeFile } from 'fs/promises' | ||
|
||
export async function updateText(options: { | ||
rootPath: string | ||
rules: { | ||
path: string | ||
replaces: [string, string][] | ||
}[] | ||
}) { | ||
for (const rule of options.rules) { | ||
let content = await readFile( | ||
path.resolve(options.rootPath, rule.path), | ||
'utf-8', | ||
) | ||
for (const [from, to] of rule.replaces) { | ||
content = content.replace(from, to) | ||
} | ||
await writeFile(path.resolve(options.rootPath, rule.path), content) | ||
} | ||
} |
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,28 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
lerna-debug.log* | ||
|
||
node_modules | ||
dist | ||
dist-ssr | ||
*.local | ||
.temp | ||
docs/.vitepress/dist | ||
docs/.vitepress/cache | ||
publish | ||
|
||
# Editor directories and files | ||
.vscode/* | ||
!.vscode/extensions.json | ||
.idea | ||
.DS_Store | ||
*.suo | ||
*.ntvs* | ||
*.njsproj | ||
*.sln | ||
*.sw? |
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,3 @@ | ||
# @novachat/plugin-template | ||
|
||
NovaChat Plugin Template. |
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,29 @@ | ||
{ | ||
"name": "@novachat/plugin-template", | ||
"keywords": [ | ||
"novachat", | ||
"novachat-plugin" | ||
], | ||
"version": "0.1.0", | ||
"license": "MIT", | ||
"type": "module", | ||
"files": [ | ||
"publish" | ||
], | ||
"scripts": { | ||
"setup": "pnpm build", | ||
"build": "novachat", | ||
"dev": "pnpm build --watch" | ||
}, | ||
"sideEffects": false, | ||
"devDependencies": { | ||
"@novachat/cli": "workspace:*", | ||
"typescript": "^5.3.3", | ||
"@novachat/plugin": "workspace:*", | ||
"vitest": "^1.2.2" | ||
}, | ||
"publishConfig": { | ||
"access": "public", | ||
"registry": "https://registry.npmjs.org/" | ||
} | ||
} |
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,5 @@ | ||
import * as novachat from '@novachat/plugin' | ||
|
||
export async function activate() { | ||
console.log('Default model:', await novachat.model.getDefault()) | ||
} |
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,6 @@ | ||
{ | ||
"id": "novachat.template", | ||
"name": "Template", | ||
"description": "Template Plugin", | ||
"version": "0.1.0" | ||
} |
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,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"] | ||
} |
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
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,28 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
lerna-debug.log* | ||
|
||
node_modules | ||
dist | ||
dist-ssr | ||
*.local | ||
.temp | ||
docs/.vitepress/dist | ||
docs/.vitepress/cache | ||
publish | ||
|
||
# Editor directories and files | ||
.vscode/* | ||
!.vscode/extensions.json | ||
.idea | ||
.DS_Store | ||
*.suo | ||
*.ntvs* | ||
*.njsproj | ||
*.sln | ||
*.sw? |
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,3 @@ | ||
# plugin-gemini | ||
|
||
NovaChat Plugin Template. |
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,30 @@ | ||
{ | ||
"name": "@novachat/plugin-gemini", | ||
"keywords": [ | ||
"novachat", | ||
"novachat-plugin" | ||
], | ||
"version": "0.1.0", | ||
"license": "MIT", | ||
"type": "module", | ||
"files": [ | ||
"publish" | ||
], | ||
"scripts": { | ||
"setup": "pnpm build", | ||
"build": "novachat", | ||
"dev": "pnpm build --watch" | ||
}, | ||
"sideEffects": false, | ||
"devDependencies": { | ||
"@google/generative-ai": "^0.21.0", | ||
"@novachat/cli": "workspace:*", | ||
"@novachat/plugin": "workspace:*", | ||
"typescript": "^5.3.3", | ||
"vitest": "^1.2.2" | ||
}, | ||
"publishConfig": { | ||
"access": "public", | ||
"registry": "https://registry.npmjs.org/" | ||
} | ||
} |
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,67 @@ | ||
import * as novachat from '@novachat/plugin' | ||
import GoogleAI, { GoogleGenerativeAI } from '@google/generative-ai' | ||
|
||
function parseRequest( | ||
req: novachat.QueryRequest, | ||
): GoogleAI.GenerateContentRequest { | ||
const systemInstruction = () => { | ||
const system = req.messages.find((m) => m.role === 'system')?.content | ||
if (!system) { | ||
return undefined | ||
} | ||
return system | ||
} | ||
return { | ||
systemInstruction: systemInstruction(), | ||
contents: req.messages.map( | ||
(m) => | ||
({ | ||
role: m.role, | ||
parts: [{ text: m.content }], | ||
} as GoogleAI.Content), | ||
), | ||
} | ||
} | ||
|
||
export async function activate() { | ||
console.log('Default model:', await novachat.model.getDefault()) | ||
|
||
async function createClient(req: novachat.QueryRequest) { | ||
const apiKey = await novachat.setting.get('gemini.apiKey') | ||
if (!apiKey) { | ||
throw new Error('API key is not set') | ||
} | ||
const genAI = new GoogleGenerativeAI(apiKey) | ||
const model = genAI.getGenerativeModel({ model: req.model }) | ||
return model | ||
} | ||
novachat.model.registerProvider({ | ||
name: 'Gemini', | ||
models: [ | ||
{ id: 'gemini-1.0-pro', name: 'Gemini 1.0 Pro' }, | ||
{ id: 'gemini-1.5-pro', name: 'Gemini 1.5 Pro' }, | ||
{ id: 'gemini-1.5-pro-exp-0801', name: 'Gemini 1.5 Pro Exp 0801' }, | ||
{ id: 'gemini-1.5-flash', name: 'Gemini 1.5 Flash' }, | ||
{ id: 'gemma-2-2b-it', name: 'Gemma 2.2b IT' }, | ||
{ id: 'gemma-2-9b-it', name: 'Gemma 2.9b IT' }, | ||
{ id: 'gemma-2-27b-it', name: 'Gemma 2.27b IT' }, | ||
], | ||
invoke: async (req) => { | ||
const { response } = await ( | ||
await createClient(req) | ||
).generateContent(parseRequest(req)) | ||
return { | ||
content: response.text(), | ||
} | ||
}, | ||
async *stream(req) { | ||
const model = await createClient(req) | ||
const stream = await model.generateContentStream(parseRequest(req)) | ||
for await (const chunk of stream.stream) { | ||
yield { | ||
content: chunk.text(), | ||
} | ||
} | ||
}, | ||
}) | ||
} |
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,15 @@ | ||
{ | ||
"id": "novachat.plugin-gemini", | ||
"name": "Gemini", | ||
"description": "Gemini Provider", | ||
"version": "0.1.0", | ||
"configuration": { | ||
"title": "gemini", | ||
"properties": { | ||
"gemini.apiKey": { | ||
"type": "string", | ||
"description": "Google Generative AI API Key" | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.