Skip to content

Commit e680029

Browse files
Add CrewAI to docs (#165)
* Add CrewAI to docs * Add prereq sections * Update pages/home/crewai/use-arcade-tools.mdx Co-authored-by: Nate Barbettini <nate@arcade-ai.com> * Add prereq section to langchain too * why use custom auth flow * Update pages/home/crewai/use-arcade-tools.mdx Co-authored-by: Nate Barbettini <nate@arcade.dev> * Update pages/home/crewai/custom-auth-flow.mdx Co-authored-by: Nate Barbettini <nate@arcade.dev> * move code to python file * Address nate's comment * Update pages/home/crewai/_meta.ts Co-authored-by: Nate Barbettini <nate@arcade.dev> --------- Co-authored-by: Nate Barbettini <nate@arcade-ai.com> Co-authored-by: Nate Barbettini <nate@arcade.dev>
1 parent e0cdcf9 commit e680029

File tree

10 files changed

+396
-1
lines changed

10 files changed

+396
-1
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from typing import Any
2+
3+
from crewai import Agent, Crew, Task
4+
from crewai.llm import LLM
5+
from crewai_arcade import ArcadeToolManager
6+
7+
USER_ID = "user@example.com"
8+
9+
def custom_auth_flow(
10+
manager: ArcadeToolManager, tool_name: str, **tool_input: dict[str, Any]
11+
) -> Any:
12+
"""Custom auth flow for the ArcadeToolManager
13+
14+
This function is called when CrewAI needs to call a tool that requires authorization.
15+
Authorization is handled before executing the tool.
16+
This function overrides the ArcadeToolManager's default auth flow performed by ArcadeToolManager.authorize_tool
17+
"""
18+
# Get authorization status
19+
auth_response = manager.authorize(tool_name, USER_ID)
20+
21+
# If the user is not authorized for the tool,
22+
# then we need to handle the authorization before executing the tool
23+
if not manager.is_authorized(auth_response.id):
24+
print(f"Authorization required for tool: '{tool_name}' with inputs:")
25+
for input_name, input_value in tool_input.items():
26+
print(f" {input_name}: {input_value}")
27+
# Handle authorization
28+
print(f"\nTo authorize, visit: {auth_response.url}")
29+
# Block until the user has completed the authorization
30+
auth_response = manager.wait_for_auth(auth_response)
31+
32+
# Ensure authorization completed successfully
33+
if not manager.is_authorized(auth_response.id):
34+
raise ValueError(f"Authorization failed for {tool_name}. URL: {auth_response.url}")
35+
else:
36+
print(f"Authorization already granted for tool: '{tool_name}' with inputs:")
37+
for input_name, input_value in tool_input.items():
38+
print(f" {input_name}: {input_value}")
39+
40+
41+
def tool_manager_callback(tool_manager: ArcadeToolManager, tool_name: str, **tool_input: dict[str, Any]) -> Any:
42+
"""Tool executor callback with custom auth flow for the ArcadeToolManager
43+
44+
ArcadeToolManager's default executor handles authorization and tool execution.
45+
This function overrides the default executor to handle authorization in a custom way and then executes the tool.
46+
"""
47+
custom_auth_flow(tool_manager, tool_name, **tool_input)
48+
return tool_manager.execute_tool(USER_ID, tool_name, **tool_input)
49+
50+
51+
manager = ArcadeToolManager(executor=tool_manager_callback)
52+
53+
tools = manager.get_tools(tools=["Google.ListEmails"], toolkits=["Slack"])
54+
55+
crew_agent = Agent(
56+
role="Main Agent",
57+
backstory="You are a helpful assistant",
58+
goal="Help the user with their requests",
59+
tools=tools,
60+
allow_delegation=False,
61+
verbose=True,
62+
llm=LLM(model="gpt-4o"),
63+
)
64+
65+
task = Task(
66+
description="Get the 5 most recent emails from the user's inbox and summarize them and recommend a response for each.",
67+
expected_output="A bulleted list with a one sentence summary of each email and a recommended response to the email.",
68+
agent=crew_agent,
69+
tools=crew_agent.tools,
70+
)
71+
72+
crew = Crew(
73+
agents=[crew_agent],
74+
tasks=[task],
75+
verbose=True,
76+
memory=True,
77+
)
78+
79+
result = crew.kickoff()
80+
81+
print("\n\n\n ------------ Result ------------ \n\n\n")
82+
print(result)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from typing import Any
2+
3+
from crewai_arcade import ArcadeToolManager
4+
5+
USER_ID = "user@example.com"
6+
7+
def custom_auth_flow(
8+
manager: ArcadeToolManager, tool_name: str, **tool_input: dict[str, Any]
9+
) -> Any:
10+
"""Custom auth flow for the ArcadeToolManager
11+
12+
This function is called when CrewAI needs to call a tool that requires authorization.
13+
Authorization is handled before executing the tool.
14+
This function overrides the ArcadeToolManager's default auth flow performed by ArcadeToolManager.authorize_tool
15+
"""
16+
# Get authorization status
17+
auth_response = manager.authorize(tool_name, USER_ID)
18+
19+
# If the user is not authorized for the tool,
20+
# then we need to handle the authorization before executing the tool
21+
if not manager.is_authorized(auth_response.id):
22+
print(f"Authorization required for tool: '{tool_name}' with inputs:")
23+
for input_name, input_value in tool_input.items():
24+
print(f" {input_name}: {input_value}")
25+
# Handle authorization
26+
print(f"\nTo authorize, visit: {auth_response.url}")
27+
# Block until the user has completed the authorization
28+
auth_response = manager.wait_for_auth(auth_response)
29+
30+
# Ensure authorization completed successfully
31+
if not manager.is_authorized(auth_response.id):
32+
raise ValueError(f"Authorization failed for {tool_name}. URL: {auth_response.url}")
33+
else:
34+
print(f"Authorization already granted for tool: '{tool_name}' with inputs:")
35+
for input_name, input_value in tool_input.items():
36+
print(f" {input_name}: {input_value}")
37+
38+
39+
def tool_manager_callback(tool_manager: ArcadeToolManager, tool_name: str, **tool_input: dict[str, Any]) -> Any:
40+
"""Tool executor callback with custom auth flow for the ArcadeToolManager
41+
42+
ArcadeToolManager's default executor handles authorization and tool execution.
43+
This function overrides the default executor to handle authorization in a custom way and then executes the tool.
44+
"""
45+
custom_auth_flow(tool_manager, tool_name, **tool_input)
46+
return tool_manager.execute_tool(USER_ID, tool_name, **tool_input)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from crewai import Agent, Crew, Task
2+
from crewai.llm import LLM
3+
from crewai_arcade import ArcadeToolManager
4+
5+
manager = ArcadeToolManager(default_user_id="user@example.com")
6+
7+
tools = manager.get_tools(tools=["Google.ListEmails"])
8+
9+
10+
crew_agent = Agent(
11+
role="Main Agent",
12+
backstory="You are a helpful assistant",
13+
goal="Help the user with their requests",
14+
tools=tools,
15+
allow_delegation=False,
16+
verbose=True,
17+
llm=LLM(model="gpt-4o"),
18+
)
19+
20+
task = Task(
21+
description="Get the 5 most recent emails from the user's inbox and summarize them and recommend a response for each.",
22+
expected_output="A bulleted list with a one sentence summary of each email and a recommended response to the email.",
23+
agent=crew_agent,
24+
tools=crew_agent.tools,
25+
)
26+
27+
crew = Crew(
28+
agents=[crew_agent],
29+
tasks=[task],
30+
verbose=True,
31+
memory=True,
32+
)
33+
34+
result = crew.kickoff()
35+
36+
print("\n\n\n ------------ Result ------------ \n\n\n")
37+
print(result)

pages/home/crewai.mdx

Lines changed: 0 additions & 1 deletion
This file was deleted.

pages/home/crewai/_meta.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
"use-arcade-tools": "Using Arcade tools",
3+
"custom-auth-flow": "Custom auth flow",
4+
};
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
title: "Custom Auth Flow with CrewAI"
3+
description: "Learn how to create a custom auth flow with CrewAI"
4+
---
5+
6+
import { Steps } from "nextra/components";
7+
8+
## Custom Auth Flow with CrewAI
9+
10+
In this guide, we will explore how to create a custom auth flow that will be performed before executing Arcade tools within your CrewAI agent team.
11+
12+
The `ArcadeToolManager`'s built-in authorization and tool execution flows work well for many typical use cases. However, some scenarios call for a tailored approach. By implementing a custom auth flow, you gain flexibility in handling tool authorization. If your use case calls for a unique interface, additional approval steps, or specialized error handling, then this guide is for you.
13+
14+
<Steps>
15+
16+
### Prerequisites
17+
18+
- [Obtain an Arcade API key](/home/api-key)
19+
20+
### Set up your environment
21+
22+
Install the required package, and ensure your environment variables are set with your Arcade and OpenAI API keys:
23+
24+
```bash
25+
pip install crewai-arcade
26+
```
27+
28+
### Configure API keys
29+
30+
Provide your Arcade and OpenAI API keys. You can store them in environment variables like so:
31+
32+
```bash
33+
export ARCADE_API_KEY="your_arcade_api_key"
34+
export OPENAI_API_KEY="your_openai_api_key"
35+
```
36+
37+
### Define your custom auth flow
38+
39+
The custom auth flow defined in the following code snippet is a function that will be called whenever CrewAI needs to call a tool.
40+
41+
```python file=<rootDir>/examples/code/home/crewai/custom_auth_flow_callback_section.py
42+
```
43+
44+
### Get Arcade tools
45+
46+
You can now provide the tool manager callback to the `ArcadeToolManager` upon initialization:
47+
48+
```python
49+
# Provide the tool manager callback to the ArcadeToolManager
50+
manager = ArcadeToolManager(executor=tool_manager_callback)
51+
52+
# Retrieve the provided tools and/or toolkits as CrewAI StructuredTools.
53+
tools = manager.get_tools(tools=["Google.ListEmails"], toolkits=["Slack"])
54+
```
55+
56+
### Use tools in your CrewAI agent team
57+
58+
Create a Crew that uses your tools with the custom auth flow. When the tool is called, your tool manager callback will be called to handle the authorization and then the tool will be executed.
59+
60+
```python
61+
from crewai import Agent, Crew, Task
62+
from crewai.llm import LLM
63+
64+
crew_agent = Agent(
65+
role="Main Agent",
66+
backstory="You are a helpful assistant",
67+
goal="Help the user with their requests",
68+
tools=tools,
69+
allow_delegation=False,
70+
verbose=True,
71+
llm=LLM(model="gpt-4o"),
72+
)
73+
74+
task = Task(
75+
description="Get the 5 most recent emails from the user's inbox and summarize them and recommend a response for each.",
76+
expected_output="A bulleted list with a one sentence summary of each email and a recommended response to the email.",
77+
agent=crew_agent,
78+
tools=crew_agent.tools,
79+
)
80+
81+
crew = Crew(
82+
agents=[crew_agent],
83+
tasks=[task],
84+
verbose=True,
85+
memory=True,
86+
)
87+
88+
result = crew.kickoff()
89+
90+
print("\n\n\n ------------ Result ------------ \n\n\n")
91+
print(result)
92+
```
93+
94+
</Steps>
95+
96+
<ToggleContent showText="Click to view a full example" hideText="Hide example">
97+
98+
```python file=<rootDir>/examples/code/home/crewai/custom_auth_flow.py
99+
```
100+
101+
</ToggleContent>
102+
103+
## Next steps
104+
105+
Now you're ready to integrate Arcade tools with a custom auth flow into your own CrewAI agent team.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
title: "Use Arcade tools with CrewAI"
3+
description: "Integrate Arcade tools into your CrewAI applications"
4+
---
5+
6+
import { Steps } from "nextra/components";
7+
8+
## Use CrewAI with Arcade
9+
10+
In this guide, we will explore how to integrate Arcade tools into your CrewAI application. Follow the step-by-step instructions below. If a tool requires authorization, an authorization URL will appear in the console, waiting for your approval. This process ensures that only the tools you choose to authorize are executed.
11+
12+
To tailor the tool authorization flow to meet your application's specific needs, check out the [Custom Auth Flow with CrewAI](/home/crewai/custom-auth-flow) guide.
13+
14+
<Steps>
15+
16+
### Prerequisites
17+
18+
- [Obtain an Arcade API key](/home/api-key)
19+
20+
### Set up your environment
21+
22+
Install the required package, and ensure your environment variables are set with your Arcade and OpenAI API keys:
23+
24+
```bash
25+
pip install crewai-arcade
26+
```
27+
28+
### Configure API keys
29+
30+
Provide your Arcade and OpenAI API keys. You can store them in environment variables like so:
31+
32+
```bash
33+
export ARCADE_API_KEY="your_arcade_api_key"
34+
export OPENAI_API_KEY="your_openai_api_key"
35+
```
36+
37+
### Get Arcade tools
38+
39+
Use the `ArcadeToolManager` to initialize, add, and get Arcade tools:
40+
41+
```python
42+
from crewai_arcade import ArcadeToolManager
43+
44+
manager = ArcadeToolManager(default_user_id="user@example.com")
45+
46+
"""
47+
Retrieves the provided tools and/or toolkits as CrewAI StructuredTools.
48+
"""
49+
tools = manager.get_tools(tools=["Google.ListEmails"], toolkits=["Slack"])
50+
```
51+
52+
### Use tools in your CrewAI agent team
53+
54+
Create a Crew that uses your tools. When the tool is called, you will be prompted to go visit an authorization page to authorize the tool before it executes.
55+
56+
```python
57+
from crewai import Agent, Crew, Task
58+
from crewai.llm import LLM
59+
60+
crew_agent = Agent(
61+
role="Main Agent",
62+
backstory="You are a helpful assistant",
63+
goal="Help the user with their requests",
64+
tools=tools,
65+
allow_delegation=False,
66+
verbose=True,
67+
llm=LLM(model="gpt-4o"),
68+
)
69+
70+
task = Task(
71+
description="Get the 5 most recent emails from the user's inbox and summarize them and recommend a response for each.",
72+
expected_output="A bulleted list with a one sentence summary of each email and a recommended response to the email.",
73+
agent=crew_agent,
74+
tools=crew_agent.tools,
75+
)
76+
77+
crew = Crew(
78+
agents=[crew_agent],
79+
tasks=[task],
80+
verbose=True,
81+
memory=True,
82+
)
83+
84+
result = crew.kickoff()
85+
86+
print("\n\n\n ------------ Result ------------ \n\n\n")
87+
print(result)
88+
```
89+
90+
</Steps>
91+
92+
<ToggleContent showText="Click to view a full example" hideText="Hide example">
93+
94+
```python file=<rootDir>/examples/code/home/crewai/use_arcade_tools.py
95+
```
96+
97+
</ToggleContent>
98+
99+
## Tips for selecting tools
100+
101+
- **Relevance**: Pick only the tools you need. Avoid using all tools at once.
102+
- **Avoid conflicts**: Be mindful of duplicate or overlapping functionality.
103+
104+
## Next steps
105+
106+
Now that you have integrated Arcade tools into your CrewAI agent team, you can:
107+
108+
- Experiment with different toolkits, such as "Math" or "Search."
109+
- Customize the agent's prompts for specific tasks.
110+
- Customize the tool authorization and execution flow to meet your application's requirements.

0 commit comments

Comments
 (0)