Skip to content

Commit 18e585b

Browse files
committed
Initial commit
0 parents  commit 18e585b

File tree

6 files changed

+270
-0
lines changed

6 files changed

+270
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

1-first-agent/.env.example

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Rename this file to .env once you have filled in the below environment variables!
2+
3+
# Get your Open AI API Key by following these instructions -
4+
https://help.openai.com/en/articles/4936850-where-do-i-find-my-openai-api-key
5+
OPENAI_API_KEY=
6+
7+
# See all Open AI models you can use here -
8+
# https://platform.openai.com/docs/models
9+
# A good default to go with here is gpt-4o
10+
OPENAI_MODEL=
11+
12+
# Get your personal Asana access token through the developer console in Asana.
13+
# Feel free to follow these instructions -
14+
# https://developers.asana.com/docs/personal-access-token
15+
ASANA_ACCESS_TOKEN=
16+
17+
# The Asana project ID is in the URL when you visit a project in the Asana UI.
18+
# If your URL is https://app.asana.com/0/123456789/1212121212, then your
19+
# Asana project ID is 123456789
20+
ASANA_PROJECT_ID=

1-first-agent/agents.py

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import asana
2+
from asana.rest import ApiException
3+
from openai import OpenAI
4+
from dotenv import load_dotenv
5+
from datetime import datetime
6+
import json
7+
import os
8+
9+
load_dotenv()
10+
11+
client = OpenAI()
12+
model = os.getenv('OPENAI_MODEL', 'gpt-4o')
13+
14+
configuration = asana.Configuration()
15+
configuration.access_token = os.getenv('ASANA_ACCESS_TOKEN', '')
16+
api_client = asana.ApiClient(configuration)
17+
18+
tasks_api_instance = asana.TasksApi(api_client)
19+
20+
def create_asana_task(task_name, due_on="today"):
21+
"""
22+
Creates a task in Asana given the name of the task and when it is due
23+
24+
Example call:
25+
26+
create_asana_task("Test Task", "2024-06-24")
27+
Args:
28+
task_name (str): The name of the task in Asana
29+
due_on (str): The date the task is due in the format YYYY-MM-DD. If not given, the current day is used
30+
Returns:
31+
str: The API response of adding the task to Asana or an error message if the API call threw an error
32+
"""
33+
if due_on == "today":
34+
due_on = str(datetime.now().date())
35+
36+
task_body = {
37+
"data": {
38+
"name": task_name,
39+
"due_on": due_on,
40+
"projects": [os.getenv("ASANA_PROJECT_ID", "")]
41+
}
42+
}
43+
44+
try:
45+
api_response = tasks_api_instance.create_task(task_body, {})
46+
return json.dumps(api_response, indent=2)
47+
except ApiException as e:
48+
return f"Exception when calling TasksApi->create_task: {e}"
49+
50+
def get_tools():
51+
tools = [
52+
{
53+
"type": "function",
54+
"function": {
55+
"name": "create_asana_task",
56+
"description": "Creates a task in Asana given the name of the task and when it is due",
57+
"parameters": {
58+
"type": "object",
59+
"properties": {
60+
"task_name": {
61+
"type": "string",
62+
"description": "The name of the task in Asana"
63+
},
64+
"due_on": {
65+
"type": "string",
66+
"description": "The date the task is due in the format YYYY-MM-DD. If not given, the current day is used"
67+
},
68+
},
69+
"required": ["task_name"]
70+
},
71+
},
72+
}
73+
]
74+
75+
return tools
76+
77+
def prompt_ai(messages):
78+
# First, prompt the AI with the latest user message
79+
completion = client.chat.completions.create(
80+
model=model,
81+
messages=messages,
82+
tools=get_tools()
83+
)
84+
85+
response_message = completion.choices[0].message
86+
tool_calls = response_message.tool_calls
87+
88+
# Second, see if the AI decided it needs to invoke a tool
89+
if tool_calls:
90+
# If the AI decided to invoke a tool, invoke it
91+
available_functions = {
92+
"create_asana_task": create_asana_task
93+
}
94+
95+
# Add the tool request to the list of messages so the AI knows later it invoked the tool
96+
messages.append(response_message)
97+
98+
# Next, for each tool the AI wanted to call, call it and add the tool result to the list of messages
99+
for tool_call in tool_calls:
100+
function_name = tool_call.function.name
101+
function_to_call = available_functions[function_name]
102+
function_args = json.loads(tool_call.function.arguments)
103+
function_response = function_to_call(**function_args)
104+
105+
messages.append({
106+
"tool_call_id": tool_call.id,
107+
"role": "tool",
108+
"name": function_name,
109+
"content": function_response
110+
})
111+
112+
# Call the AI again so it can produce a response with the result of calling the tool(s)
113+
second_response = client.chat.completions.create(
114+
model=model,
115+
messages=messages,
116+
)
117+
118+
return second_response.choices[0].message.content
119+
120+
return response_message.content
121+
122+
def main():
123+
messages = [
124+
{
125+
"role": "system",
126+
"content": f"You are a personal assistant who helps manage tasks in Asana. The current date is: {datetime.now().date()}"
127+
}
128+
]
129+
130+
while True:
131+
user_input = input("Chat with AI (q to quit): ").strip()
132+
133+
if user_input == 'q':
134+
break
135+
136+
messages.append({"role": "user", "content": user_input})
137+
ai_response = prompt_ai(messages)
138+
139+
print(ai_response)
140+
messages.append({"role": "assistant", "content": ai_response})
141+
142+
143+
if __name__ == "__main__":
144+
main()

