ai-fetcher
Version:
A Node.js package that provides integration with popular language models. It is designed to facilitate easy and efficient ai-fetching tasks for your application.
73 lines (72 loc) • 3.43 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Claude = void 0;
const axios_1 = __importDefault(require("axios"));
// The Claude Agent class
class Claude {
/**
* Constructs a new instance of Claude class
* @param claudeKey: string - The user's Claude authentication key
* @param model: ClaudeModel - Should be one of the model string provided by Claude
*/
constructor(claudeKey, model = "claude-3-haiku-20240307") {
this.api = claudeKey;
this.endpoint = "https://api.anthropic.com/v1/messages";
this.model = model;
this.headers = {
"x-api-key": this.api,
"anthropic-version": "2023-06-01", // This is the current version provided by the Claude
"Content-Type": "application/json",
};
}
/**
* Generates a response from Claude.
* @param system: string - The system prompt or context that guides the model's behavior.
* @param messages: ClaudeMessage[] - An array of ClaudeMessages representing the conversation history.
* @param temperature (Optional): number - A number between 0 and 1 that controls the randomness of the output. Defaults to 0.
* @param max_tokens (Optional): number - A number represents the maximum output tokens (words or word fragments). Defaults to 1000.
*
* @returns A promise that resolves to a ClaudeResult containing the generated text result.
* @throws An error if the API request fails.
*/
generate(system_1, messages_1) {
return __awaiter(this, arguments, void 0, function* (system, messages, temperature = 0, max_tokens = 1000) {
// Limits the temperature between 0.0 and 1.0
const validatedTemp = Math.min(1.0, Math.max(0.0, temperature));
// Limits the minimum max_token to 0
const validatedToken = Math.max(0, Math.round(max_tokens));
const data = {
model: this.model,
max_tokens: validatedToken,
temperature: validatedTemp,
system,
messages,
};
try {
const response = yield axios_1.default.post(this.endpoint, data, {
headers: this.headers,
});
return response.data;
}
catch (error) {
if (error instanceof Error)
throw new Error(error.message);
else
throw new Error(String(error));
}
});
}
}
exports.Claude = Claude;