Skip to content
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

Fal tool #1526

Merged
merged 49 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
987f285
feat: hunyuan_video
anuragts Dec 9, 2024
9092897
fix: lint
anuragts Dec 9, 2024
d9951ab
fix: create a fal tool
anuragts Dec 9, 2024
37b577e
fix: remove print
anuragts Dec 9, 2024
c9895bb
Update cookbook/agents/42_generate_fal_video.py
anuragts Dec 9, 2024
87c1afb
Update cookbook/agents/42_generate_fal_video.py
anuragts Dec 9, 2024
3243c5b
fix: move to fal tools
anuragts Dec 9, 2024
a59d392
fix: enum type
anuragts Dec 9, 2024
31f86fc
fix: name
anuragts Dec 9, 2024
7cf736d
fix: improve instructions
anuragts Dec 9, 2024
d04acf6
fix: return video/image url to agent
anuragts Dec 9, 2024
1b9513c
fix: add fal video agent to playground
anuragts Dec 9, 2024
a50da31
fix: send as schema
anuragts Dec 9, 2024
cf895cc
fix: return as mp4
anuragts Dec 9, 2024
c939f7e
fix: add enum
anuragts Dec 9, 2024
d0cb754
fix: data
anuragts Dec 9, 2024
45e9e86
fix: instruction for video model
anuragts Dec 9, 2024
dd75ea2
fix: more instruction
anuragts Dec 9, 2024
710aa30
Add replicate toolkit
dirkvolter Dec 9, 2024
22a07bb
Add replicate toolkit
dirkvolter Dec 9, 2024
b3bf340
Ignore missing imports
dirkvolter Dec 9, 2024
11adf3e
Fix cookbook for replicate
dirkvolter Dec 10, 2024
2522dd7
Update image/video serialization
dirkvolter Dec 10, 2024
74bde7f
Fix style
dirkvolter Dec 10, 2024
d3a5543
Merge remote-tracking branch 'origin/feature/replicate-toolkit' into …
anuragts Dec 10, 2024
4f16362
fix: improvements
anuragts Dec 10, 2024
a8b3c61
fix: rename file
anuragts Dec 10, 2024
935b680
fix: instruction update
anuragts Dec 10, 2024
c0b018c
Fix typo
dirkvolter Dec 10, 2024
5740cf8
fix: modal labs type mismatch
anuragts Dec 10, 2024
d03c89f
Merge branch 'release/2.7.0' of https://github.com/phidatahq/phidata …
dirkvolter Dec 10, 2024
eccea9a
Add image cookbook
dirkvolter Dec 10, 2024
2443c27
fix: send gif in image
anuragts Dec 10, 2024
a643f21
Update
dirkvolter Dec 10, 2024
72605b4
Merge branch 'release/2.7.0' of https://github.com/phidatahq/phidata …
dirkvolter Dec 10, 2024
499693e
Merge branch 'feature/replicate-toolkit' of https://github.com/phidat…
dirkvolter Dec 10, 2024
312895b
Fix FAL interface
dirkvolter Dec 10, 2024
7b1f696
Merge
dirkvolter Dec 10, 2024
398e227
Fix FAL_KEY
dirkvolter Dec 10, 2024
5e3d3e2
Add modellabs gif to playground app
dirkvolter Dec 10, 2024
7ddc56c
Update name of replicate tools
dirkvolter Dec 10, 2024
a5c4681
Fix style
dirkvolter Dec 10, 2024
0d8c2b4
fix: remove duplicate
anuragts Dec 10, 2024
0e98701
Fix gemini reference
dirkvolter Dec 10, 2024
ad2023b
Merge branch 'hunyuan_video' of https://github.com/phidatahq/phidata …
dirkvolter Dec 10, 2024
8620e57
fix: remove correct
anuragts Dec 10, 2024
2ce1fe6
Fix mypy
dirkvolter Dec 10, 2024
b608861
Merge branch 'hunyuan_video' of https://github.com/phidatahq/phidata …
dirkvolter Dec 10, 2024
d280dfa
Improve instructions
dirkvolter Dec 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cookbook/agents/43_generate_replicate_video.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.tools.replicate import ReplicateToolKit
from phi.tools.replicate import ReplicateToolkit

