PitchHut logo
ailib
A simple library for AI models using OpenRouter in JavaScript.
Pitch

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.

Description

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.

Key Features

  • Thread-based Conversation Management: Effortlessly manage conversations through a simple thread structure.
  • Streaming Responses: Receive real-time feedback through streaming event responses.
  • Tool Calling Support: Enhance AI capabilities by allowing model interactions with custom-defined tools.
  • TypeScript Compatibility: Enjoy proper typing support, ensuring a smooth development experience.
  • Modern Async/Await API: Easily work with asynchronous operations using a straightforward async/await pattern.
  • Extensible Provider Support: Built for the OpenRouter API with potential for integration with other AI providers in the future.
  • Model Pricing and Cost Tracking: Keep track of model usage costs and manage budget efficiently.
  • Configurable Model Options: Customize and configure model behavior according to your needs.
  • Reasoning Output Support: Benefit from detailed reasoning outputs if supported by the selected model.

Usage Example

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.

API Overview

  • createThread(options): Initializes a new conversation thread with specific options such as provider, model, initial messages, and tools.
  • Message Methods: Simplified methods to add or remove messages and manage conversation state.
  • Stream Events: The library offers events to track the streaming process, receiving immediate updates on state changes and reasoning output.

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.

0 comments

No comments yet.

Sign in to be the first to comment.