Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for cache control in ChatCompletionMessage #963

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 17 additions & 0 deletions chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ type ChatMessagePart struct {
ImageURL *ChatMessageImageURL `json:"image_url,omitempty"`
}

type CacheControlType string

const (
CacheControlTypeEphemeral CacheControlType = "ephemeral"
)

type CacheControl struct {
Type CacheControlType `json:"type,omitempty"`
}

type ChatCompletionMessage struct {
Role string `json:"role"`
Content string `json:"content,omitempty"`
Expand All @@ -111,6 +121,9 @@ 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"`

// CacheControl is used to control the caching behavior of the message. Used by Litellm.
CacheControl *CacheControl `json:"cache_control,omitempty"`
}

func (m ChatCompletionMessage) MarshalJSON() ([]byte, error) {
Expand All @@ -127,6 +140,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"`
CacheControl *CacheControl `json:"cache_control,omitempty"`
}(m)
return json.Marshal(msg)
}
Expand All @@ -140,6 +154,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"`
CacheControl *CacheControl `json:"cache_control,omitempty"`
}(m)
return json.Marshal(msg)
}
Expand All @@ -154,6 +169,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"`
CacheControl *CacheControl `json:"cache_control,omitempty"`
}{}

if err := json.Unmarshal(bs, &msg); err == nil {
Expand All @@ -169,6 +185,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"`
CacheControl *CacheControl `json:"cache_control,omitempty"`
}{}
if err := json.Unmarshal(bs, &multiMsg); err != nil {
return err
Expand Down
Loading