AKBXR AI
Examples

Code Examples

Explore practical examples and implementation guides for integrating the AI API into your applications. All examples use the OpenAI-compatible SDK and can be easily adapted to your specific use cases.

Quick Start Examples

Basic Chat Completion

import OpenAI from "openai";

const openai = new OpenAI({
  baseURL: "https://api.akbxr.com/v1",
  apiKey: "YOUR_API_KEY",
});

const completion = await openai.chat.completions.create({
  model: "auto",
  messages: [{ role: "user", content: "Hello!" }],
});

console.log(completion.choices[0].message.content);

Python Example

import openai

client = openai.OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.akbxr.com/v1"
)

response = client.chat.completions.create(
    model="auto",
    messages=[{"role": "user", "content": "Hello!"}]
)

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

cURL Example

curl -X POST https://api.akbxr.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "auto",
    "messages": [
      {
        "role": "user",
        "content": "Hello!"
      }
    ]
  }'

Use Case Examples

Customer Support Bot

const customerSupportBot = async (userMessage, customerContext = {}) => {
  const systemMessage = {
    role: "system",
    content: `You are a helpful customer support agent for an e-commerce platform.
    Be polite, professional, and provide accurate information.
    Customer context: ${JSON.stringify(customerContext)}`
  };

  const completion = await openai.chat.completions.create({
    model: "auto",
    messages: [
      systemMessage,
      { role: "user", content: userMessage }
    ],
    temperature: 0.3, // Lower temperature for consistent responses
  });

  return completion.choices[0].message.content;
};

// Usage
const response = await customerSupportBot(
  "I need help with my order #12345",
  {
    customerTier: "premium",
    orderStatus: "shipped",
    previousTickets: 2
  }
);

Language Translation

const translateText = async (text, targetLanguage) => {
  const completion = await openai.chat.completions.create({
    model: "auto",
    messages: [
      {
        role: "system",
        content: `You are a professional translator. Translate the given text to ${targetLanguage}. Only return the translation, no additional text.`
      },
      {
        role: "user",
        content: text
      }
    ],
    temperature: 0.1, // Very low for accurate translations
  });

  return completion.choices[0].message.content;
};

// Usage
const translated = await translateText(
  "Hello, how are you today?",
  "Spanish"
);
console.log(translated); // "Hola, ¿cómo estás hoy?"

Text Summarization

const summarizeText = async (text, maxLength = 100) => {
  const completion = await openai.chat.completions.create({
    model: "auto",
    messages: [
      {
        role: "system",
        content: `Summarize the following text in approximately ${maxLength} words. Focus on the key points and main ideas.`
      },
      {
        role: "user",
        content: text
      }
    ],
    temperature: 0.3,
  });

  return completion.choices[0].message.content;
};

Code Generation

const generateCode = async (description, language = "javascript") => {
  const completion = await openai.chat.completions.create({
    model: "auto",
    messages: [
      {
        role: "system",
        content: `You are a helpful programming assistant. Generate clean, well-commented ${language} code based on the user's requirements.`
      },
      {
        role: "user",
        content: description
      }
    ],
    temperature: 0.2, // Lower temperature for more consistent code
  });

  return completion.choices[0].message.content;
};

// Usage
const code = await generateCode(
  "Create a function that validates an email address using regex",
  "javascript"
);