Skip to content

Commit c93bbe5

Browse files
committed
feat: add fallback to regular completion on function‑call failures
- Add comment noting that some models do not support function calls and should use regular completion directly - Attempt a function‑call based completion first, then fall back to regular completion if it fails - Include error handling to retry with regular completion when the function call returns an error - Return an error when the completion response does not contain exactly one choice Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
1 parent a3c7f0e commit c93bbe5

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

provider/openai/openai.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,25 @@ func (c *Client) Completion(ctx context.Context, content string) (*core.Response
5858
func (c *Client) GetSummaryPrefix(ctx context.Context, content string) (*core.Response, error) {
5959
var resp openai.ChatCompletionResponse
6060
var err error
61+
62+
// For known models that don't support function calls, use regular completion directly
6163
if checkOSeriesModels.MatchString(c.model) ||
6264
strings.Contains(strings.ToLower(c.model), "deepseek") {
6365
resp, err = c.CreateChatCompletion(ctx, content)
6466
if err != nil || len(resp.Choices) != 1 {
6567
return nil, err
6668
}
6769
} else {
70+
// Try function call first
6871
resp, err = c.CreateFunctionCall(ctx, content, SummaryPrefixFunc)
69-
if err != nil || len(resp.Choices) != 1 {
72+
73+
// If function call fails (model doesn't support it), fallback to regular completion
74+
if err != nil {
75+
resp, err = c.CreateChatCompletion(ctx, content)
76+
if err != nil || len(resp.Choices) != 1 {
77+
return nil, err
78+
}
79+
} else if len(resp.Choices) != 1 {
7080
return nil, err
7181
}
7282
}

0 commit comments

Comments
 (0)