Basic Usage

The SDK provides two distinct methods for different use cases:

Event-Driven Conversational Method: chat.send()

Use client.chat.send() for chat applications with automatic conversation management and event-driven responses:

import { AnimusClient } from 'animus-client';

const client = new AnimusClient({
  tokenProviderUrl: 'https://your-backend.com/api/get-animus-token',
  chat: {
    model: 'vivian-llama3.1-70b-1.0-fp8',
    systemMessage: 'You are a helpful assistant.',
    historySize: 10, // Automatically manages conversation history
    check_image_generation: true // Automatically generates and manages images
  }
});

// Listen for events (chat.send is event-driven, not awaited)
client.on('messageComplete', (data) => {
  console.log('AI Response:', data.content);
  updateChatUI(data.content);
});

client.on('imageGenerationComplete', (data) => {
  console.log('Image generated:', data.imageUrl);
  displayImage(data.imageUrl);
});

// Send message - triggers events, no await needed
client.chat.send("Hello! Can you create an image of a sunset?");
// Image is automatically generated and added to conversation history

Direct API Method: chat.completions()

Use client.chat.completions() for direct API calls with manual control:

import { ChatMessage } from 'animus-client';

// Chat Completions Response Structure
interface ChatCompletionResponse {
  id: string;                    // Unique response ID
  object: 'chat.completion';     // Response type identifier
  created: number;               // Unix timestamp
  model: string;                 // Model used for completion
  choices: Array<{
    message: {
      role: 'assistant';
      content: string | null;        // Main response text (null if only tool_calls)
      reasoning?: string;            // Model's reasoning process (optional)
      tool_calls?: ToolCall[];       // Function calls requested (optional)
      image_prompt?: string;         // Image generation prompt (optional)
      turns?: string[];              // Conversation turns for autoTurn (optional)
      next?: boolean;                // Indicates follow-up expected (optional)
    };
    index: number;
    finish_reason: string;
  }>;
  usage?: {                      // Token usage statistics (optional)
    prompt_tokens: number;
    completion_tokens: number;
    total_tokens: number;
  };
  compliance_violations?: string[]; // Any compliance issues (optional)
}

// You manage the message array manually
const messages: ChatMessage[] = [
  { role: 'system', content: 'You are a helpful assistant.' },
  { role: 'user', content: 'I need some inspiration for my desktop wallpaper. Can you create something peaceful?' }
];

const response = await client.chat.completions({
  messages: messages,
  temperature: 0.7,
  max_tokens: 500,
  check_image_generation: true
});

console.log('AI Response:', response.choices[0].message.content);
console.log('Response ID:', response.id);
console.log('Model used:', response.model);

// Check if an image prompt was generated (manual handling required)
if (response.choices[0].message.image_prompt) {
  console.log('Image prompt:', response.choices[0].message.image_prompt);
  // Use this prompt with your own hosted image generation service
  const imageUrl = await yourImageService.generate(response.choices[0].message.image_prompt);
  console.log('Generated image:', imageUrl);
}

// Check for compliance violations
if (response.compliance_violations && response.compliance_violations.length > 0) {
  console.log('Compliance violations:', response.compliance_violations);
}

// Check token usage
if (response.usage) {
  console.log('Tokens used:', response.usage.total_tokens);
}

Configuration Options

Chat Configuration

When initializing the client, you can configure default chat settings:

const client = new AnimusClient({
  tokenProviderUrl: 'your-token-url',
  chat: {
    // Required
    model: 'vivian-llama3.1-70b-1.0-fp8',
    systemMessage: 'You are a helpful assistant.',
    
    // Optional API parameter defaults
    temperature: 0.7,           // Default: 1
    top_p: 1.0,                // Default: 1
    n: 1,                      // Default: 1
    max_tokens: 150,           // Default: 150
    stop: ["<|im_end|>"],      // Default: ["<|im_end|>"]
    stream: false,             // Default: false
    presence_penalty: 1.0,     // Default: 1
    frequency_penalty: 1.0,    // Default: 1
    best_of: 1,               // Default: 1
    top_k: 40,                // Default: 40
    repetition_penalty: 1.0,   // Default: 1
    min_p: 0.0,               // Default: 0
    length_penalty: 1.0,       // Default: 1
    compliance: true,          // Default: true
    
    // Optional SDK-specific features
    historySize: 20,           // SDK feature: conversation history
    reasoning: false,          // SDK feature: extract <think> blocks
    check_image_generation: false, // When true, AI analyzes its response and creates image_prompt if image is requested/desired
    autoTurn: false           // SDK feature: conversational turns
  }
});

Request Parameters

