|
import openai |
|
import os |
|
|
|
|
|
from openai import OpenAI |
|
|
|
API_KEY = os.getenv("OPENAI_API_KEY") |
|
client = OpenAI(api_key=API_KEY) |
|
|
|
import gradio as gr |
|
|
|
def gpt41(users_input): |
|
response = client.responses.create( |
|
model="gpt-4.1-mini", |
|
input=f''' |
|
You name is AI Academy, the purpose of this chatbot is to support the user |
|
On their learning journey to create AI Agents. Diego have created a youtube video under 'AI Academy' youtube Channel, |
|
Explaining how to create AI Agents, for beginners that dont know how to code. The proocess shows them how to download |
|
an IDE then start to code. README: |
|
'Intro |
|
|
|
This repository is a basic guide on how to build AI Agents using pyhton and OpenAI Agents SDK |
|
|
|
Requirements |
|
|
|
OpenAI API Key (This has a cost attached to it, you will need a credit card, the good news is that is very cheap, model: GPT 4.1 mini. Input price: $0.40 / 1M tokens (like 'words') and $1.60 for 1M output) Get your key here -> https://www.google.com/url?sa=t&source=web&rct=j&opi=89978449&url=https://platform.openai.com/api-keys&ved=2ahUKEwi3m7vSg--NAxUzTTABHY5LJRcQFnoECCkQAQ&usg=AOvVaw1YhcGDWJXhiKSfmL59Pnfn |
|
Beginner friendly step by step |
|
|
|
-Download Cursor, VS Code or Windsurf to your computer (Google it) -Open your IDE (Cursor, Windsurf, VSCode) and then click on Open Folder (create a folder for this project) -Go to the top left and chose Terminal -> New Terminal |
|
|
|
In the Terminal: copy and paste this + enter: |
|
git clone https://github.com/diegocp01/openai_agents.git |
|
Make sure your folder name on the left is the same as the terminal. (if not, take a screenshoot and ask chatgpt. Is a quick cd command) |
|
|
|
-Create a new file inside of the openai-sdk-agent folder, called '.env' -Inside of the .env file copy this: |
|
|
|
# Copy and paste your openai api key below instead of the 'sk-12....' |
|
OPENAI_API_KEY=sk-12232432 |
|
|
|
# The LLM to use find more llm names here |
|
#https://platform.openai.com/docs/models |
|
MODEL_CHOICE=gpt-4.1-nano |
|
-In the Terminal: Install the required dependencies: Create a virtual enviroment like this -> |
|
|
|
python -m venv .venv |
|
If your IDE asks you to create a python ENV click YES |
|
|
|
-Then in the terminal paste this (This activates the enviroment): |
|
|
|
source .venv/bin/activate |
|
-Now you will install the openai agents sdk and other frameworks needed -(The frameworks are listed in the requirements file) |
|
|
|
pip install -r requirements.txt |
|
Then paste this in the terminal (with your openai key from .env file) |
|
|
|
export OPENAI_API_KEY=sk-122 |
|
Files |
|
|
|
v1_basic_agent.py - Basic Agent |
|
v2_structured_output.py - Agent with organized outputs |
|
v3_tool_calls.py - Agent with access to tools |
|
v4_handoffs.py - Orchestrator Agents with Specialized agents |
|
Running the Agents |
|
|
|
Basic Agent (v1) |
|
|
|
Run the basic agent example: |
|
|
|
python v1_basic_agent.py |
|
Structured Output Agent (v2) |
|
|
|
Run the Agent with organized outputs: |
|
|
|
python v2_structured_output.py |
|
Tool Calls Agent (v3) |
|
|
|
Run the tool calls travel agent example: |
|
|
|
python v3_tool_calls.py |
|
Now we will give our agent some TOOLS! (This is when it gets fun!) Recipe Agent |
|
|
|
Handoffs Agent (v4) |
|
|
|
Run the Orchestrator Agents with Specialized agents |
|
|
|
python v4_handoffs.py |
|
Run the interactive user interface app |
|
|
|
streamlit run app.py' |
|
Here is the code for the basic structure: |
|
v1_basic_agent.py ' |
|
|
|
# ============================================================================== |
|
# This Agent is only a regular talk to ChatGPT, you send a prompt and get a response |
|
# ============================================================================== |
|
|
|
|
|
# --- Imports --- |
|
from agents import Agent, Runner |
|
from dotenv import load_dotenv |
|
|
|
# --- Load environment variables --- |
|
load_dotenv() |
|
|
|
# --- Agent --- |
|
agent = Agent( |
|
name="Assistant", |
|
instructions="You are a helpful assistant", # This is the system prompt that tells the AI how to behave, change it if you want |
|
model="gpt-4.1-nano" # Change the model here |
|
) |
|
|
|
# --- Main -- |
|
# The main function runs the AI agent synchronously with a predefined prompt, |
|
# asking it to write a haiku about recursion, and then prints the generated response. |
|
def main(): |
|
result = Runner.run_sync(agent, """ |
|
Say: Hello my name is ChatGPT, this is a test. Now add something you will |
|
like to tell the user. Your whole output is max 30 words. |
|
""") # This is the PROMPT |
|
print(result.final_output) |
|
|
|
# --- Run --- |
|
# This ensures that the main() function only runs when this script is executed directly, |
|
# not when it is imported as a module in another file. |
|
if __name__ == "__main__": |
|
main()' |
|
|
|
If the user asks for code for the other files you ask the user to provide it. |
|
The user's question: {users_input} |
|
''', |
|
max_output_tokens=700 |
|
) |
|
return(response.output_text) |
|
|
|
|
|
demo = gr.Interface( |
|
fn=gpt41, |
|
inputs="text", |
|
outputs="text", |
|
title="π§ AI Academy Support Assistant", |
|
description="Welcome to your AI Agent learning! π\n" |
|
"**Disclaimer**: This chatbot is stateless β it does NOT remember past messages. " |
|
"Ask one question at a time." |
|
) |
|
|
|
demo.launch() |
|
|