6
6
"fmt"
7
7
"net/http"
8
8
"net/url"
9
+ "regexp"
9
10
10
11
"github.com/appleboy/CodeGPT/core"
11
12
@@ -54,25 +55,36 @@ func (c *Client) Completion(ctx context.Context, content string) (*core.Response
54
55
return & core.Response {
55
56
Content : resp .Content ,
56
57
Usage : core.Usage {
57
- PromptTokens : resp .Usage .PromptTokens ,
58
- CompletionTokens : resp .Usage .CompletionTokens ,
59
- TotalTokens : resp .Usage .TotalTokens ,
58
+ PromptTokens : resp .Usage .PromptTokens ,
59
+ CompletionTokens : resp .Usage .CompletionTokens ,
60
+ TotalTokens : resp .Usage .TotalTokens ,
61
+ CompletionTokensDetails : resp .Usage .CompletionTokensDetails ,
60
62
},
61
63
}, nil
62
64
}
63
65
64
66
// GetSummaryPrefix is an API call to get a summary prefix using function call.
65
67
func (c * Client ) GetSummaryPrefix (ctx context.Context , content string ) (* core.Response , error ) {
66
- resp , err := c .CreateFunctionCall (ctx , content , SummaryPrefixFunc )
67
- if err != nil || len (resp .Choices ) != 1 {
68
- return nil , err
68
+ var resp openai.ChatCompletionResponse
69
+ var err error
70
+ if checkO1Serial .MatchString (c .model ) {
71
+ resp , err = c .CreateChatCompletion (ctx , content )
72
+ if err != nil || len (resp .Choices ) != 1 {
73
+ return nil , err
74
+ }
75
+ } else {
76
+ resp , err = c .CreateFunctionCall (ctx , content , SummaryPrefixFunc )
77
+ if err != nil || len (resp .Choices ) != 1 {
78
+ return nil , err
79
+ }
69
80
}
70
81
71
82
msg := resp .Choices [0 ].Message
72
83
usage := core.Usage {
73
- PromptTokens : resp .Usage .PromptTokens ,
74
- CompletionTokens : resp .Usage .CompletionTokens ,
75
- TotalTokens : resp .Usage .TotalTokens ,
84
+ PromptTokens : resp .Usage .PromptTokens ,
85
+ CompletionTokens : resp .Usage .CompletionTokens ,
86
+ TotalTokens : resp .Usage .TotalTokens ,
87
+ CompletionTokensDetails : resp .Usage .CompletionTokensDetails ,
76
88
}
77
89
if len (msg .ToolCalls ) == 0 {
78
90
return & core.Response {
@@ -88,6 +100,8 @@ func (c *Client) GetSummaryPrefix(ctx context.Context, content string) (*core.Re
88
100
}, nil
89
101
}
90
102
103
+ var checkO1Serial = regexp .MustCompile (`o1-(mini|preview)` )
104
+
91
105
// CreateChatCompletion is an API call to create a function call for a chat message.
92
106
func (c * Client ) CreateFunctionCall (
93
107
ctx context.Context ,
@@ -108,7 +122,7 @@ func (c *Client) CreateFunctionCall(
108
122
PresencePenalty : c .presencePenalty ,
109
123
Messages : []openai.ChatCompletionMessage {
110
124
{
111
- Role : openai .ChatMessageRoleSystem ,
125
+ Role : openai .ChatMessageRoleAssistant ,
112
126
Content : "You are a helpful assistant." ,
113
127
},
114
128
{
@@ -125,6 +139,11 @@ func (c *Client) CreateFunctionCall(
125
139
},
126
140
}
127
141
142
+ if checkO1Serial .MatchString (c .model ) {
143
+ req .MaxTokens = 0
144
+ req .MaxCompletionsTokens = c .maxTokens
145
+ }
146
+
128
147
return c .client .CreateChatCompletion (ctx , req )
129
148
}
130
149
@@ -142,7 +161,7 @@ func (c *Client) CreateChatCompletion(
142
161
PresencePenalty : c .presencePenalty ,
143
162
Messages : []openai.ChatCompletionMessage {
144
163
{
145
- Role : openai .ChatMessageRoleSystem ,
164
+ Role : openai .ChatMessageRoleAssistant ,
146
165
Content : "You are a helpful assistant." ,
147
166
},
148
167
{
@@ -152,6 +171,11 @@ func (c *Client) CreateChatCompletion(
152
171
},
153
172
}
154
173
174
+ if checkO1Serial .MatchString (c .model ) {
175
+ req .MaxTokens = 0
176
+ req .MaxCompletionsTokens = c .maxTokens
177
+ }
178
+
155
179
return c .client .CreateChatCompletion (ctx , req )
156
180
}
157
181
0 commit comments