fix: update text handling to ensure nil checks and pointer usage for message content

This commit is contained in:
Laisky.Cai 2025-03-17 03:31:43 +00:00
parent 34c7523f01
commit d236477531
5 changed files with 13 additions and 9 deletions

View File

@ -159,7 +159,9 @@ func ConvertRequest(c *gin.Context, textRequest model.GeneralOpenAIRequest) (*Re
var content Content
if part.Type == model.ContentTypeText {
content.Type = "text"
content.Text = part.Text
if part.Text != nil {
content.Text = *part.Text
}
} else if part.Type == model.ContentTypeImageURL {
content.Type = "image"
content.Source = &ImageSource{

View File

@ -20,7 +20,7 @@ var ModelsSupportSystemInstruction = []string{
"gemini-2.0-flash", "gemini-2.0-flash-exp",
"gemini-2.0-flash-thinking-exp-01-21",
"gemini-2.0-flash-lite",
"gemini-2.0-flash-exp-image-generation",
// "gemini-2.0-flash-exp-image-generation",
"gemini-2.0-pro-exp-02-05",
}

View File

@ -109,9 +109,9 @@ func ConvertRequest(textRequest model.GeneralOpenAIRequest) *ChatRequest {
var parts []Part
imageNum := 0
for _, part := range openaiContent {
if part.Type == model.ContentTypeText {
if part.Type == model.ContentTypeText && part.Text != nil && *part.Text != "" {
parts = append(parts, Part{
Text: part.Text,
Text: *part.Text,
})
} else if part.Type == model.ContentTypeImageURL {
imageNum += 1
@ -276,7 +276,7 @@ func responseGeminiChat2OpenAI(response *ChatResponse) *openai.TextResponse {
// Add to content items
contentItems = append(contentItems, model.MessageContent{
Type: model.ContentTypeText,
Text: part.Text,
Text: &part.Text,
})
}

View File

@ -43,7 +43,9 @@ func ConvertRequest(request model.GeneralOpenAIRequest) *ChatRequest {
for _, part := range openaiContent {
switch part.Type {
case model.ContentTypeText:
contentText = part.Text
if part.Text != nil {
contentText = *part.Text
}
case model.ContentTypeImageURL:
_, data, _ := image.GetImageFromUrl(part.ImageURL.Url)
imageUrls = append(imageUrls, data)

View File

@ -140,7 +140,7 @@ func (m Message) ParseContent() []MessageContent {
if ok {
contentList = append(contentList, MessageContent{
Type: ContentTypeText,
Text: content,
Text: &content,
})
return contentList
}
@ -157,7 +157,7 @@ func (m Message) ParseContent() []MessageContent {
if subStr, ok := contentMap["text"].(string); ok {
contentList = append(contentList, MessageContent{
Type: ContentTypeText,
Text: subStr,
Text: &subStr,
})
}
case ContentTypeImageURL:
@ -197,7 +197,7 @@ type ImageURL struct {
type MessageContent struct {
// Type should be one of the following: text/input_audio
Type string `json:"type,omitempty"`
Text string `json:"text"`
Text *string `json:"text,omitempty"`
ImageURL *ImageURL `json:"image_url,omitempty"`
InputAudio *InputAudio `json:"input_audio,omitempty"`
// -------------------------------------