-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement AI provider architecture with OpenAI and Anthropic support
- Loading branch information
Showing
8 changed files
with
614 additions
and
49 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { AnthropicServiceProvider } from './providers/anthropic-provider'; | ||
import { OpenAIServiceProvider } from './providers/openai-provider'; | ||
import { AIModelProvider, AIProvider } from './types'; | ||
|
||
export class AIFactory { | ||
private static instance: AIFactory; | ||
private currentProvider: AIProvider; | ||
private providers: Map<AIModelProvider, AIProvider>; | ||
|
||
private constructor() { | ||
this.providers = new Map(); | ||
this.providers.set(AIModelProvider.OPENAI, new OpenAIServiceProvider()); | ||
this.providers.set( | ||
AIModelProvider.ANTHROPIC, | ||
new AnthropicServiceProvider(), | ||
); | ||
this.currentProvider = this.providers.get(AIModelProvider.ANTHROPIC)!; | ||
} | ||
|
||
static getInstance(): AIFactory { | ||
if (!AIFactory.instance) { | ||
AIFactory.instance = new AIFactory(); | ||
} | ||
return AIFactory.instance; | ||
} | ||
|
||
setProvider(provider: AIModelProvider): void { | ||
const newProvider = this.providers.get(provider); | ||
if (!newProvider) { | ||
throw new Error(`Provider ${provider} not found`); | ||
} | ||
this.currentProvider = newProvider; | ||
} | ||
|
||
getCurrentProvider(): AIProvider { | ||
return this.currentProvider; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { AIProvider } from '../types'; | ||
import { anthropic } from '@ai-sdk/anthropic'; | ||
import { CoreMessage, generateText } from 'ai'; | ||
|
||
export class AnthropicServiceProvider implements AIProvider { | ||
constructor() {} | ||
|
||
async generateResponse(messages: CoreMessage[]): Promise<string> { | ||
const response = await generateText({ | ||
model: anthropic('claude-3-5-sonnet-latest'), | ||
maxTokens: 256, | ||
messages, | ||
}); | ||
|
||
return response.text; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { AIProvider } from '../types'; | ||
import { openai } from '@ai-sdk/openai'; | ||
import { CoreMessage, generateText } from 'ai'; | ||
|
||
export class OpenAIServiceProvider implements AIProvider { | ||
constructor() {} | ||
|
||
async generateResponse(messages: CoreMessage[]): Promise<string> { | ||
const response = await generateText({ | ||
model: openai('gpt-4o'), | ||
messages, | ||
maxTokens: 256, | ||
}); | ||
|
||
return response.text; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { CoreMessage } from 'ai'; | ||
|
||
export interface AIProvider { | ||
generateResponse(messages: CoreMessage[]): Promise<string>; | ||
} | ||
|
||
export enum AIModelProvider { | ||
OPENAI = 'openai', | ||
ANTHROPIC = 'anthropic', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,30 @@ | ||
import { ChatCompletionAssistantMessageParam, ChatCompletionContentPart, ChatCompletionContentPartText, ChatCompletionUserMessageParam } from 'openai/resources'; | ||
import { | ||
AssistantContent, | ||
CoreAssistantMessage, | ||
CoreMessage, | ||
CoreUserMessage, | ||
UserContent, | ||
} from 'ai'; | ||
|
||
export const createUserMessage = ({content, name}: {content: Array<ChatCompletionContentPart>, name?: string}): ChatCompletionUserMessageParam => { | ||
export const createUserMessage = ({ | ||
content, | ||
}: { | ||
content: UserContent; | ||
name?: string; | ||
}): CoreUserMessage => { | ||
return { | ||
role: 'user', | ||
content, | ||
name | ||
} | ||
} | ||
content, | ||
}; | ||
}; | ||
|
||
export const createAssistantMessage = ({content}: {content: Array<ChatCompletionContentPartText>}): ChatCompletionAssistantMessageParam => { | ||
export const createAssistantMessage = ({ | ||
content, | ||
}: { | ||
content: AssistantContent; | ||
}): CoreAssistantMessage => { | ||
return { | ||
role: 'assistant', | ||
content, | ||
name: 'Nuki' | ||
} | ||
} | ||
}; | ||
}; |