@@ -11,8 +11,11 @@ const (
11
11
ChatMessageRoleSystem = "system"
12
12
ChatMessageRoleUser = "user"
13
13
ChatMessageRoleAssistant = "assistant"
14
+ ChatMessageRoleFunction = "function"
14
15
)
15
16
17
+ const chatCompletionsSuffix = "/chat/completions"
18
+
16
19
var (
17
20
ErrChatCompletionInvalidModel = errors .New ("this model is not supported with this method, please use CreateCompletion client method instead" ) //nolint:lll
18
21
ErrChatCompletionStreamNotSupported = errors .New ("streaming is not supported with this method, please use CreateChatCompletionStream" ) //nolint:lll
@@ -27,6 +30,14 @@ type ChatCompletionMessage struct {
27
30
// - https://github.com/openai/openai-python/blob/main/chatml.md
28
31
// - https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
29
32
Name string `json:"name,omitempty"`
33
+
34
+ FunctionCall * FunctionCall `json:"function_call,omitempty"`
35
+ }
36
+
37
+ type FunctionCall struct {
38
+ Name string `json:"name,omitempty"`
39
+ // call function with arguments in JSON format
40
+ Arguments string `json:"arguments,omitempty"`
30
41
}
31
42
32
43
// ChatCompletionRequest represents a request structure for chat completion API.
@@ -43,12 +54,70 @@ type ChatCompletionRequest struct {
43
54
FrequencyPenalty float32 `json:"frequency_penalty,omitempty"`
44
55
LogitBias map [string ]int `json:"logit_bias,omitempty"`
45
56
User string `json:"user,omitempty"`
57
+ Functions []* FunctionDefine `json:"functions,omitempty"`
58
+ FunctionCall string `json:"function_call,omitempty"`
46
59
}
47
60
61
+ type FunctionDefine struct {
62
+ Name string `json:"name"`
63
+ Description string `json:"description,omitempty"`
64
+ // it's required in function call
65
+ Parameters * FunctionParams `json:"parameters"`
66
+ }
67
+
68
+ type FunctionParams struct {
69
+ // the Type must be JSONSchemaTypeObject
70
+ Type JSONSchemaType `json:"type"`
71
+ Properties map [string ]* JSONSchemaDefine `json:"properties,omitempty"`
72
+ Required []string `json:"required,omitempty"`
73
+ }
74
+
75
+ type JSONSchemaType string
76
+
77
+ const (
78
+ JSONSchemaTypeObject JSONSchemaType = "object"
79
+ JSONSchemaTypeNumber JSONSchemaType = "number"
80
+ JSONSchemaTypeString JSONSchemaType = "string"
81
+ JSONSchemaTypeArray JSONSchemaType = "array"
82
+ JSONSchemaTypeNull JSONSchemaType = "null"
83
+ JSONSchemaTypeBoolean JSONSchemaType = "boolean"
84
+ )
85
+
86
+ // JSONSchemaDefine is a struct for JSON Schema.
87
+ type JSONSchemaDefine struct {
88
+ // Type is a type of JSON Schema.
89
+ Type JSONSchemaType `json:"type,omitempty"`
90
+ // Description is a description of JSON Schema.
91
+ Description string `json:"description,omitempty"`
92
+ // Enum is a enum of JSON Schema. It used if Type is JSONSchemaTypeString.
93
+ Enum []string `json:"enum,omitempty"`
94
+ // Properties is a properties of JSON Schema. It used if Type is JSONSchemaTypeObject.
95
+ Properties map [string ]* JSONSchemaDefine `json:"properties,omitempty"`
96
+ // Required is a required of JSON Schema. It used if Type is JSONSchemaTypeObject.
97
+ Required []string `json:"required,omitempty"`
98
+ }
99
+
100
+ type FinishReason string
101
+
102
+ const (
103
+ FinishReasonStop FinishReason = "stop"
104
+ FinishReasonLength FinishReason = "length"
105
+ FinishReasonFunctionCall FinishReason = "function_call"
106
+ FinishReasonContentFilter FinishReason = "content_filter"
107
+ FinishReasonNull FinishReason = "null"
108
+ )
109
+
48
110
type ChatCompletionChoice struct {
49
- Index int `json:"index"`
50
- Message ChatCompletionMessage `json:"message"`
51
- FinishReason string `json:"finish_reason"`
111
+ Index int `json:"index"`
112
+ Message ChatCompletionMessage `json:"message"`
113
+ // FinishReason
114
+ // stop: API returned complete message,
115
+ // or a message terminated by one of the stop sequences provided via the stop parameter
116
+ // length: Incomplete model output due to max_tokens parameter or token limit
117
+ // function_call: The model decided to call a function
118
+ // content_filter: Omitted content due to a flag from our content filters
119
+ // null: API response still in progress or incomplete
120
+ FinishReason FinishReason `json:"finish_reason"`
52
121
}
53
122
54
123
// ChatCompletionResponse represents a response structure for chat completion API.
@@ -71,7 +140,7 @@ func (c *Client) CreateChatCompletion(
71
140
return
72
141
}
73
142
74
- urlSuffix := "/chat/completions"
143
+ urlSuffix := chatCompletionsSuffix
75
144
if ! checkEndpointSupportsModel (urlSuffix , request .Model ) {
76
145
err = ErrChatCompletionInvalidModel
77
146
return
0 commit comments