You can override defaults on a per-request basis:

// For direct API calls with parameters, use completions()
const response = await client.chat.completions({
  messages: [{ role: 'user', content: 'Tell me a joke' }],
  temperature: 0.9,        // More creative
  max_tokens: 100,         // Shorter response
  top_k: 20,              // More focused sampling
  repetition_penalty: 1.1  // Reduce repetition
});

Chat History Management

Conversation history is automatically managed when historySize is configured:

// Get current chat history
const history = client.chat.getChatHistory();

// Clear chat history
client.chat.clearChatHistory();

// Set custom history
const importedHistory = [
  { role: 'user', content: 'Previous message' },
  { role: 'assistant', content: 'Previous response' }
];
client.chat.setChatHistory(importedHistory);

// Update specific message
client.chat.updateHistoryMessage(0, { content: 'Updated message' });

// Delete specific message
client.chat.deleteHistoryMessage(1);

Message Format

Messages follow a structured format:

interface ChatMessage {
  role: 'system' | 'user' | 'assistant' | 'tool';
  content: string | null;
  name?: string;
  reasoning?: string;        // Extracted from <think> tags
  timestamp?: string;        // ISO timestamp
  tool_calls?: ToolCall[];   // For function calling
  tool_call_id?: string;     // For tool responses
  compliance_violations?: string[]; // Content moderation flags
}

Event-Driven Responses

When using client.chat.send(), you can listen for events:

// Message Event Data Structure
interface MessageEventData {
  content: string;                    // The message content
  messageType?: 'regular' | 'auto' | 'followup'; // Type of message
  turnIndex?: number;                 // For auto-turn: current turn index (0-based)
  totalTurns?: number;                // For auto-turn: total number of turns
  totalMessages?: number;             // When all messages in sequence are complete
  timestamp: string;                  // ISO timestamp
  error?: string;                     // For error events only
}

// Listen for message events
client.on('messageStart', (data: MessageEventData) => {
  console.log('Message started:', data.content);
  console.log('Message type:', data.messageType || 'regular');
});

client.on('messageComplete', (data: MessageEventData) => {
  console.log('Message completed:', data.content);
  console.log('Timestamp:', data.timestamp);
  
  // Check if this is part of a sequence
  if (data.totalMessages) {
    console.log(`All ${data.totalMessages} messages completed`);
  }
  
  // Update your UI here
  displayMessage(data.content);
});

client.on('messageError', (data: MessageEventData) => {
  console.error('Message error:', data.error);
  console.log('Failed message type:', data.messageType || 'regular');
});

// Send message (triggers events - do NOT await)
client.chat.send("Hello!");

Error Handling

Handle errors gracefully with specific error types:

import { ApiError, AuthenticationError } from 'animus-client';

try {
  // For event-driven chat, don't await - use events instead
  client.chat.send("Hello!");
  
  // For direct API calls that return responses, use completions()
  const response = await client.chat.completions({
    messages: [{ role: 'user', content: 'Hello!' }]
  });
} catch (error) {
  if (error instanceof AuthenticationError) {
    console.error('Authentication failed:', error.message);
    // Handle auth errors (redirect to login, etc.)
  } else if (error instanceof ApiError) {
    console.error(`API Error (${error.status}):`, error.message);
    // Handle API errors (show user-friendly message)
  } else {
    console.error('Unexpected error:', error);
  }
}

Best Practices

When to Use Each Method

Use chat.send() when:

  • Building conversational chat interfaces
  • You want automatic conversation history management
  • You need event-driven, real-time UI updates
  • You want automatic image generation and management
  • You prefer conversational turns with natural delays
  • Building simple chat applications

Use chat.completions() when:

  • You need direct API responses (await pattern)
  • Building non-conversational AI features (analysis, generation, etc.)
  • You want manual control over message arrays and history
  • You need streaming with custom chunk processing
  • You want to handle image prompts with your own generation service
  • Integrating with existing systems that expect synchronous responses

Configuration Tips

API Parameters:

  • Use temperature 0.7-0.9 for creative responses, 0.1-0.3 for factual responses
  • Set top_k to 20-40 for focused responses, higher for more diversity
  • Use repetition_penalty 1.1-1.2 to reduce repetition
  • Keep compliance enabled (true) in production for content safety
  • Adjust max_tokens based on your use case (default: 150)

SDK Features:

  • Set historySize to 10-30 for most conversational applications
  • Use reasoning: true for debugging or transparency (extracts <think> blocks)
  • Enable check_image_generation so AI analyzes its responses and creates image_prompt when images are requested or desired
  • Use autoTurn for natural conversation flow with realistic delays

Next Steps