Documentation
¶
Overview ¶
Package chatvllm is the vLLM provider for gitlab.com/phpboyscout/go/chat. It speaks vLLM's OpenAI-compatible routes on its own HTTP client, and adds what only vLLM serves: log-probabilities, answer scoring, model and LoRA adapter discovery, and loading adapters at runtime.
Index ¶
- Constants
- Variables
- func LoadAdapter(ctx context.Context, cfg chat.Config, name, path string) error
- func New(ctx context.Context, settings chat.Settings, opts ...Option) (chat.ChatClient, error)
- func UnloadAdapter(ctx context.Context, cfg chat.Config, name string) error
- type APIError
- type Client
- func (c *Client) Add(ctx context.Context, prompt string, media ...chat.Media) error
- func (c *Client) AddCached(ctx context.Context, prompt string, media ...chat.Media) error
- func (c *Client) ApplyPolicyNow(ctx context.Context, policy chat.HistoryPolicy) (int, error)
- func (c *Client) Ask(ctx context.Context, question string, target any, media ...chat.Media) error
- func (c *Client) Chat(ctx context.Context, prompt string, media ...chat.Media) (string, error)
- func (c *Client) ChatWithLogprobs(ctx context.Context, prompt string, top int, media ...chat.Media) (Scored, error)
- func (c *Client) ChatWithTokenLogprobs(ctx context.Context, prompt string, ids []int, media ...chat.Media) (TokenScores, error)
- func (c *Client) History() chat.History
- func (c *Client) Likelihood(ctx context.Context, prompt, answer string) (Likelihood, error)
- func (c *Client) Model() string
- func (c *Client) ProposeToolCalls(ctx context.Context, prompt string, tools []chat.Tool, media ...chat.Media) (Proposal, error)
- func (c *Client) Provider() chat.Provider
- func (c *Client) Restore(snapshot *chat.Snapshot) error
- func (c *Client) Save() (*chat.Snapshot, error)
- func (c *Client) SetTools(tools []chat.Tool) error
- func (c *Client) StreamChat(ctx context.Context, prompt string, callback chat.StreamCallback, ...) (string, error)
- func (c *Client) SummariserFactory() chat.ProviderFactory
- func (c *Client) SupportsSampling()
- func (c *Client) SupportsStateless()
- type IDLogprob
- type Likelihood
- type Model
- type Option
- type PositionScores
- type Proposal
- type Scored
- type TokenLogprob
- type TokenScores
- type ToolCall
Constants ¶
const EnvVLLMKey = "VLLM_API_KEY"
EnvVLLMKey is the environment variable read for the server's API key when neither Config.Credential nor Config.Token is set. Unset, no key is sent, since vLLM authenticates only when started with --api-key (spec 0035 D4).
Variables ¶
var ErrAdapterNotLoaded = errors.NewSentinel("chatvllm.adapter_not_loaded",
"no adapter is loaded under that name")
ErrAdapterNotLoaded is returned when unloading a name the server has no adapter loaded under.
var ErrBaseURLPath = errors.NewSentinel("chatvllm.base_url_path",
"vLLM base URL must name the server's /v1 root, such as https://host:8000/v1")
ErrBaseURLPath is returned when Config.BaseURL does not end in /v1.
ErrLogprobsUnavailable is chat.ErrLogprobsUnavailable, so errors.Is matches either.
var ErrRuntimeAdaptersDisabled = errors.NewSentinel("chatvllm.runtime_adapters_disabled",
"runtime adapter loading is not enabled on this server")
ErrRuntimeAdaptersDisabled is returned when the server has no adapter routes, because it was started without VLLM_ALLOW_RUNTIME_LORA_UPDATING.
Functions ¶
func LoadAdapter ¶
LoadAdapter loads the LoRA adapter at path, a directory or Hugging Face repository the server can read, under name. An adapter already loaded under name from the same path is left as it is, so loading twice is not an error.
Types ¶
type APIError ¶
type APIError struct {
// StatusCode is the HTTP status.
StatusCode int
// Type is vLLM's error type, such as NotFoundError, when it sent one.
Type string
// Param names the request field at fault, when vLLM said.
Param string
// Message is the server's own account of the failure.
Message string
}
APIError is a non-2xx answer from the vLLM server, decoded from either of its error bodies.
type Client ¶
type Client struct {
chat.UsageTracker
// contains filtered or unexported fields
}
Client is a chat.ChatClient for a vLLM server.
func (*Client) AddCached ¶
AddCached appends a user turn and pins it, so a history policy cannot drop the prefix vLLM's automatic prefix caching keeps hitting. No cache key is sent (spec 0035 D11).
func (*Client) ApplyPolicyNow ¶
ApplyPolicyNow bounds the retained conversation now rather than before the next request. A nil policy means the configured one; with none, it does nothing.
func (*Client) Ask ¶
Ask sends a question and decodes the answer into target, as JSON when Config.ResponseSchema is set and as text otherwise. It offers no tools.
func (*Client) ChatWithLogprobs ¶
func (c *Client) ChatWithLogprobs(ctx context.Context, prompt string, top int, media ...chat.Media) (Scored, error)
ChatWithLogprobs sends prompt after the system prompt alone and returns the reply with each token's log-probability and up to top alternatives. top is 0 to 20, or -1 for the whole vocabulary on a server started with --max-logprobs -1. It offers no tools and neither reads nor records history.
func (*Client) ChatWithTokenLogprobs ¶
func (c *Client) ChatWithTokenLogprobs(ctx context.Context, prompt string, ids []int, media ...chat.Media) (TokenScores, error)
ChatWithTokenLogprobs returns the log-probability of each named token ID at every generated position. ids holds 1 to 128 distinct, non-negative IDs; the server rejects one outside its vocabulary. It offers no tools and neither reads nor records history.
func (*Client) History ¶
History reports the turns this client will re-send, the system turn and any buffered Add turns included. Known is true: this client owns its transcript.
func (*Client) Likelihood ¶
Likelihood returns the log-probability of each token of answer as the assistant's reply to prompt, under the server's chat template, without generating. It neither reads nor records history.
It makes two requests: /tokenize on the conversation with an empty answer gives where the answer starts, and one chat request with prompt_logprobs scores it. Only the second is counted in usage.
func (*Client) ProposeToolCalls ¶
func (c *Client) ProposeToolCalls(ctx context.Context, prompt string, tools []chat.Tool, media ...chat.Media) (Proposal, error)
ProposeToolCalls offers tools to the model and returns the calls it makes, in order, without running them. The tools must carry no Handler. It sends prompt after the system prompt alone, with tool_choice auto, and neither reads nor records history.
func (*Client) Restore ¶
Restore replaces the conversation with a snapshot Save wrote. It waits its turn, so a call in flight cannot commit over the restored conversation.
func (*Client) Save ¶
Save captures the conversation. A stateless client keeps none, so its snapshot carries only the system turn.
func (*Client) StreamChat ¶
func (c *Client) StreamChat(ctx context.Context, prompt string, callback chat.StreamCallback, media ...chat.Media) (string, error)
StreamChat is Chat with the reply delivered to callback as it arrives.
func (*Client) SummariserFactory ¶ added in v0.4.0
func (c *Client) SummariserFactory() chat.ProviderFactory
SummariserFactory implements chat.SummariserFactory with the registry factory: every option this module owns shapes output, so a summariser carries none of them. See https://gitlab.com/phpboyscout/go/chat/-/wikis/specs/0040-summariser-on-the-callers-credentials.
func (*Client) SupportsSampling ¶
func (c *Client) SupportsSampling()
SupportsSampling marks Client as able to carry Config.Temperature and TopP.
func (*Client) SupportsStateless ¶
func (c *Client) SupportsStateless()
SupportsStateless marks Client as honouring chat.Config.Stateless.
type Likelihood ¶
type Likelihood struct {
Scored string
Tokens []TokenLogprob
Sum float64
}
Likelihood is the log-probability of an answer, token by token. Scored is the answer as the chat template rendered it, which may trim its edges.
type Model ¶
type Model struct {
ID string
Parent string // empty for a base model
MaxModelLen int // zero for an adapter
}
Model is one model a server serves: a base model or a LoRA adapter on one.
type Option ¶
type Option func(*options)
Option configures a Client at construction with a knob only vLLM understands. Each becomes a field on every chat request.
func WithChatTemplateKwargs ¶
WithChatTemplateKwargs passes arguments to the model's chat template, such as enable_thinking. Later calls add to, and override, earlier ones.
func WithRepetitionPenalty ¶
WithRepetitionPenalty penalises tokens already in the prompt or reply; 1 is no penalty.
type PositionScores ¶
PositionScores is one generated position: the token chosen there, and each named ID in the order requested.
type Proposal ¶
Proposal and ToolCall are the core's, kept under these names so callers written before the core took them over still compile.
type Scored ¶
Scored and TokenLogprob are the core's, kept under these names so callers written before the core took them over still compile.
type TokenLogprob ¶
type TokenLogprob = chat.TokenLogprob
Scored and TokenLogprob are the core's, kept under these names so callers written before the core took them over still compile.
type TokenScores ¶
type TokenScores struct {
Text string
Positions []PositionScores
}
TokenScores is a reply with the log-probability of each named token ID at every generated position.