Skip to content

Commit

Permalink
feat(api): switch func calling to tools (#157)
Browse files Browse the repository at this point in the history
- Add imports for "fmt" and "log" in `commit.go`
- Refactor message output to use a variable in `commit.go`
- Update error handling for completion response to check for both errors and the number of choices in `commit.go`
- Simplify the handling of summary prefix by removing conditional checks and replacing them with a single error check and assignment in `commit.go`
- Change `CreateFunctionCall` to accept a single `FunctionDefinition` instead of a variadic slice in `openai.go`
- Remove hardcoded system message content in `openai.go`
- Replace `Functions` field with `Tools` in `CreateFunctionCall` to use the new `Tool` struct in `openai.go`
- Remove specific function call strings from the allow list in `openai.go` and replace with a single entry

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Apr 27, 2024
1 parent 3059df1 commit 3ea1edf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
24 changes: 16 additions & 8 deletions cmd/commit.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cmd

import (
"fmt"
"html"
"log"
"os"
"path"
"strconv"
Expand Down Expand Up @@ -203,25 +205,31 @@ var commitCmd = &cobra.Command{
if err != nil {
return err
}
color.Cyan("We are trying to get conventional commit prefix")
message := "We are trying to get conventional commit prefix"
summaryPrix := ""
if client.AllowFuncCall() {
color.Cyan(message + " (Tools)")
resp, err := client.CreateFunctionCall(cmd.Context(), out, openai.SummaryPrefixFunc)
if err != nil {
if err != nil || len(resp.Choices) != 1 {
log.Printf("Completion error: err:%v len(choices):%v\n", err,
len(resp.Choices))
return err
}
if len(resp.Choices) > 0 {
summaryPrix = strings.TrimSpace(resp.Choices[0].Message.Content)
if resp.Choices[0].Message.FunctionCall != nil {
args := openai.GetSummaryPrefixArgs(resp.Choices[0].Message.FunctionCall.Arguments)
summaryPrix = args.Prefix
}

msg := resp.Choices[0].Message
if len(msg.ToolCalls) == 0 {
return fmt.Errorf("current model doesn't support function call")
}

args := openai.GetSummaryPrefixArgs(msg.ToolCalls[len(msg.ToolCalls)-1].Function.Arguments)
summaryPrix = args.Prefix

color.Magenta("PromptTokens: " + strconv.Itoa(resp.Usage.PromptTokens) +
", CompletionTokens: " + strconv.Itoa(resp.Usage.CompletionTokens) +
", TotalTokens: " + strconv.Itoa(resp.Usage.TotalTokens),
)
} else {
color.Cyan(message)
resp, err := client.Completion(cmd.Context(), out)
if err != nil {
return err
Expand Down
20 changes: 9 additions & 11 deletions openai/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,13 @@ type Response struct {
func (c *Client) CreateFunctionCall(
ctx context.Context,
content string,
funcs ...openai.FunctionDefinition,
f openai.FunctionDefinition,
) (resp openai.ChatCompletionResponse, err error) {
t := openai.Tool{
Type: openai.ToolTypeFunction,
Function: &f,
}

req := openai.ChatCompletionRequest{
Model: c.model,
MaxTokens: c.maxTokens,
Expand All @@ -103,18 +108,14 @@ func (c *Client) CreateFunctionCall(
FrequencyPenalty: c.frequencyPenalty,
PresencePenalty: c.presencePenalty,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "You are a helpful assistant.",
},
{
Role: openai.ChatMessageRoleUser,
Content: content,
},
},
Functions: funcs,
FunctionCall: "auto",
Tools: []openai.Tool{t},
}

return c.client.CreateChatCompletion(ctx, req)
}

Expand Down Expand Up @@ -326,10 +327,7 @@ func (c *Client) allowFuncCall(cfg *config) bool {
openai.GPT3Dot5Turbo0125,
openai.GPT3Dot5Turbo0613,
openai.GPT3Dot5Turbo1106,
groq.LLaMA38b.String(),
groq.LLaMA370b.String(),
groq.Mixtral8x7b.String(),
groq.Gemma7b.String():
groq.LLaMA38b.String():
return true
default:
return false
Expand Down

0 comments on commit 3ea1edf

Please sign in to comment.