Skip to content

Legacy binding examples

These examples preserve additional target-specific usage patterns alongside the fixture-generated reference matrix.

using LiterLlm;
using var client = LiterLlmLib.CreateClient(
apiKey: "",
baseUrl: "http://localhost:11434/v1",
timeoutSecs: null,
maxRetries: null,
modelHint: "ollama/qwen2:0.5b");
var response = await client.ChatAsync(new ChatCompletionRequest
{
Model = "ollama/qwen2:0.5b",
Messages = new[]
{
new Message
{
Role = MessageRoleEnum.User,
Content = "Hello!"
}
}
});
Console.WriteLine(response.Choices[0].Message.Content);
Terminal window
curl http://localhost:4000/v1/audio/speech \
-H "Authorization: Bearer $LITER_LLM_MASTER_KEY" \
-H "Content-Type: application/json" \
--output speech.mp3 \
-d '{
"model": "tts-1",
"voice": "alloy",
"input": "Backoff with jitter prevents synchronised retries."
}'
Terminal window
curl http://localhost:4000/v1/batches \
-H "Authorization: Bearer $LITER_LLM_MASTER_KEY" \
-H "Content-Type: application/json" \
-d '{
"input_file_id": "file_abc123",
"endpoint": "/v1/chat/completions",
"completion_window": "24h"
}'
Terminal window
curl http://localhost:4000/v1/embeddings \
-H "Authorization: Bearer $LITER_LLM_MASTER_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "text-embedding-3-small",
"input": ["the quick brown fox", "the lazy dog"]
}'
Terminal window
curl http://localhost:4000/v1/files \
-H "Authorization: Bearer $LITER_LLM_MASTER_KEY" \
-F purpose=batch \
-F file=@requests.jsonl
Terminal window
# Liveness probe: returns 200 as long as the process is running.
curl -fsS http://localhost:4000/health/liveness
# Readiness probe: returns 200 once service pool and file store are initialised.
curl -fsS http://localhost:4000/health/readiness
# Full status: includes configured model list.
curl -fsS http://localhost:4000/health
Terminal window
curl http://localhost:4000/v1/images/generations \
-H "Authorization: Bearer $LITER_LLM_MASTER_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "dall-e-3",
"prompt": "A cross-section diagram of a Rust async runtime",
"size": "1024x1024",
"n": 1
}'
Terminal window
curl http://localhost:4000/v1/responses \
-H "Authorization: Bearer $LITER_LLM_MASTER_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"input": "Explain eventual consistency to a backend engineer."
}'
import 'package:liter_llm/liter_llm.dart';
import 'dart:io';
void main() async {
final client = await LiterLlmBridge.createClient(
apiKey: Platform.environment['OPENAI_API_KEY'] ?? '',
);
final request = ChatCompletionRequest(
model: 'openai/gpt-4o',
messages: [Message.user(UserMessage(content: UserContent.of('Hello!')))],
);
final response = await client.chat(request);
print(response.choices[0].message.content);
}
import 'package:liter_llm/liter_llm.dart';
void main() async {
// Example snippet
}
import 'package:liter_llm/liter_llm.dart';
void main() async {
// Example snippet
}
import 'package:liter_llm/liter_llm.dart';
void main() async {
// Example snippet
}
import 'package:liter_llm/liter_llm.dart';
void main() async {
// Example snippet
}
import 'package:liter_llm/liter_llm.dart';
void main() async {
// Example snippet
}
import 'package:liter_llm/liter_llm.dart';
void main() async {
// Example snippet
}
import 'package:liter_llm/liter_llm.dart';
void main() async {
// Example snippet
}
import 'package:liter_llm/liter_llm.dart';
void main() async {
// Example snippet
}
import 'package:liter_llm/liter_llm.dart';
void main() async {
// Example snippet
}
import 'package:liter_llm/liter_llm.dart';
void main() async {
// Example snippet
}
import 'package:liter_llm/liter_llm.dart';
void main() async {
// Example snippet
}
import 'package:liter_llm/liter_llm.dart';
void main() async {
final client = await LiterLlmBridge.createClient(
apiKey: '',
baseUrl: 'http://localhost:11434/v1',
);
final request = ChatCompletionRequest(
model: 'ollama/qwen2:0.5b',
messages: [Message.user(UserMessage(content: UserContent.of('Hello!')))],
);
final response = await client.chat(request);
print(response.choices[0].message.content);
}
import 'package:liter_llm/liter_llm.dart';
void main() async {
// Example snippet
}
import 'package:liter_llm/liter_llm.dart';
void main() async {
// Example snippet
}
import 'package:liter_llm/liter_llm.dart';
void main() async {
// Example snippet
}
import 'package:liter_llm/liter_llm.dart';
void main() async {
// Example snippet
}
import 'package:liter_llm/liter_llm.dart';
void main() async {
// Example snippet
}
import 'package:liter_llm/liter_llm.dart';
void main() async {
// Example snippet
}
{:ok, client} = LiterLlm.create_client("")
request =
Jason.encode!(%{
"model" => "ollama/qwen2:0.5b",
"messages" => [
%{
"role" => "user",
"content" => "Hello!"
}
]
})
{:ok, response} = LiterLlm.defaultclient_chat(client, request, "http://localhost:11434/v1")
IO.puts(Enum.at(response.choices, 0).message.content)
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
ll "github.com/xberg-io/liter-llm/packages/go"
)
func main() {
ctx := context.Background()
// Create client with OpenAI API key
client, err := ll.CreateClient(os.Getenv("OPENAI_API_KEY"), "", 0, 0, "")
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
// Build a multimodal user message with text and image
imageURL := ll.ImageURL{
URL: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
Detail: ll.Ptr(ll.ImageDetailLow),
}
userMessage := ll.Message{
Role: "user",
User: &ll.UserMessage{
Content: ll.UserContent(json.RawMessage(`[
{"type":"text","text":"Describe this image in one sentence."},
{"type":"image_url","image_url":{"url":"https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg","detail":"Low"}}
]`)),
Name: nil,
},
}
// Create chat request with JSON schema response format
schema := ll.JSONSchemaFormat{
Name: "ImageDescription",
Description: ll.Ptr("A single-sentence description of an image"),
Schema: json.RawMessage(`{
"type": "object",
"properties": {
"description": {"type": "string"}
},
"required": ["description"]
}`),
Strict: ll.Ptr(true),
}
request := ll.ChatCompletionRequest{
Model: "openai/gpt-4o",
Messages: []ll.Message{userMessage},
ResponseFormat: ll.ResponseFormatJSONSchema{
JSONSchema: schema,
},
Modalities: []ll.Modality{ll.ModalityText},
}
// Send request and get response
response, err := client.Chat(ctx, request)
if err != nil {
log.Fatalf("failed to call chat: %v", err)
}
// Extract response text
if len(response.Choices) > 0 {
choice := response.Choices[0]
if choice.Message.Content != nil {
fmt.Printf("Response: %s\n", string(*choice.Message.Content))
}
}
// Example: Request multimodal output (image + text)
requestWithImage := ll.ChatCompletionRequest{
Model: "openai/gpt-4o",
Messages: []ll.Message{userMessage},
Modalities: []ll.Modality{ll.ModalityText, ll.ModalityImage},
}
responseWithImage, err := client.Chat(ctx, requestWithImage)
if err != nil {
log.Fatalf("failed to call chat with image output: %v", err)
}
// Extract output images using helper method
if len(responseWithImage.Choices) > 0 {
choice := responseWithImage.Choices[0]
outputImages, err := choice.Message.OutputImages()
if err == nil {
for i, img := range outputImages {
fmt.Printf("Output image %d: %s\n", i, img.URL)
}
}
}
// Example: Stream multimodal response
stream, err := client.ChatStream(ctx, requestWithImage)
if err != nil {
log.Fatalf("failed to start stream: %v", err)
}
defer stream.Close()
for {
chunk, err := stream.Next()
if err != nil {
log.Fatalf("stream error: %v", err)
}
if chunk == nil {
break
}
if len(chunk.Choices) > 0 && chunk.Choices[0].Delta.Content != nil {
fmt.Printf("Streamed: %s", string(*chunk.Choices[0].Delta.Content))
}
}
}
import io.xberg.literllm.*;
import java.util.List;
public class LocalLlm {
public static void main(String[] args) throws Exception {
// No API key needed for local providers
try (var client = LiterLlm.createClient("", "http://localhost:11434/v1")) {
var request = ChatCompletionRequest.builder()
.withModel("ollama/qwen2:0.5b")
.withMessages(List.of(
new Message.User(new UserMessage(UserContent.of("Hello!"), null))
))
.build();
var response = client.chat(request);
System.out.println(response.choices().getFirst().message().content());
}
}
}
import io.xberg.literllm.android.*
import kotlinx.coroutines.runBlocking
fun main() = runBlocking {
val client = LiterLlm.createClient(System.getenv("OPENAI_API_KEY") ?: "")
val request = ChatCompletionRequest(
model = "openai/gpt-4o",
messages = listOf(Message.User(UserMessage(content = UserContent.of("Hello!"))))
)
val response = client.chat(request)
println(response.choices[0].message.content)
}
import io.xberg.literllm.android.*
suspend fun main() {
// Example snippet
}
import io.xberg.literllm.android.*
suspend fun main() {
// Example snippet
}
import io.xberg.literllm.android.*
suspend fun main() {
// Example snippet
}
import io.xberg.literllm.android.*
suspend fun main() {
// Example snippet
}
import io.xberg.literllm.android.*
suspend fun main() {
// Example snippet
}
import io.xberg.literllm.android.*
suspend fun main() {
// Example snippet
}
import io.xberg.literllm.android.*
suspend fun main() {
// Example snippet
}
import io.xberg.literllm.android.*
suspend fun main() {
// Example snippet
}
import io.xberg.literllm.android.*
suspend fun main() {
// Example snippet
}
import io.xberg.literllm.android.*
suspend fun main() {
// Example snippet
}
import io.xberg.literllm.android.*
suspend fun main() {
// Example snippet
}
import io.xberg.literllm.android.*
import kotlinx.coroutines.runBlocking
fun main() = runBlocking {
val client = LiterLlm.createClient(apiKey = "", baseUrl = "http://localhost:11434/v1")
val request = ChatCompletionRequest(
model = "ollama/qwen2:0.5b",
messages = listOf(Message.User(UserMessage(content = UserContent.of("Hello!"))))
)
val response = client.chat(request)
println(response.choices[0].message.content)
}
import io.xberg.literllm.android.*
suspend fun main() {
// Example snippet
}
import io.xberg.literllm.android.*
suspend fun main() {
// Example snippet
}
import io.xberg.literllm.android.*
suspend fun main() {
// Example snippet
}
import io.xberg.literllm.android.*
suspend fun main() {
// Example snippet
}
import io.xberg.literllm.android.*
suspend fun main() {
// Example snippet
}
import io.xberg.literllm.android.*
suspend fun main() {
// Example snippet
}
<?php
declare(strict_types=1);
use Liter\Llm\LiterLlm;
use Liter\Llm\ChatCompletionRequest;
use Liter\Llm\Message;
use Liter\Llm\UserMessage;
use Liter\Llm\UserContent;
// No API key needed for local providers
$client = LiterLlm::createClient(
apiKey: "",
baseUrl: "http://localhost:11434/v1"
);
$request = new ChatCompletionRequest(
model: "ollama/qwen2:0.5b",
messages: [
new Message(
role: "user",
content: "Hello!",
name: null
)
]
);
$response = $client->chat($request);
echo $response->choices[0]->message->content . PHP_EOL;
<?php
declare(strict_types=1);
use Liter\Llm\LiterLlm;
use Liter\Llm\Message;
use Liter\Llm\ContentPart;
use Liter\Llm\Image;
use Liter\Llm\ImageDetail;
use Liter\Llm\ChatCompletionRequest;
$client = LiterLlm::createClient(getenv('OPENAI_API_KEY') ?: '');
// Vision: Send an image and ask the model to analyze it
$response = $client->chat(
ChatCompletionRequest::from_json(json_encode([
'model' => 'gpt-4o',
'messages' => [
Message::userWithParts([
ContentPart::text('What is in this image?'),
ContentPart::imageUrl(
'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg/1280px-Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg',
ImageDetail::High
),
])->toArray(),
],
]))
);
echo "Analysis: " . $response->choices[0]->message->text() . PHP_EOL;
// Multimodal with base64 image
$imageBytes = file_get_contents('path/to/image.png');
$response = $client->chat(
ChatCompletionRequest::from_json(json_encode([
'model' => 'gpt-4o',
'messages' => [
Message::userWithParts([
ContentPart::text('Describe what you see'),
ContentPart::imagePng($imageBytes),
])->toArray(),
],
]))
);
echo $response->choices[0]->message->text() . PHP_EOL;
// Audio input (if supported by model)
$audioBytes = file_get_contents('path/to/audio.wav');
$audioBase64 = base64_encode($audioBytes);
$response = $client->chat(
ChatCompletionRequest::from_json(json_encode([
'model' => 'gpt-4o-audio-preview',
'messages' => [
Message::userWithParts([
ContentPart::text('Transcribe this audio'),
ContentPart::audio($audioBase64, 'wav'),
])->toArray(),
],
]))
);
echo "Transcription: " . $response->choices[0]->message->text() . PHP_EOL;
// Structured output with JSON schema
$response = $client->chat(
ChatCompletionRequest::from_json(json_encode([
'model' => 'gpt-4o',
'messages' => [
Message::user('Extract the person name and age from: "John Doe is 30 years old"'),
],
'response_format' => [
'type' => 'json_schema',
'json_schema' => [
'name' => 'person',
'schema' => json_encode([
'type' => 'object',
'properties' => [
'name' => ['type' => 'string'],
'age' => ['type' => 'integer'],
],
'required' => ['name', 'age'],
]),
'strict' => true,
],
],
]))
);
$structured = json_decode($response->choices[0]->message->text(), true);
echo "Extracted: {$structured['name']}, age {$structured['age']}" . PHP_EOL;

