-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat: tools structured response #3165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
41fff21
92460d3
af01a3f
cbb351d
6ebfd57
296ca74
c2ce6cb
6aa43b4
4cdd713
fcfa1a2
a6445f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
"""Run `pip install openai` to install dependencies.""" | ||
|
||
from pathlib import Path | ||
|
||
from agno.agent import Agent | ||
from agno.tools.dalle import DalleTools | ||
from agno.utils.media import download_image | ||
|
||
# Create an Agent with the DALL-E tool | ||
agent = Agent(tools=[DalleTools()], name="DALL-E Image Generator") | ||
|
||
# Example 1: Generate a basic image with default settings | ||
agent.print_response( | ||
"Generate an image of a white furry cat sitting on a couch. What is the color of the cat?", | ||
markdown=True, | ||
) | ||
|
||
# agent.print_response( | ||
# "What is the color of the cat?", | ||
# markdown=True | ||
# ) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
from agno.media import ImageArtifact | ||
from agno.team.team import Team | ||
from agno.tools import Toolkit | ||
from agno.tools.function import FunctionCallResult | ||
from agno.utils.log import log_debug, logger | ||
|
||
try: | ||
|
@@ -58,17 +59,9 @@ def __init__( | |
# - Add support for saving images | ||
# - Add support for editing images | ||
|
||
def create_image(self, agent: Union[Agent, Team], prompt: str) -> str: | ||
"""Use this function to generate an image for a prompt. | ||
|
||
Args: | ||
prompt (str): A text description of the desired image. | ||
|
||
Returns: | ||
str: str: A message indicating if the image has been generated successfully or an error message. | ||
""" | ||
def create_image(self, agent: Union[Agent, Team], prompt: str) -> FunctionCallResult: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You still need the docstring, for the model |
||
if not self.api_key: | ||
return "Please set the OPENAI_API_KEY" | ||
return FunctionCallResult(content="Please set the OPENAI_API_KEY") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't have to always use the FunctionCallResult, strings and other types should still be allowed. |
||
|
||
try: | ||
client = OpenAI(api_key=self.api_key) | ||
|
@@ -83,18 +76,24 @@ def create_image(self, agent: Union[Agent, Team], prompt: str) -> str: | |
) | ||
log_debug("Image generated successfully") | ||
|
||
generated_images = [] | ||
|
||
# Update the run response with the image URLs | ||
response_str = "" | ||
if response.data: | ||
for img in response.data: | ||
if img.url: | ||
agent.add_image( | ||
ImageArtifact( | ||
id=str(uuid4()), url=img.url, original_prompt=prompt, revised_prompt=img.revised_prompt | ||
) | ||
image_artifact = ImageArtifact( | ||
id=str(uuid4()), url=img.url, original_prompt=prompt, revised_prompt=img.revised_prompt | ||
) | ||
agent.add_image(image_artifact) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally we won't do this anymore but automatically handle it in the model/agent. But yeh for simplicity lets keep it here for now. We can clean up in Agno 2.0 |
||
response_str += f"Image has been generated at the URL {img.url}\n" | ||
return response_str or "No images were generated" | ||
generated_images.append(image_artifact) | ||
|
||
# Create a more descriptive response that includes details about the image | ||
content = f"The image shows exactly what was requested - {prompt}. " | ||
|
||
return FunctionCallResult(content=content, images=generated_images if generated_images else None) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah this is the winner |
||
except Exception as e: | ||
logger.error(f"Failed to generate image: {e}") | ||
return f"Error: {e}" | ||
return FunctionCallResult(content=f"Error: {e}", images=None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll have to create a separate message for "user" with the image, as if the user sent an image after the tool result. Still not sure whether it would work, but worth a try.