Skip to content

Commit 2bd65aa

Browse files
authored
feat(chat): support function call api (#369)
* feat(chat): support function call api * rename struct & add const ChatMessageRoleFunction
1 parent 7e76a68 commit 2bd65aa

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed

chat.go

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1619
var (
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+
48110
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"`
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

chat_stream.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (c *Client) CreateChatCompletionStream(
4040
ctx context.Context,
4141
request ChatCompletionRequest,
4242
) (stream *ChatCompletionStream, err error) {
43-
urlSuffix := "/chat/completions"
43+
urlSuffix := chatCompletionsSuffix
4444
if !checkEndpointSupportsModel(urlSuffix, request.Model) {
4545
err = ErrChatCompletionInvalidModel
4646
return

completion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ var disabledModelsForEndpoints = map[string]map[string]bool{
6565
GPT432K0314: true,
6666
GPT432K0613: true,
6767
},
68-
"/chat/completions": {
68+
chatCompletionsSuffix: {
6969
CodexCodeDavinci002: true,
7070
CodexCodeCushman001: true,
7171
CodexCodeDavinci001: true,

0 commit comments

Comments
 (0)