Generative AI has revolutionized the way we approach creative and problem-solving tasks. By integrating external tools and APIs into CrewAI, we can significantly expand the capabilities of our multi-agent systems, enabling them to tackle more complex challenges and produce even more impressive results.
To begin integrating external tools and APIs into your CrewAI project, follow these steps:
Identify Needed Functionality: Determine which external capabilities would benefit your generative AI system.
Choose Appropriate APIs: Research and select APIs that offer the required functionality and have good documentation.
Install Required Libraries: Use pip to install any necessary Python libraries for API interaction.
pip install requests
Obtain API Credentials: Sign up for API access and obtain the necessary authentication keys or tokens.
Create API Wrapper Functions: Develop Python functions that handle API requests and responses.
import requests def get_weather_data(city): api_key = "your_api_key_here" url = f"https://api.weatherapi.com/v1/current.json?key={api_key}&q={city}" response = requests.get(url) return response.json()
from crewai import Agent, Task weather_agent = Agent( name="Weather Analyst", goal="Provide accurate weather information", backstory="Expert in analyzing weather patterns and providing forecasts", tools=[get_weather_data] ) weather_task = Task( description="Get current weather for New York City", agent=weather_agent )
Let's explore how we can enhance a creative writing agent by integrating external APIs for image generation and language translation.
First, we'll create wrapper functions for image generation and translation APIs:
import requests from PIL import Image from io import BytesIO def generate_image(prompt): api_key = "your_dalle_api_key_here" url = "https://api.openai.com/v1/images/generations" headers = { "Content-Type": "application/json", "Authorization": f"Bearer {api_key}" } data = { "prompt": prompt, "n": 1, "size": "512x512" } response = requests.post(url, headers=headers, json=data) image_url = response.json()['data'][0]['url'] return Image.open(BytesIO(requests.get(image_url).content)) def translate_text(text, target_language): api_key = "your_google_translate_api_key_here" url = f"https://translation.googleapis.com/language/translate/v2?key={api_key}" data = { "q": text, "target": target_language } response = requests.post(url, json=data) return response.json()['data']['translations'][0]['translatedText']
Now, let's create agents that utilize these external tools:
from crewai import Agent, Task writer_agent = Agent( name="Creative Writer", goal="Write engaging short stories with visual elements", backstory="Experienced author with a flair for vivid storytelling", tools=[generate_image] ) translator_agent = Agent( name="Multilingual Adapter", goal="Translate stories into multiple languages", backstory="Skilled linguist with expertise in various languages", tools=[translate_text] ) writing_task = Task( description="Write a short story about a magical forest and generate an accompanying image", agent=writer_agent ) translation_task = Task( description="Translate the story into French and Spanish", agent=translator_agent )
Finally, we can run our enhanced CrewAI workflow:
from crewai import Crew creative_crew = Crew( agents=[writer_agent, translator_agent], tasks=[writing_task, translation_task] ) result = creative_crew.kickoff()
This example demonstrates how integrating external APIs can significantly expand the capabilities of our CrewAI agents, allowing them to generate images and translate text as part of their creative process.
As you become more comfortable with basic API integration, consider exploring these advanced techniques:
By integrating external tools and APIs into your CrewAI projects, you can create more powerful, versatile, and intelligent generative AI systems. This approach opens up a world of possibilities for creating sophisticated multi-agent applications that can tackle a wide range of creative and analytical challenges.
31/08/2024 | Generative AI
25/11/2024 | Generative AI
08/11/2024 | Generative AI
27/11/2024 | Generative AI
27/11/2024 | Generative AI
03/12/2024 | Generative AI
03/12/2024 | Generative AI
06/10/2024 | Generative AI
27/11/2024 | Generative AI
27/11/2024 | Generative AI
27/11/2024 | Generative AI
27/11/2024 | Generative AI