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
| Provider | Class | Models |
|---|---|---|
| OpenAI | OpenAIClient | GPT-4, GPT-3.5-turbo |
| Azure OpenAI | AzureOpenAIClient | GPT-4, GPT-3.5 |
| Anthropic | AnthropicClient | Claude 3, Claude 2 |
| GeminiClient | Gemini Pro, Gemini Ultra | |
| Ollama | OllamaClient | Llama 2, Mistral, etc. |
| Groq | GroqClient | Llama, Mixtral |
| OpenRouter | OpenRouterClient | Multiple providers |
| Mistral | MistralClient | Mistral models |
| AWS Bedrock | BedrockClient | Claude, 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:
-
ClassDescriptionRepresents a single chunk in a streaming LLM response.The type of streaming chunk.The reason why generation stopped.Unified request model for LLM chat operations.Fluent builder for ChatRequest.Represents a chat response from an LLM, potentially including tool calls.Represents a tool/function call from the LLM.LLM client with automatic fallback support.Builder for FallbackLLMClient.Describes the capabilities of an LLM provider/model.Input modalities the model can process.Base interface for LLM (Large Language Model) client implementations.SPI for creating LLM clients from provider configuration.Resolves the appropriate LLM client using a fallback chain.Builder for LLM resolution with full fallback chain support.LLM configuration record for passing model settings.Result of LLM resolution with source tracking.Source of the resolved LLM in the fallback chain.Common interface for LLM configuration sources.Default implementation of LLMCapabilities with builder pattern.Represents information about an LLM model from a provider's API.Parallel LLM execution utilities.Result of a parallel LLM call.Aggregated results from multiple LLMs.