Multimodal I/O in Python

Process images, documents, and audio alongside text using liter-llm’s multimodal message API.

Text with Images

Combine text and image references in a single user message:

import asyncio
import liter_llm
from liter_llm import ChatCompletionRequest, UserMessage, ImageUrl
async def describe_image():
client = liter_llm.create_client(api_key="your-api-key")
req = ChatCompletionRequest(
model="gpt-4o-mini", # Vision-capable model
messages=[
UserMessage(content=[
"What's the main subject in this image?",
ImageUrl(url="https://example.com/photo.jpg", detail="high")
])
]
)
resp = await liter_llm.chat(client, req)
print(resp.choices[0].message.text())
asyncio.run(describe_image())

Image Data URLs

Embed image bytes inline using base64 data URLs:

from pathlib import Path
import liter_llm
from liter_llm import ChatCompletionRequest, UserMessage, ImageUrl
async def analyze_local_image():
client = liter_llm.create_client(api_key="your-api-key")
# Read image file
image_bytes = Path("diagram.png").read_bytes()
# Encode as data URL
data_url = liter_llm.encode_data_url(image_bytes, mime="image/png")
# Use in message
req = ChatCompletionRequest(
model="gpt-4o-mini",
messages=[
UserMessage(content=[
"Analyze this diagram:",
ImageUrl(url=data_url, detail="high")
])
]
)
resp = await liter_llm.chat(client, req)
print(resp.choices[0].message.text())
asyncio.run(analyze_local_image())

