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

Add support for o1-preview #14356

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/ai-chat/src/browser/ai-chat-frontend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
51 changes: 51 additions & 0 deletions packages/ai-chat/src/common/o1-chat-agent.ts
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few linting errors in this file (no copyright, extra blank lines).

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<SystemMessageDescription | undefined> {
// O1 currently does not support system prompts
return undefined;
}
}
2 changes: 1 addition & 1 deletion packages/ai-openai/src/browser/openai-preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
25 changes: 24 additions & 1 deletion packages/ai-openai/src/node/openai-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
LanguageModelRequest,
LanguageModelRequestMessage,
LanguageModelResponse,
LanguageModelStreamResponsePart
LanguageModelStreamResponsePart,
LanguageModelTextResponse
} from '@theia/ai-core';
import { CancellationToken } from '@theia/core';
import OpenAI from 'openai';
Expand Down Expand Up @@ -60,6 +61,10 @@ export class OpenAiModel implements LanguageModel {
async request(request: LanguageModelRequest, cancellationToken?: CancellationToken): Promise<LanguageModelResponse> {
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);
}
Expand Down Expand Up @@ -133,6 +138,24 @@ export class OpenAiModel implements LanguageModel {
return { stream: asyncIterator };
}

protected async handleNonStreamingRequest(openai: OpenAI, request: LanguageModelRequest): Promise<LanguageModelTextResponse> {
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 [
Expand Down
Loading