feat: Add API host formatting utility function

This commit is contained in:
kangfenmao 2025-02-21 13:49:36 +08:00
parent f062c56de4
commit 69bb661b5a
3 changed files with 19 additions and 2 deletions

View File

@ -22,6 +22,7 @@ import { isProviderSupportAuth, isProviderSupportCharge } from '@renderer/servic
import { useAppDispatch } from '@renderer/store'
import { setModel } from '@renderer/store/assistants'
import { Model, ModelType, Provider } from '@renderer/types'
import { formatApiHost } from '@renderer/utils/api'
import { providerCharge } from '@renderer/utils/oauth'
import { Avatar, Button, Card, Checkbox, Divider, Flex, Input, Popover, Space, Switch } from 'antd'
import Link from 'antd/es/typography/Link'
@ -160,7 +161,7 @@ const ProviderSetting: FC<Props> = ({ provider: _provider }) => {
return apiHost.replace('#', '')
}
return (apiHost.endsWith('/') ? apiHost : `${apiHost}/v1/`) + 'chat/completions'
return formatApiHost(apiHost) + 'chat/completions'
}
const onUpdateModelTypes = (model: Model, types: ModelType[]) => {

View File

@ -6,6 +6,7 @@ import store from '@renderer/store'
import type { Assistant, GenerateImageParams, Message, Model, Provider, Suggestion } from '@renderer/types'
import { delay, isJSON, parseJSON } from '@renderer/utils'
import { addAbortController, removeAbortController } from '@renderer/utils/abortController'
import { formatApiHost } from '@renderer/utils/api'
import { t } from 'i18next'
import type OpenAI from 'openai'
@ -34,7 +35,7 @@ export default abstract class BaseProvider {
public getBaseURL(): string {
const host = this.provider.apiHost
return host.endsWith('/') ? host : `${host}/v1/`
return formatApiHost(host)
}
public getApiKey() {

View File

@ -0,0 +1,15 @@
export function formatApiHost(host: string) {
const forceUseOriginalHost = () => {
if (host.endsWith('/')) {
return true
}
if (host.endsWith('volces.com/api/v3')) {
return true
}
return false
}
return forceUseOriginalHost() ? host : `${host}/v1/`
}