feat: openrouter model settings (#3422)
This commit is contained in:
parent
c3520e5c52
commit
9aa3a61347
@ -118,10 +118,18 @@ export abstract class BaseExtension implements ExtensionType {
|
|||||||
setting.extensionName = this.name
|
setting.extensionName = this.name
|
||||||
})
|
})
|
||||||
try {
|
try {
|
||||||
|
if (!(await fs.existsSync(extensionSettingFolderPath)))
|
||||||
await fs.mkdir(extensionSettingFolderPath)
|
await fs.mkdir(extensionSettingFolderPath)
|
||||||
const settingFilePath = await joinPath([extensionSettingFolderPath, this.settingFileName])
|
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))
|
await fs.writeFileSync(settingFilePath, JSON.stringify(settings, null, 2))
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
|
|||||||
@ -19,5 +19,15 @@
|
|||||||
"value": "",
|
"value": "",
|
||||||
"type": "password"
|
"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": ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@ -8,22 +8,16 @@
|
|||||||
|
|
||||||
import { RemoteOAIEngine } from '@janhq/core'
|
import { RemoteOAIEngine } from '@janhq/core'
|
||||||
import { PayloadType } from '@janhq/core'
|
import { PayloadType } from '@janhq/core'
|
||||||
import { ChatCompletionRole } from '@janhq/core'
|
|
||||||
|
|
||||||
declare const SETTINGS: Array<any>
|
declare const SETTINGS: Array<any>
|
||||||
declare const MODELS: Array<any>
|
declare const MODELS: Array<any>
|
||||||
|
|
||||||
enum Settings {
|
enum Settings {
|
||||||
apiKey = 'openrouter-api-key',
|
apiKey = 'openrouter-api-key',
|
||||||
|
model = 'openrouter-model',
|
||||||
chatCompletionsEndPoint = 'chat-completions-endpoint',
|
chatCompletionsEndPoint = 'chat-completions-endpoint',
|
||||||
}
|
}
|
||||||
|
|
||||||
enum RoleType {
|
|
||||||
user = 'USER',
|
|
||||||
chatbot = 'CHATBOT',
|
|
||||||
system = 'SYSTEM',
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class that implements the InferenceExtension interface from the @janhq/core package.
|
* 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.
|
* 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 {
|
export default class JanInferenceOpenRouterExtension extends RemoteOAIEngine {
|
||||||
inferenceUrl: string = ''
|
inferenceUrl: string = ''
|
||||||
provider: string = 'openrouter'
|
provider: string = 'openrouter'
|
||||||
|
model?: string | undefined
|
||||||
|
|
||||||
override async onLoad(): Promise<void> {
|
override async onLoad(): Promise<void> {
|
||||||
super.onLoad()
|
super.onLoad()
|
||||||
@ -45,6 +40,9 @@ export default class JanInferenceOpenRouterExtension extends RemoteOAIEngine {
|
|||||||
Settings.chatCompletionsEndPoint,
|
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) {
|
if (this.inferenceUrl.length === 0) {
|
||||||
SETTINGS.forEach((setting) => {
|
SETTINGS.forEach((setting) => {
|
||||||
if (setting.key === Settings.chatCompletionsEndPoint) {
|
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 {
|
onSettingUpdate<T>(key: string, value: T): void {
|
||||||
if (key === Settings.apiKey) {
|
if (key === Settings.apiKey) {
|
||||||
this.apiKey = value as string
|
this.apiKey = value as string
|
||||||
@ -69,8 +75,14 @@ export default class JanInferenceOpenRouterExtension extends RemoteOAIEngine {
|
|||||||
} else {
|
} else {
|
||||||
this.inferenceUrl = value
|
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,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user