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

feat(integration): Crew AI youtube video analysis and query integration #444

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
75 changes: 75 additions & 0 deletions integrations/crewai-youtube-integration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# CrewAI YouTube Query Integration with uAgents

This project demonstrates the integration of CrewAI's capabilities to analyze YouTube video content with uAgents, enabling automated video content analysis and reporting. The system uses CrewAI to perform detailed analysis of YouTube videos based on user queries and utilizes uAgents to facilitate communication between agents.

## Project Structure

- **crewai_agent.py:** Main agent script that handles the YouTube video analysis using CrewAI.
- **crewagents.py:** Defines the CrewAI agents used for analyzing YouTube content.
- **user.py:** User agent script to interact with the main agent and submit YouTube video queries.
- **tasks.py:** Defines the tasks for analyzing YouTube content.
- **requirements.txt:** Lists the dependencies required for the project.

## Setup Instructions

### Prerequisites
- Python 3.7 or later
- pip (Python package installer)

## Installation

1. Clone the Repository

```bash
git clone https://github.com/fetchai/uAgents.git
cd uAgents/integrations/Crewai-youtube-integration
```

2. Create a virtual environment and activate it:

```bash
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
```

3. Install the dependencies:

```bash
pip install -r requirements.txt
```

## Running the Agents

`Before running the agent please make sure to replace your seed phrases`

**Step 1:** Run the Main Agent
This agent handles the YouTube video analysis using CrewAI.

```bash
python3 crewai_agent.py
```

**Step 2:** Run the User Agent
This agent interacts with the main agent and submits YouTube video queries.
Once you run the main agent take the agent address from the logs and replace it in user agent's code.

```bash
python3 user.py
```

## Interacting with the Agents

1. When you run the user.py script, it will prompt you to enter the YouTube video URL and the search query:

```bash
Provide the YouTube video URL (leave blank for general search):
https://www.youtube.com/watch?v=bBThAij8Mqk
What is the search query for the YouTube video content?
```

2. The user agent sends this information to the main agent, which processes the video and returns the analysis results.

## Expected Output:

![CrewAI integration 1](crewai1.png)
![CrewAI integration 2](crewai2.png)
17 changes: 17 additions & 0 deletions integrations/crewai-youtube-integration/crewagents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from crewai import Agent
from crewai_tools.tools import YoutubeVideoSearchTool

class Agents():
def youtube_search_agent(self, youtube_video_url=None):
if youtube_video_url:
tool = YoutubeVideoSearchTool(youtube_video_url=youtube_video_url)
else:
tool = YoutubeVideoSearchTool()

return Agent(
role='YouTube Researcher',
goal='Analyze YouTube video content to extract insights on relevant topics.',
tools=[tool],
backstory='Expert in analyzing YouTube videos to identify key information and insights.',
verbose=True
)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions integrations/crewai-youtube-integration/crewai_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from dotenv import load_dotenv
load_dotenv()

from crewai import Crew
from crewagents import Agents
from tasks import Tasks
import pydantic
from uagents import Agent, Protocol, Context, Model
from uagents.setup import fund_agent_if_low

class YoutubeQuery(Model):
youtube_video_url : str
search_query : str

class YoutubeResponse(Model):
result: str

agent = Agent(
name = 'Youtube Query Agent',
seed = '<Replace with your seed phrase>',
port = 8000,
endpoint = ['http://localhost:8000/submit']
)

fund_agent_if_low(agent.wallet.address())

@agent.on_event('startup')
async def agent_details(ctx: Context):
ctx.logger.info(f'My agent address is: {agent.address}')


async def crew_ai(youtube_video_url, search_query):
agents = Agents()
tasks = Tasks()

youtube_search_agent = agents.youtube_search_agent(youtube_video_url)
research_youtube_content_task = tasks.research_youtube_content_task(youtube_search_agent, search_query, youtube_video_url)

# Instantiate the crew with a single agent and task
crew = Crew(
agents=[youtube_search_agent],
tasks=[research_youtube_content_task]
)

# Kick off the process
result = crew.kickoff()
return result

youtube_protocol = Protocol('Youtube Protocol')

@youtube_protocol.on_message(model =YoutubeQuery, replies = {YoutubeResponse})
async def youtube_query(ctx: Context, sender: str, msg: YoutubeQuery):
ctx.logger.info(f'User youtube url {msg.youtube_video_url} and query is {msg.search_query}')
result = await crew_ai(msg.youtube_video_url, msg.search_query)
ctx.logger.info(result)
await ctx.send(sender, YoutubeResponse(result = str(result)))
Comment on lines +54 to +56
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap the YouTube query processing in a try-except block to handle any errors gracefully.

try:
        result = await crew_ai(msg.youtube_video_url, msg.search_query)
        ctx.logger.info(result)
        await ctx.send(sender, YoutubeResponse(result=str(result)))
except Exception as e:
        ctx.logger.error(f'Error processing YouTube query: {e}')
        await ctx.send(sender, YoutubeResponse(result='An error occurred while processing your request.'))
        


agent.include(youtube_protocol, publish_manifest=True)

if __name__ == "__main__":
agent.run()
4 changes: 4 additions & 0 deletions integrations/crewai-youtube-integration/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
python-dotenv==1.0.1
crewai==0.14.3
uagents==0.12.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uagents latest version is 0.14.0

https://pypi.org/project/uagents/

crewai-tools
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

20 changes: 20 additions & 0 deletions integrations/crewai-youtube-integration/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from textwrap import dedent
from crewai import Task

class Tasks():
def research_youtube_content_task(self, agent, search_query, youtube_video_url=None):
if youtube_video_url:
description = dedent(f"""\
Analyze the YouTube video at {youtube_video_url} for the search query: "{search_query}". Focus on extracting insights, key points, and relevant information presented in the video.
Compile a report summarizing these insights and how they can be applied to the specific context of the job posting.""")
else:
description = dedent(f"""\
Perform a general search on YouTube for the query: "{search_query}". Identify relevant videos and extract key insights, points, and information from them.
Compile a report summarizing these insights and how they can be applied to the specific context of the job posting.""")

return Task(
description=description,
expected_output=dedent("""\
A comprehensive report detailing the insights, key points, and relevant information extracted from the YouTube video(s). Suggestions on incorporating these insights into the job posting should be included."""),
agent=agent
)
38 changes: 38 additions & 0 deletions integrations/crewai-youtube-integration/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from uagents import Agent, Protocol, Context, Model
from uagents.setup import fund_agent_if_low

class YoutubeQuery(Model):
youtube_video_url : str
search_query : str

class YoutubeResponse(Model):
result: str

agent = Agent(
name = 'User Agent',
seed = '<Replace with your seed phrase>',
port = 8001,
endpoint = ['http://localhost:8001/submit']
)

fund_agent_if_low(agent.wallet.address())

address = '<Replace this with your crewai_agent>'

@agent.on_event('startup')
async def agent_details(ctx: Context):
youtube_video_url = input("Provide the YouTube video URL (leave blank for general search):\n")
search_query = input("What is the search query for the YouTube video content?\n")
await ctx.send(address, YoutubeQuery(youtube_video_url=youtube_video_url,search_query=search_query ))
ctx.logger.info(f'My agent address is: {agent.address}')

youtube_protocol = Protocol('Youtube Protocol')

@youtube_protocol.on_message(model =YoutubeResponse)
async def youtube_query(ctx: Context, sender: str, msg: YoutubeResponse):
ctx.logger.info(f'Results by the user {msg.result}')

agent.include(youtube_protocol, publish_manifest=True)

if __name__ == "__main__":
agent.run()
Loading