ballerinax/ai.openrouter Ballerina library

1.0.0

Overview

This module offers APIs for connecting with 200+ Large Language Models (LLMs) through the OpenRouter unified API, including models from OpenAI, Anthropic, Google, Meta, Mistral, and many more providers.

Prerequisites

Before using this module in your Ballerina application, you must first obtain an OpenRouter API key.

Quickstart

To use the ai.openrouter module in your Ballerina application, update the .bal file as follows:

Step 1: Import the module

Import the ai.openrouter module.

Copy
import ballerinax/ai.openrouter;

Step 2: Initialize the Model Provider

Here's how to initialize the Model Provider:

Copy
import ballerina/ai;
import ballerinax/ai.openrouter;

final ai:ModelProvider openRouterModel = check new openrouter:ModelProvider(
    "openRouterApiKey",
    modelType = "anthropic/claude-3.5-sonnet"
);

Step 3: Invoke chat completion

Copy
ai:ChatMessage[] chatMessages = [{role: "user", content: "hi"}];
ai:ChatAssistantMessage response = check openRouterModel->chat(chatMessages, tools = []);

chatMessages.push(response);

Step 4: Generate typed output

Copy
type Sentiment record {|
    string label;
    decimal score;
|};

@ai:JsonSchema {
    "type": "object",
    "required": ["label", "score"],
    "properties": {
        "label": {"type": "string", "enum": ["positive", "neutral", "negative"]},
        "score": {"type": "number"}
    }
}
type SentimentType Sentiment;

Sentiment|error result = openRouterModel->generate(
    `Analyze the sentiment of: "I love this product!"`
);

Import

import ballerinax/ai.openrouter;Copy

Other versions

1.0.0

Metadata

Released date: 15 days ago

Version: 1.0.0

License: Apache-2.0


Compatibility

Platform: java21

Ballerina version: 2201.12.0

GraalVM compatible: Yes


Pull count

Total: 154

Current verison: 154


Weekly downloads


Source repository


Keywords

Agent

Model

Provider

Vendor/OpenRouter

Area/AI

Type/Connector


Contributors