1-first-agent/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
asana==5.0.0
2+
openai==1.10.0
3+
python-dotenv==0.13.0

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Cole Medin
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<div align="center">
2+
<a href="https://www.youtube.com/channel/UCMwVTLZIRRUyyVrkjDpn4pA">
3+
<img alt="AI Agents Masterclass" src="https://i.imgur.com/8Gr2pBA.png">
4+
<h1 align="center">AI Agents Masterclass</h1>
5+
</a>
6+
</div>
7+
8+
<p align="center">
9+
Artificial Intelligence is the #1 thing for all developers to spend their time on now.
10+
The problem is, most developers aren't focusing on AI agents, which is the real way to unleash the full power of AI.
11+
This is why I'm creating this AI Agents Masterclass - so I can show YOU how to use AI agents to transform
12+
businesses and create incredibly powerful software like I've already done many times!
13+
Click the image or link above to go to the masterclass on YouTube.
14+
</p>
15+
16+
<p align="center" style="margin-top: 25px">
17+
<a href="#what-are-ai-agents"><strong>What are AI Agents?</strong></a> ·
18+
<a href="#how-this-repo-works"><strong>How this Repo Works</strong></a> ·
19+
<a href="#instructions-to-follow-along"><strong>Instructions to Follow Along</strong></a>
20+
</p>
21+
<br/>
22+
23+
## What are AI Agents?
24+
25+
AI agents are simply Large Language Models that have been given the ability to interact with the outside world. They
26+
can do things like draft emails, book appointments in your CRM, create tasks in your task management software, and
27+
really anything you can dream of! I hope that everything I show here can really help you dream big
28+
and create incredible things with AI!
29+
30+
AI agents can be very powerful without having to create a lot of code. That doesn't mean there isn't room though
31+
to create more complex applications to tie together many different agents to accomplish truly incredible things!
32+
That's where we'll be heading with this masterclass and I really look forward to it!
33+
34+
Below is a very basic diagram just to get an idea of what an AI agent looks like:
35+
36+
<div align="center" style="margin-top: 25px;margin-bottom:25px">
37+
<img width="700" alt="Trainers Ally LangGraph graph" src="https://i.imgur.com/ChRoV8W.png">
38+
</div>
39+
40+
<br/>
41+
42+
## How this Repo Works
43+
44+
Each week there will be a new video for my AI Agents Masterclass! Each video will have its own folder
45+
in this repo, starting with [/1-first-agent/](/1-first-agent) for the first video in the masterclass
46+
where I create our very first AI agent!
47+
48+
The code in each folder will be exactly what I used/created in the accompanying masterclass video.
49+
50+
<br/>
51+
52+
## Instructions to Follow Along
53+
54+
The below instructions assume you already have Git, Python, and Pip installed. If you do not, you can install
55+
[Python + Pip from here](https://www.python.org/downloads/) and [Git from here](https://git-scm.com/).
56+
57+
To follow along with any of my videos, first clone this GitHub repository, open up a terminal,
58+
and change your directory to the folder for the current video you are watching (example: 1st video is [/1-first-agent/](/1-first-agent)).
59+
60+
The below instructions work on any OS - Windows, Linux, or Mac!
61+
62+
You will need to use the environment variables defined in the .env.example file in the folder (example for the first video: [`1-first-agent/.env.example`](/1-first-agent/.env.example)) to set up your API keys and other configuration. Turn the .env.example file into a `.env` file, and supply the necessary environment variables.
63+
64+
After setting up the .env file, run the below commands to create a Python virtual environment and install the necessary Python packages to run the code from the masterclass. Creating a virtual environment is optional but recommended! Creating a virtual environment for the entire masterclass is a one time thing. Make sure to run the pip install for each video though!
65+
66+
```bash
67+
python -m venv ai-agents-masterclass
68+
69+
On Windows: .\ai-agents-masterclass\Scripts\activate
70+
On MacOS/Linux: source ai-agents-masterclass/bin/activate
71+
72+
cd 1-first-agent (or whichever folder)
73+
pip install -r requirements.txt
74+
```
75+
76+
Then, you can execute the code in the folder with:
77+
78+
```bash
79+
python [script name].py
80+
```

0 commit comments

Comments
 (0)