openaichat

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OpenAIChatDefaultMaxTokens = 4096
)

Variables

This section is empty.

Functions

func ConvertAIChatToUIChat

func ConvertAIChatToUIChat(aiChat uctypes.AIChat) (*uctypes.UIChat, error)

ConvertAIChatToUIChat converts stored chat to UI format

func ConvertToolResultsToNativeChatMessage

func ConvertToolResultsToNativeChatMessage(toolResults []uctypes.AIToolResult) ([]uctypes.GenAIMessage, error)

ConvertToolResultsToNativeChatMessage converts tool results to OpenAI tool messages

func GetFunctionCallInputByToolCallId

func GetFunctionCallInputByToolCallId(aiChat uctypes.AIChat, toolCallId string) *uctypes.AIFunctionCallInput

GetFunctionCallInputByToolCallId searches for a tool call by ID in the chat history

func UpdateToolUseData

func UpdateToolUseData(chatId string, callId string, newToolUseData uctypes.UIMessageDataToolUse) error

UpdateToolUseData updates the ToolUseData for a specific tool call in the chat history

Types

type ChatRequest

type ChatRequest struct {
	Model               string               `json:"model"`
	Messages            []ChatRequestMessage `json:"messages"`
	Stream              bool                 `json:"stream"`
	MaxTokens           int                  `json:"max_tokens,omitempty"`            // legacy
	MaxCompletionTokens int                  `json:"max_completion_tokens,omitempty"` // newer
	Temperature         float64              `json:"temperature,omitempty"`
	Tools               []ToolDefinition     `json:"tools,omitempty"`       // if you use tools
	ToolChoice          any                  `json:"tool_choice,omitempty"` // "auto", "none", or struct
}

type ChatRequestMessage

type ChatRequestMessage struct {
	Role       string     `json:"role"`                   // "system","user","assistant","tool"
	Content    string     `json:"content,omitempty"`      // normal text messages
	ToolCalls  []ToolCall `json:"tool_calls,omitempty"`   // assistant tool-call message
	ToolCallID string     `json:"tool_call_id,omitempty"` // for role:"tool"
	Name       string     `json:"name,omitempty"`         // tool name on role:"tool"
}

func (*ChatRequestMessage) FindToolCallIndex

func (cm *ChatRequestMessage) FindToolCallIndex(toolCallId string) int

type ChatUsage

type ChatUsage struct {
	Model        string `json:"model,omitempty"`
	InputTokens  int    `json:"prompt_tokens,omitempty"`
	OutputTokens int    `json:"completion_tokens,omitempty"`
	TotalTokens  int    `json:"total_tokens,omitempty"`
}

type ContentDelta

type ContentDelta struct {
	Role      string          `json:"role,omitempty"`
	Content   string          `json:"content,omitempty"`
	ToolCalls []ToolCallDelta `json:"tool_calls,omitempty"`
}

This is the important part:

type StoredChatMessage

type StoredChatMessage struct {
	MessageId string             `json:"messageid"`
	Message   ChatRequestMessage `json:"message"`
	Usage     *ChatUsage         `json:"usage,omitempty"`
}

StoredChatMessage is the stored message type

func ConvertAIMessageToStoredChatMessage

func ConvertAIMessageToStoredChatMessage(aiMsg uctypes.AIMessage) (*StoredChatMessage, error)

ConvertAIMessageToStoredChatMessage converts an AIMessage to StoredChatMessage These messages are ALWAYS role "user"

func RunChatStep

RunChatStep executes a chat step using the chat completions API

func (*StoredChatMessage) Copy

func (*StoredChatMessage) GetMessageId

func (m *StoredChatMessage) GetMessageId() string

func (*StoredChatMessage) GetRole

func (m *StoredChatMessage) GetRole() string

func (*StoredChatMessage) GetUsage

func (m *StoredChatMessage) GetUsage() *uctypes.AIUsage

type StreamChoice

type StreamChoice struct {
	Index        int          `json:"index"`
	Delta        ContentDelta `json:"delta"`
	FinishReason *string      `json:"finish_reason"` // "stop", "length" | "tool_calls" | "content_filter"
}

type StreamChunk

type StreamChunk struct {
	ID      string         `json:"id"`
	Object  string         `json:"object"`
	Created int64          `json:"created"`
	Model   string         `json:"model"`
	Choices []StreamChoice `json:"choices"`
}

type ToolCall

type ToolCall struct {
	ID          string                        `json:"id"`
	Type        string                        `json:"type"` // "function"
	Function    ToolFunctionCall              `json:"function"`
	ToolUseData *uctypes.UIMessageDataToolUse `json:"toolusedata,omitempty"` // Internal field (must be cleaned before sending to API)
}

type ToolCallDelta

type ToolCallDelta struct {
	Index    int                `json:"index"`
	ID       string             `json:"id,omitempty"`   // only on first chunk
	Type     string             `json:"type,omitempty"` // "function"
	Function *ToolFunctionDelta `json:"function,omitempty"`
}

type ToolDefinition

type ToolDefinition struct {
	Type     string          `json:"type"` // "function"
	Function ToolFunctionDef `json:"function"`
}

type ToolFunctionCall

type ToolFunctionCall struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"` // raw JSON string
}

type ToolFunctionDef

type ToolFunctionDef struct {
	Name        string         `json:"name"`
	Description string         `json:"description,omitempty"`
	Parameters  map[string]any `json:"parameters,omitempty"` // or jsonschema struct
}

type ToolFunctionDelta

type ToolFunctionDelta struct {
	Name      string `json:"name,omitempty"`      // only on first chunk
	Arguments string `json:"arguments,omitempty"` // streamed, append across chunks
}

Jump to

Keyboard shortcuts

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