add demo notebook

This commit is contained in:
zachary62 2025-03-23 12:15:54 -04:00
parent 1d560e7737
commit 4e09e3025a
2 changed files with 369 additions and 297 deletions

View File

@ -2,6 +2,9 @@
This project demonstrates a simple yet powerful LLM-powered research agent. This implementation is based directly on the tutorial: [LLM Agents are simply Graph — Tutorial For Dummies](https://zacharyhuang.substack.com/p/llm-agent-internal-as-a-graph-tutorial).
👉 Run the tutorial in your browser: [Try Google Colab Notebook](
https://colab.research.google.com/github/The-Pocket/PocketFlow/blob/main/cookbook/pocketflow-agent/demo.ipynb)
## Features
- Performs web searches to gather information

View File

@ -2,11 +2,12 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"id": "8MeqVASIxKBH"
},
"outputs": [],
"source": [
@ -18,13 +19,49 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "wUp_sNU1xKBI",
"outputId": "a647f919-b253-48c8-c132-5eef582e29c7"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"## Testing call_llm\n",
"## Prompt: In a few words, what is the meaning of life?\n",
"## Response: The meaning of life is a deeply personal and philosophical question. For many, it involves seeking happiness, forming relationships, pursuing knowledge, or finding purpose and fulfillment. It's a journey that varies for each individual.\n",
"## Testing search_web\n",
"## Query: Who won the Nobel Prize in Physics 2024?\n",
"## Results: Title: Press release: The Nobel Prize in Physics 2024 - NobelPrize.org\n",
"URL: https://www.nobelprize.org/prizes/physics/2024/press-release/\n",
"Snippet: The Nobel Prize in Physics 2024 was awarded jointly to John J. Hopfield and Geoffrey Hinton \"for foundational discoveries and inventions that enable machine learning with artificial neural networks\"\n",
"\n",
"Title: Pioneers in artificial intelligence win the Nobel Prize in physics\n",
"URL: https://apnews.com/article/nobel-prize-physics-fc0567de3f2ca45f81a7359a017cd542\n",
"Snippet: Two pioneers of artificial intelligence have won the Nobel Prize in physics. John Hopfield and Geoffrey Hinton were awarded the prize Tuesday for discoveries and inventions that formed the building blocks of machine learning.\n",
"\n",
"Title: Nobel Prize 2024: All the Winners | TIME\n",
"URL: https://time.com/7065011/nobel-prize-2024-winners/\n",
"Snippet: The 2024 Nobel Prize announcements began on Oct. 7, recognizing groundbreaking contributions to humanity. The first prize, in the category of physiology or medicine, went to a pair of American ...\n",
"\n",
"Title: Nobel physics prize 2024 won by AI pioneers John Hopfield and Geoffrey ...\n",
"URL: https://www.reuters.com/science/hopfield-hinton-win-2024-nobel-prize-physics-2024-10-08/\n",
"Snippet: John Hopfield and Geoffrey Hinton won for discoveries that paved the way for the AI boom.\n",
"\n",
"Title: Nobel Prize in physics 2024 awarded for work on artificial intelligence ...\n",
"URL: https://www.cnn.com/2024/10/08/science/nobel-prize-physics-hopfield-hinton-machine-learning-intl/index.html\n",
"Snippet: The 2024 Nobel Prize in physics has been awarded to John Hopfield and Geoffrey Hinton for their fundamental discoveries in machine learning, which paved the way for how artificial intelligence is ...\n"
]
}
],
"source": [
"# utils.py\n",
"from openai import OpenAI\n",
@ -32,7 +69,7 @@
"from duckduckgo_search import DDGS\n",
"\n",
"def call_llm(prompt):\n",
" client = OpenAI(api_key=os.environ.get(\"OPENAI_API_KEY\", \"your-api-key\"))\n",
" client = OpenAI(api_key=\"os.environ.get(\"OPENAI_API_KEY\", \"your-api-key\")\")\n",
" r = client.chat.completions.create(\n",
" model=\"gpt-4o\",\n",
" messages=[{\"role\": \"user\", \"content\": prompt}]\n",
@ -60,17 +97,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"id": "T0ETd4C2xKBI"
},
"outputs": [],
"source": [
"# nodes.py\n",
"from pocketflow import Node\n",
"from utils import call_llm, search_web\n",
"import yaml\n",
"\n",
"class DecideAction(Node):\n",
@ -208,11 +245,12 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"id": "0B4jCAmXxKBI"
},
"outputs": [],
"source": [
@ -253,13 +291,37 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "bIwsNEDCxKBI",
"outputId": "e6c02020-6fae-4377-8f0a-01d2580dd659"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"🤔 Processing question: Who won the Nobel Prize in Physics 2024?\n",
"🤔 Agent deciding what to do next...\n",
"🔍 Agent decided to search for: 2024 Nobel Prize in Physics winner\n",
"🌐 Searching the web for: 2024 Nobel Prize in Physics winner\n",
"📚 Found information, analyzing results...\n",
"🤔 Agent deciding what to do next...\n",
"💡 Agent decided to answer the question\n",
"✍️ Crafting final answer...\n",
"✅ Answer generated successfully\n",
"\n",
"🎯 Final Answer:\n",
"John J. Hopfield and Geoffrey Hinton won the 2024 Nobel Prize in Physics. They were awarded this prestigious recognition for their foundational discoveries and inventions that have significantly advanced the field of machine learning by enabling the use of artificial neural networks. These contributions have had a profound impact on the development and application of machine learning technologies.\n"
]
}
],
"source": [
"# main.py\n",
"import sys\n",
@ -286,15 +348,22 @@
" print(\"\\n🎯 Final Answer:\")\n",
" print(shared.get(\"answer\", \"No answer found\"))\n",
"\n",
"main()\n"
"main()"
]
}
],
"metadata": {
"language_info": {
"name": "python"
},
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 0
}