File size: 6,010 Bytes
4676f73 00e6dc2 d3cc0cc 1670fb7 d3cc0cc 355eab2 4676f73 d3cc0cc 1670fb7 d3cc0cc 4676f73 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
import openai
import os
# Initialize the OpenAI client
from openai import OpenAI
# HF
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)
# Gradio Interface
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()
|