Documentation
¶
Overview ¶
Package interactions is a streaming LLM service backed by Google's Gemini Interactions API. It consumes an LLMContextFrame and emits the response as LLM response frames, like every other jargo LLM service.
It takes the same settings as the Gemini service (package gemini), and the two read each other's thought signatures from the conversation, so a conversation can move between them.
Each request carries the full conversation with store set to false, so Google keeps no copy of it. Sending only the new turns, with previous_interaction_id, would need store set to true, which retains every interaction on Google's servers for days; the Interactions API has no connection-local alternative like the OpenAI Responses WebSocket mode.
The Vertex AI variant is vertex.NewInteractionsLLM.
Index ¶
- Constants
- func ToInteractionsTools(tools []map[string]any) []map[string]any
- type Adapter
- func (*Adapter) IDForLLMSpecificMessages() string
- func (a *Adapter) LLMInvocationParams(convo *frames.LLMContext, opts adapter.Options) (Params, error)
- func (a *Adapter) MessagesForLogging(convo *frames.LLMContext) []map[string]any
- func (a *Adapter) ToProviderToolsFormat(schema frames.ToolsSchema) []map[string]any
- type Config
- type LLMService
- func (s *LLMService) Generate(ctx context.Context, convo *frames.LLMContext, emit llm.Emit) error
- func (s *LLMService) GenerateWithTools(ctx context.Context, convo *frames.LLMContext, sink llm.Sink) error
- func (s *LLMService) LLMAdapter() llm.BuiltinToolHolder
- func (s *LLMService) MessagesForLogging(convo *frames.LLMContext) []map[string]any
- func (s *LLMService) ModelSupportsResponseSchema(string) bool
- func (s *LLMService) RunInference(ctx context.Context, convo *frames.LLMContext, opts llm.InferenceOptions) (string, error)
- func (s *LLMService) Settings() any
- func (s *LLMService) SupportsResponseSchema() bool
- func (s *LLMService) ToolsForLogging(schema frames.ToolsSchema) []any
- type LLMSettings
- type Params
- type RequestShaper
- type SafetySetting
- type ThinkingConfig
Constants ¶
const ( StepUserInput = "user_input" StepModelOutput = "model_output" StepFunctionCall = "function_call" StepFunctionResult = "function_result" StepThought = "thought" )
The step types of an Interactions input.
Variables ¶
This section is empty.
Functions ¶
func ToInteractionsTools ¶ added in v1.0.0
ToInteractionsTools converts tools in Gemini's generateContent format to the Interactions API's.
A function_declarations entry becomes one function tool per declaration, and a built-in tool keyed by its name, such as {"google_search": {}}, becomes that tool's type. A tool already carrying a type passes through as it is.
Types ¶
type Adapter ¶ added in v1.0.0
Adapter converts a universal conversation into an Interactions request. The zero value is ready to use.
func (*Adapter) IDForLLMSpecificMessages ¶ added in v1.0.0
IDForLLMSpecificMessages implements adapter.LLMAdapter. It is the Gemini adapter's, so the thought signatures either Gemini service stores serve both.
func (*Adapter) LLMInvocationParams ¶ added in v1.0.0
func (a *Adapter) LLMInvocationParams(convo *frames.LLMContext, opts adapter.Options) (Params, error)
LLMInvocationParams converts the conversation into the parameters of an Interactions request.
The steps always end on the user's side: the API rejects an input that ends with a model turn. The system instruction is carried beside the steps, so an instruction given for this call replaces the conversation's own prompt.
func (*Adapter) MessagesForLogging ¶ added in v1.0.0
func (a *Adapter) MessagesForLogging(convo *frames.LLMContext) []map[string]any
MessagesForLogging implements adapter.LLMAdapter: the conversation's steps, with media data and thought signatures replaced with "...".
func (*Adapter) ToProviderToolsFormat ¶ added in v1.0.0
func (a *Adapter) ToProviderToolsFormat(schema frames.ToolsSchema) []map[string]any
ToProviderToolsFormat implements adapter.LLMAdapter, rendering the tools in the Interactions API's format. Each standard tool becomes a function tool, with its schema adapted to the subset Gemini accepts, and the custom tools written for Gemini are included.
type Config ¶
type Config struct {
// APIKey is the Gemini API key. Required.
APIKey string `validate:"required"`
// Model is the model id; empty uses a low-latency flash default.
Model string
// MaxTokens caps the response length; 0 uses 4096.
MaxTokens int
// Temperature is the sampling temperature (0.0 to 2.0); nil omits it.
Temperature *float64
// TopP is the nucleus-sampling parameter (0.0 to 1.0); nil omits it.
TopP *float64
// TopK is accepted for parity with the Gemini service, and ignored with a
// warning: the Interactions API has no top-k.
TopK *int
// SafetySettings are the content-safety filters, one per harm category.
// Empty sends none, leaving every category at the API's default.
SafetySettings []SafetySetting `validate:"omitempty,dive"`
// Thinking controls the model's internal reasoning: Level and
// IncludeThoughts apply, and a Budget is ignored. If it is nil, the service
// sends the lowest thinking level Google documents for the model, for the
// fastest responses, and logs what it sent. Models it does not list, and
// every model when the use_provider_thinking_default setting is set, run at
// Google's default.
Thinking *ThinkingConfig `validate:"omitempty"`
// Seed makes sampling repeatable; nil omits it.
Seed *int
// Tools are offered to the model when the conversation advertises none, in
// Gemini's generateContent format ({"function_declarations": [...]},
// {"google_search": {}}) or the Interactions API's ({"type": ...}).
Tools []map[string]any
// ToolConfig configures tool use, in Gemini's generateContent format
// ({"function_calling_config": {"mode": "ANY", "allowed_function_names":
// [...]}}). It is sent as the Interactions API's tool_choice.
ToolConfig map[string]any
// StreamIdleTimeout bounds how long to wait for the next event of a
// streamed response before giving up on it. It bounds the wait when the API
// accepts a request and then stops producing without closing the stream.
// Nil uses 20 seconds and a zero value waits indefinitely.
StreamIdleTimeout *time.Duration
// RetryOnTimeout re-issues the request once when its first event does not
// arrive within RetryTimeout.
RetryOnTimeout bool
// RetryTimeout is how long the first event is waited for before the request
// is re-issued; 0 uses 5 seconds. It applies only with RetryOnTimeout.
RetryTimeout time.Duration `validate:"omitempty,min=0"`
// Extra sets additional request fields not modeled above, applied to every
// request. A generation_config entry holding an object is merged into the
// generation configuration field by field; any other entry replaces the
// request field of its name.
Extra map[string]any
}
Config configures the Interactions LLM service. It holds the same settings as the Gemini service's, so a service's settings carry over between the two. The Interactions API has no top-k, frequency penalty or presence penalty, and controls thinking by level only; a setting it has no field for is ignored with a warning.
type LLMService ¶ added in v1.0.0
LLMService is a streaming Gemini LLM processor using the Interactions API.
func NewLLM ¶
func NewLLM(cfg Config) *LLMService
NewLLM builds an Interactions LLM service on the Gemini API.
func NewShapedLLM ¶
func NewShapedLLM(name string, shaper RequestShaper, cfg Config, opts ...llm.Option) *LLMService
NewShapedLLM builds an Interactions LLM service whose requests are addressed and authorized by shaper. It is the base for deployments that do not use the Gemini API's own URL or api-key auth; name is the processor label.
func (*LLMService) Generate ¶ added in v1.0.0
func (s *LLMService) Generate(ctx context.Context, convo *frames.LLMContext, emit llm.Emit) error
Generate streams a response to the conversation, emitting each text delta. The conversation advertises no tools, but the service's own may still be offered, and the calls the model makes to them are run.
func (*LLMService) GenerateWithTools ¶ added in v1.0.0
func (s *LLMService) GenerateWithTools(ctx context.Context, convo *frames.LLMContext, sink llm.Sink) error
GenerateWithTools streams a tool-capable response, emitting text deltas to the sink as they arrive and reporting each function call the model made once the response is complete.
func (*LLMService) LLMAdapter ¶ added in v1.0.0
func (s *LLMService) LLMAdapter() llm.BuiltinToolHolder
LLMAdapter returns the adapter this service converts through, so the base can add the tools it implements itself to what every request advertises. It implements llm.AdapterHolder.
func (*LLMService) MessagesForLogging ¶ added in v1.0.0
func (s *LLMService) MessagesForLogging(convo *frames.LLMContext) []map[string]any
MessagesForLogging renders the conversation as this provider will see it, for the generation span. It implements llm.TraceRenderer.
func (*LLMService) ModelSupportsResponseSchema ¶ added in v1.0.0
func (s *LLMService) ModelSupportsResponseSchema(string) bool
ModelSupportsResponseSchema implements llm.ResponseSchemaSupporter: every model the Interactions API serves can enforce a response schema.
func (*LLMService) RunInference ¶ added in v1.0.0
func (s *LLMService) RunInference( ctx context.Context, convo *frames.LLMContext, opts llm.InferenceOptions, ) (string, error)
RunInference answers the conversation once, off to the side of the pipeline: no streaming, no frames, just the text. It implements llm.Inferencer.
func (*LLMService) Settings ¶ added in v1.0.0
func (s *LLMService) Settings() any
Settings returns the service's runtime-updatable settings, implementing llm.SettingsHolder.
func (*LLMService) SupportsResponseSchema ¶ added in v1.0.0
func (s *LLMService) SupportsResponseSchema() bool
SupportsResponseSchema implements llm.ResponseSchemaSupporter.
func (*LLMService) ToolsForLogging ¶ added in v1.0.0
func (s *LLMService) ToolsForLogging(schema frames.ToolsSchema) []any
ToolsForLogging renders the toolset as this provider will see it, for the generation span. It implements llm.TraceRenderer.
type LLMSettings ¶
type LLMSettings = gemini.LLMSettings
LLMSettings is the part of the configuration that can change while the pipeline runs, through an LLMUpdateSettingsFrame. They are the Gemini service's settings, so a service's settings carry over between the two Gemini services. Every request is built from them, so an update reaches the next request.
type Params ¶ added in v1.0.0
Params is what one Interactions request takes from the conversation: the system instruction carried beside the steps, the steps themselves, and the tools to advertise.
type RequestShaper ¶
type RequestShaper interface {
// Endpoint returns the URL interactions are created at.
Endpoint() string
// Authorize sets the authorization headers on req. It takes a context
// because a scheme may have to mint or refresh a token to do so.
Authorize(ctx context.Context, req *http.Request) error
}
RequestShaper says where an Interactions request is sent and how it is authorized, so a deployment with a different URL layout or auth scheme (Vertex AI, which addresses the API per project and location and authorizes with an OAuth token) can reuse this implementation. The default shaper targets the Gemini API with an api-key header.
type SafetySetting ¶
type SafetySetting = gemini.SafetySetting
SafetySetting is one content-safety filter, in the Gemini service's form. It is sent in the Interactions API's: HARM_CATEGORY_HARASSMENT becomes harassment, BLOCK_ONLY_HIGH becomes block_only_high, and so on.
type ThinkingConfig ¶
type ThinkingConfig = gemini.ThinkingConfig
ThinkingConfig controls the model's internal reasoning. The Interactions API controls thinking by level only: Level and IncludeThoughts apply, and a Budget is ignored with a warning.