Structured Output (JSON Schema)

Enforce JSON schema conformance for model outputs:

import json
import asyncio
import liter_llm
from liter_llm import (
ChatCompletionRequest,
UserMessage,
JsonSchemaFormat
)
async def extract_structured_data():
client = liter_llm.create_client(api_key="your-api-key")
# Define output schema
schema = {
"type": "object",
"properties": {
"person_name": {"type": "string"},
"company": {"type": "string"},
"role": {"type": "string"}
},
"required": ["person_name", "company", "role"]
}
req = ChatCompletionRequest(
model="gpt-4o-mini",
messages=[
UserMessage(content="Extract from: John Doe works at TechCorp as Senior Engineer")
],
response_format=JsonSchemaFormat(
name="person_info",
schema=json.dumps(schema),
strict=True,
description="Extract person, company, and role"
)
)
resp = await liter_llm.chat(client, req)
result_text = resp.choices[0].message.text()
# Parse structured output
extracted = json.loads(result_text)
print(f"Name: {extracted['person_name']}")
print(f"Company: {extracted['company']}")
print(f"Role: {extracted['role']}")
asyncio.run(extract_structured_data())

Multimodal Output

Request multimodal responses (text, images, audio) from capable models:

import asyncio
import liter_llm
from liter_llm import ChatCompletionRequest, UserMessage, Modality
async def multimodal_response():
client = liter_llm.create_client(api_key="your-api-key")
req = ChatCompletionRequest(
model="gpt-4o-mini",
messages=[
UserMessage(content="Write a short poem and generate accompanying audio")
],
modalities=[Modality.TEXT, Modality.AUDIO] # Request text + audio
)
resp = await liter_llm.chat(client, req)
# Extract output
msg = resp.choices[0].message
text_output = msg.text()
audio_outputs = msg.output_audio()
print(f"Text: {text_output}")
print(f"Audio parts: {len(audio_outputs)}")
asyncio.run(multimodal_response())

