openai

package
v0.260604.2 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2026 License: MPL-2.0 Imports: 11 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package openai provides OpenAI-protocol virtual models.

All concrete virtual models in this package implement VirtualModel — a base vmodel.VirtualModel extended with HandleOpenAIChat / HandleOpenAIChatStream. Models are stored in a Registry that is scoped to this protocol; lookups never cross over to Anthropic.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultStream

func DefaultStream(vm VirtualModel, req *protocol.OpenAIChatCompletionRequest, emit func(any)) error

DefaultStream is a stream adapter for batch-only OpenAI Chat models. It calls HandleOpenAIChat, chunks text content, and emits typed stream events. Batch-only models should delegate here.

func RegisterDefaults

func RegisterDefaults(r *Registry)

RegisterDefaults registers the default OpenAI-protocol virtual models into r: the OpenAI-only "virtual-gpt-4" mock plus the shared mocks ("echo-model", "ask-user-question", "ask-confirmation", "web-search-example").

Compact-style transform models are Anthropic-only and live in the anthropic sub-package; they are intentionally not registered here.

func RegisterErrorMocks added in v0.260604.1

func RegisterErrorMocks(r *Registry)

RegisterErrorMocks registers the opt-in error-injection fixtures (virtual-fail-precontent-{429,500}, virtual-fail-midstream-{close,event}) into r. These always fail and exist so consumers can simulate a broken upstream by model name without standing up an ad-hoc httptest.Server. Intentionally separate from RegisterDefaults so production registries stay clean.

func RegisterStreamTestMocks added in v0.260604.1

func RegisterStreamTestMocks(r *Registry)

RegisterStreamTestMocks registers the opt-in stream-test fixtures (virtual-stream-test, virtual-stream-test-tool) into r. These advertise deterministic, fully-populated usage (prompt / completion / cached input / reasoning) so streaming converters can be exercised end-to-end without a real upstream. Intentionally separate from RegisterDefaults so production registries stay clean.

Types

type DelayConfig

type DelayConfig struct {
	// Delay before the first SSE chunk is emitted (controls TTFT).
	MinFirstTokenDelayMs int
	MaxFirstTokenDelayMs int

	// Delay between the last content chunk and [DONE] (controls stream duration / TPS).
	MinEndDelayMs int
	MaxEndDelayMs int
}

DelayConfig configures the random delay ranges for a DelayProvider.

func DefaultDelayConfig

func DefaultDelayConfig() DelayConfig

DefaultDelayConfig returns sensible defaults: 50–500ms TTFT, 100–1000ms end delay.

type DelayProvider

type DelayProvider struct {
	// contains filtered or unexported fields
}

DelayProvider is an embedded OpenAI-compatible HTTP server with configurable random delays. It accepts any chat completions request, streams the fixed response "I am testing and random sleep", and produces realistic TTFT / TPS / latency values for metrics E2E testing.

Use it as a typ.Provider in routing rules so requests flow through the full proxy pipeline (routing → OpenAI client → metrics tracking).

func NewDelayProvider

func NewDelayProvider() *DelayProvider

NewDelayProvider creates and starts a DelayProvider with default config.

func NewDelayProviderWithConfig

func NewDelayProviderWithConfig(cfg DelayConfig) *DelayProvider

NewDelayProviderWithConfig creates and starts a DelayProvider with a custom config.

func (*DelayProvider) Close

func (dp *DelayProvider) Close()

Close shuts down the embedded HTTP server.

func (*DelayProvider) Provider

func (dp *DelayProvider) Provider(name string) *typ.Provider

Provider returns a *typ.Provider configured to route to this delay server. name is used as both UUID and Name; it must be unique within the test config.

func (*DelayProvider) URL

func (dp *DelayProvider) URL() string

URL returns the base URL of the embedded server.

type DeltaEvent

type DeltaEvent struct {
	Index   int
	Content string
}

DeltaEvent carries a text content chunk.

type DoneEvent

type DoneEvent struct {
	FinishReason string
}

DoneEvent signals end of stream with finish reason.

type MockModel

