-
Notifications
You must be signed in to change notification settings - Fork 0
/
gemini_agent.go
172 lines (157 loc) · 4.64 KB
/
gemini_agent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package corefactorer
import (
"context"
"fmt"
"log/slog"
"github.com/google/generative-ai-go/genai"
)
type GeminiAgent struct {
client *genai.Client
chatSession *genai.ChatSession
model *genai.GenerativeModel
logger *slog.Logger
}
func NewGeminiAgent(client *genai.Client, logger *slog.Logger) Agent {
return &GeminiAgent{
client: client,
logger: logger,
}
}
func (a *GeminiAgent) CreateRefactoringTarget(ctx context.Context, prompt string, modelName string, temperature float32) (*RefactoringTarget, error) {
model := a.client.GenerativeModel(modelName)
model.Temperature = &temperature
tool := &genai.Tool{
FunctionDeclarations: []*genai.FunctionDeclaration{
{
Name: functionName,
Description: functionDescription,
Parameters: &genai.Schema{
Type: genai.TypeObject,
Properties: map[string]*genai.Schema{
functionParameter1Name: {
Type: genai.TypeArray,
Description: functionParameter1Description,
Items: &genai.Schema{
Type: genai.TypeString,
},
},
functionParameter2Name: {
Type: genai.TypeArray,
Description: functionParameter2Description,
Items: &genai.Schema{
Type: genai.TypeString,
},
},
},
Required: []string{functionParameter1Name, functionParameter2Name},
},
},
},
}
model.Tools = []*genai.Tool{tool}
//model.ToolConfig = &genai.ToolConfig{
// FunctionCallingConfig: &genai.FunctionCallingConfig{
// Mode: genai.FunctionCallingAuto,
// },
//}
a.model = model
chatSession := model.StartChat()
resp, err := chatSession.SendMessage(ctx, genai.Text(prompt))
if err != nil {
return nil, err
}
a.chatSession = chatSession
if len(resp.Candidates) == 0 {
return nil, fmt.Errorf("no candicates in response")
}
functionCalls := resp.Candidates[0].FunctionCalls()
if len(functionCalls) == 0 {
return nil, fmt.Errorf("no function calls in response")
}
a.logger.Debug("functionCalls[0]", slog.String("name", functionCalls[0].Name), slog.Any("args", functionCalls[0].Args))
target := &RefactoringTarget{
UserPrompt: prompt,
ToolCallID: "",
}
for _, functionCall := range functionCalls {
var tmp RefactoringTarget
for name, value := range functionCall.Args {
switch name {
case functionParameter1Name:
values, ok := value.([]interface{})
if !ok {
return nil, fmt.Errorf("%s: []interface{} type assertion failed", functionParameter1Name)
}
for _, v := range values {
s, ok := v.(string)
if !ok {
return nil, fmt.Errorf("%s: string type assertion failed", functionParameter1Name)
}
tmp.PullRequestURLs = append(tmp.PullRequestURLs, s)
}
case functionParameter2Name:
values, ok := value.([]interface{})
if !ok {
return nil, fmt.Errorf("%s: []interface{} type assertion failed", functionParameter2Name)
}
for _, v := range values {
s, ok := v.(string)
if !ok {
return nil, fmt.Errorf("%s: string type assertion failed", functionParameter2Name)
}
tmp.Files = append(tmp.Files, s)
}
default:
return nil, fmt.Errorf("unknown argument for function call: %s=%+v", name, value)
}
}
target.PullRequestURLs = append(target.PullRequestURLs, tmp.PullRequestURLs...)
target.Files = append(target.Files, tmp.Files...)
}
return target.Unique(), nil
}
func (a *GeminiAgent) CreateRefactoringResult(ctx context.Context, req *RefactoringRequest) (*RefactoringResult, error) {
assistanceMessage, err := req.CreateAssistanceMessage()
if err != nil {
return nil, fmt.Errorf("failed to create assistance message: %w", err)
}
// Pattern 1
//resp, err := a.model.GenerateContent(
// ctx,
// genai.Text(req.UserPrompt)+"\n\n"+genai.Text(assistanceMessage),
//)
// Pattern 2
//resp, err := a.model.GenerateContent(
// ctx,
// genai.Text(req.UserPrompt),
// genai.Text(assistanceMessage),
//)
// Pattern 3
functionResponse := map[string]any{
"pullRequestDiff": req.PullRequests[0].Diff,
}
for _, f := range req.TargetFiles {
functionResponse[f.Path] = f.Content
}
resp, err := a.chatSession.SendMessage(
ctx,
genai.Text(req.UserPrompt),
genai.Text(assistanceMessage),
&genai.FunctionResponse{
Name: functionName,
Response: functionResponse,
},
)
if err != nil {
return nil, err
}
for _, c := range resp.Candidates {
for i, p := range c.Content.Parts {
a.logger.Debug("candidates", slog.Int("index", int(c.Index)), slog.Int("partIndex", i), slog.Any("part", p))
}
}
//fmt.Printf("----- response -----\n%v", resp.Candidates[0].Content.Parts[0])
return &RefactoringResult{
RawContent: fmt.Sprint(resp.Candidates[0].Content.Parts[0]),
}, nil
}