-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenai.go
62 lines (52 loc) · 1.63 KB
/
genai.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
package main
import (
"context"
"fmt"
"regexp"
"strings"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/genai"
adminpb "cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
)
func geminiComposeQuery(ctx context.Context, resp *adminpb.GetDatabaseDdlResponse, project, s string) (string, error) {
client, err := genai.NewClient(ctx, &genai.ClientConfig{
Project: project,
Location: "us-central1",
Backend: genai.BackendVertexAI,
})
if err != nil {
return "", err
}
var fds descriptorpb.FileDescriptorSet
err = proto.Unmarshal(resp.GetProtoDescriptors(), &fds)
if err != nil {
return "", err
}
response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash-exp",
genai.Text(s),
&genai.GenerateContentConfig{
SystemInstruction: &genai.Content{
Parts: []*genai.Part{{Text: `
Answer in valid Spanner GoogleSQL syntax or valid Spanner Graph GQL syntax.
GoogleSQL syntax is not PostgreSQL syntax.
The output must be valid.
The output should be terminated with terminating semicolon.
NULL_FILTERED indexes can be dropped using DROP INDEX statement, not DROP NULL_FILTERED INDEX statement.
Here is the DDL.
` +
fmt.Sprintf("```\n%v\n```", strings.Join(resp.GetStatements(), ";\n")+";") + `
Here is the Proto Descriptors.
` + fmt.Sprintf("```\n%v\n```", prototext.Format(&fds)),
},
},
},
})
if err != nil {
return "", err
}
// pp.Print(response)
re := regexp.MustCompile("(?s)```[^\\n]*\\n(.*)\\n```")
return re.FindStringSubmatch(response.Candidates[0].Content.Parts[0].Text)[1], nil
}