Documentation
¶
Overview ¶
Package llm provides a Starlark module that calls OpenAI models.
Configuration options:
- openai_provider: Provider type (openai, azure, anthropic)
- openai_endpoint_url: API endpoint URL
- openai_api_key: API key for authentication
- openai_gpt_model: Default GPT model name
- openai_dalle_model: Default DALL-E model name
- api_version: API version (used by Azure and Anthropic)
- legacy_mode: Use legacy mode for data conversion (default: true)
- request_timeout: Per-request timeout in seconds (default: 120). Total deadline for blocking chat/draw; connect+first-response bound for streaming
The historical constructors let a script set the provider, endpoint, and key. Hosts running untrusted scripts can opt into NewModuleWithHostPolicy to bind those values, reject cross-origin redirects, and optionally jail image_file reads. See docs/API.md "Safety / trust model".
The chat function supports both blocking and streaming modes:
- In blocking mode (default), the function waits for the complete response
- In streaming mode (stream=True), responses are received incrementally and can be processed via a callback
- Streaming mode can improve responsiveness for large responses or when displaying partial results is desired
- To use streaming mode, set stream=True and optionally provide stream_callback=function
- The stream_callback receives each chunk of the response as it arrives
- In both streaming and blocking modes, the function returns the same format: either the complete content or full response
- For streaming, the content is automatically aggregated from all chunks
Custom parameters support:
- The kwargs parameter allows passing custom or non-standard parameters to the API
- Useful for provider-specific features, experimental parameters, or custom deployments
- Any dictionary passed as kwargs will be included in the ChatTemplateKwargs field of the API request
Token parameters for different models:
- max_tokens: Maximum number of tokens to generate; 0 (the default) leaves it unset so the API applies its own default - works with most models
- max_completion_tokens: Upper bound for generated completion tokens - for o1 series models
- For o1, o3, o4 series models, use max_completion_tokens instead of max_tokens
When legacy_mode is true (default), response objects are converted using direct struct access (ConvertJSONStruct). When false, JSON conversion is used (GoToStarlarkViaJSON).
Index ¶
Constants ¶
const ( // ProviderOpenAI represents the default OpenAI API provider ProviderOpenAI = "openai" // ProviderAzure represents the Azure OpenAI Service provider ProviderAzure = "azure" // ProviderAnthropic represents the Anthropic Claude API provider ProviderAnthropic = "anthropic" )
Provider type constants
const ModuleName = "llm"
ModuleName defines the expected name for this module when used in Starlark's load() function, e.g., load('llm', 'chat')
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HostPolicy ¶ added in v0.1.1
HostPolicy binds host-provided credentials and file access to a fixed endpoint. It is opt-in through NewModuleWithHostPolicy; NewModule and NewModuleWithConfig retain their historical self-configuring behavior.
Provider, EndpointURL, and APIKey are host-only in the policy constructor, so a script cannot redirect a host credential. Redirects must stay on the same scheme, host, and port as EndpointURL. FileRoot is resolved once when the module is constructed; image_file paths are anchored to it and symlinks that leave it are rejected. An empty FileRoot denies image_file inputs.
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module wraps the ConfigurableModule with specific functionality for calling OpenAI models.
func NewModule ¶
func NewModule() *Module
NewModule creates a new instance of Module with default empty configurations.
func NewModuleWithConfig ¶
func NewModuleWithConfig(serviceProvider, endpointURL, apiKey, gptModel, dalleModel, apiVersion string) *Module
NewModuleWithConfig creates a new instance of Module with the given configuration values.
func NewModuleWithHostPolicy ¶ added in v0.1.1
func NewModuleWithHostPolicy(policy HostPolicy) *Module
NewModuleWithHostPolicy creates a module for a host-controlled endpoint and credential. The provider, endpoint, and API key cannot be changed by Starlark. Models and ordinary request options remain script-settable, which preserves the module's small self-configuring surface without exposing the host credential to a script-selected endpoint.
FileRoot enables image_file only beneath the supplied directory. Relative paths are resolved from that root. Leave it empty to deny host file reads. Invalid policy values are reported when the module loader or a request is used, matching the existing deferred configuration-validation contract.
func (*Module) LoadModule ¶
func (m *Module) LoadModule() starlet.ModuleLoader
LoadModule returns the Starlark module loader exposing the llm builtins (message, chat, draw) plus the generated config setters/getters.