sglang

package
v0.0.0-...-648dfe1 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: Apache-2.0, BSD-3-Clause, MIT Imports: 9 Imported by: 0

Documentation

Overview

Package sglang provides an SGLang API client.

Index

Constants

View Source
const (
	// Port is the port used by the sglang server.
	Port = 30000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type MetaInfo

type MetaInfo struct {
	// E2ELatency only exists in the last token.
	E2ELatency float64 `json:"e2e_latency"`
}

MetaInfo contains the meta information about the response. Every token has a meta info.

type ModelLoadStats

type ModelLoadStats struct {
	// ClientReportedDuration is the duration to load the model as perceived
	// by the client, measured by HTTP client metrics.
	ClientReportedDuration time.Duration
}

ModelLoadStats holds metrics about the model loading process.

type Prompt

type Prompt struct {

	// Text is the prompt string.
	// Common leading whitespace will be removed.
	Text string

	// Options maps parameter names to JSON-compatible values.
	Options map[string]any
	// contains filtered or unexported fields
}

Prompt is an sglang prompt.

func ZeroTemperaturePrompt

func ZeroTemperaturePrompt(text string) *Prompt

ZeroTemperaturePrompt returns a Prompt with the given text and an initial temperature setting of zero. This setting allows for consistent settings.

func (*Prompt) AddImage

func (p *Prompt) AddImage(data []byte) *Prompt

AddImage attaches an image to the prompt. Returns itself for chainability.

func (*Prompt) CleanQuery

func (p *Prompt) CleanQuery() string

CleanQuery removes common whitespace from query lines, and all leading/ending whitespace-only lines. It is useful to be able to specify query string as indented strings without breaking visual continuity in Go code. For example (where dots are spaces):

"""\n ..The Quick Brown Fox\n ..Jumps Over\n ....The Lazy Dog\n ."""

becomes: Jumps Over\n ..The Lazy Dog"""

func (*Prompt) Copy

func (p *Prompt) Copy() *Prompt

Copy returns a copy of the prompt.

func (*Prompt) RaiseTemperature

func (p *Prompt) RaiseTemperature()

RaiseTemperature increases the "temperature" option of the model, if any.

func (*Prompt) SetMaxNewTokens

func (p *Prompt) SetMaxNewTokens(maxNewTokens int)

SetMaxNewTokens sets the "max_new_tokens" option of the prompt to the given value.

func (*Prompt) SetTemperature

func (p *Prompt) SetTemperature(temperature float64)

SetTemperature sets the "temperature" option of the prompt to the given value.

func (*Prompt) WithHotterModel

func (p *Prompt) WithHotterModel() *Prompt

WithHotterModel returns a copy of this prompt with the same model having a higher temperature.

type Response

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

Response represents a response to a query from SGLang.

func (*Response) Done

func (r *Response) Done() bool

Done returns whether the response was completely generated.

func (*Response) E2ELatency

func (r *Response) E2ELatency() float64

E2ELatency returns the elapsed time between when start_time was recorded and the current moment in seconds. See https://github.com/sgl-project/sglang/blob/4a2768a86b2905b9b7f19d415261b9d4af639e19/sgl-router/src/routers/grpc/regular/streaming.rs#L904

func (*Response) NumTokens

func (r *Response) NumTokens() int

NumTokens returns the number of tokens in the response.

func (*Response) OutputTokensPerSecond

func (r *Response) OutputTokensPerSecond() float64

OutputTokensPerSecond computes the average number of output tokens generated per second.

func (*Response) String

func (r *Response) String() string

String returns the response text, if it is done.

func (*Response) Text

func (r *Response) Text() string

Text returns the body of the response, if it is done.

func (*Response) TimeToFirstToken

func (r *Response) TimeToFirstToken() time.Duration

TimeToFirstToken returns the time it took between the request starting and the first token being received by the client.

func (*Response) TimeToLastToken

func (r *Response) TimeToLastToken() time.Duration

TimeToLastToken returns the time it took between the request starting and the last token being received by the client.

type SGLang

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

SGLang is an sglang client.

func New

func New(ctx context.Context, server llmutil.Server, logger testutil.Logger) (*SGLang, error)

New starts a new SGLang server in the given container, then waits for it to serve and returns the client.

func NewDocker

func NewDocker(ctx context.Context, cont *dockerutil.Container, logger testutil.Logger) (*SGLang, error)

NewDocker returns a new SGLang client talking to an SGLang server that runs in a local Docker container.

func (*SGLang) Prompt

func (llm *SGLang) Prompt(ctx context.Context, prompt *Prompt) (*Response, error)

Prompt returns the result of prompting the given `model` with `prompt`.

func (*SGLang) PromptUntil

func (llm *SGLang) PromptUntil(ctx context.Context, prompt *Prompt, iterate func(*Prompt, *Response) (*Prompt, error)) (*Response, error)

PromptUntil repeatedly issues a prompt until `iterate` returns a nil error. `iterate` may optionally return an updated `Prompt` which will be used to follow up. This is useful to work around the flakiness of LLMs in tests.

func (*SGLang) WaitUntilServing

func (llm *SGLang) WaitUntilServing(ctx context.Context) error

WaitUntilServing waits until sglang is serving, or the context expires.

func (*SGLang) WarmModel

func (llm *SGLang) WarmModel(ctx context.Context) (*ModelLoadStats, error)

WarmModel pre-warms a model in memory and keeps it warm for `keepWarmFor`. If `unloadFirst` is true, another model will be loaded before loading the requested model. This ensures that the model was loaded from a cold state.

Jump to

Keyboard shortcuts

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