Documentation
¶
Overview ¶
Package openai adapts the Embedder and Generator ports to any OpenAI-compatible HTTP API (OpenAI, Ollama's /v1, vLLM, LM Studio, OpenRouter). Configuration is a base URL, an optional API key, and model names. The HTTP plumbing (POST, retry, auth) lives in the shared internal/adapters/httpjson package.
Index ¶
- Constants
- type AuthStyle
- type Capabilities
- type Embedder
- type Generator
- func (g *Generator) Synthesize(ctx context.Context, question string, hits []domain.ChunkHit, ...) (app.Answer, error)
- func (g *Generator) SynthesizeDeterministic(ctx context.Context, question string, hits []domain.ChunkHit, ...) (app.Answer, error)
- func (g *Generator) SynthesizeStream(ctx context.Context, question string, hits []domain.ChunkHit, ...) (app.Answer, error)
- type Verifier
Constants ¶
const ( AuthBearer = httpjson.AuthBearer AuthAPIKey = httpjson.AuthAPIKey )
const PromptVersion = "1"
PromptVersion identifies the prompt/contract this Generator emits answers under. The composition root folds it into the answer-cache salt so a prompt change invalidates cached answers rather than serving ones the new prompt would not have produced. Bump it whenever systemPrompt/structuredInstruction or the citation contract changes meaningfully.
const VerifierPromptVersion = "1"
VerifierPromptVersion identifies the entailment prompt/contract. The composition root folds it into the verdict-cache salt so a prompt change invalidates cached verdicts. Bump it when verifierSystemPrompt or the verdict contract changes.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AuthStyle ¶
AuthStyle and its values alias the shared httpjson auth scheme so existing callers (the composition root, NewEmbedder/NewGenerator) keep using openai.AuthBearer / openai.AuthAPIKey unchanged.
type Capabilities ¶
type Capabilities struct {
StructuredOutput bool // JSON-schema response_format
ImageInput bool // image attachments via image_url
DocumentInput bool // document attachments via a file part
}
Capabilities declares what the configured provider supports beyond plain chat. They are config-declared, not probed: a capability set here that the backend lacks surfaces as an error at call time.
type Embedder ¶
type Embedder struct {
// contains filtered or unexported fields
}
Embedder turns texts into vectors via an OpenAI-compatible /embeddings endpoint. Its EmbeddingSpace (model + dimensions) is fixed at construction so Space is a cheap, network-free check the use cases can run before any work.
func NewEmbedder ¶
func NewEmbedder(baseURL, apiKey, model string, dimensions int, auth AuthStyle, httpClient *http.Client) (*Embedder, error)
NewEmbedder constructs an Embedder. dimensions pins the space the operator expects; Embed verifies the API returns vectors of that size. auth selects the API-key scheme (AuthBearer for OpenAI, AuthAPIKey for Azure). A nil httpClient uses http.DefaultClient.
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator synthesizes grounded answers via an OpenAI-compatible /chat/completions endpoint. With StructuredOutput it requests JSON-schema output and reads the model's own citations; otherwise it returns plain text and cites the whole grounding set. Attachments are encoded into the user message when the matching capability is set.
func NewGenerator ¶
func NewGenerator(baseURL, apiKey, model string, caps Capabilities, auth AuthStyle, httpClient *http.Client) (*Generator, error)
NewGenerator constructs a Generator. caps must reflect only what the provider actually supports. auth selects the API-key scheme (AuthBearer for OpenAI, AuthAPIKey for Azure). A nil httpClient uses http.DefaultClient.
func (*Generator) Synthesize ¶
func (g *Generator) Synthesize(ctx context.Context, question string, hits []domain.ChunkHit, attachments []domain.Attachment) (app.Answer, error)
Synthesize asks the model to answer the question grounded in the given hits and any attachments.
func (*Generator) SynthesizeDeterministic ¶ added in v1.0.0
func (g *Generator) SynthesizeDeterministic(ctx context.Context, question string, hits []domain.ChunkHit, attachments []domain.Attachment) (app.Answer, error)
SynthesizeDeterministic is Synthesize with the generation pinned for reproducibility — temperature 0 and a fixed seed — recording the pinned values in the answer's Provenance. It satisfies app.DeterministicGenerator, the seam the audited `ask --reproducible` path requires.
func (*Generator) SynthesizeStream ¶
func (g *Generator) SynthesizeStream(ctx context.Context, question string, hits []domain.ChunkHit, attachments []domain.Attachment, onDelta func(string)) (app.Answer, error)
SynthesizeStream is the streaming form of Synthesize: it emits the model's prose to onDelta as server-sent chunks arrive, then returns the same canonical Answer Synthesize would (the model's inline [n] rewritten to [chunkID], with citations). Structured-output mode cannot stream prose (the response is JSON), so it degrades to a buffered Synthesize and emits the whole answer once.
type Verifier ¶ added in v1.0.0
type Verifier struct {
// contains filtered or unexported fields
}
Verifier checks claim/evidence entailment via an OpenAI-compatible /chat/completions endpoint, reusing the chat model and client. With structuredOutput it requests JSON-schema output; otherwise it parses the JSON object out of the model's text. It shares the chat connection (decision-60 split endpoints), so verification runs against whatever serves chat.