|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package types |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/sashabaranov/go-openai" |
| 24 | +) |
| 25 | + |
| 26 | +// KVCacheChatCompletionRequest is a struct that represents the fields from an |
| 27 | +// OpenAI API ChatCompletionRequest that are relevant for KV cache generation. |
| 28 | +// Model is not included as it is contained in the LLMRequest struct. |
| 29 | +// |
| 30 | +// Multimodal requests are not supported in the current implementation. |
| 31 | +type KVCacheChatCompletionRequest struct { |
| 32 | + Messages []openai.ChatCompletionMessage `json:"messages"` |
| 33 | + Tools []openai.Tool `json:"tools,omitempty"` |
| 34 | + ToolChoices []openai.ToolChoice `json:"tool_choices,omitempty"` |
| 35 | +} |
| 36 | + |
| 37 | +// NewKVCacheChatCompletionRequest creates a new KVCacheChatCompletionRequest |
| 38 | +// from a json request. |
| 39 | +// |
| 40 | +// The call marshals the input map to JSON and then unmarshals it into the |
| 41 | +// KVCacheChatCompletionRequest struct. |
| 42 | +func NewKVCacheChatCompletionRequest(input map[string]interface{}) (*KVCacheChatCompletionRequest, error) { |
| 43 | + var req KVCacheChatCompletionRequest |
| 44 | + |
| 45 | + if messagesRaw, ok := input["messages"]; ok { |
| 46 | + bytes, err := json.Marshal(messagesRaw) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + if err := json.Unmarshal(bytes, &req.Messages); err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if toolsRaw, ok := input["tools"]; ok { |
| 56 | + bytes, err := json.Marshal(toolsRaw) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + if err := json.Unmarshal(bytes, &req.Tools); err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if choicesRaw, ok := input["tool_choices"]; ok { |
| 66 | + bytes, err := json.Marshal(choicesRaw) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + if err := json.Unmarshal(bytes, &req.ToolChoices); err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return &req, nil |
| 76 | +} |
| 77 | + |
| 78 | +// ToString generates a string representation of the KVCacheChatCompletionRequest. |
| 79 | +func (r *KVCacheChatCompletionRequest) ToString() string { |
| 80 | + var builder strings.Builder |
| 81 | + |
| 82 | + for _, msg := range r.Messages { |
| 83 | + builder.WriteString(msg.Role) |
| 84 | + builder.WriteString(":") |
| 85 | + builder.WriteString(msg.Content) |
| 86 | + builder.WriteString("\n") |
| 87 | + } |
| 88 | + |
| 89 | + if len(r.Tools) > 0 { |
| 90 | + toolsJSON, err := json.Marshal(r.Tools) |
| 91 | + if err == nil { |
| 92 | + builder.WriteString("tools:") |
| 93 | + builder.Write(toolsJSON) |
| 94 | + builder.WriteString("\n") |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if len(r.ToolChoices) > 0 { |
| 99 | + choicesJSON, err := json.Marshal(r.ToolChoices) |
| 100 | + if err == nil { |
| 101 | + builder.WriteString("tool_choices:") |
| 102 | + builder.Write(choicesJSON) |
| 103 | + builder.WriteString("\n") |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return builder.String() |
| 108 | +} |
0 commit comments