Skip to content
Open
Show file tree
Hide file tree
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
370 changes: 370 additions & 0 deletions 2_openai/community_contributions/manzoor/2_lab2_.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,370 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "79df28ad",
"metadata": {},
"outputs": [],
"source": [
"import os \n",
"import json\n",
"import agent_tools as t\n",
"from agent_tools import save_structure\n",
"from openai import AsyncOpenAI \n",
"import handoff_instructions\n",
"import instructions\n",
"from agents import Agent, Runner, OpenAIChatCompletionsModel, trace"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "75f7b883",
"metadata": {},
"outputs": [],
"source": [
"# import environment variables\n",
"from dotenv import load_dotenv\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5d717319",
"metadata": {},
"outputs": [],
"source": [
"deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "631c97b2",
"metadata": {},
"outputs": [],
"source": [
"if not deepseek_api_key:\n",
" print(f\"Missing deepseek api key\")\n",
"else:\n",
" print(f\"Deepseek api key: {deepseek_api_key[:8]} ... {deepseek_api_key[-8:]}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "730d1284",
"metadata": {},
"outputs": [],
"source": [
"DEEPSEEK_BASE_URL = \"https://api.deepseek.com/v1\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0140c966",
"metadata": {},
"outputs": [],
"source": [
"# Our models \n",
"openai_model_mini = \"gpt-4o-mini\"\n",
"openai_model_gpt5 = \"gpt-5-nano\"\n",
"deepseek_model_reasoner = \"deepseek-reasoner\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3dd6f5e4",
"metadata": {},
"outputs": [],
"source": [
"# Skipping...\n",
"deepseek_client = AsyncOpenAI(api_key=deepseek_api_key,base_url=DEEPSEEK_BASE_URL)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b75dd4aa",
"metadata": {},
"outputs": [],
"source": [
"# Skipping deepseek model, not compatible with openai/cant' runt tools \n",
"deepseek_model = OpenAIChatCompletionsModel(model=deepseek_model_reasoner,openai_client=deepseek_client)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "46f2a9d7",
"metadata": {},
"outputs": [],
"source": [
"\"\"\" Innovator agent comes up with innovative ideas,to help \n",
"solve major problems, for small business.\"\"\"\n",
"\n",
"innovator_agent = Agent(\n",
" name=\"Idea Generator Agent\",\n",
" instructions=instructions.innovator_instructions, \n",
" model=openai_model_mini\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "23f7bcf0",
"metadata": {},
"outputs": [],
"source": [
"\"\"\" Idea Selector Agent selects the most effective idea from a list of ideas, \n",
"selects the one that addresses user's pain points, considering cost\"\"\"\n",
"\n",
"idea_selector_agent = Agent(\n",
" name=\"Idea Selector Agent\",\n",
" instructions=instructions.idea_selector_instructions,\n",
" model=openai_model_mini\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9d1431d6",
"metadata": {},
"outputs": [],
"source": [
"\"\"\" Create app structure for the selected idea\"\"\"\n",
"app_structure_agent = Agent(\n",
" name='App Structure Agent',\n",
" instructions=instructions.app_file_structure_instructions,\n",
" model=openai_model_gpt5\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f475692d",
"metadata": {},
"outputs": [],
"source": [
"\n",
"\"\"\" Developer Agent writes the backend source code for the app \"\"\"\n",
"developer_agent = Agent(\n",
" name='Developer Agent', \n",
" instructions=instructions.developer_instructions,\n",
" model=openai_model_gpt5,\n",
" tools=[t.save_source_code]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d42ba3a2",
"metadata": {},
"outputs": [],
"source": [
"\"\"\" Database Creator Agent designs the database schema and relationships.\"\"\"\n",
"database_creator_agent = Agent(\n",
" name='Database Designer Agent',\n",
" instructions=instructions.database_instructions,\n",
" model=openai_model_gpt5,\n",
" tools=[t.save_database_content]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fd3d4b66",
"metadata": {},
"outputs": [],
"source": [
"\"\"\" Let's skip QA for now, since the model takes long time to run\"\"\"\n",
"qa_tester_agent = Agent(\n",
" name=\"Tester Agent\",\n",
" instructions=instructions.qa_tester_instructions,\n",
" model=openai_model_mini\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0847e046",
"metadata": {},
"outputs": [],
"source": [
"\"\"\" Double checks app structure for correctness\"\"\"\n",
"structure_verifier_agent = Agent(\n",
" name=\"JSON Structure Verifier\",\n",
" instructions=instructions.structure_verifier_instructions,\n",
" model=openai_model_gpt5, \n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0909bf51",
"metadata": {},
"outputs": [],
"source": [
"# Convert all agents as tools for manager agent\n",
"ideas_tool = innovator_agent.as_tool(\n",
" tool_name=\"Idea_Tool\",\n",
" tool_description=\"Use this tool to help you come up with list of ideas, for small businesses.\"\n",
" )\n",
"idea_selector_tool = idea_selector_agent.as_tool(\n",
" tool_name=\"Idea_Selector_Tool\",\n",
" tool_description=f\"Use this tool to select the best idea from generated list of ideas.\"\n",
" )\n",
"app_structure_tool = app_structure_agent.as_tool(\n",
" tool_name=\"App_Structure_Tool\",\n",
" tool_description=f\"Use this tool to help you create a correct and complete React app structure for the selected idea.\"\n",
" )\n",
"database_tool = database_creator_agent.as_tool(\n",
" tool_name=\"Database_Designer_Tool\",\n",
" tool_description=\"Use this tool to help you write code for creating database files/schemas.\"\n",
" )\n",
"\"\"\"\n",
"structure_verifier_agent.as_tool(\n",
" tool_name=\"JSON Structure Verifier\",\n",
" tool_description=\"Use this tool to double check the JSON structure is correct format and error free\"\n",
")\"\"\"\n",
"\n",
"dev_tool = developer_agent.as_tool(\n",
" tool_name=\"Developer_Tool\",\n",
" tool_description=\"Use this tool after you have gathered all the required information, to help you write all the React app source code for the selected idea.\"\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4d0a0f1a",
"metadata": {},
"outputs": [],
"source": [
"manager_tools = [\n",
" ideas_tool,\n",
" idea_selector_tool,\n",
" app_structure_tool,\n",
" database_tool,\n",
" dev_tool,\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "daf25488",
"metadata": {},
"outputs": [],
"source": [
"\"\"\"Equip manger agent with tools \"\"\"\n",
"manager_agent = Agent(\n",
" name='Manager Agent',\n",
" instructions=instructions.manager_instructions, \n",
" model=openai_model_gpt5,\n",
" tools=manager_tools,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "632cda88",
"metadata": {},
"outputs": [],
"source": [
"# Pass the ideas to the agent so it can select the best one\n",
"innovator_agent.handoffs = [manager_agent, developer_agent]\n",
"innovator_agent.handoff_description = handoff_instructions.innovator_handoff\n",
"\n",
"# Create the app file structure for the selected idea\n",
"idea_selector_agent.handoffs = [manager_agent, developer_agent]\n",
"idea_selector_agent.handoff_description = handoff_instructions.idea_selector_handoff\n",
"\n",
"# Create structure for the selected app \n",
"app_structure_agent.handoffs = [manager_agent, developer_agent]\n",
"app_structure_agent.handoff_description = handoff_instructions.app_structure_handoff\n",
"\n",
"\"\"\"\n",
"# Double check the structure, make sure correct key and values with correct content\n",
"structure_verifier_agent.handoffs = [developer_agent]\n",
"structure_verifier_agent.handoff_description = handoff_instructions.structure_verifier_handoff\n",
"\"\"\"\n",
"\n",
"# Database Creator Agent handoffs to Developer Agent its work\n",
"database_creator_agent.handoffs = [manager_agent, developer_agent]\n",
"database_creator_agent.handoff_description = handoff_instructions.database_handoff\n",
"\n",
"qa_tester_agent.handoffs = [manager_agent]\n",
"qa_tester_agent.handoff_description = handoff_instructions.qa_tester_handoff \n",
"\n",
"developer_agent.handoffs = [manager_agent]\n",
"developer_agent.handoff_description = handoff_instructions.developer_handoff\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "660d4446",
"metadata": {},
"outputs": [],
"source": [
"\"\"\"It's show time\"\"\"\n",
"with trace('Manager Agent Run'):\n",
" result = await Runner.run(\n",
" starting_agent=manager_agent, \n",
" input=\"Come up with an innovative idea, then build the idea using your tools.\",\n",
" max_turns=10\n",
" )\n",
"\n",
"print(result.final_output)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e7609e0e",
"metadata": {},
"outputs": [],
"source": [
"\"\"\" Create app \"\"\"\n",
"with open(\"app_code.json\", \"r\") as f:\n",
" project_structure = json.load(f)\n",
" print(project_structure)\n",
" save_structure(project_structure)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading