ExLLM.Adapters.Shared.ResponseBuilder (ex_llm v0.5.0)
View SourceShared utilities for building standardized responses across adapters.
Provides consistent response construction for:
- Chat completions
- Streaming chunks
- Function calls
- Error responses
- Embeddings
Summary
Functions
Build an audio transcription response from provider data.
Build a standard chat response from provider-specific data.
Build a completion response (non-chat format) from provider data.
Build an embedding response from provider data.
Build an error response from provider data.
Build an image generation response from provider data.
Build a moderation response from provider data.
Build a streaming chunk from provider data.
Build a response with tool calls from provider data.
Extract metadata from response data.
Extract and normalize usage data from various formats.
Functions
Build an audio transcription response from provider data.
Used for Whisper and similar audio transcription endpoints.
@spec build_chat_response(map(), String.t(), keyword()) :: ExLLM.Types.LLMResponse.t()
Build a standard chat response from provider-specific data.
Options
:calculate_cost
- Whether to calculate cost (default: true):provider
- Provider name for cost calculation
Examples
ResponseBuilder.build_chat_response(%{
"choices" => [%{"message" => %{"content" => "Hello!"}}],
"usage" => %{"prompt_tokens" => 10, "completion_tokens" => 5}
}, "gpt-4", provider: :openai)
@spec build_completion_response(map(), String.t(), keyword()) :: ExLLM.Types.LLMResponse.t()
Build a completion response (non-chat format) from provider data.
Used for providers that support traditional completion endpoints.
@spec build_embedding_response(map(), String.t(), keyword()) :: ExLLM.Types.EmbeddingResponse.t()
Build an embedding response from provider data.
Build an error response from provider data.
Build an image generation response from provider data.
Used for DALL-E and similar image generation endpoints.
Build a moderation response from provider data.
Used for content moderation endpoints.
@spec build_stream_chunk( map(), keyword() ) :: ExLLM.Types.StreamChunk.t() | nil
Build a streaming chunk from provider data.
@spec build_tool_call_response(map(), String.t(), keyword()) :: ExLLM.Types.LLMResponse.t()
Build a response with tool calls from provider data.
Extract metadata from response data.
Includes timing information, model details, and provider-specific metadata.
Extract and normalize usage data from various formats.
Maps provider-specific field names to standardized ExLLM format:
- API field names: prompt_tokens, completion_tokens (OpenAI, OpenRouter, etc.)
- ExLLM field names: input_tokens, output_tokens