update openai struct

This commit is contained in:
Sakurasan
2024-12-20 02:42:49 +08:00
parent 07bcabc5d4
commit fb5b1a55ae
3 changed files with 30 additions and 20 deletions

View File

@@ -50,9 +50,9 @@ type VisionImageURL struct {
} }
type ChatCompletionMessage struct { type ChatCompletionMessage struct {
Role string `json:"role"` Role string `json:"role"`
Content json.RawMessage `json:"content"` Content any `json:"content"`
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
// MultiContent []VisionContent // MultiContent []VisionContent
} }
@@ -185,27 +185,31 @@ func ChatProxy(c *gin.Context, chatReq *ChatCompletionRequest) {
var prompt string var prompt string
for _, msg := range chatReq.Messages { for _, msg := range chatReq.Messages {
// prompt += "<" + msg.Role + ">: " + msg.Content + "\n" switch ct := msg.Content.(type) {
var visioncontent []VisionContent case string:
if err := json.Unmarshal(msg.Content, &visioncontent); err != nil { prompt += "<" + msg.Role + ">: " + msg.Content.(string) + "\n"
prompt += "<" + msg.Role + ">: " + string(msg.Content) + "\n" case []any:
} else { for _, item := range ct {
if len(visioncontent) > 0 { if m, ok := item.(map[string]interface{}); ok {
for _, content := range visioncontent { if m["type"] == "text" {
if content.Type == "text" { prompt += "<" + msg.Role + ">: " + m["text"].(string) + "\n"
prompt += "<" + msg.Role + ">: " + content.Text + "\n" } else if m["type"] == "image_url" {
} else if content.Type == "image_url" { if url, ok := m["image_url"].(map[string]interface{}); ok {
if strings.HasPrefix(content.ImageURL.URL, "http") { fmt.Printf(" URL: %v\n", url["url"])
fmt.Println("链接:", content.ImageURL.URL) if strings.HasPrefix(url["url"].(string), "http") {
} else if strings.HasPrefix(content.ImageURL.URL, "data:image") { fmt.Println("网络图片:", url["url"].(string))
fmt.Println("base64:", content.ImageURL.URL[:20]) }
} }
// todo image tokens
} }
} }
} }
default:
c.JSON(http.StatusInternalServerError, gin.H{
"error": gin.H{
"message": "Invalid content type",
},
})
return
} }
if len(chatReq.Tools) > 0 { if len(chatReq.Tools) > 0 {
tooljson, _ := json.Marshal(chatReq.Tools) tooljson, _ := json.Marshal(chatReq.Tools)

View File

@@ -183,6 +183,8 @@ func Cost(model string, promptCount, completionCount int) float64 {
cost = (0.00035/1000)*float64(prompt) + (0.00053/1000)*float64(completion) cost = (0.00035/1000)*float64(prompt) + (0.00053/1000)*float64(completion)
case "gemini-2.0-flash-exp": case "gemini-2.0-flash-exp":
cost = (0.00035/1000)*float64(prompt) + (0.00053/1000)*float64(completion) cost = (0.00035/1000)*float64(prompt) + (0.00053/1000)*float64(completion)
case "gemini-2.0-flash-thinking-exp-1219":
cost = (0.00035/1000)*float64(prompt) + (0.00053/1000)*float64(completion)
case "learnlm-1.5-pro-experimental", " gemini-exp-1114", "gemini-exp-1121", "gemini-exp-1206": case "learnlm-1.5-pro-experimental", " gemini-exp-1114", "gemini-exp-1121", "gemini-exp-1206":
cost = (0.00035/1000)*float64(prompt) + (0.00053/1000)*float64(completion) cost = (0.00035/1000)*float64(prompt) + (0.00053/1000)*float64(completion)

View File

@@ -4,6 +4,7 @@ import (
"errors" "errors"
"log" "log"
"math/rand" "math/rand"
"os"
"strings" "strings"
"time" "time"
@@ -76,6 +77,9 @@ func SelectKeyCache(apitype string) (Key, error) {
func SelectKeyCacheByModel(model string) (Key, error) { func SelectKeyCacheByModel(model string) (Key, error) {
var keys []Key var keys []Key
if os.Getenv("OPENAI_API_KEY") != "" {
keys = append(keys, Key{ApiType: "openai", Key: os.Getenv("OPENAI_API_KEY")})
}
items := KeysCache.Items() items := KeysCache.Items()
for _, item := range items { for _, item := range items {
if strings.Contains(model, "realtime") { if strings.Contains(model, "realtime") {