type MockModel struct {
	vmodel.BaseMockModel
	// contains filtered or unexported fields
}

MockModel is an OpenAI-Chat-only mock virtual model.

func NewMockModel

func NewMockModel(cfg *MockModelConfig) *MockModel

NewMockModel creates a MockModel. FinishReason defaults to "stop" (or "tool_calls" if ToolCall is set).

func (*MockModel) ErrorInjection added in v0.260604.1

func (m *MockModel) ErrorInjection() *vmodel.ErrorInjection

ErrorInjection implements vmodel.ErrorInjectingModel.

func (*MockModel) HandleOpenAIChat

HandleOpenAIChat returns fixed content from config in OpenAI Chat format.

func (*MockModel) HandleOpenAIChatStream

func (m *MockModel) HandleOpenAIChatStream(req *protocol.OpenAIChatCompletionRequest, emit func(any)) error

HandleOpenAIChatStream streams fixed content using configured chunks with simulated delay.

type MockModelConfig

type MockModelConfig struct {
	ID           string
	Name         string
	Description  string
	Content      string        // static: fixed response text
	FinishReason string        // default: "stop" (or "tool_calls" if ToolCall is set)
	Delay        time.Duration // simulated latency
	StreamChunks []string      // optional custom stream chunks

	// tool-type: if set, the response includes tool calls.
	ToolCall *vmodel.ToolCallConfig

	// Usage, when set, is emitted as a UsageEvent immediately before
	// DoneEvent so streaming consumers can be exercised against a
	// deterministic, fully-populated usage shape.
	Usage *vmodel.MockUsage

	// Error, when non-nil, makes this mock simulate a failure. See
	// vmodel.ErrorInjection for the two supported stages.
	Error *vmodel.ErrorInjection
}

MockModelConfig holds configuration for an OpenAI mock virtual model (static or tool).

type MockScenario

type MockScenario struct {
	ID           string
	Name         string
	Delay        time.Duration
	StreamChunks []string

	Content      string
	ToolCalls    []VToolCall
	FinishReason string // defaults to "stop" (or "tool_calls" if ToolCalls non-empty)

	// Error, when non-nil, makes the scenario simulate a failure instead of
	// returning a normal response. See vmodel.ErrorInjection for the two
	// supported stages (pre-content vs mid-stream).
	Error *vmodel.ErrorInjection
}

MockScenario describes a named test scenario with an OpenAI Chat-protocol mock response.

type Registry

Registry holds OpenAI Chat-protocol virtual models. It is a thin alias around vmodel.GenericRegistry instantiated for the OpenAI VirtualModel sub-interface.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new OpenAI virtual model registry.

type ToolEvent

type ToolEvent struct {
	Index    int
	ToolCall VToolCall
}

ToolEvent carries a tool call.

type UsageEvent added in v0.260604.1

type UsageEvent struct {
	Usage vmodel.MockUsage
}

UsageEvent carries explicit token usage that the model wants the wire stream to advertise (typically just before DoneEvent).

type VModelResponse

type VModelResponse struct {
	Content      string
	ToolCalls    []VToolCall
	FinishReason string
}

VModelResponse describes what the virtual model wants to respond for OpenAI Chat.

type VToolCall

type VToolCall struct {
	ID        string
	Name      string
	Arguments string // JSON-encoded arguments
}

VToolCall is a protocol-agnostic tool call returned by OpenAI Chat models. Using an intermediate type avoids coupling to the OpenAI SDK in this package; callers (e.g. virtualserver) translate it to the SDK form when serializing.

type VirtualModel

type VirtualModel interface {
	vmodel.VirtualModel
	HandleOpenAIChat(req *protocol.OpenAIChatCompletionRequest) (VModelResponse, error)
	HandleOpenAIChatStream(req *protocol.OpenAIChatCompletionRequest, emit func(any)) error
}

VirtualModel handles OpenAI Chat Completions requests.

func NewMockFromScenario

func NewMockFromScenario(s *MockScenario) VirtualModel

NewMockFromScenario creates an OpenAI Chat VirtualModel from a MockScenario.

Jump to

Keyboard shortcuts

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