Skip to content

Commit 0e02def

Browse files
committed
Updated to latest Go ADK API
1 parent 56c8067 commit 0e02def

3 files changed

Lines changed: 16 additions & 17 deletions

File tree

examples/go/snippets/artifacts/main.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func BeforeModelCallback(ctx agent.CallbackContext, req *model.LLMRequest) (*mod
3737
// Create a unique filename for the image.
3838
fileName := fmt.Sprintf("user_image_%d.%s", i, strings.Split(part.InlineData.MIMEType, "/")[1])
3939
// Save the image as an artifact.
40-
if err := artifacts.Save(fileName, *part); err != nil {
40+
if _, err := artifacts.Save(ctx, fileName, part); err != nil {
4141
log.Printf("[WARN] Failed to save user image: %v\n", err)
4242
} else {
4343
log.Printf("[INFO] Saved user image artifact: %s\n", fileName)
@@ -69,7 +69,7 @@ func configureRunner() {
6969
Model: model,
7070
Name: "artifact_user_agent",
7171
Instruction: "You are an agent that describes images.",
72-
BeforeModel: []llmagent.BeforeModelCallback{
72+
BeforeModelCallbacks: []llmagent.BeforeModelCallback{
7373
BeforeModelCallback,
7474
},
7575
})
@@ -125,12 +125,14 @@ func loadArtifactsCallback(ctx agent.CallbackContext, req *model.LLMRequest) (*m
125125
const filenameToLoad = "generated_report.pdf"
126126

127127
// Load the artifact from the artifact service.
128-
loadedPart, err := ctx.Artifacts().Load(filenameToLoad)
128+
loadedPartResponse, err := ctx.Artifacts().Load(ctx, filenameToLoad)
129129
if err != nil {
130130
log.Printf("Callback could not load artifact '%s': %v", filenameToLoad, err)
131131
return nil, nil // File not found or error, continue to model.
132132
}
133133

134+
loadedPart := loadedPartResponse.Part
135+
134136
log.Printf("Callback successfully loaded artifact '%s'.", filenameToLoad)
135137

136138
// Ensure there's at least one content in the request to append to.
@@ -142,7 +144,7 @@ func loadArtifactsCallback(ctx agent.CallbackContext, req *model.LLMRequest) (*m
142144

143145
// Add the loaded artifact to the request for the model.
144146
lastContent := req.Contents[len(req.Contents)-1]
145-
lastContent.Parts = append(lastContent.Parts, &loadedPart)
147+
lastContent.Parts = append(lastContent.Parts, loadedPart)
146148
log.Printf("Added artifact '%s' to LLM request.", filenameToLoad)
147149

148150
// Return nil to continue to the next callback or the model.
@@ -240,7 +242,7 @@ func saveReportCallback(ctx agent.CallbackContext, req *model.LLMRequest) (*mode
240242
// Set the filename for the artifact.
241243
filename := "generated_report.pdf"
242244
// Save the artifact to the artifact service.
243-
err = ctx.Artifacts().Save(filename, *reportArtifact)
245+
_, err = ctx.Artifacts().Save(ctx, filename, reportArtifact)
244246
if err != nil {
245247
log.Printf("An unexpected error occurred during Go artifact save: %v", err)
246248
// Depending on requirements, you might want to return an error to the user.
@@ -259,12 +261,14 @@ func saveReportCallback(ctx agent.CallbackContext, req *model.LLMRequest) (*mode
259261
func listUserFilesCallback(ctx agent.CallbackContext, req *model.LLMRequest) (*model.LLMResponse, error) {
260262
log.Println("[Callback] listUserFilesCallback triggered.")
261263
// List the available artifacts from the artifact service.
262-
availableFiles, err := ctx.Artifacts().List()
264+
listResponse, err := ctx.Artifacts().List(ctx)
263265
if err != nil {
264266
log.Printf("An unexpected error occurred during Go artifact list: %v", err)
265267
return nil, nil // Continue, but log the error.
266268
}
267269

270+
availableFiles := listResponse.FileNames
271+
268272
log.Printf("Found %d available files.", len(availableFiles))
269273

270274
// If there are available files, add them to the LLM request.
@@ -325,7 +329,7 @@ func main() {
325329
Model: model,
326330
Name: "reporting_agent",
327331
Instruction: "You are a reporting agent. You can see available files and their contents if they are loaded for you. Summarize any provided files.",
328-
BeforeModel: []llmagent.BeforeModelCallback{
332+
BeforeModelCallbacks: []llmagent.BeforeModelCallback{
329333
saveReportCallback, // Saves report from state
330334
listUserFilesCallback, // Lists available files and adds to prompt
331335
loadArtifactsCallback, // Loads a specific file and adds to prompt
@@ -356,13 +360,13 @@ func main() {
356360
log.Println("\n--- Agent Run 1: Triggering callbacks ---")
357361
log.Println("This run will trigger `saveReportCallback` (from session state), `listUserFilesCallback` (will see the newly saved file), and `loadArtifactsCallback` (will load it).")
358362
userInput := &genai.Content{Parts: []*genai.Part{genai.NewPartFromText("Please summarize the report for me.")}}
359-
for event, err := range r.Run(ctx, session.Session.UserID(), session.Session.ID(), userInput, &agent.RunConfig{
363+
for event, err := range r.Run(ctx, session.Session.UserID(), session.Session.ID(), userInput, agent.RunConfig{
360364
StreamingMode: agent.StreamingModeSSE,
361365
}) {
362366
if err != nil {
363367
log.Printf("AGENT ERROR: %v\n", err)
364-
} else if event.LLMResponse != nil && event.LLMResponse.Content != nil {
365-
for _, p := range event.LLMResponse.Content.Parts {
368+
} else if event != nil && event.Content != nil {
369+
for _, p := range event.Content.Parts {
366370
fmt.Print(string(p.Text))
367371
}
368372
}

examples/go/snippets/callbacks/types_of_callbacks/main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,14 @@ func runBeforeAgentExample() {
8181
// --8<-- [end:before_agent_example]
8282

8383
// --8<-- [start:after_agent_example]
84-
func onAfterAgent(ctx agent.CallbackContext, finalEvent *session.Event, runErr error) (*genai.Content, error) {
84+
func onAfterAgent(ctx agent.CallbackContext) (*genai.Content, error) {
8585
agentName := ctx.AgentName()
8686
invocationID := ctx.InvocationID()
8787
state := ctx.State()
8888

8989
log.Printf("\n[Callback] Exiting agent: %s (Inv: %s)", agentName, invocationID)
9090
log.Printf("[Callback] Current State: %v", state)
9191

92-
if runErr != nil {
93-
log.Printf("[Callback] Agent run produced an error: %v. Passing through.", runErr)
94-
return nil, runErr
95-
}
96-
9792
if addNote, _ := state.Get("add_concluding_note"); addNote == true {
9893
log.Printf("[Callback] State condition 'add_concluding_note=True' met: Replacing agent %s's output.", agentName)
9994
return genai.NewContentFromText(

examples/go/snippets/context/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func runMyToolExample() {
322322

323323
// --8<-- [start:accessing_state_callback]
324324
// Pseudocode: In a Callback function
325-
func myCallback(ctx agent.CallbackContext, event *session.Event, err error) (*genai.Content, error) {
325+
func myCallback(ctx agent.CallbackContext) (*genai.Content, error) {
326326
lastToolResult, err := ctx.State().Get("temp:last_api_result") // Read temporary state
327327
if err == nil {
328328
fmt.Printf("Found temporary result from last tool: %v\n", lastToolResult)

0 commit comments

Comments
 (0)