-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiAccountExample.java
More file actions
107 lines (89 loc) · 4 KB
/
Copy pathMultiAccountExample.java
File metadata and controls
107 lines (89 loc) · 4 KB
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
///usr/bin/env jbang "$0" "$@" ; exit $?
// Multi-account load distribution example
// Run with: jbang MultiAccountExample.java
//DEPS io.github.xyz-jphil:xyz-jphil-ai-gemini-oauth-tool:1.0-SNAPSHOT
import xyz.jphil.ai.gemini.oauth.CodeAssistOAuth;
import xyz.jphil.ai.gemini.oauth.CodeAssistOAuth.RotationStrategy;
import io.github.xyz.jphil.ai.genai.client.CodeAssistApiClient;
import com.google.genai.types.*;
import java.util.List;
/**
* Example demonstrating multi-account usage with load distribution.
*
* Prerequisites:
* 1. Add multiple OAuth accounts:
* java -jar gemini-oauth-tool.jar account add
* java -jar gemini-oauth-tool.jar account add
* 2. Run this example:
* jbang MultiAccountExample.java
*/
public class MultiAccountExample {
public static void main(String[] args) throws Exception {
System.out.println("=== Multi-Account Load Distribution Example ===\n");
// Create OAuth manager with round-robin strategy
CodeAssistOAuth oauth = CodeAssistOAuth.builder()
.rotationStrategy(RotationStrategy.ROUND_ROBIN)
.build();
// List all accounts
var accounts = oauth.accounts();
System.out.println("Available accounts: " + accounts.size());
accounts.forEach(acc -> System.out.println(" - " + acc.email() + " (" + acc.id() + ")"));
System.out.println();
if (accounts.size() < 2) {
System.out.println("⚠️ Add at least 2 accounts to see rotation in action!");
System.out.println(" Run: java -jar gemini-oauth-tool.jar account add");
return;
}
// Get client
CodeAssistApiClient client = oauth.client();
// Make multiple requests with round-robin rotation
System.out.println("Making 5 requests with round-robin rotation:\n");
for (int i = 1; i <= 5; i++) {
// Round-robin automatically selects next account
String token = oauth.accessTokenRoundRobin();
String projectId = oauth.projectId();
// Build request
Content content = Content.builder()
.role("user")
.parts(Part.fromText("Say hello in language #" + i))
.build();
System.out.println("Request " + i + ":");
// Generate content
GenerateContentResponse response = client.generateContent(
"gemini-2.5-flash",
List.of(content),
GenerateContentConfig.builder().build(),
projectId,
token
);
// Extract and print response
String responseText = extractText(response);
System.out.println(" Response: " + responseText);
System.out.println();
}
System.out.println("=== All Strategies ===");
System.out.println("Round-robin: " + oauth.accessTokenRoundRobin());
System.out.println("Least-used: " + oauth.accessTokenLRU());
System.out.println("Random: " + oauth.accessTokenRandom());
System.out.println("Active: " + oauth.accessToken());
System.out.println("\n=== Example Complete ===");
}
private static String extractText(GenerateContentResponse response) {
if (response.candidates() != null && response.candidates().isPresent()) {
var candidates = response.candidates().get();
if (!candidates.isEmpty()) {
var candidate = candidates.get(0);
if (candidate.content() != null && candidate.content().isPresent()) {
var content = candidate.content().get();
if (content.parts() != null && content.parts().isPresent()) {
var parts = content.parts().get();
if (!parts.isEmpty() && parts.get(0).text() != null) {
return parts.get(0).text().orElse("");
}
}
}
}
}
return "[no response]";
}
}