Documents and PDFs

Send documents to vision-capable models:

import asyncio
import liter_llm
from liter_llm import (
ChatCompletionRequest,
UserMessage,
DocumentContent
)
async def analyze_document():
client = liter_llm.create_client(api_key="your-api-key")
# Read PDF and encode
pdf_bytes = open("report.pdf", "rb").read()
import base64
b64_pdf = base64.b64encode(pdf_bytes).decode()
req = ChatCompletionRequest(
model="gpt-4o-mini",
messages=[
UserMessage(content=[
"Summarize the key findings:",
DocumentContent(data=b64_pdf, media_type="application/pdf")
])
]
)
resp = await liter_llm.chat(client, req)
print(resp.choices[0].message.text())
asyncio.run(analyze_document())

Audio Input

Transcribe or process audio with speech-capable models:

import asyncio
import liter_llm
from liter_llm import (
ChatCompletionRequest,
UserMessage,
AudioContent
)
async def process_audio():
client = liter_llm.create_client(api_key="your-api-key")
# Read audio and encode
audio_bytes = open("recording.mp3", "rb").read()
import base64
b64_audio = base64.b64encode(audio_bytes).decode()
req = ChatCompletionRequest(
model="gpt-4o-mini",
messages=[
UserMessage(content=[
"Transcribe and summarize:",
AudioContent(data=b64_audio, format="mp3")
])
]
)
resp = await liter_llm.chat(client, req)
print(resp.choices[0].message.text())
asyncio.run(process_audio())

