Examples Cookbook

This page provides practical examples for using the Serika.dev API with the official OpenAI client libraries.

Prerequisites

Ensure you have the OpenAI library installed:

pip install openai

Chat Completions

Basic Chat

from openai import OpenAI

client = OpenAI(
    base_url="https://api.serika.dev/api/openai/v1",
    api_key="your_api_key"
)

response = client.chat.completions.create(
    model="openai/gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is the capital of France?"}
    ]
)

print(response.choices[0].message.content)

Streaming Responses

from openai import OpenAI

client = OpenAI(
    base_url="https://api.serika.dev/api/openai/v1",
    api_key="your_api_key"
)

stream = client.chat.completions.create(
    model="openai/gpt-4o-mini",
    messages=[{"role": "user", "content": "Tell me a story about a dragon."}],
    stream=True,
)

for chunk in stream:
    if chunk.choices[0].delta.content is not None:
        print(chunk.choices[0].delta.content, end="")

Image Generation

Generate an Image

from openai import OpenAI

client = OpenAI(
    base_url="https://api.serika.dev/api/openai/v1",
    api_key="your_api_key"
)

response = client.images.generate(
    model="novelai/nai-diffusion-3",
    prompt="A cute anime girl with pink hair, high quality",
    size="1024x1024",
    quality="standard",
    n=1,
)

image_url = response.data[0].url
print(f"Image URL: {image_url}")

Advanced Usage

Using a Specific Character

You can influence the AI’s personality by passing a character_id in the extra_body parameter (Python) or directly in the options (Node.js, if typed loosely, otherwise via custom request).

response = client.chat.completions.create(
    model="sao10k/l3.3-euryale-70b",
    messages=[{"role": "user", "content": "Hello there!"}],
    extra_body={
        "character_id": "your_character_id_here"
    }
)