llm_chatgpt library

OpenAI/ChatGPT backend implementation for LLM interactions.

This package provides a ChatGPT-specific implementation of LLMChatRepository with support for streaming chat, embeddings, and tool calling.

Example usage:

import 'package:llm_chatgpt/llm_chatgpt.dart';

final repo = ChatGPTChatRepository(apiKey: 'your-api-key');
final stream = repo.streamChat('gpt-4o', messages: [
  LLMMessage(role: LLMRole.user, content: 'Hello!')
]);
await for (final chunk in stream) {
  print(chunk.message?.content ?? '');
}

Classes

BackendErrorHandler
Base class for backend-specific error handlers.
CacheKeyGenerator
Utility for generating cache keys from request parameters.
CacheStats
Statistics about cache usage.
ChatGPTChatRepository
Repository for chatting with OpenAI's ChatGPT.
ChatGPTChatRepositoryBuilder
Builder for creating ChatGPTChatRepository instances with complex configurations.
ChatGPTEmbedding
A single embedding in the response.
ChatGPTEmbeddingsResponse
Response from OpenAI embeddings endpoint.
ChatGPTEmbeddingsUsage
Token usage for embedding requests.
ChatRepositoryBuilderBase<T>
Base class for chat repository builders.
DefaultLLMLogger
Default implementation of LLMLogger using the logging package.
DefaultLLMMetrics
Default implementation of LLMMetrics that tracks basic statistics.
ErrorHandlers
Utility functions for error handling in HTTP-based repositories.
GPTChoice
A choice in a GPT response.
GPTChunk
Streaming chunk from OpenAI.
GPTChunkChoice
A choice in a streaming chunk.
GPTChunkChoiceDelta
Delta content in a streaming chunk.
GPTMessage
A message in a GPT response.
GPTResponse
Response from OpenAI chat completions endpoint.
GPTStreamDecoder
Stream transformer for decoding OpenAI SSE (Server-Sent Events) streams.
GPTToolCall
A tool call in a GPT response.
GPTToolFunctionCall
A function call within a tool call.
GPTUsage
Token usage statistics.
GPTUsageTokenDetails
Token usage details.
HttpClientHelper
Helper class for making HTTP requests with standardized timeout handling and error management.
JsonFormat
Requests JSON output with no schema enforcement.
JsonSchemaFormat
Requests JSON output conforming to a specific JSON Schema.
LLMCapabilities
Capabilities advertised by a repository/model combination.
LLMChatOptions
Options for chat requests.
LLMChatRepository
Abstract repository interface for LLM chat operations.
LLMChunk
Represents a streaming chunk from an LLM response.
LLMChunkMessage
The message portion of an LLM streaming chunk.
LLMEmbedding
Represents a text embedding generated by an LLM.
LLMImageContent
Image content inside an LLMMessage.
LLMLogger
Interface for logging in LLM packages.
LLMMessage
Represents a message in an LLM conversation.
LLMMessageContent
A typed content part inside an LLMMessage.
LLMMetrics
Interface for collecting metrics about LLM operations.
LLMResponse
Represents a complete (non-streaming) response from an LLM.
LLMResponseFormat
Structured output format for LLM responses.
LLMTextContent
Text content inside an LLMMessage.
LLMTool
Abstract base class for LLM tools (function calling).
LLMToolCall
Represents a tool call made by an LLM.
LLMToolParam
Represents a parameter for an LLM tool.
LLMUsage
Token usage reported by a model provider.
MemoryResponseCache
In-memory response cache implementation.
MergedOptions
Result of merging StreamChatOptions with individual parameters.
RateLimiter
Configuration for rate limiting requests.
RateLimiterUtil
Rate limiter utility for managing request rate limits.
ResponseCache
Interface for caching LLM responses.
RetryConfig
Configuration for retry behavior when making API requests.
RetryUtil
Utility class for retrying operations with exponential backoff.
StreamChatOptions
Backward-compatible name for LLMChatOptions.
StreamChatOptionsMerger
Utility for merging StreamChatOptions with individual parameters.
StreamToolExecutor
Executes tools from LLM chunks and manages the tool execution loop.
TimeoutConfig
Configuration for request timeouts.
TokenBucketRateLimiter
Token bucket rate limiter implementation.
Validation
Validation utilities for LLM requests.
WriteGatedHttpClient
Web stand-in for the dart:io WriteGatedHttpClient.

Enums

LLMFinishReason
Why a model response finished.
LLMLogLevel
Log levels for LLM logging.
LLMRole
The role of a message participant in an LLM conversation.
ReasoningEffort
Portable reasoning-depth levels.

Mixins

LLMRepositoryFeatures
Shared cache and metrics behavior for repository implementations.

Extensions

ChatGPTChatRepositoryBuilderExtension on ChatGPTChatRepository
Extension to add builder method to ChatGPTChatRepository.
ChatGPTLLMEmbedding on ChatGPTEmbeddingsResponse
Extension to convert ChatGPT embeddings response to LLM embeddings.
GPTMessageToLLMMessageExt on GPTMessage
Extension to convert GPT message to LLM message.
GPTToolCallToLLMToolCallExt on List<GPTToolCall>
Extension to convert GPT tool calls to LLM tool calls.

Constants

kLLMIdleTimeout → const Duration
Idle timeout applied to pooled connections.
kLLMMaxConcurrentWrites → const int
Default bound on concurrent connect+write phases on kqueue platforms (macOS, iOS).
kLLMMaxConnectionsPerHost → const int
Upper bound on simultaneous connections to one host.

Functions

createLLMHttpClient({TimeoutConfig? timeoutConfig, int maxConnectionsPerHost = kLLMMaxConnectionsPerHost, int? maxConcurrentWrites}) → Client
Creates the HTTP client the LLM backends use by default.
gptEffortWireValue(String model, ReasoningEffort effort) String?
Maps a portable ReasoningEffort onto the reasoning_effort value the given model family accepts, or null when model does not take the parameter at all.
gptIsReasoningModel(String model) bool
Whether model is an OpenAI reasoning model (o-series or gpt-5 family, excluding the non-reasoning gpt-5-chat ids).
gptRejectsSamplingParams(String model) bool
Whether model rejects temperature and top_p with a 400.
gptSupportsReasoningEffort(String model) bool
Whether model accepts the reasoning_effort parameter.
reasoningEffortForBudget(int budget) ReasoningEffort
Canonical mapping from a reasoning token budget to a portable effort level.

Exceptions / Errors

LLMApiException
Exception thrown when an LLM API request fails.
ModelLoadException
Exception thrown when model loading fails.
ThinkingNotSupportedException
Exception thrown when trying to use thinking on a model that doesn't support it.
ToolLoopIncompleteException
Exception thrown when strict tool-loop mode does not reach a final assistant answer.
ToolsNotSupportedException
Exception thrown when trying to use tools on a model that doesn't support them.
VisionNotSupportedException
Exception thrown when trying to use images/vision on a model that doesn't support it.