Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
4a49d6c7
authored
Feb 21, 2025
by
1808837298@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: Improve message content parsing with robust type handling
parent
4194f4bd
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
39 deletions
+53
-39
dto/openai_request.go
+53
-39
No files found.
dto/openai_request.go
View file @
4a49d6c7
...
...
@@ -190,72 +190,86 @@ func (m *Message) ParseContent() []MediaContent {
if
m
.
parsedContent
!=
nil
{
return
m
.
parsedContent
}
var
contentList
[]
MediaContent
defer
func
()
{
if
len
(
contentList
)
>
0
{
m
.
parsedContent
=
contentList
}
}()
// 先尝试解析为字符串
var
stringContent
string
if
err
:=
json
.
Unmarshal
(
m
.
Content
,
&
stringContent
);
err
==
nil
{
contentList
=
append
(
contentList
,
MediaContent
{
contentList
=
[]
MediaContent
{
{
Type
:
ContentTypeText
,
Text
:
stringContent
,
})
}}
m
.
parsedContent
=
contentList
return
contentList
}
var
arrayContent
[]
json
.
RawMessage
// 尝试解析为数组
var
arrayContent
[]
map
[
string
]
interface
{}
if
err
:=
json
.
Unmarshal
(
m
.
Content
,
&
arrayContent
);
err
==
nil
{
for
_
,
contentItem
:=
range
arrayContent
{
var
contentMap
map
[
string
]
any
if
err
:=
json
.
Unmarshal
(
contentItem
,
&
contentMap
);
err
!=
nil
{
contentType
,
ok
:=
contentItem
[
"type"
]
.
(
string
)
if
!
ok
{
continue
}
switch
contentMap
[
"type"
]
{
switch
contentType
{
case
ContentTypeText
:
if
subStr
,
ok
:=
contentMap
[
"text"
]
.
(
string
);
ok
{
if
text
,
ok
:=
contentItem
[
"text"
]
.
(
string
);
ok
{
contentList
=
append
(
contentList
,
MediaContent
{
Type
:
ContentTypeText
,
Text
:
subStr
,
Text
:
text
,
})
}
case
ContentTypeImageURL
:
if
subObj
,
ok
:=
contentMap
[
"image_url"
]
.
(
map
[
string
]
any
);
ok
{
detail
,
ok
:=
subObj
[
"detail"
]
if
ok
{
subObj
[
"detail"
]
=
detail
.
(
string
)
}
else
{
subObj
[
"detail"
]
=
"high"
}
contentList
=
append
(
contentList
,
MediaContent
{
Type
:
ContentTypeImageURL
,
ImageUrl
:
MessageImageUrl
{
Url
:
subObj
[
"url"
]
.
(
string
),
Detail
:
subObj
[
"detail"
]
.
(
string
),
},
})
}
else
if
url
,
ok
:=
contentMap
[
"image_url"
]
.
(
string
);
ok
{
imageUrl
:=
contentItem
[
"image_url"
]
switch
v
:=
imageUrl
.
(
type
)
{
case
string
:
contentList
=
append
(
contentList
,
MediaContent
{
Type
:
ContentTypeImageURL
,
ImageUrl
:
MessageImageUrl
{
Url
:
url
,
Url
:
v
,
Detail
:
"high"
,
},
})
case
map
[
string
]
interface
{}
:
url
,
ok1
:=
v
[
"url"
]
.
(
string
)
detail
,
ok2
:=
v
[
"detail"
]
.
(
string
)
if
!
ok2
{
detail
=
"high"
}
if
ok1
{
contentList
=
append
(
contentList
,
MediaContent
{
Type
:
ContentTypeImageURL
,
ImageUrl
:
MessageImageUrl
{
Url
:
url
,
Detail
:
detail
,
},
})
}
}
case
ContentTypeInputAudio
:
if
subObj
,
ok
:=
contentMap
[
"input_audio"
]
.
(
map
[
string
]
any
);
ok
{
contentList
=
append
(
contentList
,
MediaContent
{
Type
:
ContentTypeInputAudio
,
InputAudio
:
MessageInputAudio
{
Data
:
subObj
[
"data"
]
.
(
string
),
Format
:
subObj
[
"format"
]
.
(
string
),
},
})
if
audioData
,
ok
:=
contentItem
[
"input_audio"
]
.
(
map
[
string
]
interface
{});
ok
{
data
,
ok1
:=
audioData
[
"data"
]
.
(
string
)
format
,
ok2
:=
audioData
[
"format"
]
.
(
string
)
if
ok1
&&
ok2
{
contentList
=
append
(
contentList
,
MediaContent
{
Type
:
ContentTypeInputAudio
,
InputAudio
:
MessageInputAudio
{
Data
:
data
,
Format
:
format
,
},
})
}
}
}
}
return
contentList
}
return
nil
if
len
(
contentList
)
>
0
{
m
.
parsedContent
=
contentList
}
return
contentList
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment