@@ -12,8 +12,79 @@ import (
1212 "github.com/openai/openai-go/option"
1313 "github.com/openai/openai-go/packages/ssestream"
1414 "github.com/openai/openai-go/shared"
15+ "github.com/tidwall/sjson"
1516)
1617
18+ func UserMessage (content string ) ChatCompletionMessageParamUnion {
19+ return UserMessageParts (TextPart (content ))
20+ }
21+
22+ func UserMessageParts (parts ... ChatCompletionContentPartUnionParam ) ChatCompletionUserMessageParam {
23+ return ChatCompletionUserMessageParam {
24+ Role : F (ChatCompletionUserMessageParamRoleUser ),
25+ Content : F (parts ),
26+ }
27+ }
28+
29+ func TextPart (content string ) ChatCompletionContentPartTextParam {
30+ return ChatCompletionContentPartTextParam {
31+ Type : F (ChatCompletionContentPartTextTypeText ),
32+ Text : F (content ),
33+ }
34+ }
35+
36+ func RefusalPart (refusal string ) ChatCompletionContentPartRefusalParam {
37+ return ChatCompletionContentPartRefusalParam {
38+ Type : F (ChatCompletionContentPartRefusalTypeRefusal ),
39+ Refusal : F (refusal ),
40+ }
41+ }
42+
43+ func ImagePart (url string ) ChatCompletionContentPartImageParam {
44+ return ChatCompletionContentPartImageParam {
45+ Type : F (ChatCompletionContentPartImageTypeImageURL ),
46+ ImageURL : F (ChatCompletionContentPartImageImageURLParam {
47+ URL : F (url ),
48+ }),
49+ }
50+ }
51+
52+ func AssistantMessage (content string ) ChatCompletionAssistantMessageParam {
53+ return ChatCompletionAssistantMessageParam {
54+ Role : F (ChatCompletionAssistantMessageParamRoleAssistant ),
55+ Content : F ([]ChatCompletionAssistantMessageParamContentUnion {
56+ TextPart (content ),
57+ }),
58+ }
59+ }
60+
61+ func ToolMessage (toolCallID , content string ) ChatCompletionToolMessageParam {
62+ return ChatCompletionToolMessageParam {
63+ Role : F (ChatCompletionToolMessageParamRoleTool ),
64+ ToolCallID : F (toolCallID ),
65+ Content : F ([]ChatCompletionContentPartTextParam {
66+ TextPart (content ),
67+ }),
68+ }
69+ }
70+
71+ func SystemMessage (content string ) ChatCompletionMessageParamUnion {
72+ return ChatCompletionSystemMessageParam {
73+ Role : F (ChatCompletionSystemMessageParamRoleSystem ),
74+ Content : F ([]ChatCompletionContentPartTextParam {
75+ TextPart (content ),
76+ }),
77+ }
78+ }
79+
80+ func FunctionMessage (name , content string ) ChatCompletionMessageParamUnion {
81+ return ChatCompletionFunctionMessageParam {
82+ Role : F (ChatCompletionFunctionMessageParamRoleFunction ),
83+ Name : F (name ),
84+ Content : F (content ),
85+ }
86+ }
87+
1788// ChatCompletionService contains methods and other services that help with
1889// interacting with the openai API.
1990//
@@ -870,10 +941,35 @@ func (r *ChatCompletionMessage) UnmarshalJSON(data []byte) (err error) {
870941 return apijson .UnmarshalRoot (data , r )
871942}
872943
944+ func (r ChatCompletionMessage ) MarshalJSON () (data []byte , err error ) {
945+ s := ""
946+ s , _ = sjson .Set (s , "role" , r .Role )
947+
948+ if r .FunctionCall .Name != "" {
949+ b , err := apijson .Marshal (r .FunctionCall )
950+ if err != nil {
951+ return nil , err
952+ }
953+ s , _ = sjson .SetRaw (s , "function_call" , string (b ))
954+ } else if len (r .ToolCalls ) > 0 {
955+ b , err := apijson .Marshal (r .ToolCalls )
956+ if err != nil {
957+ return nil , err
958+ }
959+ s , _ = sjson .SetRaw (s , "tool_calls" , string (b ))
960+ } else {
961+ s , _ = sjson .Set (s , "content" , r .Content )
962+ }
963+
964+ return []byte (s ), nil
965+ }
966+
873967func (r chatCompletionMessageJSON ) RawJSON () string {
874968 return r .raw
875969}
876970
971+ func (r ChatCompletionMessage ) implementsChatCompletionMessageParamUnion () {}
972+
877973// The role of the author of this message.
878974type ChatCompletionMessageRole string
879975
@@ -944,6 +1040,8 @@ func (r ChatCompletionMessageParam) implementsChatCompletionMessageParamUnion()
9441040// [ChatCompletionUserMessageParam], [ChatCompletionAssistantMessageParam],
9451041// [ChatCompletionToolMessageParam], [ChatCompletionFunctionMessageParam],
9461042// [ChatCompletionMessageParam].
1043+ //
1044+ // This union is additionally satisfied by the return types [ChatCompletionMessage]
9471045type ChatCompletionMessageParamUnion interface {
9481046 implementsChatCompletionMessageParamUnion ()
9491047}
0 commit comments