set num_ctx through extra body

This commit is contained in:
ParthSareen 2025-01-29 13:13:11 -08:00
parent 2ef3c803a1
commit 35e97db03b

View File

@ -80,19 +80,21 @@ type StreamOptions struct {
} }
type ChatCompletionRequest struct { type ChatCompletionRequest struct {
Model string `json:"model"` Model string `json:"model"`
Messages []Message `json:"messages"` Messages []Message `json:"messages"`
Stream bool `json:"stream"` Stream bool `json:"stream"`
StreamOptions *StreamOptions `json:"stream_options"` StreamOptions *StreamOptions `json:"stream_options"`
MaxTokens *int `json:"max_tokens"` MaxCompletionTokens *int `json:"max_completion_tokens"`
Seed *int `json:"seed"` MaxTokens *int `json:"max_tokens" deprecated:"use max_completion_tokens instead"`
Stop any `json:"stop"` Seed *int `json:"seed"`
Temperature *float64 `json:"temperature"` Stop any `json:"stop"`
FrequencyPenalty *float64 `json:"frequency_penalty"` Temperature *float64 `json:"temperature"`
PresencePenalty *float64 `json:"presence_penalty"` FrequencyPenalty *float64 `json:"frequency_penalty"`
TopP *float64 `json:"top_p"` PresencePenalty *float64 `json:"presence_penalty"`
ResponseFormat *ResponseFormat `json:"response_format"` TopP *float64 `json:"top_p"`
Tools []api.Tool `json:"tools"` ResponseFormat *ResponseFormat `json:"response_format"`
Tools []api.Tool `json:"tools"`
NumCtx *int `json:"num_ctx"`
} }
type ChatCompletion struct { type ChatCompletion struct {
@ -475,8 +477,24 @@ func fromChatRequest(r ChatCompletionRequest) (*api.ChatRequest, error) {
options["stop"] = stops options["stop"] = stops
} }
// Deprecated: MaxTokens is deprecated, use MaxCompletionTokens instead
if r.MaxTokens != nil { if r.MaxTokens != nil {
options["num_predict"] = *r.MaxTokens r.MaxCompletionTokens = r.MaxTokens
}
if r.NumCtx != nil {
options["num_ctx"] = *r.NumCtx
}
DEFAULT_NUM_CTX := 2048
if r.MaxCompletionTokens != nil {
options["num_predict"] = *r.MaxCompletionTokens
if numCtx, ok := options["num_ctx"].(int); ok && *r.MaxCompletionTokens > numCtx {
options["num_ctx"] = *r.MaxCompletionTokens
} else if *r.MaxCompletionTokens > DEFAULT_NUM_CTX {
options["num_ctx"] = DEFAULT_NUM_CTX
}
} }
if r.Temperature != nil { if r.Temperature != nil {