Fetching Image as a Tool for the agent
Thanks for your help in advance sincerely....
** Please make sure you read the contribution guide and file the issues in the rigth place. ** Contribution guide.
Is your feature request related to a problem? Please describe. I define a FigAgent that can describe the academic figures. The code below works fine. But I want the function 'load_png_as_part' as a tool for FigAgent. But when I try to incorporate it, I get an context-length-limit error, as the " genai 'Blob' " is given as the input directly to the LLM.
import base64 from google.adk.agents import LlmAgent from google.adk.models.lite_llm import LiteLlm from google.adk.sessions import InMemorySessionService from google.adk.runners import Runner from google.genai import types from IPython.display import Image, display, Audio, Markdown
-
Create the Figure-describer agent FigAgent = LlmAgent( model=LiteLlm(model="openai/gpt-4o-mini-2024-07-18"), name="FigAgent", description="Describes academic figures", instruction="Respond in Markdown with a concise but detailed description of the image.", )
-
ADK session / runner boiler-plate APP, USER, SESSION = "Test", "u-1234", "sess-1"
svc = InMemorySessionService() svc.create_session(app_name=APP, user_id=USER, session_id=SESSION) runner = Runner(agent=FigAgent, app_name=APP, session_service=svc)
-
Helper: load PNG bytes into a genai
Blobdef load_png_as_part(path: str) -> types.Part: with open(path, "rb") as f: png_bytes = f.read() display(Image(path)) return types.Part(inline_data=types.Blob(mime_type="image/png", data=png_bytes)) -
Fire the request def describe_figure(png_path: str): content = types.Content( role="user", parts=[ types.Part(text="Please provide a detailed description of the figure."), load_png_as_part(png_path) ] ) events = runner.run(user_id=USER, session_id=SESSION, new_message=content) for e in events: if e.is_final_response(): print("\nMarkdown description ↴\n") print(e.content.parts[0].text)
Example call
describe_figure("./scratch3/Paper3-picture-1.png")
Describe the solution you'd like Eventually, I want to use the FigAgent as another tool for another agent, so that is the reason why I want to incorporate the fetching ability as one tool. In this way, I think I can easily incorporate FigAgent in the existing framework provided by Google ADK, such as LoopAgent. But if you have another solution, please let me know!
Can you provide more details on your expectations. If the image bytes are not sent as Blob to the model, what would be your expectation here?