chatcompletions

package
v0.28.13 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package chatcompletions is a transport-agnostic codec for the OpenAI Chat Completions wire format (`/chat/completions`-style request/response and SSE streaming). It maps between contenox's neutral modelrepo types and the OpenAI-compatible JSON shape, performing tool-name sanitization and round-tripping.

It does NO I/O: callers build a Request, marshal and POST it through their own transport (API-key header for direct OpenAI/Mistral), then hand the raw response bytes back here to decode. This is what lets the direct Mistral / OpenAI provider stay a thin transport wrapper around the shared codec.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeResponse

func DecodeResponse(raw []byte, nameMap map[string]string) (modelrepo.ChatResult, error)

DecodeResponse parses a non-streaming response into a neutral ChatResult, translating sanitized tool-call names back via nameMap.

Types

type Request

type Request struct {
	Model       string        `json:"model"`
	Messages    []wireMessage `json:"messages"`
	Temperature *float64      `json:"temperature,omitempty"`
	MaxTokens   *int          `json:"max_tokens,omitempty"`
	TopP        *float64      `json:"top_p,omitempty"`
	Seed        *int          `json:"seed,omitempty"`
	Tools       []wireTool    `json:"tools,omitempty"`
	ToolChoice  string        `json:"tool_choice,omitempty"`
	Stream      bool          `json:"stream,omitempty"`
}

Request is the OpenAI-compatible chat/completions request body.

Note: this codec emits `max_tokens` (the field Mistral's OpenAI-compatible endpoint accepts), not the newer `max_completion_tokens`.

func Build

func Build(model string, messages []modelrepo.Message, cfg *modelrepo.ChatConfig) (Request, map[string]string)

Build converts neutral messages + config into a chat/completions Request. It returns a nameMap (sanitized tool name -> original) so DecodeResponse / the StreamDecoder can translate tool-call names back to what the caller used.

model is placed verbatim in the body; the transport decides the exact string (e.g. "mistral-large-latest", or whatever id the provider expects).

type Response

type Response struct {
	Choices []struct {
		Index        int         `json:"index"`
		Message      responseMsg `json:"message"`
		FinishReason string      `json:"finish_reason"`
	} `json:"choices"`
}

Response is the non-streaming chat/completions response body.

type StreamDecoder

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

StreamDecoder assembles a streamed chat/completions response. Text and reasoning deltas are returned as parcels; tool-call fragments are accumulated per index and exposed via ToolCalls() once the stream ends.

func NewStreamDecoder

func NewStreamDecoder(nameMap map[string]string) *StreamDecoder

NewStreamDecoder returns a decoder. nameMap is the sanitized->original map from Build (may be nil if no tools).

func (*StreamDecoder) DecodeLine

func (d *StreamDecoder) DecodeLine(payload []byte) (*modelrepo.StreamParcel, error)

DecodeLine parses one SSE data payload (the bytes AFTER the "data: " prefix, excluding the "[DONE]" sentinel which the caller should skip). It returns a parcel to emit if the chunk carried visible text/reasoning, or nil.

func (*StreamDecoder) ToolCalls

func (d *StreamDecoder) ToolCalls() []modelrepo.ToolCall

ToolCalls returns the tool calls assembled across the stream, in index order, with names translated back via nameMap.

Jump to

Keyboard shortcuts

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