-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentWithFileUpload.java
More file actions
74 lines (62 loc) · 2.98 KB
/
AgentWithFileUpload.java
File metadata and controls
74 lines (62 loc) · 2.98 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
package examples;
import com.glean.api_client.glean_api_client.Glean;
import com.glean.api_client.glean_api_client.models.components.AgentRunCreate;
import com.glean.api_client.glean_api_client.models.components.ChatFile;
import com.glean.api_client.glean_api_client.models.components.UploadChatFilesRequest;
import com.glean.api_client.glean_api_client.models.components.UploadChatFilesResponse;
import com.glean.api_client.glean_api_client.models.operations.CreateAndWaitRunResponse;
import com.glean.api_client.glean_api_client.models.operations.UploadchatfilesResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* Example demonstrating the correct workflow for uploading files and using them with agent runs.
*
* This example shows the two-step process required:
* 1. Upload the file using chat.uploadFiles() to get a file ID
* 2. Pass the file ID (as a string) to agents.run() in the input map
*/
public class AgentWithFileUpload {
public static void main(String[] args) throws Exception {
// Initialize the SDK client
Glean sdk = Glean.builder()
.apiToken(System.getenv().getOrDefault("GLEAN_API_TOKEN", ""))
.build();
// Step 1: Upload a file to get a file ID
File sampleFile = new File("sample_data.csv");
UploadchatfilesResponse uploadResponse = sdk.client().chat().uploadFiles(
UploadChatFilesRequest.builder()
.files(List.of(sampleFile))
.build());
// Extract the file ID from the response
String fileId = uploadResponse.uploadChatFilesResponse()
.flatMap(UploadChatFilesResponse::files)
.map(files -> {
if (files.isEmpty()) {
throw new RuntimeException("No files returned from upload");
}
return files.get(0);
})
.flatMap(ChatFile::id)
.orElseThrow(() -> new RuntimeException("File ID not found in upload response"));
System.out.println("File uploaded successfully. File ID: " + fileId);
// Step 2: Create an agent run with the file ID in the input map
// Note: The file ID must be passed as a string, not as a file object
Map<String, Object> input = new HashMap<>();
input.put("fileId", fileId);
// Add any other input parameters your agent requires
input.put("query", "Analyze this file");
AgentRunCreate runRequest = AgentRunCreate.builder()
.agentId("your-agent-id")
.input(Optional.of(input))
.build();
CreateAndWaitRunResponse response = sdk.client().agents().run(runRequest);
// Handle the response
if (response.agentRunWaitResponse().isPresent()) {
System.out.println("Agent run completed successfully");
// Process the response as needed
}
}
}