4
4
import com .alibaba .fastjson .JSONObject ;
5
5
import com .codingapi .gemini .pojo .Embedding ;
6
6
import com .codingapi .gemini .pojo .Generate ;
7
+ import com .codingapi .gemini .pojo .Model ;
8
+ import com .codingapi .gemini .pojo .Models ;
7
9
import lombok .extern .slf4j .Slf4j ;
8
10
import org .springframework .core .io .Resource ;
9
11
import org .springframework .http .HttpEntity ;
@@ -27,13 +29,13 @@ public class GeminiClient {
27
29
28
30
private final RestTemplate restTemplate ;
29
31
private final String apiKey ;
30
-
31
- private final static String baseUrl = "https://generativelanguage.googleapis.com/v1beta/" ;
32
-
32
+ private final String baseUrl ;
33
33
private final HttpHeaders headers ;
34
34
35
- public GeminiClient (String apiKey , String proxyHost , int proxyPort ) {
35
+
36
+ public GeminiClient (String version , String apiKey , String proxyHost , int proxyPort ) {
36
37
this .apiKey = apiKey ;
38
+ this .baseUrl = "https://generativelanguage.googleapis.com/" + version + "/" ;
37
39
this .restTemplate = new RestTemplate ();
38
40
39
41
this .headers = new HttpHeaders ();
@@ -48,7 +50,7 @@ public GeminiClient(String apiKey, String proxyHost, int proxyPort) {
48
50
}
49
51
50
52
public void stream (Generate .Request request , Consumer <Generate .Response > consumer ) throws IOException {
51
- String url = baseUrl + "models/gemini-pro :streamGenerateContent?key=" + apiKey ;
53
+ String url = baseUrl + request . getModel () + " :streamGenerateContent?key=" + apiKey ;
52
54
String json = request .toJSONString ();
53
55
log .info ("json:{}" , json );
54
56
HttpEntity <String > httpEntity = new HttpEntity <>(json , headers );
@@ -68,26 +70,50 @@ public void stream(Generate.Request request, Consumer<Generate.Response> consume
68
70
69
71
70
72
public Generate .Response generate (Generate .Request request ) {
71
- String url ;
72
- if (request .isVision ()) {
73
- url = baseUrl + "models/gemini-pro-vision:generateContent?key=" + apiKey ;
74
- } else {
75
- url = baseUrl + "models/gemini-pro:generateContent?key=" + apiKey ;
76
- }
73
+ String url = baseUrl + request .getModel () + ":generateContent?key=" + apiKey ;
77
74
String json = request .toJSONString ();
78
75
log .info ("json:{}" , json );
79
76
HttpEntity <String > httpEntity = new HttpEntity <>(json , headers );
80
77
ResponseEntity <String > response = restTemplate .exchange (url , HttpMethod .POST , httpEntity , String .class );
81
78
return JSONObject .parseObject (response .getBody (), Generate .Response .class );
82
79
}
83
80
81
+
82
+ public int counts (Generate .Request request ) {
83
+ String url = baseUrl + request .getModel () + ":countTokens?key=" + apiKey ;
84
+ String json = request .toJSONString ();
85
+ log .info ("json:{}" , json );
86
+ HttpEntity <String > httpEntity = new HttpEntity <>(json , headers );
87
+ ResponseEntity <String > response = restTemplate .exchange (url , HttpMethod .POST , httpEntity , String .class );
88
+ Generate .TotalToken result = JSONObject .parseObject (response .getBody (), Generate .TotalToken .class );
89
+ assert result != null ;
90
+ return result .getTotalTokens ();
91
+ }
92
+
93
+
84
94
public Embedding .Response embedding (Embedding .Request request ) {
85
- String url = baseUrl + "models//embedding-001 :embedContent?key=" + apiKey ;
95
+ String url = baseUrl + request . getModel () + " :embedContent?key=" + apiKey ;
86
96
String json = request .toJSONString ();
87
97
log .info ("json:{}" , json );
88
98
HttpEntity <String > httpEntity = new HttpEntity <>(json , headers );
89
99
ResponseEntity <String > response = restTemplate .exchange (url , HttpMethod .POST , httpEntity , String .class );
90
100
return JSONObject .parseObject (response .getBody (), Embedding .Response .class );
91
101
}
92
102
103
+
104
+ public Model model (String model ) {
105
+ String url = baseUrl + model + "?key=" + apiKey ;
106
+ HttpEntity <String > httpEntity = new HttpEntity <>(headers );
107
+ ResponseEntity <String > response = restTemplate .exchange (url , HttpMethod .GET , httpEntity , String .class );
108
+ return JSONObject .parseObject (response .getBody (), Model .class );
109
+ }
110
+
111
+ public Models models () {
112
+ String url = baseUrl + "models" + "?key=" + apiKey ;
113
+ HttpEntity <String > httpEntity = new HttpEntity <>(headers );
114
+ ResponseEntity <String > response = restTemplate .exchange (url , HttpMethod .GET , httpEntity , String .class );
115
+ return JSONObject .parseObject (response .getBody (), Models .class );
116
+ }
117
+
118
+
93
119
}
0 commit comments