feat: openrouter model settings (#3422)

This commit is contained in:
Louis 2024-08-21 14:04:37 +07:00 committed by GitHub
parent c3520e5c52
commit 9aa3a61347
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 40 additions and 10 deletions

View File

@ -118,10 +118,18 @@ export abstract class BaseExtension implements ExtensionType {
setting.extensionName = this.name
})
try {
await fs.mkdir(extensionSettingFolderPath)
if (!(await fs.existsSync(extensionSettingFolderPath)))
await fs.mkdir(extensionSettingFolderPath)
const settingFilePath = await joinPath([extensionSettingFolderPath, this.settingFileName])
if (await fs.existsSync(settingFilePath)) return
// Persists new setting only
if (await fs.existsSync(settingFilePath)) {
const oldSettings = JSON.parse(await fs.readFileSync(settingFilePath, 'utf-8'))
if (Array.isArray(oldSettings))
settings = oldSettings.concat(
settings.filter((e) => !oldSettings.some((o) => o.key === e.key))
)
}
await fs.writeFileSync(settingFilePath, JSON.stringify(settings, null, 2))
} catch (err) {
console.error(err)

View File

@ -19,5 +19,15 @@
"value": "",
"type": "password"
}
},
{
"key": "openrouter-model",
"title": "Model",
"description": "If the model parameter is omitted, the user or payer's default is used. Otherwise, remember to select a value for model from the [supported models](https://openrouter.ai/docs/models) or API, and include the organization prefix.",
"controllerType": "input",
"controllerProps": {
"placeholder": "Leave empty for default model",
"value": ""
}
}
]

View File

@ -8,22 +8,16 @@
import { RemoteOAIEngine } from '@janhq/core'
import { PayloadType } from '@janhq/core'
import { ChatCompletionRole } from '@janhq/core'
declare const SETTINGS: Array<any>
declare const MODELS: Array<any>
enum Settings {
apiKey = 'openrouter-api-key',
model = 'openrouter-model',
chatCompletionsEndPoint = 'chat-completions-endpoint',
}
enum RoleType {
user = 'USER',
chatbot = 'CHATBOT',
system = 'SYSTEM',
}
/**
* A class that implements the InferenceExtension interface from the @janhq/core package.
* The class provides methods for initializing and stopping a model, and for making inference requests.
@ -32,6 +26,7 @@ enum RoleType {
export default class JanInferenceOpenRouterExtension extends RemoteOAIEngine {
inferenceUrl: string = ''
provider: string = 'openrouter'
model?: string | undefined
override async onLoad(): Promise<void> {
super.onLoad()
@ -45,6 +40,9 @@ export default class JanInferenceOpenRouterExtension extends RemoteOAIEngine {
Settings.chatCompletionsEndPoint,
''
)
this.model = await this.getSetting<string>(Settings.model, '')
// Openrouter uses default model on no model param set
if (!this.model?.length) this.model = undefined
if (this.inferenceUrl.length === 0) {
SETTINGS.forEach((setting) => {
if (setting.key === Settings.chatCompletionsEndPoint) {
@ -54,6 +52,14 @@ export default class JanInferenceOpenRouterExtension extends RemoteOAIEngine {
}
}
override async headers(): Promise<HeadersInit> {
return {
'Content-Type': 'application/json',
'HTTP-Referer': 'https://jan.ai',
'Authorization': `Bearer ${this.apiKey}`,
}
}
onSettingUpdate<T>(key: string, value: T): void {
if (key === Settings.apiKey) {
this.apiKey = value as string
@ -69,8 +75,14 @@ export default class JanInferenceOpenRouterExtension extends RemoteOAIEngine {
} else {
this.inferenceUrl = value
}
} else if (key === Settings.model) {
this.model =
typeof value === 'string' && value.length > 0 ? value : undefined
}
}
transformPayload = (payload: PayloadType)=>({...payload,model:"openrouter/auto"})
transformPayload = (payload: PayloadType) => ({
...payload,
model: this.model,
})
}