2024-04-04 00:47:44 +00:00
|
|
|
import * as plugins from './plugins.js';
|
|
|
|
import * as paths from './paths.js';
|
|
|
|
import { MultiModalModel } from './abstract.classes.multimodal.js';
|
|
|
|
|
2024-04-29 09:18:40 +00:00
|
|
|
export interface IAnthropicProviderOptions {
|
|
|
|
anthropicToken: string;
|
|
|
|
}
|
|
|
|
|
2024-04-04 00:47:44 +00:00
|
|
|
export class AnthropicProvider extends MultiModalModel {
|
2024-04-29 09:18:40 +00:00
|
|
|
private options: IAnthropicProviderOptions;
|
2024-04-04 00:47:44 +00:00
|
|
|
public anthropicApiClient: plugins.anthropic.default;
|
|
|
|
|
2024-04-29 09:18:40 +00:00
|
|
|
constructor(optionsArg: IAnthropicProviderOptions) {
|
2024-04-04 00:47:44 +00:00
|
|
|
super();
|
2024-04-29 09:18:40 +00:00
|
|
|
this.options = optionsArg // Ensure the token is stored
|
2024-04-04 00:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async start() {
|
|
|
|
this.anthropicApiClient = new plugins.anthropic.default({
|
2024-04-29 09:18:40 +00:00
|
|
|
apiKey: this.options.anthropicToken,
|
2024-04-04 00:47:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async stop() {}
|
|
|
|
|
2024-04-29 09:18:40 +00:00
|
|
|
public async chatStream(input: ReadableStream<string>): Promise<ReadableStream<string>> {
|
|
|
|
// TODO: implement for OpenAI
|
2024-04-04 00:47:44 +00:00
|
|
|
|
2024-04-29 09:18:40 +00:00
|
|
|
const returnStream = new ReadableStream();
|
|
|
|
return returnStream;
|
2024-04-04 00:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implementing the synchronous chat interaction
|
2024-04-29 09:18:40 +00:00
|
|
|
public async chat(optionsArg: {
|
|
|
|
systemMessage: string;
|
|
|
|
userMessage: string;
|
2024-04-04 00:47:44 +00:00
|
|
|
messageHistory: {
|
|
|
|
role: 'assistant' | 'user';
|
|
|
|
content: string;
|
2024-04-29 09:18:40 +00:00
|
|
|
}[];
|
|
|
|
}) {
|
2024-04-04 00:47:44 +00:00
|
|
|
const result = await this.anthropicApiClient.messages.create({
|
|
|
|
model: 'claude-3-opus-20240229',
|
2024-04-29 09:18:40 +00:00
|
|
|
system: optionsArg.systemMessage,
|
2024-04-04 00:47:44 +00:00
|
|
|
messages: [
|
2024-04-29 09:18:40 +00:00
|
|
|
...optionsArg.messageHistory,
|
|
|
|
{ role: 'user', content: optionsArg.userMessage },
|
2024-04-04 00:47:44 +00:00
|
|
|
],
|
|
|
|
max_tokens: 4000,
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
2024-04-29 09:18:40 +00:00
|
|
|
role: result.role as 'assistant',
|
|
|
|
message: result.content.join('\n'),
|
2024-04-04 00:47:44 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-04-29 09:18:40 +00:00
|
|
|
private async audio(messageArg: string) {
|
2024-04-04 00:47:44 +00:00
|
|
|
// Anthropic does not provide an audio API, so this method is not implemented.
|
2024-04-29 09:18:40 +00:00
|
|
|
throw new Error('Audio generation is not yet supported by Anthropic.');
|
2024-04-04 00:47:44 +00:00
|
|
|
}
|
|
|
|
}
|