Factory

The LLM Factory implements a flexible provider pattern for managing multiple language model integrations. It provides a unified interface for generating responses while supporting provider-specific f

Architecture

Core Interfaces

interface LLMResponse {
  content: string;
  totalTokens?: number;
  toolCalls?: Array<{
    id: string;
    type: 'function';
    function: {
      name: string;
      arguments: string;
    }
  }>;
}

interface LLMProvider {
  generateResponse(
    messages: Array<{ role: string; content: string }>, 
    cleanContent: string
  ): Promise<LLMResponse>;
}

export type ModelType = 'OPENAI' | 'GEMINI' | 'GROK' | 'DEEPSEEK';

Factory Implementation

Providers

OpenAI Provider

  • Model: gpt-4o

  • Full function calling support

  • Token usage tracking

  • Error handling and retries

  • Standard OpenAI SDK integration

Gemini Provider

  • Model: gemini-pro

  • Chat support without function calling

  • Role-based message history (assistant → model)

  • Custom error handling

  • Google AI SDK integration

Grok Provider

  • Model: grok-2-latest

  • OpenAI-compatible API

  • Function calling support

  • Response normalization

  • Tool call format standardization

DeepSeek Provider

  • Model: deepseek-chat

  • Direct API integration

  • Function calling support

  • Custom message transformation

  • Direct HTTP request handling

Function Calling

Supported tools across compatible providers:

Core Tools

Usage

Basic Usage

Model Selection

Error Handling

Best Practices

Provider Selection

  • Use OpenAI for complex tasks requiring function calling

  • Use Gemini for basic chat interactions

  • Use Grok for OpenAI-compatible function calling

  • Use DeepSeek for specialized tasks

  • Consider rate limits and costs

  • Match provider capabilities to task requirements

Error Handling

  • Implement provider-specific error handling

  • Handle rate limits and quotas

  • Validate responses

  • Log errors appropriately

  • Implement retries where appropriate

  • Handle network failures

  • Normalize error formats

Performance

  • Reuse factory instance (singleton pattern)

  • Monitor token usage

  • Implement caching where appropriate

  • Handle streaming responses efficiently

  • Optimize message history

  • Clean content processing

  • Validate tool calls

Last updated