Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow alternative OpenAI-compatible endpoints #494

Merged
merged 10 commits into from
Oct 4, 2024
Prev Previous commit
Next Next commit
[AI-32] sort models list in chat settings
  • Loading branch information
betschki committed Aug 2, 2024
commit e0345d4059002748c8662188479aab4866416134
31 changes: 26 additions & 5 deletions src/lib/Models.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script context="module" lang="ts">
import { apiKeyStorage, globalStorage } from './Storage.svelte'
import { apiKeyStorage, getApiBase, globalStorage } from './Storage.svelte'
import { get } from 'svelte/store'
import type { ModelDetail, Model, SelectOption, Chat } from './Types.svelte'
import { mergeProfileFields } from './Profiles.svelte'
Expand Down Expand Up @@ -162,21 +162,42 @@ export const hasActiveModels = (): boolean => {
return !!get(apiKeyStorage) || !!globalSettings.enablePetals
}

const sortModelsAlphabetically = (a: SelectOption, b: SelectOption): number => {
const aText = a.text.toLowerCase()
const bText = b.text.toLowerCase()

if (aText < bText) return -1
if (aText > bText) return 1
return 0
}

export async function getChatModelOptions (): Promise<SelectOption[]> {
const remoteModels = await fetchRemoteModels()
const isOpenAi = getApiBase().includes('openai.com')

// We are checking if the OpenAI endpoint is used, so we only fetch
// additional models for non-OpenAI endpoints
const remoteModels = isOpenAi ? {} : await fetchRemoteModels()

const models = Object.keys({ ...supportedChatModels, ...remoteModels })
const result:SelectOption[] = []
const modelOptionsActive:SelectOption[] = []
const modelOptionsInactive:SelectOption[] = []

for (let i = 0, l = models.length; i < l; i++) {
const model = models[i]
const modelDetail = getModelDetail(model)
await modelDetail.check(modelDetail)
result.push({
const pushTarget = modelDetail.enabled ? modelOptionsActive : modelOptionsInactive
pushTarget.push({
value: model,
text: modelDetail.label || model,
disabled: !modelDetail.enabled
})
}
return result

modelOptionsActive.sort(sortModelsAlphabetically)
modelOptionsInactive.sort(sortModelsAlphabetically)

return [...modelOptionsActive, ...modelOptionsInactive]
}

export async function getImageModelOptions (): Promise<SelectOption[]> {
Expand Down