Package com.tnsai.llm


package com.tnsai.llm
LLM (Large Language Model) client interfaces and abstractions.

This package provides a unified interface for interacting with various LLM providers. The LLMClient interface defines the contract that all provider implementations must follow.

Architecture

 ┌─────────────────────────────────────────────────────────────┐
 │                      LLMClient                               │
 │              (Interface - this package)                      │
 └──────────────────────────┬──────────────────────────────────┘
                            │
        ┌───────────────────┼───────────────────┐
        │                   │                   │
        ▼                   ▼                   ▼
 ┌─────────────┐    ┌─────────────┐    ┌─────────────┐
 │OpenAIClient │    │AnthropicCl  │    │GeminiClient │
 └─────────────┘    └─────────────┘    └─────────────┘
        │                   │                   │
        └───────────────────┴───────────────────┘
                            │
                            ▼
              ┌─────────────────────────┐
              │  com.tnsai.llm.providers │
              │  (Implementation package) │
              └─────────────────────────┘
 

Supported Providers

ProviderClassModels
OpenAIOpenAIClientGPT-4, GPT-3.5-turbo
Azure OpenAIAzureOpenAIClientGPT-4, GPT-3.5
AnthropicAnthropicClientClaude 3, Claude 2
GoogleGeminiClientGemini Pro, Gemini Ultra
OllamaOllamaClientLlama 2, Mistral, etc.
GroqGroqClientLlama, Mixtral
OpenRouterOpenRouterClientMultiple providers
MistralMistralClientMistral models
AWS BedrockBedrockClientClaude, Llama

Key Features

  • Unified API: Same interface for all providers
  • Tool Support: Function/tool calling across providers
  • Retry Logic: Built-in exponential backoff for resilience
  • Streaming: Support for streaming responses (provider-dependent)
  • Multimodal: Image support for capable models (Gemini, GPT-4V)

Usage Example


 // Create a client using factory method
 LLMClient client = LLMClient.create("openai");

 // Or create directly
 LLMClient client = new OpenAIClient("gpt-4");

 // Simple chat
 String response = client.chat("What is the capital of France?");

 // Chat with messages
 List<ChatMessage> messages = List.of(
     ChatMessage.system("You are a helpful assistant"),
     ChatMessage.user("Hello!")
 );
 ChatMessage response = client.chat(messages);

 // Chat with tools
 List<Tool> tools = List.of(new Tool("calculator", "Perform math"));
 ChatMessage response = client.chat(messages, tools);
 

Configuration

API keys can be provided via:

  • Environment variables (e.g., OPENAI_API_KEY)
  • .env file in project root
  • Constructor parameters
See Also: