pig/ai/openai
Types
Configuration for an OpenAI-compatible provider.
pub type OpenAIConfig {
OpenAIConfig(
api_key: String,
model: String,
base_url: String,
http_timeout_ms: Int,
)
}
Constructors
-
OpenAIConfig( api_key: String, model: String, base_url: String, http_timeout_ms: Int, )
An OpenAI-compatible provider wrapping a callable function with its config.
pub type OpenAIProvider {
OpenAIProvider(
config: OpenAIConfig,
call: fn(
List(message.Message),
List(tool_definition.ToolDefinition),
) -> Result(provider.InferenceResult, error.AiError),
)
}
Constructors
-
OpenAIProvider( config: OpenAIConfig, call: fn( List(message.Message), List(tool_definition.ToolDefinition), ) -> Result(provider.InferenceResult, error.AiError), )
The function signature for a provider call.
pub type ProviderFn =
fn(List(message.Message), List(tool_definition.ToolDefinition)) -> Result(
provider.InferenceResult,
error.AiError,
)
Values
pub fn build_request_body(
messages: List(message.Message),
tools: List(tool_definition.ToolDefinition),
model: String,
) -> String
Build the JSON request body for the OpenAI Chat Completions API. Pure function — no IO.
pub const default_http_timeout_ms: Int
The default HTTP timeout for OpenAI API calls (120 seconds).
pub fn parse_response(
raw: String,
) -> Result(provider.InferenceResult, error.AiError)
Parse an OpenAI Chat Completions JSON response into an InferenceResult. Pure function — no IO.
pub fn provider(api_key: String, model: String) -> OpenAIProvider
Create a provider with the default OpenAI base URL.
pub fn provider_with_base_url(
api_key: String,
model: String,
base_url: String,
) -> OpenAIProvider
Create a provider with a custom base URL (for Ollama, Together, etc).
pub fn provider_with_base_url_and_timeout(
api_key: String,
model: String,
base_url: String,
http_timeout_ms: Int,
) -> OpenAIProvider
Create a provider with a custom base URL and HTTP timeout.
pub fn with_http_timeout(
provider: OpenAIProvider,
timeout_ms: Int,
) -> OpenAIProvider
Set the HTTP timeout in milliseconds on an OpenAI provider.