@@ -12,8 +12,79 @@ import (
12
12
"github.com/openai/openai-go/option"
13
13
"github.com/openai/openai-go/packages/ssestream"
14
14
"github.com/openai/openai-go/shared"
15
+ "github.com/tidwall/sjson"
15
16
)
16
17
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
+
17
88
// ChatCompletionService contains methods and other services that help with
18
89
// interacting with the openai API.
19
90
//
@@ -870,10 +941,35 @@ func (r *ChatCompletionMessage) UnmarshalJSON(data []byte) (err error) {
870
941
return apijson .UnmarshalRoot (data , r )
871
942
}
872
943
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
+
873
967
func (r chatCompletionMessageJSON ) RawJSON () string {
874
968
return r .raw
875
969
}
876
970
971
+ func (r ChatCompletionMessage ) implementsChatCompletionMessageParamUnion () {}
972
+
877
973
// The role of the author of this message.
878
974
type ChatCompletionMessageRole string
879
975
@@ -944,6 +1040,8 @@ func (r ChatCompletionMessageParam) implementsChatCompletionMessageParamUnion()
944
1040
// [ChatCompletionUserMessageParam], [ChatCompletionAssistantMessageParam],
945
1041
// [ChatCompletionToolMessageParam], [ChatCompletionFunctionMessageParam],
946
1042
// [ChatCompletionMessageParam].
1043
+ //
1044
+ // This union is additionally satisfied by the return types [ChatCompletionMessage]
947
1045
type ChatCompletionMessageParamUnion interface {
948
1046
implementsChatCompletionMessageParamUnion ()
949
1047
}
0 commit comments