diff --git a/packages/ai-chat/src/browser/ai-chat-frontend-module.ts b/packages/ai-chat/src/browser/ai-chat-frontend-module.ts index 396bcb7331a06..0fc833519c61d 100644 --- a/packages/ai-chat/src/browser/ai-chat-frontend-module.ts +++ b/packages/ai-chat/src/browser/ai-chat-frontend-module.ts @@ -37,6 +37,7 @@ import { aiChatPreferences } from './ai-chat-preferences'; import { AICustomAgentsFrontendApplicationContribution } from './custom-agent-frontend-application-contribution'; import { FrontendChatServiceImpl } from './frontend-chat-service'; import { CustomAgentFactory } from './custom-agent-factory'; +import { O1ChatAgent } from '../common/o1-chat-agent'; export default new ContainerModule(bind => { bindContributionProvider(bind, Agent); @@ -63,6 +64,10 @@ export default new ContainerModule(bind => { bind(Agent).toService(OrchestratorChatAgent); bind(ChatAgent).toService(OrchestratorChatAgent); + bind(O1ChatAgent).toSelf().inSingletonScope(); + bind(Agent).toService(O1ChatAgent); + bind(ChatAgent).toService(O1ChatAgent); + bind(UniversalChatAgent).toSelf().inSingletonScope(); bind(Agent).toService(UniversalChatAgent); bind(ChatAgent).toService(UniversalChatAgent); diff --git a/packages/ai-chat/src/common/o1-chat-agent.ts b/packages/ai-chat/src/common/o1-chat-agent.ts new file mode 100644 index 0000000000000..cc31984b11613 --- /dev/null +++ b/packages/ai-chat/src/common/o1-chat-agent.ts @@ -0,0 +1,51 @@ +// ***************************************************************************** +// Copyright (C) 2024 EclipseSource GmbH. +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License v. 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0. +// +// This Source Code may also be made available under the following Secondary +// Licenses when the conditions for such availability set forth in the Eclipse +// Public License v. 2.0 are satisfied: GNU General Public License, version 2 +// with the GNU Classpath Exception which is available at +// https://www.gnu.org/software/classpath/license.html. +// +// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 +// ***************************************************************************** + +import { + ChatAgent, + AbstractStreamParsingChatAgent, + SystemMessageDescription +} from './chat-agents'; + +import { injectable } from '@theia/core/shared/inversify'; +import { AgentSpecificVariables, PromptTemplate } from '@theia/ai-core'; + +@injectable() +export class O1ChatAgent extends AbstractStreamParsingChatAgent implements ChatAgent { + + public name = 'O1-Preview'; + public description = 'An agent for interacting with ChatGPT o1-preview'; + public promptTemplates: PromptTemplate[] = []; + readonly agentSpecificVariables: AgentSpecificVariables[] = []; + readonly variables: string[] = []; + readonly functions: string[] = []; + + constructor() { + super( + 'o1-preview', + [{ + purpose: 'chat', + identifier: 'openai/o1-preview', + }], + 'chat' + ); + } + + protected async getSystemMessageDescription(): Promise { + // O1 currently does not support system prompts + return undefined; + } +} diff --git a/packages/ai-openai/src/browser/openai-preferences.ts b/packages/ai-openai/src/browser/openai-preferences.ts index ce937578ce623..1b245a3738752 100644 --- a/packages/ai-openai/src/browser/openai-preferences.ts +++ b/packages/ai-openai/src/browser/openai-preferences.ts @@ -34,7 +34,7 @@ export const OpenAiPreferencesSchema: PreferenceSchema = { type: 'array', description: 'Official OpenAI models to use', title: AI_CORE_PREFERENCES_TITLE, - default: ['gpt-4o', 'gpt-4o-2024-08-06', 'gpt-4o-2024-05-13', 'gpt-4o-mini', 'gpt-4-turbo', 'gpt-4', 'gpt-3.5-turbo'], + default: ['gpt-4o', 'gpt-4o-2024-08-06', 'gpt-4o-2024-05-13', 'gpt-4o-mini', 'gpt-4-turbo', 'gpt-4', 'gpt-3.5-turbo', 'o1-preview'], items: { type: 'string' } diff --git a/packages/ai-openai/src/node/openai-language-model.ts b/packages/ai-openai/src/node/openai-language-model.ts index 7d47f72d99584..05ab15061ed48 100644 --- a/packages/ai-openai/src/node/openai-language-model.ts +++ b/packages/ai-openai/src/node/openai-language-model.ts @@ -20,7 +20,8 @@ import { LanguageModelRequest, LanguageModelRequestMessage, LanguageModelResponse, - LanguageModelStreamResponsePart + LanguageModelStreamResponsePart, + LanguageModelTextResponse } from '@theia/ai-core'; import { CancellationToken } from '@theia/core'; import OpenAI from 'openai'; @@ -60,6 +61,10 @@ export class OpenAiModel implements LanguageModel { async request(request: LanguageModelRequest, cancellationToken?: CancellationToken): Promise { const openai = this.initializeOpenAi(); + if (this.isNonStreamingModel(this.model)) { + return this.handleNonStreamingRequest(openai, request); + } + if (request.response_format?.type === 'json_schema' && this.supportsStructuredOutput()) { return this.handleStructuredOutputRequest(openai, request); } @@ -133,6 +138,24 @@ export class OpenAiModel implements LanguageModel { return { stream: asyncIterator }; } + protected async handleNonStreamingRequest(openai: OpenAI, request: LanguageModelRequest): Promise { + const response = await openai.chat.completions.create({ + model: this.model, + messages: request.messages.map(toOpenAIMessage), + ...request.settings + }); + + const message = response.choices[0].message; + + return { + text: message.content ?? '' + }; + } + + protected isNonStreamingModel(model: string): boolean { + return ['o1-preview'].includes(model); + } + protected supportsStructuredOutput(): boolean { // see https://platform.openai.com/docs/models/gpt-4o return [