mirror of
https://github.com/stackblitz/bolt.new
synced 2025-06-26 18:17:50 +00:00
refactor(llm): remove unused model.ts file and update OpenAI LLM implementation
This commit is contained in:
@@ -1,14 +1,10 @@
|
||||
import {streamText as _streamText, convertToCoreMessages } from 'ai';
|
||||
import { createAnthropic } from '@ai-sdk/anthropic';
|
||||
import { MAX_TOKENS } from './constants';
|
||||
import { getPrompts } from './prompts';
|
||||
import type { LLM } from './llm-interface';
|
||||
import type { Prompts } from './prompts-interface';
|
||||
import type { Messages, StreamingOptions }from './llm-interface';
|
||||
|
||||
// export type Messages = Message[];
|
||||
|
||||
// export type StreamingOptions = Omit<Parameters<typeof _streamText>[0], 'model'>;
|
||||
import { AnthropicPrompts } from './anthropic-prompts';
|
||||
|
||||
export class AnthropicLLM implements LLM {
|
||||
private apiKey: string = '';
|
||||
@@ -42,6 +38,6 @@ export class AnthropicLLM implements LLM {
|
||||
}
|
||||
|
||||
getPrompts(): Prompts {
|
||||
return getPrompts();
|
||||
return new AnthropicPrompts();
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,10 @@
|
||||
import { streamText as _streamText, convertToCoreMessages } from 'ai';
|
||||
import { createOpenAI } from '@ai-sdk/openai';
|
||||
import { MAX_TOKENS } from './constants';
|
||||
import { getPrompts } from './prompts';
|
||||
import type { LLM } from './llm-interface';
|
||||
import type { Prompts } from './prompts-interface';
|
||||
import type { Message, Messages, StreamingOptions }from './llm-interface';
|
||||
|
||||
// export type Messages = Message[];
|
||||
|
||||
// export type StreamingOptions = Omit<Parameters<typeof _streamText>[0], 'model'>;
|
||||
import { OpenAIPrompts } from './openai-prompts';
|
||||
|
||||
export class OpenAILLM implements LLM {
|
||||
private apiKey: string = '';
|
||||
@@ -28,8 +24,8 @@ export class OpenAILLM implements LLM {
|
||||
}
|
||||
|
||||
const openai = createOpenAI({ apiKey: this.apiKey, compatibility: 'strict' });
|
||||
type model_name_t = 'gpt-4o' | 'o1-mini' | 'o1-preview';
|
||||
const model_name: model_name_t = process.env.OPEN_AI_MODEL as model_name_t;
|
||||
type model_name_t = 'gpt-4o' | 'gpt-4o-mini' | 'o1-mini' | 'o1-preview';
|
||||
const model_name = process.env.OPENAI_MODEL as model_name_t;
|
||||
const model = openai(model_name);
|
||||
|
||||
if (model_name === 'o1-mini' || model_name === 'o1-preview') {
|
||||
@@ -39,13 +35,13 @@ export class OpenAILLM implements LLM {
|
||||
};
|
||||
|
||||
return _streamText({
|
||||
model: model as any, // Use type assertion to bypass strict type checking
|
||||
model,
|
||||
messages: [o1sysmessage, ...convertToCoreMessages(messages)],
|
||||
...options,
|
||||
});
|
||||
} else {
|
||||
return _streamText({
|
||||
model: model as any, // Use type assertion to bypass strict type checking
|
||||
model,
|
||||
system: this.getPrompts().getSystemPrompt(),
|
||||
messages: convertToCoreMessages(messages),
|
||||
maxTokens: MAX_TOKENS,
|
||||
@@ -55,6 +51,6 @@ export class OpenAILLM implements LLM {
|
||||
}
|
||||
|
||||
getPrompts(): Prompts {
|
||||
return getPrompts();
|
||||
return new OpenAIPrompts();
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
import { MODIFICATIONS_TAG_NAME, WORK_DIR } from '~/utils/constants';
|
||||
import { allowedHTMLElements } from '~/utils/markdown';
|
||||
import { stripIndents } from '~/utils/stripIndent';
|
||||
import type { Prompts } from './prompts-interface';
|
||||
import { getCurrentLLMType } from './llm-selector';
|
||||
import { AnthropicPrompts } from './anthropic-prompts';
|
||||
import { OpenAIPrompts } from './openai-prompts';
|
||||
|
||||
class GenericPrompts implements Prompts {
|
||||
getSystemPrompt(cwd: string = WORK_DIR): string {
|
||||
return `
|
||||
You are an AI assistant. Please help the user with their task.
|
||||
The current working directory is ${cwd}.
|
||||
`;
|
||||
}
|
||||
|
||||
getContinuePrompt(): string {
|
||||
return stripIndents`
|
||||
Continue your prior response. Please begin from where you left off without any interruptions.
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
export function getPrompts(): Prompts {
|
||||
const llmType = getCurrentLLMType();
|
||||
switch (llmType) {
|
||||
case 'anthropic':
|
||||
return new AnthropicPrompts();
|
||||
case 'openai':
|
||||
return new OpenAIPrompts();
|
||||
default:
|
||||
return new GenericPrompts();
|
||||
}
|
||||
}
|
||||
|
||||
export function getSystemPrompt(cwd: string = WORK_DIR): string {
|
||||
return getPrompts().getSystemPrompt(cwd);
|
||||
}
|
||||
|
||||
export function getContinuePrompt(): string {
|
||||
return getPrompts().getContinuePrompt();
|
||||
}
|
||||
Reference in New Issue
Block a user