"""Create an agent specialized for Replicate AI content generation"""

video_agent = Agent(
name="Video Generator Agent",
model=OpenAIChat(id="gpt-4o"),
tools=[
ReplicateToolKit(model="tencent/hunyuan-video:847dfa8b01e739637fc76f480ede0c1d76408e1d694b830b5dfb8e547bf98405")
ReplicateToolkit(model="tencent/hunyuan-video:847dfa8b01e739637fc76f480ede0c1d76408e1d694b830b5dfb8e547bf98405")
],
description="You are an AI agent that can generate images and videos using the Replicate API.",
description="You are an AI agent that can generate videos using the Replicate API.",
instructions=[
"When the user asks you to create a video, use the `generate_media` tool to create the video.",
"Return the URL as raw to the user.",
Expand Down
24 changes: 24 additions & 0 deletions cookbook/agents/44_generate_replicate_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.tools.replicate import ReplicateToolkit

"""Create an agent specialized for Replicate AI content generation"""

video_agent = Agent(
name="Image Generator Agent",
model=OpenAIChat(id="gpt-4o"),
tools=[
ReplicateToolkit(model="luma/photon-flash")
],
description="You are an AI agent that can generate images using the Replicate API.",
instructions=[
"When the user asks you to create an image, use the `generate_media` tool to create the image.",
"Return the URL as raw to the user.",
"Don't convert image URL to markdown or anything else.",
],
markdown=True,
debug_mode=True,
show_tool_calls=True,
)

video_agent.print_response("Generate an image of a horse in the dessert.")
47 changes: 35 additions & 12 deletions phi/tools/replicate.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os
from os import getenv
from urllib.parse import urlparse
from uuid import uuid4

from phi.agent import Agent
from phi.model.content import Video
from phi.model.content import Video, Image
from phi.tools import Toolkit
from phi.utils.log import logger

Expand All @@ -13,19 +15,16 @@
raise ImportError("`replicate` not installed. Please install using `pip install replicate`.")


class ReplicateToolKit(Toolkit):
class ReplicateToolkit(Toolkit):
def __init__(
self,
model: str = "minimax/video-01",
wait_for_completion: bool = True,
max_wait_time: int = 300, # 5 minutes
):
super().__init__(name="replicate_toolkit")
self.api_key = getenv("REPLICATE_API_TOKEN")
if not self.api_key:
logger.error("REPLICATE_API_TOKEN not set. Please set the REPLICATE_API_TOKEN environment variable.")
self.model = model

self.register(self.generate_media)

def generate_media(self, agent: Agent, prompt: str) -> str:
Expand All @@ -38,12 +37,36 @@ def generate_media(self, agent: Agent, prompt: str) -> str:
"""
output: FileOutput = replicate.run(ref=self.model, input={"prompt": prompt})

# Update the run response with the video URLs
agent.add_video(
Video(
id=str(uuid4()),
url=output.url,
# Parse the URL to extract the file extension
parsed_url = urlparse(output.url)
path = parsed_url.path
_, ext = os.path.splitext(path)
ext = ext.lower()

# Define supported extensions
image_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp'}
video_extensions = {'.mp4', '.mov', '.avi', '.mkv', '.flv', '.wmv', '.webm'}

media_id = str(uuid4())

if ext in image_extensions:
agent.add_image(
Image(
id=media_id,
url=output.url,
)
)
media_type = "image"
elif ext in video_extensions:
agent.add_video(
Video(
id=media_id,
url=output.url,
)
)
)
media_type = "video"
else:
logger.error(f"Unsupported media type with extension '{ext}' for URL: {output.url}")
return f"Unsupported media type with extension '{ext}'."

return f"Media generated successfully at {output.url}"
return f"{media_type.capitalize()} generated successfully at {output.url}"