Skip to content

Commit 4690f33

Browse files
authored
feat(anthropic): tool handling and improve summary prefix logic (#211)
- Add `.codegpt.yaml` to `.gitignore` - Import `encoding/json` in `anthropic.go` - Replace `MessageContentToolResult` with `MessageContentToolUse` in `GetSummaryPrefix` function - Add JSON unmarshalling for `toolUse.Input` in `GetSummaryPrefix` function - Change content assignment from `toolResult.Content[0].GetText()` to `result.Prefix` in `GetSummaryPrefix` function - Define a new `tool` struct with a `Prefix` field in `func.go` - Rename tool definition key from `unit` to `prefix` in `tools` slice Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 193e00b commit 4690f33

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ dist
1818
**/.DS_Store
1919
bin
2020
release
21+
.codegpt.yaml

provider/anthropic/anthropic.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package anthropic
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
67
"fmt"
78

@@ -68,20 +69,25 @@ func (c *Client) GetSummaryPrefix(ctx context.Context, content string) (*core.Re
6869
return nil, err
6970
}
7071

71-
var toolResult *anthropic.MessageContentToolResult
72+
var toolUse *anthropic.MessageContentToolUse
7273

7374
for _, c := range resp.Content {
74-
if c.Type == anthropic.MessagesContentTypeToolResult {
75-
toolResult = c.MessageContentToolResult
75+
if c.Type == anthropic.MessagesContentTypeToolUse {
76+
toolUse = c.MessageContentToolUse
7677
}
7778
}
7879

79-
if toolResult == nil {
80+
if toolUse == nil {
8081
return nil, errors.New("no tool use found in response")
8182
}
8283

84+
var result tool
85+
if err := json.Unmarshal(toolUse.Input, &result); err != nil {
86+
return nil, fmt.Errorf("failed to unmarshal tool use input: %w", err)
87+
}
88+
8389
return &core.Response{
84-
Content: toolResult.Content[0].GetText(),
90+
Content: result.Prefix,
8591
Usage: core.Usage{
8692
PromptTokens: resp.Usage.InputTokens,
8793
CompletionTokens: resp.Usage.OutputTokens,

provider/anthropic/func.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,30 @@ import (
55
"github.com/sashabaranov/go-openai/jsonschema"
66
)
77

8+
// tool represents a structure with a single field Prefix.
9+
// Prefix is a string that is serialized to JSON with the key "prefix".
10+
type tool struct {
11+
Prefix string `json:"prefix"`
12+
}
13+
14+
// tools is a slice of ToolDefinition that contains the definition for various tools.
15+
// Each ToolDefinition includes the following fields:
16+
// - Name: The name of the tool.
17+
// - Description: A brief description of what the tool does.
18+
// - InputSchema: The schema for the input that the tool expects, defined using jsonschema.Definition.
19+
// - Type: The type of the input, which is an object.
20+
// - Properties: A map of property names to their definitions. In this case, it includes:
21+
// - "prefix": A string that must be one of the specified values ("build", "chore", "ci", "docs", "feat", "fix", "perf", "refactor", "style", "test").
22+
// This property also has a description indicating that it is the prefix to use for the summary.
23+
// - Required: A list of required properties, which includes "prefix".
824
var tools = []anthropic.ToolDefinition{
925
{
1026
Name: "get_summary_prefix",
1127
Description: "Get a summary prefix using function call",
1228
InputSchema: jsonschema.Definition{
1329
Type: jsonschema.Object,
1430
Properties: map[string]jsonschema.Definition{
15-
"unit": {
31+
"prefix": {
1632
Type: jsonschema.String,
1733
Enum: []string{
1834
"build", "chore", "ci",

0 commit comments

Comments
 (0)