mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-07-26 01:45:25 +02:00
62 lines
1.9 KiB
JavaScript
62 lines
1.9 KiB
JavaScript
import Anthropic from '@anthropic-ai/sdk';
|
|
import { strictFormat } from '../utils/text.js';
|
|
import { getKey } from '../utils/keys.js';
|
|
|
|
export class Claude {
|
|
constructor(model_name, url, params) {
|
|
this.model_name = model_name;
|
|
this.params = params || {};
|
|
|
|
let config = {};
|
|
if (url)
|
|
config.baseURL = url;
|
|
|
|
config.apiKey = getKey('ANTHROPIC_API_KEY');
|
|
|
|
this.anthropic = new Anthropic(config);
|
|
}
|
|
|
|
async sendRequest(turns, systemMessage) {
|
|
const messages = strictFormat(turns);
|
|
let res = null;
|
|
try {
|
|
console.log('Awaiting anthropic api response...')
|
|
if (!this.params.max_tokens) {
|
|
if (this.params.thinking?.budget_tokens) {
|
|
this.params.max_tokens = this.params.thinking.budget_tokens + 1000;
|
|
// max_tokens must be greater than thinking.budget_tokens
|
|
} else {
|
|
this.params.max_tokens = 16000;
|
|
}
|
|
}
|
|
const resp = await this.anthropic.messages.create({
|
|
model: this.model_name || "claude-3-sonnet-20240229",
|
|
system: systemMessage,
|
|
messages: messages,
|
|
...(this.params || {})
|
|
});
|
|
|
|
console.log('Received.')
|
|
// get first content of type text
|
|
const textContent = resp.content.find(content => content.type === 'text');
|
|
if (textContent) {
|
|
res = textContent.text;
|
|
} else {
|
|
console.warn('No text content found in the response.');
|
|
res = 'No response from Claude.';
|
|
}
|
|
}
|
|
catch (err) {
|
|
console.log(err);
|
|
res = 'My brain disconnected, try again.';
|
|
}
|
|
return res;
|
|
}
|
|
|
|
async embed(text) {
|
|
throw new Error('Embeddings are not supported by Claude.');
|
|
}
|
|
}
|
|
|
|
|
|
|