Files
ai/model/openai_util.go
zeekayandHanzo Dev 0c08bd1d94 Go 1.27.1, and the code it can say more simply
Modules declare go 1.27.1 and the build images follow. go fix applied the
modernizers; omitzero is not applied, since it changes what the JSON says.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-17 01:01:10 -07:00

261 lines
8.6 KiB
Go

// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
// Portions Copyright 2023 The OpenAgent Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package model
import (
"slices"
"strings"
"github.com/hanzoai/go-openai"
"github.com/pkoukk/tiktoken-go"
)
// https://pkg.go.dev/github.com/hanzoai/[email protected]#pkg-constants
// https://platform.openai.com/docs/models/overview
var __maxTokens = map[string]int{
openai.GPT4o: 128000,
openai.GPT4o20240513: 128000,
openai.GPT4oMini: 128000,
openai.GPT4oMini20240718: 128000,
openai.GPT4VisionPreview: 128000,
openai.GPT4: 8192,
openai.GPT40613: 8192,
openai.GPT432K: 32768,
openai.GPT432K0613: 32768,
openai.GPT40314: 8192,
openai.GPT432K0314: 32768,
openai.GPT3Dot5Turbo: 4097,
openai.GPT3Dot5Turbo16K: 16385,
openai.GPT3Dot5Turbo0613: 4097,
openai.GPT3Dot5Turbo16K0613: 16385,
openai.GPT3Dot5Turbo0301: 4097,
openai.GPT3TextDavinci003: 4097,
openai.GPT3TextDavinci002: 4097,
openai.GPT3TextCurie001: 2049,
openai.GPT3TextBabbage001: 2049,
openai.GPT3TextAda001: 2049,
openai.GPT3Davinci: 2049,
openai.GPT3Curie: 2049,
openai.GPT3Ada: 2049,
openai.GPT3Babbage: 2049,
}
// GetOpenAiMaxTokens returns the max tokens for a given OpenAI model.
func GetOpenAiMaxTokens(model string) int {
res, ok := __maxTokens[model]
if !ok {
return 4097
}
return res
}
// samplesFreely reports whether a model accepts `temperature` and `top_p`. The
// reasoning families (o1/o3/o4, gpt-5) do not: they choose their own sampling
// and REJECT the parameters outright rather than ignoring them.
func samplesFreely(model string) bool {
m := strings.ToLower(model)
for _, family := range []string{"o1", "o3", "o4", "gpt-5"} {
if strings.HasPrefix(m, family) || strings.Contains(m, "/"+family) {
return false
}
}
return true
}
func getOpenAiModelType(model string) string {
// Chat model patterns (matched via contains for provider-prefixed names like "openai-gpt-5-nano")
chatPatterns := []string{
"gpt-3.5-turbo", "gpt-4-turbo", "gpt-4", "gpt-4o",
"gpt-4o-2024-08-06", "gpt-4o-mini", "gpt-4o-mini-2024-07-18", "gpt-4.1",
"gpt-4.1-mini", "gpt-4.1-nano", "gpt-4.5", "gpt-4.5-mini", "gpt-4.5-nano",
"o1", "o1-pro", "o3", "o3-mini", "o4-mini",
"gpt-5", "gpt-5-mini", "gpt-5-nano", "gpt-5.1", "gpt-5.1-mini", "gpt-5.1-nano",
"gpt-5.2", "gpt-5.2-mini", "gpt-5.2-nano", "gpt-5.2-chat", "gpt-5-chat-latest",
"gpt-oss", "deep-research",
"claude", "llama", "qwen", "deepseek", "mistral",
"custom-model",
}
completionModels := []string{
"text-davinci-003", "text-davinci-002", "text-curie-001",
"text-babbage-001", "text-ada-001", "text-davinci-001",
"davinci-instruct-beta", "davinci", "curie-instruct-beta", "curie", "ada", "babbage",
}
imageModels := []string{
"dall-e-3", "dall-e-2", "gpt-image-1",
// Hanzo Zen3 diffusion family. "zen3-image" as a substring covers the
// whole family (zen3-image, -max, -fast, -dev, -playground, -ssd, -sdxl,
// -jp) and matches no chat model, so it selects the imagesGenerations
// path for both the user-facing name and the fal-hosted upstream this
// routes to (fal-ai/flux/schnell, fal-ai/fast-sdxl).
"zen3-image", "fal-ai/flux", "fal-ai/fast-sdxl",
// DO-AI Stable Diffusion 3.5 Large — served on the synchronous OpenAI
// /images shape (isDOAIImageModel==false → client.Images.Generate). The
// "stable-diffusion" substring matches its id and no chat model.
"stable-diffusion",
}
for _, pattern := range chatPatterns {
if strings.Contains(model, pattern) {
return "Chat"
}
}
if slices.Contains(completionModels, model) {
return "Completion"
}
// Text-to-video is checked BEFORE image so a video model is never
// misclassified as an image. Both the do-ai upstream (wan2-2-t2v-a14b) and
// the Hanzo zen3-video brand family select the dedicated /v1/videos/generations
// path; none of these substrings match a chat/image model.
if isDOAIVideoModel(model) {
return "videosGenerations"
}
for _, imageModel := range imageModels {
if strings.Contains(model, imageModel) {
return "imagesGenerations"
}
}
// Default to Chat for unknown models (most modern models are chat-based)
return "Chat"
}
func OpenaiRawMessagesToMessages(messages []*RawMessage) []openai.ChatCompletionMessage {
res := []openai.ChatCompletionMessage{}
for _, message := range messages {
var role string
if message.Author == "AI" {
role = openai.ChatMessageRoleAssistant
} else if message.Author == "System" {
role = openai.ChatMessageRoleSystem
} else if message.Author == "Tool" {
role = openai.ChatMessageRoleTool
} else {
role = openai.ChatMessageRoleUser
}
item := openai.ChatCompletionMessage{
Role: role,
Content: message.Text,
}
if role == openai.ChatMessageRoleTool {
item.ToolCallID = message.ToolCallID
} else if role == openai.ChatMessageRoleAssistant {
if message.ToolCall.ID != "" {
item.ToolCalls = []openai.ToolCall{message.ToolCall}
} else {
item.ToolCalls = nil
}
}
// Set non-empty message text due to that some AI models report error for empty message
if item.Content == "" {
item.Content = " "
}
res = append(res, item)
}
return res
}
func ChatCompletionRequest(model string, messages []openai.ChatCompletionMessage, temperature float32, topP float32, frequencyPenalty float32, presencePenalty float32) openai.ChatCompletionRequest {
res := openai.ChatCompletionRequest{
Model: model,
Messages: messages,
Stream: true,
Temperature: temperature,
TopP: topP,
FrequencyPenalty: frequencyPenalty,
PresencePenalty: presencePenalty,
}
if model == "custom-model" {
res.Stop = []string{"."}
}
if model == "gpt-4-vision-preview" || model == "gpt-4-1106-vision-preview" {
res.MaxTokens = 4096
}
// Default a sane completion cap for EVERY OpenAI-compatible model. The chat
// pipeline (QueryText) does NOT thread the request's max_tokens, so without a
// default an uncapped request could bill an unbounded completion. This 4096
// floor mirrors controllers.reserveCompletionFloor, so the per-request balance
// reservation always covers the actual spend on this path (R1b). Anthropic and
// several OpenAI-compatible upstreams also require max_tokens to be present.
if res.MaxTokens == 0 {
res.MaxTokens = 4096
}
return res
}
// https://github.com/pkoukk/tiktoken-go?tab=readme-ov-file#counting-tokens-for-chat-api-calls
// https://github.com/hanzoai/go-openai/pull/223#issuecomment-1608689882
func OpenaiNumTokensFromMessages(messages []openai.ChatCompletionMessage, model string) (int, error) {
modelToUse := getCompatibleModel(model)
// Get model-specific token counts
tokensPerMessage, tokensPerName := getModelTokenCounts(modelToUse)
// Get tiktoken encoding using the compatibility layer
tkm, err := tiktoken.EncodingForModel(modelToUse)
if err != nil {
return 0, err
}
numTokens := 0
for _, message := range messages {
// Calculate tokens for the message content
var content strings.Builder
content.WriteString(message.Content)
for _, multiContentPart := range message.MultiContent {
if multiContentPart.Type == "text" {
content.WriteString(multiContentPart.Text)
}
}
numTokens += tokensPerMessage
numTokens += len(tkm.Encode(content.String(), nil, nil))
numTokens += len(tkm.Encode(message.Role, nil, nil))
numTokens += len(tkm.Encode(message.Name, nil, nil))
if message.Name != "" {
numTokens += tokensPerName
}
}
numTokens += 3 // every reply is primed with <|start|>assistant<|message|>
return numTokens, nil
}
func getModelTokenCounts(model string) (tokensPerMessage, tokensPerName int) {
// Default values for most models
defaultTokensPerMessage := 3
defaultTokensPerName := 1
// Special cases for specific model versions
switch model {
case openai.GPT3Dot5Turbo0301:
return 4, -1 // special case: if there's a name, the role is omitted
}
// Default values for unknown models
return defaultTokensPerMessage, defaultTokensPerName
}