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
| import (
"fmt"
"testing"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
"github.com/weirwei/ikit/ihttp"
"github.com/weirwei/ikit/iutil"
)
type AutoGenerated struct {
Candidates []Candidates `json:"candidates"`
PromptFeedback PromptFeedback `json:"promptFeedback"`
}
type SafetyRatings struct {
Category string `json:"category"`
Probability string `json:"probability"`
}
type Candidates struct {
Content Content `json:"content"`
FinishReason string `json:"finishReason"`
Index int `json:"index"`
SafetyRatings []SafetyRatings `json:"safetyRatings"`
}
type PromptFeedback struct {
SafetyRatings []SafetyRatings `json:"safetyRatings"`
}
// /////
type GenerateContentRequest struct {
Model string `json:"model,omitempty"`
Contents []*Content `json:"contents,omitempty"`
}
type Content struct {
Parts []*Part `json:"parts,omitempty"`
Role string `json:"role,omitempty"`
}
type Part struct {
Text string `json:"text"`
}
const apiKey = "xxxxxxxx" // 这里写你自己申请的 api key
func Gemini(req GenerateContentRequest) (*AutoGenerated, error) {
_url := "https://palm-proxy-blush-two.vercel.app/v1/models/gemini-pro:generateContent"
requestBody := make(map[string]interface{})
var result AutoGenerated
err := iutil.StructMap(&req, requestBody)
if err != nil {
return nil, err
}
resp, err := ihttp.POST(&ihttp.Options{
URL: fmt.Sprintf("%s?key=%s", _url, apiKey),
RequestBody: requestBody,
Encode: ihttp.EncodeJson,
Retry: 0,
Timeout: 300000,
})
if err != nil {
return nil, err
}
err = jsoniter.UnmarshalFromString(resp.ResponseBody, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func TestGemini(t *testing.T) {
req := GenerateContentRequest{
Contents: []*Content{
{
Parts: []*Part{
{
Text: "你好!文心一言",
},
},
Role: "user",
},
},
}
res, err := Gemini(req)
assert.Nil(t, err)
t.Log(iutil.ToJson(res))
}
|