Module openrouter

ballerinax/openrouter Ballerina library

1.0.0

Overview

OpenRouter is a unified API platform that provides access to multiple AI language models from various providers, allowing developers to easily switch between and compare different models through a single interface.

The ballerinax/openrouter package offers APIs to connect and interact with OpenRouter API endpoints, specifically based on OpenRouter API v1.

Setup guide

To use the OpenRouter connector, you must have access to the OpenRouter API through an OpenRouter developer account and obtain an API access token. If you do not have an OpenRouter account, you can sign up for one here.

Step 1: Create an OpenRouter Account

  1. Navigate to the OpenRouter website and sign up for an account or log in if you already have one.

  2. Note that OpenRouter operates on a pay-per-use model with credits, and API access is available to all registered users regardless of plan type.

Step 2: Generate an API Access Token

  1. Log in to your OpenRouter account.

  2. Navigate to the Keys section by clicking on your profile in the top right corner and selecting "Keys" from the dropdown menu.

  3. Click on "Create Key" to generate a new API key.

  4. Provide a name for your key and set any optional restrictions if needed, then click "Create Key".

Tip: You must copy and store this key somewhere safe. It won't be visible again in your account settings for security reasons.

Quickstart

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

Step 1: Import the module

Copy
import ballerinax/openrouter;

Step 2: Instantiate a new connector

  1. Create a Config.toml file and configure the obtained access token:
Copy
token = "<Your_OpenRouter_API_Token>"
  1. Create an openrouter:ConnectionConfig and initialize the client:
Copy
configurable string token = ?;

final openrouter:Client openrouterClient = check new({
    auth: {
        token
    }
});

Step 3: Invoke the connector operation

Now, utilize the available connector operations.

Create a chat completion

Copy
public function main() returns error? {
    openrouter:ChatGenerationParams chatRequest = {
        model: "openai/gpt-3.5-turbo",
        messages: [
            {
                role: "user",
                content: "Hello! Can you help me write a simple greeting message?"
            }
        ],
        max_tokens: 150,
        temperature: 0.7
    };

    openrouter:ChatResponse response = check openrouterClient->/chat/completions.post(chatRequest);
}

Step 4: Run the Ballerina application

Copy
bal run

Examples

The openrouter connector provides practical examples illustrating usage in various scenarios. Explore these examples, covering the following use cases:

  1. Ai model deployment pipeline - Demonstrates how to automate AI model deployment processes using Ballerina connector for OpenRouter.
  2. Ai security audit - Illustrates conducting security audits for AI systems and models.

Import

import ballerinax/openrouter;Copy

Other versions

1.0.0

Metadata

Released date: 16 days ago

Version: 1.0.0

License: Apache-2.0


Compatibility

Platform: any

Ballerina version: 2201.12.0

GraalVM compatible: Yes


Pull count

Total: 158

Current verison: 158


Weekly downloads


Source repository


Keywords

Agent

Model

Provider

Vendor/OpenRouter

Area/AI

Type/Connector


Contributors