Skip to content

Commit

Permalink
✨ feat: support openai new OpenAI o1-preview/o1-mini models (lobehub#…
Browse files Browse the repository at this point in the history
…3943)

* support o1

* Update openai.ts

* fix test

* fix test
  • Loading branch information
arvinxx authored Sep 13, 2024
1 parent 99ac98c commit 61bfeb2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/config/modelProviders/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@ import { ModelProviderCard } from '@/types/llm';
// ref: https://platform.openai.com/docs/deprecations
const OpenAI: ModelProviderCard = {
chatModels: [
{
description:
'o1-mini是一款针对编程、数学和科学应用场景而设计的快速、经济高效的推理模型。该模型具有128K上下文和2023年10月的知识截止日期。',
displayName: 'OpenAI o1-mini',
enabled: true,
id: 'o1-mini',
maxOutput: 65_536,
pricing: {
input: 3,
output: 12,
},
releasedAt: '2024-09-12',
tokens: 128_000,
},
{
description:
'o1是OpenAI新的推理模型,适用于需要广泛通用知识的复杂任务。该模型具有128K上下文和2023年10月的知识截止日期。',
displayName: 'OpenAI o1-preview',
enabled: true,
id: 'o1-preview',
maxOutput: 32_768,
pricing: {
input: 15,
output: 60,
},
releasedAt: '2024-09-12',
tokens: 128_000,
},
{
description:
'GPT-4o mini是OpenAI在GPT-4 Omni之后推出的最新模型,支持图文输入并输出文本。作为他们最先进的小型模型,它比其他近期的前沿模型便宜很多,并且比GPT-3.5 Turbo便宜超过60%。它保持了最先进的智能,同时具有显著的性价比。GPT-4o mini在MMLU测试中获得了 82% 的得分,目前在聊天偏好上排名高于 GPT-4。',
Expand Down
28 changes: 27 additions & 1 deletion src/libs/agent-runtime/openai/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
import { ModelProvider } from '../types';
import { ChatStreamPayload, ModelProvider, OpenAIChatMessage } from '../types';
import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory';

// TODO: 临时写法,后续要重构成 model card 展示配置
const o1Models = new Set(['o1-preview', 'o1-mini']);
const truneO1Payload = (payload: ChatStreamPayload) => ({
...payload,
frequency_penalty: 0,
messages: payload.messages.map((message: OpenAIChatMessage) => ({
...message,
role: message.role === 'system' ? 'user' : message.role,
})),
presence_penalty: 0,
stream: false,
temperature: 1,
top_p: 1,
});

export const LobeOpenAI = LobeOpenAICompatibleFactory({
baseURL: 'https://api.openai.com/v1',
chatCompletion: {
handlePayload: (payload) => {
const { model } = payload;

if (o1Models.has(model)) {
return truneO1Payload(payload) as any;
}

return { ...payload, stream: payload.stream ?? true };
},
},
debug: {
chatCompletion: () => process.env.DEBUG_OPENAI_CHAT_COMPLETION === '1',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ describe('modelProviderSelectors', () => {
const s = merge(initialState, {}) as unknown as UserStore;

const result = modelProviderSelectors.getDefaultEnabledModelsById('openai')(s);
expect(result).toEqual(['gpt-4o-mini', 'gpt-4o', 'gpt-4o-2024-08-06', 'chatgpt-4o-latest']);
expect(result).toEqual([
'o1-mini',
'o1-preview',
'gpt-4o-mini',
'gpt-4o',
'gpt-4o-2024-08-06',
'chatgpt-4o-latest',
]);
});

it('should return undefined for a non-existing provider', () => {
Expand Down

0 comments on commit 61bfeb2

Please sign in to comment.