Project details
ailib is a lightweight client library designed for Node.js, enabling seamless interactions with various AI models through OpenRouter. It boasts features like thread management, streaming responses, TypeScript support, model pricing tracking, and extensibility for other API providers, making it a versatile tool for developers.
ailib is a lightweight AI client library designed for Node.js that streamlines interactions with AI models via OpenRouter. This library provides an intuitive interface to facilitate effective conversations with various AI models, making it a valuable tool for developers seeking to integrate AI functionalities into their applications.
To utilize ailib for AI interactions, the following TypeScript example demonstrates its capabilities:
import { createThread, OpenRouter } from '@markwylde/ailib';
import { z } from 'zod';
// Create a thread
const ai = createThread({
provider: OpenRouter,
model: 'anthropic/claude-3-sonnet',
messages: [{ role: 'system', content: 'You are a helpful assistant.' }],
tools: [{
name: 'get-weather',
description: 'Get the weather for a location',
parameters: z.object({ location: z.string() }),
handler: async ({ location }) => `The weather in ${location} is sunny.`
}],
apiKey: process.env.OPENROUTER_API_KEY,
modelOptions: { temperature: 0.7, max_tokens: 1000 },
});
// Add a user message to the thread
ai.messages.add({ role: 'user', content: 'What is the weather in Tokyo?' });
// Generate a message from the AI
const stream = ai.messages.generate();
// Listen for incoming stream data
stream.on('data', ([chunk, message]) => {
console.log(chunk); // Stream text chunks as they arrive
console.log(`Tokens used: ${message.tokens}`);
console.log(`Cost: $${message.cost}`);
});
This illustrates how easily an AI can be queried for contextual information, such as weather, using a defined tool set.
This repository is crafted for developers interested in leveraging AI in JavaScript applications while maintaining an efficient, user-friendly flow. The library's design embraces modern JavaScript practices and supports developer needs through clear abstractions and comprehensive configurations.
Comments
0Start the conversation
Share the first comment.