Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ritwickbhargav80/37bed3344d6c31ea9fa3249d1ee59811 to your computer and use it in GitHub Desktop.

Select an option

Save ritwickbhargav80/37bed3344d6c31ea9fa3249d1ee59811 to your computer and use it in GitHub Desktop.
AutoGen - Single Agent [News Retrieval]
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyO1Z19A1yNPJO6FEYQmGsps"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "view-in-github"
},
"source": [
"\u003ca href=\"https://colab.research.google.com/gist/ritwickbhargav80/37bed3344d6c31ea9fa3249d1ee59811/autogen-single-agent-news-retrieval.ipynb\" target=\"_parent\"\u003e\u003cimg src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/\u003e\u003c/a\u003e\n"
]
},
{
"cell_type": "markdown",
"source": [
"# Installing AutoGen\n",
"\n",
"AutoGen supports Python version >= 3.8 and < 3.13. In the below cell we are installing `pyautogen` through pip."
],
"metadata": {
"id": "MOULUNtHQqjs"
}
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "fH8GuSrVPnJt",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "2d69d7b4-e07a-4005-9b2d-57311d0868d5"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Collecting pyautogen\n",
" Downloading pyautogen-0.2.31-py3-none-any.whl (304 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m304.9/304.9 kB\u001b[0m \u001b[31m1.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hCollecting diskcache (from pyautogen)\n",
" Downloading diskcache-5.6.3-py3-none-any.whl (45 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m45.5/45.5 kB\u001b[0m \u001b[31m2.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hCollecting docker (from pyautogen)\n",
" Downloading docker-7.1.0-py3-none-any.whl (147 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m147.8/147.8 kB\u001b[0m \u001b[31m7.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hCollecting flaml (from pyautogen)\n",
" Downloading FLAML-2.1.2-py3-none-any.whl (296 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m296.7/296.7 kB\u001b[0m \u001b[31m12.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: numpy<2,>=1.17.0 in /usr/local/lib/python3.10/dist-packages (from pyautogen) (1.25.2)\n",
"Collecting openai>=1.3 (from pyautogen)\n",
" Downloading openai-1.35.7-py3-none-any.whl (327 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m327.5/327.5 kB\u001b[0m \u001b[31m11.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from pyautogen) (24.1)\n",
"Requirement already satisfied: pydantic!=2.6.0,<3,>=1.10 in /usr/local/lib/python3.10/dist-packages (from pyautogen) (2.7.4)\n",
"Collecting python-dotenv (from pyautogen)\n",
" Downloading python_dotenv-1.0.1-py3-none-any.whl (19 kB)\n",
"Requirement already satisfied: termcolor in /usr/local/lib/python3.10/dist-packages (from pyautogen) (2.4.0)\n",
"Collecting tiktoken (from pyautogen)\n",
" Downloading tiktoken-0.7.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.1/1.1 MB\u001b[0m \u001b[31m12.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.10/dist-packages (from openai>=1.3->pyautogen) (3.7.1)\n",
"Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from openai>=1.3->pyautogen) (1.7.0)\n",
"Collecting httpx<1,>=0.23.0 (from openai>=1.3->pyautogen)\n",
" Downloading httpx-0.27.0-py3-none-any.whl (75 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m5.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from openai>=1.3->pyautogen) (1.3.1)\n",
"Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.10/dist-packages (from openai>=1.3->pyautogen) (4.66.4)\n",
"Requirement already satisfied: typing-extensions<5,>=4.7 in /usr/local/lib/python3.10/dist-packages (from openai>=1.3->pyautogen) (4.12.2)\n",
"Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic!=2.6.0,<3,>=1.10->pyautogen) (0.7.0)\n",
"Requirement already satisfied: pydantic-core==2.18.4 in /usr/local/lib/python3.10/dist-packages (from pydantic!=2.6.0,<3,>=1.10->pyautogen) (2.18.4)\n",
"Requirement already satisfied: requests>=2.26.0 in /usr/local/lib/python3.10/dist-packages (from docker->pyautogen) (2.31.0)\n",
"Requirement already satisfied: urllib3>=1.26.0 in /usr/local/lib/python3.10/dist-packages (from docker->pyautogen) (2.0.7)\n",
"Requirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.10/dist-packages (from tiktoken->pyautogen) (2024.5.15)\n",
"Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai>=1.3->pyautogen) (3.7)\n",
"Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai>=1.3->pyautogen) (1.2.1)\n",
"Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai>=1.3->pyautogen) (2024.6.2)\n",
"Collecting httpcore==1.* (from httpx<1,>=0.23.0->openai>=1.3->pyautogen)\n",
" Downloading httpcore-1.0.5-py3-none-any.whl (77 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m4.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hCollecting h11<0.15,>=0.13 (from httpcore==1.*->httpx<1,>=0.23.0->openai>=1.3->pyautogen)\n",
" Downloading h11-0.14.0-py3-none-any.whl (58 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m4.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.26.0->docker->pyautogen) (3.3.2)\n",
"Installing collected packages: python-dotenv, h11, flaml, diskcache, tiktoken, httpcore, docker, httpx, openai, pyautogen\n",
"Successfully installed diskcache-5.6.3 docker-7.1.0 flaml-2.1.2 h11-0.14.0 httpcore-1.0.5 httpx-0.27.0 openai-1.35.7 pyautogen-0.2.31 python-dotenv-1.0.1 tiktoken-0.7.0\n"
]
}
],
"source": [
"!pip install pyautogen"
]
},
{
"cell_type": "markdown",
"source": [
"# Setting Up The Environment\n",
"\n",
"Here we're using the configuration of GPT-3.5 Turbo model from Azure OpenAI and storing the News API key in environment variable.\n",
"\n",
"GPT Configuration is taken from the Google Colab Secrets.\n",
"\n",
"> **NOTE:** We want to prevent the sensitive News API key from directly being exposed to the LLM model. However, we want code generated by LLM to still have access to the News API Key. Thus, environmental variables are best suited."
],
"metadata": {
"id": "Pf97CYzxnJNe"
}
},
{
"cell_type": "code",
"source": [
"from google.colab import userdata\n",
"import os\n",
"\n",
"config_list = [\n",
" {\n",
" \"model\": \"gpt-35-turbo-16k\",\n",
" \"base_url\": userdata.get(\"AZURE_OPENAI_ENDPOINT\"),\n",
" \"api_key\": userdata.get(\"AZURE_OPENAI_API_KEY\"),\n",
" \"api_type\": \"azure\",\n",
" \"api_version\": \"2024-02-15-preview\"\n",
" }\n",
"]\n",
"\n",
"os.environ[\"NEWS_API_KEY\"] = \"<NEWSORG_API_KEY>\""
],
"metadata": {
"id": "M_XUczHOPqjA"
},
"execution_count": 2,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Creating AI Agents\n",
"\n",
"This is where our agents gets defined. Here we are creating two agents:\n",
"\n",
"1. **AssistantAgent:** It acts as an AI model. It can use LLM to generate responses. It can even write code, check the results and suggest fixes.\n",
"2. **UserProxyAgent:** It can mimic the user's interaction. Here, we have provided the UserProxyAgent the capability to perform code execution. With options like \"code_execution_config\" and \"llm_config\", you can regulate code execution and LLM utilization for response generation."
],
"metadata": {
"id": "A1s04UYNn6Br"
}
},
{
"cell_type": "code",
"source": [
"from autogen import AssistantAgent, UserProxyAgent\n",
"from autogen.coding import LocalCommandLineCodeExecutor\n",
"\n",
"assistant = AssistantAgent(\n",
" name=\"assistant\",\n",
" llm_config={\n",
" \"cache_seed\": 41,\n",
" \"config_list\": config_list,\n",
" \"temperature\": 0\n",
" },\n",
")\n",
"\n",
"user_proxy = UserProxyAgent(\n",
" name=\"user_proxy\",\n",
" human_input_mode=\"NEVER\",\n",
" max_consecutive_auto_reply=10,\n",
" is_termination_msg=lambda x: x.get(\"content\", \"\").rstrip().endswith(\"TERMINATE\"),\n",
" code_execution_config={ \"executor\": LocalCommandLineCodeExecutor(work_dir=\"coding\") },\n",
" default_auto_reply=\"Reply 'TERMINATE' if the task is done.\"\n",
")"
],
"metadata": {
"id": "OMYnFyu9P4dL"
},
"execution_count": 7,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Initiating The Conversation\n",
"\n",
"This is where we start the interaction between the agents."
],
"metadata": {
"id": "wodqel70oP81"
}
},
{
"cell_type": "code",
"source": [
"chat_res = user_proxy.initiate_chat(\n",
" assistant,\n",
" message=\"\"\"\n",
" Do you know about newsapi.org?\n",
" Can you use that to retrieve the top 10 tech news headlines and the content from techcrunch.com?\n",
" Your API KEY for newsapi.org is stored in os.environ[\"NEWS_API_KEY\"].\n",
" \"\"\",\n",
" summary_method=\"reflection_with_llm\"\n",
")"
],
"metadata": {
"id": "ba4zf8xwQIuC",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "5a22277c-5c81-4ea6-f4cf-a7b43418917e"
},
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"user_proxy (to assistant):\n",
"\n",
"\n",
" Do you know about newsapi.org?\n",
" Can you use that to retrieve the top 10 tech news headlines and the content from techcrunch.com?\n",
" Your API KEY for newsapi.org is stored in os.environ[\"NEWS_API_KEY\"].\n",
" \n",
"\n",
"--------------------------------------------------------------------------------\n",
"assistant (to user_proxy):\n",
"\n",
"Yes, I am familiar with newsapi.org. To retrieve the top 10 tech news headlines and content from techcrunch.com using the newsapi.org API, you can use the following Python code:\n",
"\n",
"```python\n",
"import requests\n",
"import os\n",
"\n",
"# Get the API key from the environment variable\n",
"api_key = os.environ[\"NEWS_API_KEY\"]\n",
"\n",
"# Define the API endpoint and parameters\n",
"url = \"https://newsapi.org/v2/top-headlines\"\n",
"params = {\n",
" \"apiKey\": api_key,\n",
" \"sources\": \"techcrunch\",\n",
" \"pageSize\": 10\n",
"}\n",
"\n",
"# Send the request to the API\n",
"response = requests.get(url, params=params)\n",
"data = response.json()\n",
"\n",
"# Print the headlines and content\n",
"for article in data[\"articles\"]:\n",
" print(\"Headline:\", article[\"title\"])\n",
" print(\"Content:\", article[\"content\"])\n",
" print()\n",
"\n",
"```\n",
"\n",
"Please make sure to replace `os.environ[\"NEWS_API_KEY\"]` with your actual API key for newsapi.org.\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\n",
">>>>>>>> EXECUTING CODE BLOCK (inferred language is python)...\n",
"user_proxy (to assistant):\n",
"\n",
"exitcode: 0 (execution succeeded)\n",
"Code output: Headline: Bye-bye bots: Altera's game-playing AI agents get backing from Eric Schmidt | TechCrunch\n",
"Content: Autonomous, AI-based players are coming to a gaming experience near you, and a new startup, Altera, is joining the fray to build this new guard of AI agents.\n",
"The company announced Wednesday that it … [+6416 chars]\n",
"\n",
"Headline: $450M for Noname, two billion-dollar rounds, and good news for crypto startups | TechCrunch\n",
"Content: Good news, crypto founders: Venture capital activity is picking up in your sector after falling to multi-year lows in late 2023. Put another way, venture folks appear more web3-bullish than before, e… [+1599 chars]\n",
"\n",
"Headline: Watch: When did iPads get as expensive as MacBooks?\n",
"Content: Apple’s iPad event had a lot to like. New iPads with new chips and new sizes, a new Apple Pencil, and even some software updates. If you are a big fan of Apple hardware, well, it was probably a good … [+1385 chars]\n",
"\n",
"Headline: Uber promises member exclusives as Uber One passes $1B run-rate | TechCrunch\n",
"Content: Uber plans to deliver more perks to Uber One members, like member-exclusive events, in a bid to gain more revenue through subscriptions. \n",
"You will see more member-exclusives coming up where members … [+4676 chars]\n",
"\n",
"Headline: Checkfirst raises $1.5M pre-seed, applying AI to remote inspections and audits | TechCrunch\n",
"Content: Weve all seen them. The inspector with a clipboard, walking around a building, ticking off the last time the fire extinguishers were checked, or if all the lights are working. They work in the TICC (… [+3279 chars]\n",
"\n",
"Headline: UK challenger bank Monzo nabs another $190M as US expansion beckons | TechCrunch\n",
"Content: Monzo has raised another £150 million ($190 million), as the challenger bank looks to expand its presence internationally particularly in the U.S.\n",
"The new round comes just two months after Monzo rai… [+1960 chars]\n",
"\n",
"Headline: iRobot names former Timex head Gary Cohen as CEO | TechCrunch\n",
"Content: iRobot Tuesday announced the successor to longtime CEO, Colin Angle. Gary Cohen, who previous held chief executive role at Timex and Qualitor Automotive, will be heading up the company, marking a maj… [+2120 chars]\n",
"\n",
"Headline: Google Deepmind debuts huge AlphaFold update and free proteomics-as-a-service web app | TechCrunch\n",
"Content: Google Deepmind has taken the wraps off a new version AlphaFold, their transformative machine learning model that predicts the shape and behavior of proteins. AlphaFold 3 is not only more accurate, b… [+7069 chars]\n",
"\n",
"Headline: Mycocycle uses mushrooms to upcycle old tires and construction waste | TechCrunch\n",
"Content: Usually, when something starts to rot, it gets pitched in the trash. But Joanne Rodriguez wants to turn the concept of rot on its head by growing fungus on trash to turn it into something better.\n",
"We… [+4229 chars]\n",
"\n",
"Headline: Controversial drone company Xtend leans into defense with new $40M round | TechCrunch\n",
"Content: Close to a decade ago, brothers Aviv and Matteo Shapira co-founded Replay, a company that created a video format for 360-degree replays — the sorts of replays that have become part and parcel of majo… [+5398 chars]\n",
"\n",
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"assistant (to user_proxy):\n",
"\n",
"The code executed successfully and retrieved the top 10 tech news headlines and content from techcrunch.com. Here are the headlines and content:\n",
"\n",
"1. Headline: Bye-bye bots: Altera's game-playing AI agents get backing from Eric Schmidt | TechCrunch\n",
" Content: Autonomous, AI-based players are coming to a gaming experience near you, and a new startup, Altera, is joining the fray to build this new guard of AI agents. The company announced Wednesday that it … [+6416 chars]\n",
"\n",
"2. Headline: $450M for Noname, two billion-dollar rounds, and good news for crypto startups | TechCrunch\n",
" Content: Good news, crypto founders: Venture capital activity is picking up in your sector after falling to multi-year lows in late 2023. Put another way, venture folks appear more web3-bullish than before, e… [+1599 chars]\n",
"\n",
"3. Headline: Watch: When did iPads get as expensive as MacBooks?\n",
" Content: Apple’s iPad event had a lot to like. New iPads with new chips and new sizes, a new Apple Pencil, and even some software updates. If you are a big fan of Apple hardware, well, it was probably a good … [+1385 chars]\n",
"\n",
"4. Headline: Uber promises member exclusives as Uber One passes $1B run-rate | TechCrunch\n",
" Content: Uber plans to deliver more perks to Uber One members, like member-exclusive events, in a bid to gain more revenue through subscriptions. You will see more member-exclusives coming up where members … [+4676 chars]\n",
"\n",
"5. Headline: Checkfirst raises $1.5M pre-seed, applying AI to remote inspections and audits | TechCrunch\n",
" Content: Weve all seen them. The inspector with a clipboard, walking around a building, ticking off the last time the fire extinguishers were checked, or if all the lights are working. They work in the TICC (… [+3279 chars]\n",
"\n",
"6. Headline: UK challenger bank Monzo nabs another $190M as US expansion beckons | TechCrunch\n",
" Content: Monzo has raised another £150 million ($190 million), as the challenger bank looks to expand its presence internationally particularly in the U.S. The new round comes just two months after Monzo rai… [+1960 chars]\n",
"\n",
"7. Headline: iRobot names former Timex head Gary Cohen as CEO | TechCrunch\n",
" Content: iRobot Tuesday announced the successor to longtime CEO, Colin Angle. Gary Cohen, who previous held chief executive role at Timex and Qualitor Automotive, will be heading up the company, marking a maj… [+2120 chars]\n",
"\n",
"8. Headline: Google Deepmind debuts huge AlphaFold update and free proteomics-as-a-service web app | TechCrunch\n",
" Content: Google Deepmind has taken the wraps off a new version AlphaFold, their transformative machine learning model that predicts the shape and behavior of proteins. AlphaFold 3 is not only more accurate, b… [+7069 chars]\n",
"\n",
"9. Headline: Mycocycle uses mushrooms to upcycle old tires and construction waste | TechCrunch\n",
" Content: Usually, when something starts to rot, it gets pitched in the trash. But Joanne Rodriguez wants to turn the concept of rot on its head by growing fungus on trash to turn it into something better. We… [+4229 chars]\n",
"\n",
"10. Headline: Controversial drone company Xtend leans into defense with new $40M round | TechCrunch\n",
" Content: Close to a decade ago, brothers Aviv and Matteo Shapira co-founded Replay, a company that created a video format for 360-degree replays — the sorts of replays that have become part and parcel of majo… [+5398 chars]\n",
"\n",
"Please let me know if there is anything else I can help you with.\n",
"\n",
"--------------------------------------------------------------------------------\n",
"user_proxy (to assistant):\n",
"\n",
"Reply 'TERMINATE' if the task is done.\n",
"\n",
"--------------------------------------------------------------------------------\n",
"assistant (to user_proxy):\n",
"\n",
"TERMINATE\n",
"\n",
"--------------------------------------------------------------------------------\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# Voila!\n",
"\n",
"Congratualtions on creating your first AutoGen agent to retrieve news from techcrunch.com."
],
"metadata": {
"id": "EuFUUDEEonPa"
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment