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

chore: Update Tasks.md documentation to include example of using output JSON in tasks #746

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
82 changes: 81 additions & 1 deletion docs/core-concepts/Tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,86 @@ print(f"""
""")
```

## Using Output JSON in Tasks

In the crewAI framework, you can specify that the output of a task should be in JSON format by using the `output_json` parameter. This requires an OpenAI client and ensures that only one output format is set per task. Below is an example demonstrating how to use this feature.

### Example: Defining a Task with JSON Output

Here we define a task that searches for local companies and outputs the results in a JSON format according to a predefined schema using Pydantic models.

```python
from pydantic import BaseModel
from typing import List

# Pydantic model definitions
class CompanyData(BaseModel):
Name: str
Address: str
Phone: str
Website: str
Email: str

class CompanyList(BaseModel):
companies: List[CompanyData] = []

# Task definition using output_json
task_search_companies = Task(
description='Search for local companies using Google Maps. '
'The search parameter is "{query}". '
'For each company, find the phone number and website. '
'Return a JSON list with the details found.',
expected_output='List of companies with name, address, phone, website and e-mail in JSON format without additional comments or character escape formatting.',
agent=search_agent,
tools=[search_tool],
async_execution=False,
output_json=CompanyList,
output_file='companies.json'
)

# Creating a crew to execute the task
crew = Crew(
agents=[search_agent],
tasks=[task_search_companies],
verbose=2
)

# Execute the task
result = crew.kickoff()

# Accessing the task output
print(f"""
Task completed!
Task: {task_search_companies.output.description}
Output: {task_search_companies.output.raw_output}
""")
```

### Example Output

The output of the task, when accessed, would be a JSON structured as follows:

```json
{
"companies": [
{
"Name": "Tech Innovators Inc.",
"Address": "123 Silicon Valley, CA",
"Phone": "+1-800-555-1234",
"Website": "https://techinnovators.com",
"Email": "[email protected]"
},
{
"Name": "Green Solutions LLC",
"Address": "456 Green Street, NY",
"Phone": "+1-800-555-5678",
"Website": "https://greensolutions.com",
"Email": "[email protected]"
}
]
}
```

## Tool Override Mechanism

Specifying tools in a task allows for dynamic adaptation of agent capabilities, emphasizing CrewAI's flexibility.
Expand All @@ -227,4 +307,4 @@ These validations help in maintaining the consistency and reliability of task ex

## Conclusion

Tasks are the driving force behind the actions of agents in crewAI. By properly defining tasks and their outcomes, you set the stage for your AI agents to work effectively, either independently or as a collaborative unit. Equipping tasks with appropriate tools, understanding the execution process, and following robust validation practices are crucial for maximizing CrewAI's potential, ensuring agents are effectively prepared for their assignments and that tasks are executed as intended.
Tasks are the driving force behind the actions of agents in crewAI. By properly defining tasks and their outcomes, you set the stage for your AI agents to work effectively, either independently or as a collaborative unit. Equipping tasks with appropriate tools, understanding the execution process, and following robust validation practices are crucial for maximizing CrewAI's potential, ensuring agents are effectively prepared for their assignments and that tasks are executed as intended.