Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ type ChatCompletionMessage struct {

// For Role=tool prompts this should be set to the ID given in the assistant's prior request to call a tool.
ToolCallID string `json:"tool_call_id,omitempty"`

// To support Anthropic's thinking blocks, we need to add a new field to the ChatCompletionMessage struct.
ThinkingBlocks []ThinkingBlock `json:"thinking_blocks,omitempty"`
}

type ThinkingBlock struct {
Type string `json:"type"`
Thinking string `json:"thinking"`
Signature string `json:"signature"`
}

func (m ChatCompletionMessage) MarshalJSON() ([]byte, error) {
Expand All @@ -136,6 +145,7 @@ func (m ChatCompletionMessage) MarshalJSON() ([]byte, error) {
FunctionCall *FunctionCall `json:"function_call,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
ThinkingBlocks []ThinkingBlock `json:"thinking_blocks,omitempty"`
}(m)
return json.Marshal(msg)
}
Expand All @@ -150,6 +160,7 @@ func (m ChatCompletionMessage) MarshalJSON() ([]byte, error) {
FunctionCall *FunctionCall `json:"function_call,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
ThinkingBlocks []ThinkingBlock `json:"thinking_blocks,omitempty"`
}(m)
return json.Marshal(msg)
}
Expand All @@ -160,11 +171,12 @@ func (m *ChatCompletionMessage) UnmarshalJSON(bs []byte) error {
Content string `json:"content"`
Refusal string `json:"refusal,omitempty"`
MultiContent []ChatMessagePart
Name string `json:"name,omitempty"`
ReasoningContent string `json:"reasoning_content,omitempty"`
FunctionCall *FunctionCall `json:"function_call,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
Name string `json:"name,omitempty"`
ReasoningContent string `json:"reasoning_content,omitempty"`
FunctionCall *FunctionCall `json:"function_call,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
ThinkingBlocks []ThinkingBlock `json:"thinking_blocks,omitempty"`
}{}

if err := json.Unmarshal(bs, &msg); err == nil {
Expand All @@ -181,6 +193,7 @@ func (m *ChatCompletionMessage) UnmarshalJSON(bs []byte) error {
FunctionCall *FunctionCall `json:"function_call,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
ThinkingBlocks []ThinkingBlock `json:"thinking_blocks,omitempty"`
}{}
if err := json.Unmarshal(bs, &multiMsg); err != nil {
return err
Expand Down Expand Up @@ -256,7 +269,14 @@ type ChatCompletionRequestExtensions struct {
// is used to constrain the model's responses to a controlled set of options,
// ensuring predictable and consistent outputs in scenarios where specific
// choices are required.
GuidedChoice []string `json:"guided_choice,omitempty"`
GuidedChoice []string `json:"guided_choice,omitempty"`
NoLog bool `json:"no-log,omitempty"`
Anthropic *Anthropic `json:"-"`
}

// Anthropic is for Anthropic specific parameter fields made through the OpenAI API
type Anthropic struct {
BetaFeatures []string `json:"beta_features,omitempty"`
}

// ChatCompletionRequest represents a request structure for chat completion API.
Expand Down Expand Up @@ -350,8 +370,10 @@ const (
)

type Tool struct {
Type ToolType `json:"type"`
Function *FunctionDefinition `json:"function,omitempty"`
Type ToolType `json:"type"`
Function *FunctionDefinition `json:"function,omitempty"`
Name string `json:"name,omitempty"`
MaxCharacters int `json:"max_characters,omitempty"`
}

type ToolChoice struct {
Expand Down
7 changes: 6 additions & 1 deletion chat_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ type ChatCompletionStreamChoiceDelta struct {
// which is not in the official documentation.
// the doc from deepseek:
// - https://api-docs.deepseek.com/api/create-chat-completion#responses
ReasoningContent string `json:"reasoning_content,omitempty"`
ReasoningContent string `json:"reasoning_content,omitempty"`
ProviderSpecificFields ProviderSpecificFields `json:"provider_specific_fields,omitempty"`
}

type ProviderSpecificFields struct {
ThinkingBlocks []ThinkingBlock `json:"thinking_blocks,omitempty"`
}

type ChatCompletionStreamChoiceLogprobs struct {
Expand Down