mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-04 20:00:00 +08:00
fix: azure-openai (#7978)
This commit is contained in:
parent
8f86c53941
commit
3dd393b840
@ -39,7 +39,7 @@ import { findFileBlocks, findImageBlocks } from '@renderer/utils/messageUtils/fi
|
|||||||
import { buildSystemPrompt } from '@renderer/utils/prompt'
|
import { buildSystemPrompt } from '@renderer/utils/prompt'
|
||||||
import { MB } from '@shared/config/constant'
|
import { MB } from '@shared/config/constant'
|
||||||
import { isEmpty } from 'lodash'
|
import { isEmpty } from 'lodash'
|
||||||
import OpenAI from 'openai'
|
import OpenAI, { AzureOpenAI } from 'openai'
|
||||||
import { ResponseInput } from 'openai/resources/responses/responses'
|
import { ResponseInput } from 'openai/resources/responses/responses'
|
||||||
|
|
||||||
import { RequestTransformer, ResponseChunkTransformer } from '../types'
|
import { RequestTransformer, ResponseChunkTransformer } from '../types'
|
||||||
@ -66,6 +66,9 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient<
|
|||||||
*/
|
*/
|
||||||
public getClient(model: Model) {
|
public getClient(model: Model) {
|
||||||
if (isOpenAILLMModel(model) && !isOpenAIChatCompletionOnlyModel(model)) {
|
if (isOpenAILLMModel(model) && !isOpenAIChatCompletionOnlyModel(model)) {
|
||||||
|
if (this.provider.id === 'azure-openai' || this.provider.type === 'azure-openai') {
|
||||||
|
this.provider = { ...this.provider, apiVersion: 'preview' }
|
||||||
|
}
|
||||||
return this
|
return this
|
||||||
} else {
|
} else {
|
||||||
return this.client
|
return this.client
|
||||||
@ -77,15 +80,25 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient<
|
|||||||
return this.sdkInstance
|
return this.sdkInstance
|
||||||
}
|
}
|
||||||
|
|
||||||
return new OpenAI({
|
if (this.provider.id === 'azure-openai' || this.provider.type === 'azure-openai') {
|
||||||
dangerouslyAllowBrowser: true,
|
this.provider = { ...this.provider, apiHost: `${this.provider.apiHost}/openai/v1` }
|
||||||
apiKey: this.apiKey,
|
return new AzureOpenAI({
|
||||||
baseURL: this.getBaseURL(),
|
dangerouslyAllowBrowser: true,
|
||||||
defaultHeaders: {
|
apiKey: this.apiKey,
|
||||||
...this.defaultHeaders(),
|
apiVersion: this.provider.apiVersion,
|
||||||
...this.provider.extra_headers
|
baseURL: this.provider.apiHost
|
||||||
}
|
})
|
||||||
})
|
} else {
|
||||||
|
return new OpenAI({
|
||||||
|
dangerouslyAllowBrowser: true,
|
||||||
|
apiKey: this.apiKey,
|
||||||
|
baseURL: this.getBaseURL(),
|
||||||
|
defaultHeaders: {
|
||||||
|
...this.defaultHeaders(),
|
||||||
|
...this.provider.extra_headers
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override async createCompletions(
|
override async createCompletions(
|
||||||
|
|||||||
@ -80,7 +80,7 @@ export default class AiProvider {
|
|||||||
builder.remove(ThinkChunkMiddlewareName)
|
builder.remove(ThinkChunkMiddlewareName)
|
||||||
}
|
}
|
||||||
// 注意:用client判断会导致typescript类型收窄
|
// 注意:用client判断会导致typescript类型收窄
|
||||||
if (!(this.apiClient instanceof OpenAIAPIClient)) {
|
if (!(this.apiClient instanceof OpenAIAPIClient) && !(this.apiClient instanceof OpenAIResponseAPIClient)) {
|
||||||
builder.remove(ThinkingTagExtractionMiddlewareName)
|
builder.remove(ThinkingTagExtractionMiddlewareName)
|
||||||
}
|
}
|
||||||
if (!(this.apiClient instanceof AnthropicAPIClient) && !(this.apiClient instanceof OpenAIResponseAPIClient)) {
|
if (!(this.apiClient instanceof AnthropicAPIClient) && !(this.apiClient instanceof OpenAIResponseAPIClient)) {
|
||||||
|
|||||||
@ -1,24 +1,28 @@
|
|||||||
import { Alert } from 'antd'
|
import { Alert } from 'antd'
|
||||||
|
import { t } from 'i18next'
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
|
||||||
|
|
||||||
const LOCALSTORAGE_KEY = 'openai_alert_closed'
|
const LOCALSTORAGE_KEY = 'openai_alert_closed'
|
||||||
|
|
||||||
const OpenAIAlert = () => {
|
interface Props {
|
||||||
const { t } = useTranslation()
|
message?: string
|
||||||
|
key?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const OpenAIAlert = ({ message = t('settings.provider.openai.alert'), key = LOCALSTORAGE_KEY }: Props) => {
|
||||||
const [visible, setVisible] = useState(false)
|
const [visible, setVisible] = useState(false)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const closed = localStorage.getItem(LOCALSTORAGE_KEY)
|
const closed = localStorage.getItem(key)
|
||||||
setVisible(!closed)
|
setVisible(!closed)
|
||||||
}, [])
|
}, [key])
|
||||||
|
|
||||||
if (!visible) return null
|
if (!visible) return null
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Alert
|
<Alert
|
||||||
style={{ width: '100%', marginTop: 5, marginBottom: 5 }}
|
style={{ width: '100%', marginTop: 5, marginBottom: 5 }}
|
||||||
message={t('settings.provider.openai.alert')}
|
message={message}
|
||||||
closable
|
closable
|
||||||
afterClose={() => {
|
afterClose={() => {
|
||||||
localStorage.setItem(LOCALSTORAGE_KEY, '1')
|
localStorage.setItem(LOCALSTORAGE_KEY, '1')
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user