openai

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

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

View Source
const (
	AuthBearer = httpjson.AuthBearer
	AuthAPIKey = httpjson.AuthAPIKey
)
View Source
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.

View Source
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

type AuthStyle = httpjson.Auth

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.

func (*Embedder) Embed

func (e *Embedder) Embed(ctx context.Context, texts []string) ([][]float32, error)

Embed returns one vector per input text, in input order. Empty input makes no request. Results are reordered by the API's reported index, and every vector is checked against the configured dimensionality.

func (*Embedder) Space

Space reports the embedding space this embedder produces.

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.

func NewVerifier added in v1.0.0

func NewVerifier(baseURL, apiKey, model string, structured bool, auth AuthStyle, httpClient *http.Client) (*Verifier, error)

NewVerifier constructs a Verifier over the chat endpoint. structured should match the chat provider's StructuredOutput capability.

func (*Verifier) Verify added in v1.0.0

func (v *Verifier) Verify(ctx context.Context, claim, evidence string) (app.Verdict, error)

Verify asks the model whether evidence supports claim. An empty evidence string is treated as unsupported without a model call (nothing can entail a claim).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL