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