@@ -11,8 +11,11 @@ const (
1111 ChatMessageRoleSystem = "system"
1212 ChatMessageRoleUser = "user"
1313 ChatMessageRoleAssistant = "assistant"
14+ ChatMessageRoleFunction = "function"
1415)
1516
17+ const chatCompletionsSuffix = "/chat/completions"
18+
1619var (
1720 ErrChatCompletionInvalidModel = errors .New ("this model is not supported with this method, please use CreateCompletion client method instead" ) //nolint:lll
1821 ErrChatCompletionStreamNotSupported = errors .New ("streaming is not supported with this method, please use CreateChatCompletionStream" ) //nolint:lll
@@ -27,6 +30,14 @@ type ChatCompletionMessage struct {
2730 // - https://github.com/openai/openai-python/blob/main/chatml.md
2831 // - https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
2932 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"`
3041}
3142
3243// ChatCompletionRequest represents a request structure for chat completion API.
@@ -43,12 +54,70 @@ type ChatCompletionRequest struct {
4354 FrequencyPenalty float32 `json:"frequency_penalty,omitempty"`
4455 LogitBias map [string ]int `json:"logit_bias,omitempty"`
4556 User string `json:"user,omitempty"`
57+ Functions []* FunctionDefine `json:"functions,omitempty"`
58+ FunctionCall string `json:"function_call,omitempty"`
4659}
4760
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+
48110type 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"`
52121}
53122
54123// ChatCompletionResponse represents a response structure for chat completion API.
@@ -71,7 +140,7 @@ func (c *Client) CreateChatCompletion(
71140 return
72141 }
73142
74- urlSuffix := "/chat/completions"
143+ urlSuffix := chatCompletionsSuffix
75144 if ! checkEndpointSupportsModel (urlSuffix , request .Model ) {
76145 err = ErrChatCompletionInvalidModel
77146 return
0 commit comments