Decoding Data URLs

Extract bytes from data URLs:

import liter_llm
# Encode
encoded = liter_llm.encode_data_url(b"image data", mime="image/jpeg")
# Decode
decoded = liter_llm.decode_data_url(encoded)
if decoded:
print(f"MIME: {decoded.mime}")
print(f"Size: {len(decoded.data)} bytes")
# frozen_string_literal: true
require 'liter_llm'
# No API key needed for local providers
client = LiterLlm.create_client("", "http://localhost:11434/v1")
request = LiterLlm::ChatCompletionRequest.new(
model: "ollama/qwen2:0.5b",
messages: [
LiterLlm::Message::User.new(
LiterLlm::UserMessage.new(
content: LiterLlm::UserContent::Text.new("Hello!"),
name: nil
)
)
]
)
response = client.chat_async(request)
puts response.choices[0].message.content
import Foundation
import LiterLlm
let client = try await LiterLlm.createClient(apiKey: ProcessInfo.processInfo.environment["OPENAI_API_KEY"] ?? "")
let request = ChatCompletionRequest(
model: "openai/gpt-4o",
messages: [.user(.init(content: .of("Hello!")))],
temperature: nil, topP: nil, maxTokens: nil, toolChoice: nil, tools: nil, responseFormat: nil
)
let response = try await client.chat(request)
print(response.choices[0].message.content ?? "")
import Foundation
import LiterLlm
// Example snippet
import Foundation
import LiterLlm
// Example snippet
import Foundation
import LiterLlm
// Example snippet
import Foundation
import LiterLlm
// Example snippet
import Foundation
import LiterLlm
// Example snippet
import Foundation
import LiterLlm
// Example snippet
import Foundation
import LiterLlm
// Example snippet
import Foundation
import LiterLlm
// Example snippet
import Foundation
import LiterLlm
// Example snippet
import Foundation
import LiterLlm
// Example snippet
import Foundation
import LiterLlm
// Example snippet
import Foundation
import LiterLlm
let client = try await LiterLlm.createClient(
apiKey: "",
baseUrl: "http://localhost:11434/v1"
)
let request = ChatCompletionRequest(
model: "ollama/qwen2:0.5b",
messages: [.user(.init(content: .of("Hello!")))],
temperature: nil, topP: nil, maxTokens: nil, toolChoice: nil, tools: nil, responseFormat: nil
)
let response = try await client.chat(request)
print(response.choices[0].message.content ?? "")
import Foundation
import LiterLlm
// Example snippet
import Foundation
import LiterLlm
// Example snippet
import Foundation
import LiterLlm
// Example snippet
import Foundation
import LiterLlm
// Example snippet
import Foundation
import LiterLlm
// Example snippet
import Foundation
import LiterLlm
// Example snippet
import init, {
createClient,
WasmChatCompletionRequest,
WasmMessage,
WasmUserContent,
} from "@xberg-io/liter-llm-wasm";
await init();
// No API key needed for local providers
const client = createClient("", "http://localhost:11434/v1");
const request = WasmChatCompletionRequest.default();
request.model = "ollama/qwen2:0.5b";
const message = WasmMessage.User(new WasmUserContent.Text("Hello!"));
request.messages = [message];
const response = await client.chat(request);
console.log(response.choices[0].message.content);
const liter_llm = @import("liter_llm");
const std = @import("std");
pub fn main() !void {
const api_key = "sk-...";
var client = try liter_llm.create_client(api_key, null, null, null, null);
defer client.close();
const req = "{\"model\":\"openai/gpt-4o\",\"messages\":[{\"role\":\"user\",\"content\":\"Hello!\"}]}";
const response = try client.chat(req);
defer liter_llm._free_string(response);
std.debug.print("Response: {s}\n", .{response});
}
// Placeholder
// Placeholder
// Placeholder
// Placeholder
// Placeholder
// Placeholder
// Placeholder
// Placeholder
// Placeholder
// Placeholder
// Placeholder
const liter_llm = @import("liter_llm");
const std = @import("std");
pub fn main() !void {
const base_url = "http://localhost:11434/v1";
var client = try liter_llm.create_client("", base_url, null, null, null);
defer client.close();
const req = "{\"model\":\"ollama/qwen2:0.5b\",\"messages\":[{\"role\":\"user\",\"content\":\"Hello!\"}]}";
const response = try client.chat(req);
defer liter_llm._free_string(response);
std.debug.print("Response: {s}\n", .{response});
}
// Placeholder
// Placeholder
// Placeholder
// Placeholder
// Placeholder
// Placeholder