Documentation
¶
Overview ¶
Package mllm ... llm was taken
Index ¶
- Variables
- func StderrHandler(cr ChatResponse) error
- func StdoutHandler(cr ChatResponse) error
- func Summary(w io.Writer, st Stats)
- type ChatHandlerT
- type ChatMessage
- type ChatMessageFormatter
- type ChatMessages
- type ChatRequest
- type ChatResponse
- type E
- func (ee E) AddHandlers(h ...StreamHandler) E
- func (ee E) Get(ctx context.Context, messages []Message, think any) (ChatResponse, error)
- func (ee E) Stream(ctx context.Context, messages []Message, think any, handlers ...StreamHandler) error
- func (ee E) StreamBG(ctx context.Context, messages []Message, think any, handlers ...StreamHandler) chan error
- func (ee E) ValidateConfig(think any) error
- func (ee E) WithHandlers(h ...StreamHandler) E
- func (ee E) WithOptions(options *Options) E
- func (ee E) WithTools(tools ...json.Marshaler) E
- type Message
- type Messages
- type Options
- type Stats
- type StreamHandler
- type Think
Constants ¶
This section is empty.
Variables ¶
var ( Errorf = stackerr.Errorf Slog = slog.Default )
var ( DefaultThinkPre = "Thinking:\n" DefaultThinkPost = "\n\n... done thinking.\n\n" )
var ( StopAtFirstHandlerError = true ErrNoLines = errors.New("no lines read") )
var Default = func() E { e := NewE(nil, nil) return e }
Default NewEndpoint no args (can be custom)
var DefaultChatMessageFormatter = func(cm ChatMessage) (Message, bool) { return cm.Message, true }
var DefaultWriter io.Writer = nil
DefaultWriter added to all new endpoints
var NewE = NewEndpoint
NewE custom NewEndpoint
Functions ¶
Types ¶
type ChatHandlerT ¶
type ChatHandlerT interface {
HandleChat(cr ChatResponse) error
}
type ChatMessage ¶
type ChatMessage struct {
UUID uuid.UUID `json:"uuid,omitempty" gorm:"default:null;`
Message
From string `json:"from,omitempty" gorm:"default:null"`
CreatedAt time.Time `json:"created_at"`
}
ChatMessage is a Message but for a multi-participant chat
func NewChatMessage ¶
func NewChatMessage(from string, role string, text string) ChatMessage
type ChatMessageFormatter ¶
type ChatMessageFormatter = func(cm ChatMessage) (Message, bool)
ChatMessageFormatter may skip a message
type ChatMessages ¶
type ChatMessages []ChatMessage
func (ChatMessages) ToMessages ¶
func (cm ChatMessages) ToMessages() []Message
func (ChatMessages) ToMessagesFormat ¶
func (cm ChatMessages) ToMessagesFormat(fn ChatMessageFormatter) []Message
ToMessagesFormat returns a **temporary** convo for llm to produce a single new Message The returned convo should NEVER be stored as the Content is modified. To prevent synthesized timestamps in the output message, a system instruction may be required.
type ChatRequest ¶
type ChatRequest struct {
Model string `json:"model,omitempty"`
Messages []Message `json:"messages,omitempty"`
Stream bool `json:"stream"`
Think *Think `json:"think,omitempty"`
KeepAlive int `json:"keep_alive,omitempty"`
Options *Options `json:"options,omitempty"`
Tools []json.Marshaler `json:"tools,omitempty"`
}
type ChatResponse ¶
type ChatResponse struct {
Model string `json:"model"`
Created json.RawMessage `json:"created_at,omitempty"`
Done bool `json:"done"` // for stream
DoneReason string `json:"done_reason,omitempty"`
Message Message `json:"message"` // for stream, handlers
Stats `gorm:"embedded"`
}
func (*ChatResponse) Handler ¶
func (c *ChatResponse) Handler(cr ChatResponse) error
func (ChatResponse) Summary ¶
func (c ChatResponse) Summary(w io.Writer)
Summary in well-known format
func (ChatResponse) SummaryString ¶
func (c ChatResponse) SummaryString() string
SummaryString collects ChatResponse.Summary into a string
type E ¶
type E struct {
Host url.URL
Model string
Client *http.Client
Headers map[string]string
Tools []json.Marshaler // Bring your own tools
Options *Options
// Writer if non-nil will be automatically added as a stream handler to [[E.Get]] and [[E.Stream]]
Writer io.Writer
ThinkPre string
ThinkPost string
// Handlers added to all requests
Handlers []StreamHandler
}
E (Endpoint) holds information for future requests
Writer for streaming output with ThinkPre and ThinkPost.
func NewEndpoint ¶
NewEndpoint reads env and returns a new Endpoint
func (E) AddHandlers ¶
func (ee E) AddHandlers(h ...StreamHandler) E
func (E) Get ¶
Get streams into a new ChatResponse and returns the finished full response.
If [[Writer]] is non-nil, adds new handler, streaming output to it.
func (E) ValidateConfig ¶
func (E) WithHandlers ¶
func (ee E) WithHandlers(h ...StreamHandler) E
func (E) WithOptions ¶
type Message ¶
type Message struct {
Role string `json:"role,omitempty"`
Content string `json:"content,omitempty"`
Reasoning string `json:"thinking,omitempty" gorm:"default:default:null"`
ToolCalls []json.RawMessage `json:"tool_calls,omitempty" gorm:"type:jsonb;serializer:json;default:null;"`
ToolName string `json:"tool_name,omitempty" gorm:"default:null"`
ToolCallID string `json:"tool_call_id,omitempty" gorm:"default:null"`
Images [][]byte `json:"images,omitempty" gorm:"serializer:json;type:jsonb;default:null;"`
}
func DefaultChatMessageFormatterWithFromTimestamp ¶
func DefaultChatMessageFormatterWithFromTimestamp(cm ChatMessage) (Message, bool)
DefaultChatMessageFormatter returns a **temporary** Message for llm to read. The returned Message should NEVER be stored as the Content is modified. To prevent synthesized timestamps in the output message, a system instruction may be required.
YMMV
func (Message) ToChatMessage ¶
func (m Message) ToChatMessage(from string) ChatMessage
ToChatMessage with new UUID,from,timestamp. should be called immediately after stream is finished..
ChatMessage MAY be stored (NOT plain Message)
type Options ¶
type Options struct {
Seed int64 `json:"seed,omitempty"`
Temperature float64 `json:"temperature"` // 0.0 to 2.0
NumCtx int `json:"num_ctx,omitempty"`
TopP float64 `json:"top_p"` // 0.0 to 1.0
TopK int `json:"top_k"` // 1 to ???
FrequencyPenalty float64 `json:"frequency_penalty"` // -2.0 to 2.0
PresencePenalty float64 `json:"presence_penalty"` // -2.0 to 2.0
}
type Stats ¶
type Stats struct {
LoadDuration time.Duration `json:"load_duration,omitempty"`
PromptEvalDuration time.Duration `json:"prompt_eval_duration,omitempty"`
EvalDuration time.Duration `json:"eval_duration,omitempty"`
TotalDuration time.Duration `json:"total_duration,omitempty"`
PromptEvalCount int `json:"prompt_eval_count,omitempty"`
EvalCount int `json:"eval_count,omitempty"`
}
type StreamHandler ¶
type StreamHandler func(cr ChatResponse) error
func NewHandlerOut ¶
func NewHandlerOut(w io.Writer, thinkpre string, thinkpost string) StreamHandler
NewHandlerOut plain 🍔
func (StreamHandler) HandleChat ¶
func (c StreamHandler) HandleChat(cr ChatResponse) error