testllm

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package testllm is a fake model service for tests: the model-list endpoint and a scripted, streaming chat endpoint for each protocol (openai, openai-responses, anthropic). Requests a real service would reject get a 400 and are marked.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Call

type Call struct {
	ID, Name string
	Args     string // JSON object
}

Call is a tool call; an empty ID gets "call_<n>".

type Request

type Request struct {
	System, Messages, Tools json.RawMessage
	Header                  http.Header
	Model, Path, Invalid    string
	Body                    json.RawMessage
}

Request is the raw JSON of one chat request. For openai-responses System is the instructions and Messages the input. Invalid says why the fake rejected it with a 400; empty means it was answered from the script.

type Server

type Server struct {
	URL string // base URL, as a user would enter it

	Mux *http.ServeMux
	// contains filtered or unexported fields
}

func Start

func Start(t *testing.T, kind string) *Server

Start serves the endpoints of kind ("openai", "openai-responses" or "anthropic").

func (*Server) Advance

func (s *Server) Advance()

Advance drops the current turn, e.g. a Status turn that keeps answering.

func (*Server) Headers

func (s *Server) Headers() []http.Header

Headers returns the headers of every request so far.

func (*Server) Release

func (s *Server) Release()

Release lets hanging turns finish. It is safe to call more than once.

func (*Server) Requests

func (s *Server) Requests() []Request

Requests returns every chat request so far.

func (*Server) RequireKey

func (s *Server) RequireKey(k string)

func (*Server) Script

func (s *Server) Script(turns ...Turn)

Script appends turns; once they run out every request gets the text "ok".

func (*Server) SetDisplayNames added in v1.1.0

func (s *Server) SetDisplayNames(n map[string]string)

SetDisplayNames lists models the way a ChatGPT subscription token sees them: models[].slug with display_name.

func (*Server) SetHidden added in v1.1.0

func (s *Server) SetHidden(m ...string)

func (*Server) SetListStatus

func (s *Server) SetListStatus(code int)

SetListStatus makes the model list answer with that HTTP status; 0 restores it.

func (*Server) SetModels

func (s *Server) SetModels(m ...string)

type Turn

type Turn struct {
	Text       []string
	Calls      []Call
	Hang       bool
	Status     int
	Code       string // openai-responses: the error.code a Status turn's body carries
	Finish     string
	Reasoning  string
	Fail       string
	FailCode   string
	Incomplete bool
	Truncate   bool
	Refusal    string // openai-responses: the reply is this refusal, not text
}

Turn is one scripted model response. Status != 0 answers with that HTTP error (its body echoes the key the request carried). Hang streams the text, then waits for Release or the client to go away. Finish, if set, is the raw finish reason sent (OpenAI finish_reason, Anthropic stop_reason).

openai-responses only: Reasoning adds a reasoning item with that encrypted_content; Fail ends with response.failed carrying that message, FailCode sets its error.code (default server_error), Incomplete with response.incomplete, Truncate with no terminal event. A Status turn keeps answering (the SDK's retries) until Advance or the next Script call.

Jump to

Keyboard shortcuts

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