Skip to content

Commit

Permalink
Add image recipe (#1336)
Browse files Browse the repository at this point in the history
  • Loading branch information
collindutter authored Nov 12, 2024
1 parent 3556fc7 commit 4481d8e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 additions & 0 deletions docs/examples/talk-to-an-image.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
In this example, we use a [Local File Manager Driver](../griptape-framework/drivers/file-manager-drivers.md) to access the `images` directory in the current working directory.
We then pass this Driver to a [File Manager Tool](../griptape-tools/official-tools/file-manager-tool.md) and an [Image Query Tool](../griptape-tools/official-tools/image-query-tool.md) to interact with the images in the directory.

Note that if you update the `workdir` on a [File Manager Driver](../griptape-framework/drivers/file-manager-drivers.md), it's important to pass that Driver to all the Tools that need to access the same directory.

```python
import os

import requests
from griptape.drivers import LocalFileManagerDriver
from griptape.engines import ImageQueryEngine
from griptape.loaders import ImageLoader
from griptape.structures import Agent
from griptape.tools import FileManagerTool, ImageQueryTool

# Create the images directory if it doesn't exist
images_dir = f"{os.getcwd()}/images"
os.makedirs(images_dir, exist_ok=True)

# Download an image from the web
image_url = "https://picsum.photos/200/300"
image_path = f"{images_dir}/image.jpg"
response = requests.get(image_url)
with open(image_path, "wb") as file:
file.write(response.content)

driver = LocalFileManagerDriver(workdir=images_dir)
agent = Agent(
tools=[
FileManagerTool(file_manager_driver=driver),
ImageQueryTool(image_query_engine=ImageQueryEngine(), image_loader=ImageLoader(file_manager_driver=driver)),
]
)

agent.run("What files are in the current directory?")
agent.run("What is in the file image.jpg?")
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ nav:
- Talk to a Webpage: "examples/talk-to-a-webpage.md"
- Talk to a PDF: "examples/talk-to-a-pdf.md"
- Talk to a Video: "examples/talk-to-a-video.md"
- Talk to an Image: "examples/talk-to-an-image.md"
- Multi Agent Workflows: "examples/multi-agent-workflow.md"
- Shared Memory Between Agents: "examples/multiple-agent-shared-memory.md"
- Chat Sessions with Amazon DynamoDB: "examples/amazon-dynamodb-sessions.md"
Expand Down

0 comments on commit 4481d8e

Please sign in to comment.