Skip to content

A programming framework for agentic AI

nbqa adedd to pre-commit, added black and ruff for notebooks (#1171)

Davor Runje1c4ae3d303cf
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index f28865413..8c2f1c5bc 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -43,3 +43,9 @@ repos:
website/yarn.lock |
notebook/.*
)$
+ - repo: https://github.com/nbQA-dev/nbQA
+ rev: 1.7.1
+ hooks:
+ - id: nbqa-ruff
+ args: ["--fix"]
+ - id: nbqa-black
diff --git a/notebook/Async_human_input.ipynb b/notebook/Async_human_input.ipynb
index dc645f2ee..07459b4a8 100644
--- a/notebook/Async_human_input.ipynb
+++ b/notebook/Async_human_input.ipynb
@@ -1,405 +1,408 @@
{
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "tLIs1YRdr8jM",
- "outputId": "909c1c70-1a22-4e9d-b7f4-a40e2d737fb0"
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Defaulting to user installation because normal site-packages is not writeable\n",
- "Requirement already satisfied: pyautogen>=0.2.3 in /home/vscode/.local/lib/python3.10/site-packages (0.2.3)\n",
- "Requirement already satisfied: openai>=1.3 in /home/vscode/.local/lib/python3.10/site-packages (from pyautogen>=0.2.3) (1.6.1)\n",
- "Requirement already satisfied: diskcache in /home/vscode/.local/lib/python3.10/site-packages (from pyautogen>=0.2.3) (5.6.3)\n",
- "Requirement already satisfied: termcolor in /home/vscode/.local/lib/python3.10/site-packages (from pyautogen>=0.2.3) (2.4.0)\n",
- "Requirement already satisfied: flaml in /home/vscode/.local/lib/python3.10/site-packages (from pyautogen>=0.2.3) (2.1.1)\n",
- "Requirement already satisfied: python-dotenv in /home/vscode/.local/lib/python3.10/site-packages (from pyautogen>=0.2.3) (1.0.0)\n",
- "Requirement already satisfied: tiktoken in /home/vscode/.local/lib/python3.10/site-packages (from pyautogen>=0.2.3) (0.5.2)\n",
- "Requirement already satisfied: pydantic<3,>=1.10 in /home/vscode/.local/lib/python3.10/site-packages (from pyautogen>=0.2.3) (1.10.9)\n",
- "Requirement already satisfied: anyio<5,>=3.5.0 in /home/vscode/.local/lib/python3.10/site-packages (from openai>=1.3->pyautogen>=0.2.3) (4.2.0)\n",
- "Requirement already satisfied: distro<2,>=1.7.0 in /home/vscode/.local/lib/python3.10/site-packages (from openai>=1.3->pyautogen>=0.2.3) (1.9.0)\n",
- "Requirement already satisfied: httpx<1,>=0.23.0 in /home/vscode/.local/lib/python3.10/site-packages (from openai>=1.3->pyautogen>=0.2.3) (0.26.0)\n",
- "Requirement already satisfied: sniffio in /home/vscode/.local/lib/python3.10/site-packages (from openai>=1.3->pyautogen>=0.2.3) (1.3.0)\n",
- "Requirement already satisfied: tqdm>4 in /home/vscode/.local/lib/python3.10/site-packages (from openai>=1.3->pyautogen>=0.2.3) (4.66.1)\n",
- "Requirement already satisfied: typing-extensions<5,>=4.7 in /home/vscode/.local/lib/python3.10/site-packages (from openai>=1.3->pyautogen>=0.2.3) (4.9.0)\n",
- "Requirement already satisfied: NumPy>=1.17.0rc1 in /home/vscode/.local/lib/python3.10/site-packages (from flaml->pyautogen>=0.2.3) (1.26.3)\n",
- "Requirement already satisfied: regex>=2022.1.18 in /home/vscode/.local/lib/python3.10/site-packages (from tiktoken->pyautogen>=0.2.3) (2023.12.25)\n",
- "Requirement already satisfied: requests>=2.26.0 in /usr/local/lib/python3.10/site-packages (from tiktoken->pyautogen>=0.2.3) (2.31.0)\n",
- "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/site-packages (from anyio<5,>=3.5.0->openai>=1.3->pyautogen>=0.2.3) (3.6)\n",
- "Requirement already satisfied: exceptiongroup>=1.0.2 in /home/vscode/.local/lib/python3.10/site-packages (from anyio<5,>=3.5.0->openai>=1.3->pyautogen>=0.2.3) (1.2.0)\n",
- "Requirement already satisfied: certifi in /usr/local/lib/python3.10/site-packages (from httpx<1,>=0.23.0->openai>=1.3->pyautogen>=0.2.3) (2023.11.17)\n",
- "Requirement already satisfied: httpcore==1.* in /home/vscode/.local/lib/python3.10/site-packages (from httpx<1,>=0.23.0->openai>=1.3->pyautogen>=0.2.3) (1.0.2)\n",
- "Requirement already satisfied: h11<0.15,>=0.13 in /home/vscode/.local/lib/python3.10/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai>=1.3->pyautogen>=0.2.3) (0.14.0)\n",
- "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/site-packages (from requests>=2.26.0->tiktoken->pyautogen>=0.2.3) (3.3.2)\n",
- "Requirement already satisfied: urllib3<3,>=1.21.1 in /home/vscode/.local/lib/python3.10/site-packages (from requests>=2.26.0->tiktoken->pyautogen>=0.2.3) (1.26.18)\n",
- "Defaulting to user installation because normal site-packages is not writeable\n",
- "Requirement already satisfied: chromadb in /home/vscode/.local/lib/python3.10/site-packages (0.4.22)\n",
- "Requirement already satisfied: build>=1.0.3 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (1.0.3)\n",
- "Requirement already satisfied: requests>=2.28 in /usr/local/lib/python3.10/site-packages (from chromadb) (2.31.0)\n",
- "Requirement already satisfied: pydantic>=1.9 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (1.10.9)\n",
- "Requirement already satisfied: chroma-hnswlib==0.7.3 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (0.7.3)\n",
- "Requirement already satisfied: fastapi>=0.95.2 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (0.108.0)\n",
- "Requirement already satisfied: uvicorn>=0.18.3 in /home/vscode/.local/lib/python3.10/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.25.0)\n",
- "Requirement already satisfied: numpy>=1.22.5 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (1.26.3)\n",
- "Requirement already satisfied: posthog>=2.4.0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (3.1.0)\n",
- "Requirement already satisfied: typing-extensions>=4.5.0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (4.9.0)\n",
- "Requirement already satisfied: pulsar-client>=3.1.0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (3.4.0)\n",
- "Requirement already satisfied: onnxruntime>=1.14.1 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (1.16.3)\n",
- "Requirement already satisfied: opentelemetry-api>=1.2.0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (1.22.0)\n",
- "Requirement already satisfied: opentelemetry-exporter-otlp-proto-grpc>=1.2.0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (1.22.0)\n",
- "Requirement already satisfied: opentelemetry-instrumentation-fastapi>=0.41b0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (0.43b0)\n",
- "Requirement already satisfied: opentelemetry-sdk>=1.2.0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (1.22.0)\n",
- "Requirement already satisfied: tokenizers>=0.13.2 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (0.15.0)\n",
- "Requirement already satisfied: pypika>=0.48.9 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (0.48.9)\n",
- "Requirement already satisfied: tqdm>=4.65.0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (4.66.1)\n",
- "Requirement already satisfied: overrides>=7.3.1 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (7.4.0)\n",
- "Requirement already satisfied: importlib-resources in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (6.1.1)\n",
- "Requirement already satisfied: grpcio>=1.58.0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (1.60.0)\n",
- "Requirement already satisfied: bcrypt>=4.0.1 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (4.1.2)\n",
- "Requirement already satisfied: typer>=0.9.0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (0.9.0)\n",
- "Requirement already satisfied: kubernetes>=28.1.0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (28.1.0)\n",
- "Requirement already satisfied: tenacity>=8.2.3 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (8.2.3)\n",
- "Requirement already satisfied: PyYAML>=6.0.0 in /usr/local/lib/python3.10/site-packages (from chromadb) (6.0.1)\n",
- "Requirement already satisfied: mmh3>=4.0.1 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (4.0.1)\n",
- "Requirement already satisfied: packaging>=19.0 in /usr/local/lib/python3.10/site-packages (from build>=1.0.3->chromadb) (23.2)\n",
- "Requirement already satisfied: pyproject_hooks in /home/vscode/.local/lib/python3.10/site-packages (from build>=1.0.3->chromadb) (1.0.0)\n",
- "Requirement already satisfied: tomli>=1.1.0 in /usr/local/lib/python3.10/site-packages (from build>=1.0.3->chromadb) (2.0.1)\n",
- "Requirement already satisfied: starlette<0.33.0,>=0.29.0 in /home/vscode/.local/lib/python3.10/site-packages (from fastapi>=0.95.2->chromadb) (0.32.0.post1)\n",
- "Requirement already satisfied: certifi>=14.05.14 in /usr/local/lib/python3.10/site-packages (from kubernetes>=28.1.0->chromadb) (2023.11.17)\n",
- "Requirement already satisfied: six>=1.9.0 in /home/vscode/.local/lib/python3.10/site-packages (from kubernetes>=28.1.0->chromadb) (1.16.0)\n",
- "Requirement already satisfied: python-dateutil>=2.5.3 in /home/vscode/.local/lib/python3.10/site-packages (from kubernetes>=28.1.0->chromadb) (2.8.2)\n",
- "Requirement already satisfied: google-auth>=1.0.1 in /home/vscode/.local/lib/python3.10/site-packages (from kubernetes>=28.1.0->chromadb) (2.26.1)\n",
- "Requirement already satisfied: websocket-client!=0.40.0,!=0.41.*,!=0.42.*,>=0.32.0 in /home/vscode/.local/lib/python3.10/site-packages (from kubernetes>=28.1.0->chromadb) (1.7.0)\n",
- "Requirement already satisfied: requests-oauthlib in /home/vscode/.local/lib/python3.10/site-packages (from kubernetes>=28.1.0->chromadb) (1.3.1)\n",
- "Requirement already satisfied: oauthlib>=3.2.2 in /home/vscode/.local/lib/python3.10/site-packages (from kubernetes>=28.1.0->chromadb) (3.2.2)\n",
- "Requirement already satisfied: urllib3<2.0,>=1.24.2 in /home/vscode/.local/lib/python3.10/site-packages (from kubernetes>=28.1.0->chromadb) (1.26.18)\n",
- "Requirement already satisfied: coloredlogs in /home/vscode/.local/lib/python3.10/site-packages (from onnxruntime>=1.14.1->chromadb) (15.0.1)\n",
- "Requirement already satisfied: flatbuffers in /home/vscode/.local/lib/python3.10/site-packages (from onnxruntime>=1.14.1->chromadb) (23.5.26)\n",
- "Requirement already satisfied: protobuf in /home/vscode/.local/lib/python3.10/site-packages (from onnxruntime>=1.14.1->chromadb) (4.25.1)\n",
- "Requirement already satisfied: sympy in /home/vscode/.local/lib/python3.10/site-packages (from onnxruntime>=1.14.1->chromadb) (1.12)\n",
- "Requirement already satisfied: deprecated>=1.2.6 in /usr/local/lib/python3.10/site-packages (from opentelemetry-api>=1.2.0->chromadb) (1.2.14)\n",
- "Requirement already satisfied: importlib-metadata<7.0,>=6.0 in /home/vscode/.local/lib/python3.10/site-packages (from opentelemetry-api>=1.2.0->chromadb) (6.11.0)\n",
- "Requirement already satisfied: backoff<3.0.0,>=1.10.0 in /home/vscode/.local/lib/python3.10/site-packages (from opentelemetry-exporter-otlp-proto-grpc>=1.2.0->chromadb) (2.2.1)\n",
- "Requirement already satisfied: googleapis-common-protos~=1.52 in /home/vscode/.local/lib/python3.10/site-packages (from opentelemetry-exporter-otlp-proto-grpc>=1.2.0->chromadb) (1.62.0)\n",
- "Requirement already satisfied: opentelemetry-exporter-otlp-proto-common==1.22.0 in /home/vscode/.local/lib/python3.10/site-packages (from opentelemetry-exporter-otlp-proto-grpc>=1.2.0->chromadb) (1.22.0)\n",
- "Requirement already satisfied: opentelemetry-proto==1.22.0 in /home/vscode/.local/lib/python3.10/site-packages (from opentelemetry-exporter-otlp-proto-grpc>=1.2.0->chromadb) (1.22.0)\n",
- "Requirement already satisfied: opentelemetry-instrumentation-asgi==0.43b0 in /home/vscode/.local/lib/python3.10/site-packages (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (0.43b0)\n",
- "Requirement already satisfied: opentelemetry-instrumentation==0.43b0 in /home/vscode/.local/lib/python3.10/site-packages (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (0.43b0)\n",
- "Requirement already satisfied: opentelemetry-semantic-conventions==0.43b0 in /home/vscode/.local/lib/python3.10/site-packages (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (0.43b0)\n",
- "Requirement already satisfied: opentelemetry-util-http==0.43b0 in /home/vscode/.local/lib/python3.10/site-packages (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (0.43b0)\n",
- "Requirement already satisfied: setuptools>=16.0 in /usr/local/lib/python3.10/site-packages (from opentelemetry-instrumentation==0.43b0->opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (69.0.2)\n",
- "Requirement already satisfied: wrapt<2.0.0,>=1.0.0 in /usr/local/lib/python3.10/site-packages (from opentelemetry-instrumentation==0.43b0->opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (1.16.0)\n",
- "Requirement already satisfied: asgiref~=3.0 in /home/vscode/.local/lib/python3.10/site-packages (from opentelemetry-instrumentation-asgi==0.43b0->opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (3.7.2)\n",
- "Requirement already satisfied: monotonic>=1.5 in /home/vscode/.local/lib/python3.10/site-packages (from posthog>=2.4.0->chromadb) (1.6)\n",
- "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/site-packages (from requests>=2.28->chromadb) (3.3.2)\n",
- "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/site-packages (from requests>=2.28->chromadb) (3.6)\n",
- "Requirement already satisfied: huggingface_hub<1.0,>=0.16.4 in /home/vscode/.local/lib/python3.10/site-packages (from tokenizers>=0.13.2->chromadb) (0.20.2)\n",
- "Requirement already satisfied: click<9.0.0,>=7.1.1 in /usr/local/lib/python3.10/site-packages (from typer>=0.9.0->chromadb) (8.1.7)\n",
- "Requirement already satisfied: h11>=0.8 in /home/vscode/.local/lib/python3.10/site-packages (from uvicorn>=0.18.3->uvicorn[standard]>=0.18.3->chromadb) (0.14.0)\n",
- "Requirement already satisfied: httptools>=0.5.0 in /home/vscode/.local/lib/python3.10/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.6.1)\n",
- "Requirement already satisfied: python-dotenv>=0.13 in /home/vscode/.local/lib/python3.10/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (1.0.0)\n",
- "Requirement already satisfied: uvloop!=0.15.0,!=0.15.1,>=0.14.0 in /home/vscode/.local/lib/python3.10/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.19.0)\n",
- "Requirement already satisfied: watchfiles>=0.13 in /home/vscode/.local/lib/python3.10/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.21.0)\n",
- "Requirement already satisfied: websockets>=10.4 in /home/vscode/.local/lib/python3.10/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (12.0)\n",
- "Requirement already satisfied: cachetools<6.0,>=2.0.0 in /home/vscode/.local/lib/python3.10/site-packages (from google-auth>=1.0.1->kubernetes>=28.1.0->chromadb) (5.3.2)\n",
- "Requirement already satisfied: pyasn1-modules>=0.2.1 in /home/vscode/.local/lib/python3.10/site-packages (from google-auth>=1.0.1->kubernetes>=28.1.0->chromadb) (0.3.0)\n",
- "Requirement already satisfied: rsa<5,>=3.1.4 in /home/vscode/.local/lib/python3.10/site-packages (from google-auth>=1.0.1->kubernetes>=28.1.0->chromadb) (4.9)\n",
- "Requirement already satisfied: filelock in /home/vscode/.local/lib/python3.10/site-packages (from huggingface_hub<1.0,>=0.16.4->tokenizers>=0.13.2->chromadb) (3.13.1)\n",
- "Requirement already satisfied: fsspec>=2023.5.0 in /home/vscode/.local/lib/python3.10/site-packages (from huggingface_hub<1.0,>=0.16.4->tokenizers>=0.13.2->chromadb) (2023.12.2)\n",
- "Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.10/site-packages (from importlib-metadata<7.0,>=6.0->opentelemetry-api>=1.2.0->chromadb) (3.17.0)\n",
- "Requirement already satisfied: anyio<5,>=3.4.0 in /home/vscode/.local/lib/python3.10/site-packages (from starlette<0.33.0,>=0.29.0->fastapi>=0.95.2->chromadb) (4.2.0)\n",
- "Requirement already satisfied: humanfriendly>=9.1 in /home/vscode/.local/lib/python3.10/site-packages (from coloredlogs->onnxruntime>=1.14.1->chromadb) (10.0)\n",
- "Requirement already satisfied: mpmath>=0.19 in /home/vscode/.local/lib/python3.10/site-packages (from sympy->onnxruntime>=1.14.1->chromadb) (1.3.0)\n",
- "Requirement already satisfied: sniffio>=1.1 in /home/vscode/.local/lib/python3.10/site-packages (from anyio<5,>=3.4.0->starlette<0.33.0,>=0.29.0->fastapi>=0.95.2->chromadb) (1.3.0)\n",
- "Requirement already satisfied: exceptiongroup>=1.0.2 in /home/vscode/.local/lib/python3.10/site-packages (from anyio<5,>=3.4.0->starlette<0.33.0,>=0.29.0->fastapi>=0.95.2->chromadb) (1.2.0)\n",
- "Requirement already satisfied: pyasn1<0.6.0,>=0.4.6 in /home/vscode/.local/lib/python3.10/site-packages (from pyasn1-modules>=0.2.1->google-auth>=1.0.1->kubernetes>=28.1.0->chromadb) (0.5.1)\n",
- "Defaulting to user installation because normal site-packages is not writeable\n",
- "Requirement already satisfied: sentence_transformers in /home/vscode/.local/lib/python3.10/site-packages (2.2.2)\n",
- "Requirement already satisfied: transformers<5.0.0,>=4.6.0 in /home/vscode/.local/lib/python3.10/site-packages (from sentence_transformers) (4.36.2)\n",
- "Requirement already satisfied: tqdm in /home/vscode/.local/lib/python3.10/site-packages (from sentence_transformers) (4.66.1)\n",
- "Requirement already satisfied: torch>=1.6.0 in /home/vscode/.local/lib/python3.10/site-packages (from sentence_transformers) (2.1.2)\n",
- "Requirement already satisfied: torchvision in /home/vscode/.local/lib/python3.10/site-packages (from sentence_transformers) (0.16.2)\n",
- "Requirement already satisfied: numpy in /home/vscode/.local/lib/python3.10/site-packages (from sentence_transformers) (1.26.3)\n",
- "Requirement already satisfied: scikit-learn in /home/vscode/.local/lib/python3.10/site-packages (from sentence_transformers) (1.3.2)\n",
- "Requirement already satisfied: scipy in /home/vscode/.local/lib/python3.10/site-packages (from sentence_transformers) (1.11.4)\n",
- "Requirement already satisfied: nltk in /home/vscode/.local/lib/python3.10/site-packages (from sentence_transformers) (3.8.1)\n",
- "Requirement already satisfied: sentencepiece in /home/vscode/.local/lib/python3.10/site-packages (from sentence_transformers) (0.1.99)\n",
- "Requirement already satisfied: huggingface-hub>=0.4.0 in /home/vscode/.local/lib/python3.10/site-packages (from sentence_transformers) (0.20.2)\n",
- "Requirement already satisfied: filelock in /home/vscode/.local/lib/python3.10/site-packages (from huggingface-hub>=0.4.0->sentence_transformers) (3.13.1)\n",
- "Requirement already satisfied: fsspec>=2023.5.0 in /home/vscode/.local/lib/python3.10/site-packages (from huggingface-hub>=0.4.0->sentence_transformers) (2023.12.2)\n",
- "Requirement already satisfied: requests in /usr/local/lib/python3.10/site-packages (from huggingface-hub>=0.4.0->sentence_transformers) (2.31.0)\n",
- "Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/site-packages (from huggingface-hub>=0.4.0->sentence_transformers) (6.0.1)\n",
- "Requirement already satisfied: typing-extensions>=3.7.4.3 in /home/vscode/.local/lib/python3.10/site-packages (from huggingface-hub>=0.4.0->sentence_transformers) (4.9.0)\n",
- "Requirement already satisfied: packaging>=20.9 in /usr/local/lib/python3.10/site-packages (from huggingface-hub>=0.4.0->sentence_transformers) (23.2)\n",
- "Requirement already satisfied: sympy in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (1.12)\n",
- "Requirement already satisfied: networkx in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (3.2.1)\n",
- "Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (3.1.2)\n",
- "Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (12.1.105)\n",
- "Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (12.1.105)\n",
- "Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (12.1.105)\n",
- "Requirement already satisfied: nvidia-cudnn-cu12==8.9.2.26 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (8.9.2.26)\n",
- "Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (12.1.3.1)\n",
- "Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (11.0.2.54)\n",
- "Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (10.3.2.106)\n",
- "Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (11.4.5.107)\n",
- "Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (12.1.0.106)\n",
- "Requirement already satisfied: nvidia-nccl-cu12==2.18.1 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (2.18.1)\n",
- "Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (12.1.105)\n",
- "Requirement already satisfied: triton==2.1.0 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (2.1.0)\n",
- "Requirement already satisfied: nvidia-nvjitlink-cu12 in /home/vscode/.local/lib/python3.10/site-packages (from nvidia-cusolver-cu12==11.4.5.107->torch>=1.6.0->sentence_transformers) (12.3.101)\n",
- "Requirement already satisfied: regex!=2019.12.17 in /home/vscode/.local/lib/python3.10/site-packages (from transformers<5.0.0,>=4.6.0->sentence_transformers) (2023.12.25)\n",
- "Requirement already satisfied: tokenizers<0.19,>=0.14 in /home/vscode/.local/lib/python3.10/site-packages (from transformers<5.0.0,>=4.6.0->sentence_transformers) (0.15.0)\n",
- "Requirement already satisfied: safetensors>=0.3.1 in /home/vscode/.local/lib/python3.10/site-packages (from transformers<5.0.0,>=4.6.0->sentence_transformers) (0.4.1)\n",
- "Requirement already satisfied: click in /usr/local/lib/python3.10/site-packages (from nltk->sentence_transformers) (8.1.7)\n",
- "Requirement already satisfied: joblib in /home/vscode/.local/lib/python3.10/site-packages (from nltk->sentence_transformers) (1.3.2)\n",
- "Requirement already satisfied: threadpoolctl>=2.0.0 in /home/vscode/.local/lib/python3.10/site-packages (from scikit-learn->sentence_transformers) (3.2.0)\n",
- "Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /home/vscode/.local/lib/python3.10/site-packages (from torchvision->sentence_transformers) (10.2.0)\n",
- "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/site-packages (from jinja2->torch>=1.6.0->sentence_transformers) (2.1.3)\n",
- "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/site-packages (from requests->huggingface-hub>=0.4.0->sentence_transformers) (3.3.2)\n",
- "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/site-packages (from requests->huggingface-hub>=0.4.0->sentence_transformers) (3.6)\n",
- "Requirement already satisfied: urllib3<3,>=1.21.1 in /home/vscode/.local/lib/python3.10/site-packages (from requests->huggingface-hub>=0.4.0->sentence_transformers) (1.26.18)\n",
- "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/site-packages (from requests->huggingface-hub>=0.4.0->sentence_transformers) (2023.11.17)\n",
- "Requirement already satisfied: mpmath>=0.19 in /home/vscode/.local/lib/python3.10/site-packages (from sympy->torch>=1.6.0->sentence_transformers) (1.3.0)\n",
- "Defaulting to user installation because normal site-packages is not writeable\n",
- "Requirement already satisfied: tiktoken in /home/vscode/.local/lib/python3.10/site-packages (0.5.2)\n",
- "Requirement already satisfied: regex>=2022.1.18 in /home/vscode/.local/lib/python3.10/site-packages (from tiktoken) (2023.12.25)\n",
- "Requirement already satisfied: requests>=2.26.0 in /usr/local/lib/python3.10/site-packages (from tiktoken) (2.31.0)\n",
- "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/site-packages (from requests>=2.26.0->tiktoken) (3.3.2)\n",
- "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/site-packages (from requests>=2.26.0->tiktoken) (3.6)\n",
- "Requirement already satisfied: urllib3<3,>=1.21.1 in /home/vscode/.local/lib/python3.10/site-packages (from requests>=2.26.0->tiktoken) (1.26.18)\n",
- "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/site-packages (from requests>=2.26.0->tiktoken) (2023.11.17)\n",
- "Defaulting to user installation because normal site-packages is not writeable\n",
- "Requirement already satisfied: pypdf in /home/vscode/.local/lib/python3.10/site-packages (3.17.4)\n"
- ]
- }
- ],
- "source": [
- "!pip install \"pyautogen>=0.2.3\"\n",
- "!pip install chromadb\n",
- "!pip install sentence_transformers\n",
- "!pip install tiktoken\n",
- "!pip install pypdf"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "yU8zQPetsW28"
- },
- "outputs": [],
- "source": [
- "from autogen import AssistantAgent\n",
- "from autogen.agentchat.contrib.retrieve_user_proxy_agent import RetrieveUserProxyAgent\n",
- "from autogen.agentchat.user_proxy_agent import UserProxyAgent"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "R2ITRFQisgzI"
- },
- "outputs": [],
- "source": [
- "from typing import Callable, Dict, Optional, Union, List, Tuple, Any\n",
- "import asyncio\n",
- "\n",
- "# Define an asynchronous function that simulates some asynchronous task (e.g., I/O operation)\n",
- "async def my_asynchronous_function():\n",
- " print(\"Start asynchronous function\")\n",
- " await asyncio.sleep(2) # Simulate some asynchronous task (e.g., I/O operation)\n",
- " print(\"End asynchronous function\")\n",
- " return \"input\"\n",
- "\n",
- "# Define a custom class `CustomisedUserProxyAgent` that extends `UserProxyAgent`\n",
- "class CustomisedUserProxyAgent(UserProxyAgent):\n",
- "\n",
- " # Asynchronous function to get human input\n",
- " async def a_get_human_input(self, prompt: str) -> str:\n",
- " # Call the asynchronous function to get user input asynchronously\n",
- " user_input=await my_asynchronous_function()\n",
- "\n",
- " return user_input\n",
- " # Asynchronous function to receive a message\n",
- " async def a_receive(\n",
- " self,\n",
- " message: Union[Dict, str],\n",
- " sender,\n",
- " request_reply: Optional[bool] = None,\n",
- " silent: Optional[bool] = False,\n",
- " ):\n",
- " # Call the superclass method to handle message reception asynchronously\n",
- " await super().a_receive(message, sender, request_reply, silent)\n",
- "\n",
- "\n",
- "\n",
- "\n",
- "\n",
- "class CustomisedAssistantAgent(AssistantAgent):\n",
- "\n",
- " # Asynchronous function to get human input\n",
- " async def a_get_human_input(self, prompt: str) -> str:\n",
- " # Call the asynchronous function to get user input asynchronously\n",
- " user_input=await my_asynchronous_function()\n",
- "\n",
- " return user_input\n",
- "\n",
- " # Asynchronous function to receive a message\n",
- " async def a_receive(\n",
- " self,\n",
- " message: Union[Dict, str],\n",
- " sender,\n",
- " request_reply: Optional[bool] = None,\n",
- " silent: Optional[bool] = False,\n",
- " ):\n",
- " # Call the superclass method to handle message reception asynchronously\n",
- " await super().a_receive(message, sender, request_reply, silent)\n",
- "\n",
- "\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "2v_DhqBFuA1O"
- },
- "outputs": [],
- "source": [
- "def create_llm_config(model, temperature, seed):\n",
- " config_list = [\n",
- " {\n",
- " \"model\": \"<model_name>\",\n",
- " \"api_key\": \"<api_key>\",\n",
- " },\n",
- " ]\n",
- "\n",
- " llm_config = {\n",
- " \"seed\": int(seed),\n",
- " \"config_list\": config_list,\n",
- " \"temperature\": float(temperature),\n",
- " }\n",
- "\n",
- " return llm_config\n"
- ]
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
},
+ "id": "tLIs1YRdr8jM",
+ "outputId": "909c1c70-1a22-4e9d-b7f4-a40e2d737fb0"
+ },
+ "outputs": [
{
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "yHIUO2CiwfIp",
- "outputId": "79ceccca-f91c-4dde-9e21-1f2129cdc1a8"
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Requirement already satisfied: nest-asyncio in /usr/local/lib/python3.10/dist-packages (1.5.8)\n"
- ]
- }
- ],
- "source": [
- "!pip install nest-asyncio"
- ]
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Defaulting to user installation because normal site-packages is not writeable\n",
+ "Requirement already satisfied: pyautogen>=0.2.3 in /home/vscode/.local/lib/python3.10/site-packages (0.2.3)\n",
+ "Requirement already satisfied: openai>=1.3 in /home/vscode/.local/lib/python3.10/site-packages (from pyautogen>=0.2.3) (1.6.1)\n",
+ "Requirement already satisfied: diskcache in /home/vscode/.local/lib/python3.10/site-packages (from pyautogen>=0.2.3) (5.6.3)\n",
+ "Requirement already satisfied: termcolor in /home/vscode/.local/lib/python3.10/site-packages (from pyautogen>=0.2.3) (2.4.0)\n",
+ "Requirement already satisfied: flaml in /home/vscode/.local/lib/python3.10/site-packages (from pyautogen>=0.2.3) (2.1.1)\n",
+ "Requirement already satisfied: python-dotenv in /home/vscode/.local/lib/python3.10/site-packages (from pyautogen>=0.2.3) (1.0.0)\n",
+ "Requirement already satisfied: tiktoken in /home/vscode/.local/lib/python3.10/site-packages (from pyautogen>=0.2.3) (0.5.2)\n",
+ "Requirement already satisfied: pydantic<3,>=1.10 in /home/vscode/.local/lib/python3.10/site-packages (from pyautogen>=0.2.3) (1.10.9)\n",
+ "Requirement already satisfied: anyio<5,>=3.5.0 in /home/vscode/.local/lib/python3.10/site-packages (from openai>=1.3->pyautogen>=0.2.3) (4.2.0)\n",
+ "Requirement already satisfied: distro<2,>=1.7.0 in /home/vscode/.local/lib/python3.10/site-packages (from openai>=1.3->pyautogen>=0.2.3) (1.9.0)\n",
+ "Requirement already satisfied: httpx<1,>=0.23.0 in /home/vscode/.local/lib/python3.10/site-packages (from openai>=1.3->pyautogen>=0.2.3) (0.26.0)\n",
+ "Requirement already satisfied: sniffio in /home/vscode/.local/lib/python3.10/site-packages (from openai>=1.3->pyautogen>=0.2.3) (1.3.0)\n",
+ "Requirement already satisfied: tqdm>4 in /home/vscode/.local/lib/python3.10/site-packages (from openai>=1.3->pyautogen>=0.2.3) (4.66.1)\n",
+ "Requirement already satisfied: typing-extensions<5,>=4.7 in /home/vscode/.local/lib/python3.10/site-packages (from openai>=1.3->pyautogen>=0.2.3) (4.9.0)\n",
+ "Requirement already satisfied: NumPy>=1.17.0rc1 in /home/vscode/.local/lib/python3.10/site-packages (from flaml->pyautogen>=0.2.3) (1.26.3)\n",
+ "Requirement already satisfied: regex>=2022.1.18 in /home/vscode/.local/lib/python3.10/site-packages (from tiktoken->pyautogen>=0.2.3) (2023.12.25)\n",
+ "Requirement already satisfied: requests>=2.26.0 in /usr/local/lib/python3.10/site-packages (from tiktoken->pyautogen>=0.2.3) (2.31.0)\n",
+ "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/site-packages (from anyio<5,>=3.5.0->openai>=1.3->pyautogen>=0.2.3) (3.6)\n",
+ "Requirement already satisfied: exceptiongroup>=1.0.2 in /home/vscode/.local/lib/python3.10/site-packages (from anyio<5,>=3.5.0->openai>=1.3->pyautogen>=0.2.3) (1.2.0)\n",
+ "Requirement already satisfied: certifi in /usr/local/lib/python3.10/site-packages (from httpx<1,>=0.23.0->openai>=1.3->pyautogen>=0.2.3) (2023.11.17)\n",
+ "Requirement already satisfied: httpcore==1.* in /home/vscode/.local/lib/python3.10/site-packages (from httpx<1,>=0.23.0->openai>=1.3->pyautogen>=0.2.3) (1.0.2)\n",
+ "Requirement already satisfied: h11<0.15,>=0.13 in /home/vscode/.local/lib/python3.10/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai>=1.3->pyautogen>=0.2.3) (0.14.0)\n",
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/site-packages (from requests>=2.26.0->tiktoken->pyautogen>=0.2.3) (3.3.2)\n",
+ "Requirement already satisfied: urllib3<3,>=1.21.1 in /home/vscode/.local/lib/python3.10/site-packages (from requests>=2.26.0->tiktoken->pyautogen>=0.2.3) (1.26.18)\n",
+ "Defaulting to user installation because normal site-packages is not writeable\n",
+ "Requirement already satisfied: chromadb in /home/vscode/.local/lib/python3.10/site-packages (0.4.22)\n",
+ "Requirement already satisfied: build>=1.0.3 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (1.0.3)\n",
+ "Requirement already satisfied: requests>=2.28 in /usr/local/lib/python3.10/site-packages (from chromadb) (2.31.0)\n",
+ "Requirement already satisfied: pydantic>=1.9 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (1.10.9)\n",
+ "Requirement already satisfied: chroma-hnswlib==0.7.3 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (0.7.3)\n",
+ "Requirement already satisfied: fastapi>=0.95.2 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (0.108.0)\n",
+ "Requirement already satisfied: uvicorn>=0.18.3 in /home/vscode/.local/lib/python3.10/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.25.0)\n",
+ "Requirement already satisfied: numpy>=1.22.5 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (1.26.3)\n",
+ "Requirement already satisfied: posthog>=2.4.0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (3.1.0)\n",
+ "Requirement already satisfied: typing-extensions>=4.5.0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (4.9.0)\n",
+ "Requirement already satisfied: pulsar-client>=3.1.0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (3.4.0)\n",
+ "Requirement already satisfied: onnxruntime>=1.14.1 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (1.16.3)\n",
+ "Requirement already satisfied: opentelemetry-api>=1.2.0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (1.22.0)\n",
+ "Requirement already satisfied: opentelemetry-exporter-otlp-proto-grpc>=1.2.0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (1.22.0)\n",
+ "Requirement already satisfied: opentelemetry-instrumentation-fastapi>=0.41b0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (0.43b0)\n",
+ "Requirement already satisfied: opentelemetry-sdk>=1.2.0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (1.22.0)\n",
+ "Requirement already satisfied: tokenizers>=0.13.2 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (0.15.0)\n",
+ "Requirement already satisfied: pypika>=0.48.9 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (0.48.9)\n",
+ "Requirement already satisfied: tqdm>=4.65.0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (4.66.1)\n",
+ "Requirement already satisfied: overrides>=7.3.1 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (7.4.0)\n",
+ "Requirement already satisfied: importlib-resources in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (6.1.1)\n",
+ "Requirement already satisfied: grpcio>=1.58.0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (1.60.0)\n",
+ "Requirement already satisfied: bcrypt>=4.0.1 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (4.1.2)\n",
+ "Requirement already satisfied: typer>=0.9.0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (0.9.0)\n",
+ "Requirement already satisfied: kubernetes>=28.1.0 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (28.1.0)\n",
+ "Requirement already satisfied: tenacity>=8.2.3 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (8.2.3)\n",
+ "Requirement already satisfied: PyYAML>=6.0.0 in /usr/local/lib/python3.10/site-packages (from chromadb) (6.0.1)\n",
+ "Requirement already satisfied: mmh3>=4.0.1 in /home/vscode/.local/lib/python3.10/site-packages (from chromadb) (4.0.1)\n",
+ "Requirement already satisfied: packaging>=19.0 in /usr/local/lib/python3.10/site-packages (from build>=1.0.3->chromadb) (23.2)\n",
+ "Requirement already satisfied: pyproject_hooks in /home/vscode/.local/lib/python3.10/site-packages (from build>=1.0.3->chromadb) (1.0.0)\n",
+ "Requirement already satisfied: tomli>=1.1.0 in /usr/local/lib/python3.10/site-packages (from build>=1.0.3->chromadb) (2.0.1)\n",
+ "Requirement already satisfied: starlette<0.33.0,>=0.29.0 in /home/vscode/.local/lib/python3.10/site-packages (from fastapi>=0.95.2->chromadb) (0.32.0.post1)\n",
+ "Requirement already satisfied: certifi>=14.05.14 in /usr/local/lib/python3.10/site-packages (from kubernetes>=28.1.0->chromadb) (2023.11.17)\n",
+ "Requirement already satisfied: six>=1.9.0 in /home/vscode/.local/lib/python3.10/site-packages (from kubernetes>=28.1.0->chromadb) (1.16.0)\n",
+ "Requirement already satisfied: python-dateutil>=2.5.3 in /home/vscode/.local/lib/python3.10/site-packages (from kubernetes>=28.1.0->chromadb) (2.8.2)\n",
+ "Requirement already satisfied: google-auth>=1.0.1 in /home/vscode/.local/lib/python3.10/site-packages (from kubernetes>=28.1.0->chromadb) (2.26.1)\n",
+ "Requirement already satisfied: websocket-client!=0.40.0,!=0.41.*,!=0.42.*,>=0.32.0 in /home/vscode/.local/lib/python3.10/site-packages (from kubernetes>=28.1.0->chromadb) (1.7.0)\n",
+ "Requirement already satisfied: requests-oauthlib in /home/vscode/.local/lib/python3.10/site-packages (from kubernetes>=28.1.0->chromadb) (1.3.1)\n",
+ "Requirement already satisfied: oauthlib>=3.2.2 in /home/vscode/.local/lib/python3.10/site-packages (from kubernetes>=28.1.0->chromadb) (3.2.2)\n",
+ "Requirement already satisfied: urllib3<2.0,>=1.24.2 in /home/vscode/.local/lib/python3.10/site-packages (from kubernetes>=28.1.0->chromadb) (1.26.18)\n",
+ "Requirement already satisfied: coloredlogs in /home/vscode/.local/lib/python3.10/site-packages (from onnxruntime>=1.14.1->chromadb) (15.0.1)\n",
+ "Requirement already satisfied: flatbuffers in /home/vscode/.local/lib/python3.10/site-packages (from onnxruntime>=1.14.1->chromadb) (23.5.26)\n",
+ "Requirement already satisfied: protobuf in /home/vscode/.local/lib/python3.10/site-packages (from onnxruntime>=1.14.1->chromadb) (4.25.1)\n",
+ "Requirement already satisfied: sympy in /home/vscode/.local/lib/python3.10/site-packages (from onnxruntime>=1.14.1->chromadb) (1.12)\n",
+ "Requirement already satisfied: deprecated>=1.2.6 in /usr/local/lib/python3.10/site-packages (from opentelemetry-api>=1.2.0->chromadb) (1.2.14)\n",
+ "Requirement already satisfied: importlib-metadata<7.0,>=6.0 in /home/vscode/.local/lib/python3.10/site-packages (from opentelemetry-api>=1.2.0->chromadb) (6.11.0)\n",
+ "Requirement already satisfied: backoff<3.0.0,>=1.10.0 in /home/vscode/.local/lib/python3.10/site-packages (from opentelemetry-exporter-otlp-proto-grpc>=1.2.0->chromadb) (2.2.1)\n",
+ "Requirement already satisfied: googleapis-common-protos~=1.52 in /home/vscode/.local/lib/python3.10/site-packages (from opentelemetry-exporter-otlp-proto-grpc>=1.2.0->chromadb) (1.62.0)\n",
+ "Requirement already satisfied: opentelemetry-exporter-otlp-proto-common==1.22.0 in /home/vscode/.local/lib/python3.10/site-packages (from opentelemetry-exporter-otlp-proto-grpc>=1.2.0->chromadb) (1.22.0)\n",
+ "Requirement already satisfied: opentelemetry-proto==1.22.0 in /home/vscode/.local/lib/python3.10/site-packages (from opentelemetry-exporter-otlp-proto-grpc>=1.2.0->chromadb) (1.22.0)\n",
+ "Requirement already satisfied: opentelemetry-instrumentation-asgi==0.43b0 in /home/vscode/.local/lib/python3.10/site-packages (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (0.43b0)\n",
+ "Requirement already satisfied: opentelemetry-instrumentation==0.43b0 in /home/vscode/.local/lib/python3.10/site-packages (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (0.43b0)\n",
+ "Requirement already satisfied: opentelemetry-semantic-conventions==0.43b0 in /home/vscode/.local/lib/python3.10/site-packages (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (0.43b0)\n",
+ "Requirement already satisfied: opentelemetry-util-http==0.43b0 in /home/vscode/.local/lib/python3.10/site-packages (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (0.43b0)\n",
+ "Requirement already satisfied: setuptools>=16.0 in /usr/local/lib/python3.10/site-packages (from opentelemetry-instrumentation==0.43b0->opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (69.0.2)\n",
+ "Requirement already satisfied: wrapt<2.0.0,>=1.0.0 in /usr/local/lib/python3.10/site-packages (from opentelemetry-instrumentation==0.43b0->opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (1.16.0)\n",
+ "Requirement already satisfied: asgiref~=3.0 in /home/vscode/.local/lib/python3.10/site-packages (from opentelemetry-instrumentation-asgi==0.43b0->opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (3.7.2)\n",
+ "Requirement already satisfied: monotonic>=1.5 in /home/vscode/.local/lib/python3.10/site-packages (from posthog>=2.4.0->chromadb) (1.6)\n",
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/site-packages (from requests>=2.28->chromadb) (3.3.2)\n",
+ "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/site-packages (from requests>=2.28->chromadb) (3.6)\n",
+ "Requirement already satisfied: huggingface_hub<1.0,>=0.16.4 in /home/vscode/.local/lib/python3.10/site-packages (from tokenizers>=0.13.2->chromadb) (0.20.2)\n",
+ "Requirement already satisfied: click<9.0.0,>=7.1.1 in /usr/local/lib/python3.10/site-packages (from typer>=0.9.0->chromadb) (8.1.7)\n",
+ "Requirement already satisfied: h11>=0.8 in /home/vscode/.local/lib/python3.10/site-packages (from uvicorn>=0.18.3->uvicorn[standard]>=0.18.3->chromadb) (0.14.0)\n",
+ "Requirement already satisfied: httptools>=0.5.0 in /home/vscode/.local/lib/python3.10/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.6.1)\n",
+ "Requirement already satisfied: python-dotenv>=0.13 in /home/vscode/.local/lib/python3.10/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (1.0.0)\n",
+ "Requirement already satisfied: uvloop!=0.15.0,!=0.15.1,>=0.14.0 in /home/vscode/.local/lib/python3.10/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.19.0)\n",
+ "Requirement already satisfied: watchfiles>=0.13 in /home/vscode/.local/lib/python3.10/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.21.0)\n",
+ "Requirement already satisfied: websockets>=10.4 in /home/vscode/.local/lib/python3.10/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (12.0)\n",
+ "Requirement already satisfied: cachetools<6.0,>=2.0.0 in /home/vscode/.local/lib/python3.10/site-packages (from google-auth>=1.0.1->kubernetes>=28.1.0->chromadb) (5.3.2)\n",
+ "Requirement already satisfied: pyasn1-modules>=0.2.1 in /home/vscode/.local/lib/python3.10/site-packages (from google-auth>=1.0.1->kubernetes>=28.1.0->chromadb) (0.3.0)\n",
+ "Requirement already satisfied: rsa<5,>=3.1.4 in /home/vscode/.local/lib/python3.10/site-packages (from google-auth>=1.0.1->kubernetes>=28.1.0->chromadb) (4.9)\n",
+ "Requirement already satisfied: filelock in /home/vscode/.local/lib/python3.10/site-packages (from huggingface_hub<1.0,>=0.16.4->tokenizers>=0.13.2->chromadb) (3.13.1)\n",
+ "Requirement already satisfied: fsspec>=2023.5.0 in /home/vscode/.local/lib/python3.10/site-packages (from huggingface_hub<1.0,>=0.16.4->tokenizers>=0.13.2->chromadb) (2023.12.2)\n",
+ "Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.10/site-packages (from importlib-metadata<7.0,>=6.0->opentelemetry-api>=1.2.0->chromadb) (3.17.0)\n",
+ "Requirement already satisfied: anyio<5,>=3.4.0 in /home/vscode/.local/lib/python3.10/site-packages (from starlette<0.33.0,>=0.29.0->fastapi>=0.95.2->chromadb) (4.2.0)\n",
+ "Requirement already satisfied: humanfriendly>=9.1 in /home/vscode/.local/lib/python3.10/site-packages (from coloredlogs->onnxruntime>=1.14.1->chromadb) (10.0)\n",
+ "Requirement already satisfied: mpmath>=0.19 in /home/vscode/.local/lib/python3.10/site-packages (from sympy->onnxruntime>=1.14.1->chromadb) (1.3.0)\n",
+ "Requirement already satisfied: sniffio>=1.1 in /home/vscode/.local/lib/python3.10/site-packages (from anyio<5,>=3.4.0->starlette<0.33.0,>=0.29.0->fastapi>=0.95.2->chromadb) (1.3.0)\n",
+ "Requirement already satisfied: exceptiongroup>=1.0.2 in /home/vscode/.local/lib/python3.10/site-packages (from anyio<5,>=3.4.0->starlette<0.33.0,>=0.29.0->fastapi>=0.95.2->chromadb) (1.2.0)\n",
+ "Requirement already satisfied: pyasn1<0.6.0,>=0.4.6 in /home/vscode/.local/lib/python3.10/site-packages (from pyasn1-modules>=0.2.1->google-auth>=1.0.1->kubernetes>=28.1.0->chromadb) (0.5.1)\n",
+ "Defaulting to user installation because normal site-packages is not writeable\n",
+ "Requirement already satisfied: sentence_transformers in /home/vscode/.local/lib/python3.10/site-packages (2.2.2)\n",
+ "Requirement already satisfied: transformers<5.0.0,>=4.6.0 in /home/vscode/.local/lib/python3.10/site-packages (from sentence_transformers) (4.36.2)\n",
+ "Requirement already satisfied: tqdm in /home/vscode/.local/lib/python3.10/site-packages (from sentence_transformers) (4.66.1)\n",
+ "Requirement already satisfied: torch>=1.6.0 in /home/vscode/.local/lib/python3.10/site-packages (from sentence_transformers) (2.1.2)\n",
+ "Requirement already satisfied: torchvision in /home/vscode/.local/lib/python3.10/site-packages (from sentence_transformers) (0.16.2)\n",
+ "Requirement already satisfied: numpy in /home/vscode/.local/lib/python3.10/site-packages (from sentence_transformers) (1.26.3)\n",
+ "Requirement already satisfied: scikit-learn in /home/vscode/.local/lib/python3.10/site-packages (from sentence_transformers) (1.3.2)\n",
+ "Requirement already satisfied: scipy in /home/vscode/.local/lib/python3.10/site-packages (from sentence_transformers) (1.11.4)\n",
+ "Requirement already satisfied: nltk in /home/vscode/.local/lib/python3.10/site-packages (from sentence_transformers) (3.8.1)\n",
+ "Requirement already satisfied: sentencepiece in /home/vscode/.local/lib/python3.10/site-packages (from sentence_transformers) (0.1.99)\n",
+ "Requirement already satisfied: huggingface-hub>=0.4.0 in /home/vscode/.local/lib/python3.10/site-packages (from sentence_transformers) (0.20.2)\n",
+ "Requirement already satisfied: filelock in /home/vscode/.local/lib/python3.10/site-packages (from huggingface-hub>=0.4.0->sentence_transformers) (3.13.1)\n",
+ "Requirement already satisfied: fsspec>=2023.5.0 in /home/vscode/.local/lib/python3.10/site-packages (from huggingface-hub>=0.4.0->sentence_transformers) (2023.12.2)\n",
+ "Requirement already satisfied: requests in /usr/local/lib/python3.10/site-packages (from huggingface-hub>=0.4.0->sentence_transformers) (2.31.0)\n",
+ "Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/site-packages (from huggingface-hub>=0.4.0->sentence_transformers) (6.0.1)\n",
+ "Requirement already satisfied: typing-extensions>=3.7.4.3 in /home/vscode/.local/lib/python3.10/site-packages (from huggingface-hub>=0.4.0->sentence_transformers) (4.9.0)\n",
+ "Requirement already satisfied: packaging>=20.9 in /usr/local/lib/python3.10/site-packages (from huggingface-hub>=0.4.0->sentence_transformers) (23.2)\n",
+ "Requirement already satisfied: sympy in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (1.12)\n",
+ "Requirement already satisfied: networkx in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (3.2.1)\n",
+ "Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (3.1.2)\n",
+ "Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (12.1.105)\n",
+ "Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (12.1.105)\n",
+ "Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (12.1.105)\n",
+ "Requirement already satisfied: nvidia-cudnn-cu12==8.9.2.26 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (8.9.2.26)\n",
+ "Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (12.1.3.1)\n",
+ "Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (11.0.2.54)\n",
+ "Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (10.3.2.106)\n",
+ "Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (11.4.5.107)\n",
+ "Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (12.1.0.106)\n",
+ "Requirement already satisfied: nvidia-nccl-cu12==2.18.1 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (2.18.1)\n",
+ "Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (12.1.105)\n",
+ "Requirement already satisfied: triton==2.1.0 in /home/vscode/.local/lib/python3.10/site-packages (from torch>=1.6.0->sentence_transformers) (2.1.0)\n",
+ "Requirement already satisfied: nvidia-nvjitlink-cu12 in /home/vscode/.local/lib/python3.10/site-packages (from nvidia-cusolver-cu12==11.4.5.107->torch>=1.6.0->sentence_transformers) (12.3.101)\n",
+ "Requirement already satisfied: regex!=2019.12.17 in /home/vscode/.local/lib/python3.10/site-packages (from transformers<5.0.0,>=4.6.0->sentence_transformers) (2023.12.25)\n",
+ "Requirement already satisfied: tokenizers<0.19,>=0.14 in /home/vscode/.local/lib/python3.10/site-packages (from transformers<5.0.0,>=4.6.0->sentence_transformers) (0.15.0)\n",
+ "Requirement already satisfied: safetensors>=0.3.1 in /home/vscode/.local/lib/python3.10/site-packages (from transformers<5.0.0,>=4.6.0->sentence_transformers) (0.4.1)\n",
+ "Requirement already satisfied: click in /usr/local/lib/python3.10/site-packages (from nltk->sentence_transformers) (8.1.7)\n",
+ "Requirement already satisfied: joblib in /home/vscode/.local/lib/python3.10/site-packages (from nltk->sentence_transformers) (1.3.2)\n",
+ "Requirement already satisfied: threadpoolctl>=2.0.0 in /home/vscode/.local/lib/python3.10/site-packages (from scikit-learn->sentence_transformers) (3.2.0)\n",
+ "Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /home/vscode/.local/lib/python3.10/site-packages (from torchvision->sentence_transformers) (10.2.0)\n",
+ "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/site-packages (from jinja2->torch>=1.6.0->sentence_transformers) (2.1.3)\n",
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/site-packages (from requests->huggingface-hub>=0.4.0->sentence_transformers) (3.3.2)\n",
+ "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/site-packages (from requests->huggingface-hub>=0.4.0->sentence_transformers) (3.6)\n",
+ "Requirement already satisfied: urllib3<3,>=1.21.1 in /home/vscode/.local/lib/python3.10/site-packages (from requests->huggingface-hub>=0.4.0->sentence_transformers) (1.26.18)\n",
+ "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/site-packages (from requests->huggingface-hub>=0.4.0->sentence_transformers) (2023.11.17)\n",
+ "Requirement already satisfied: mpmath>=0.19 in /home/vscode/.local/lib/python3.10/site-packages (from sympy->torch>=1.6.0->sentence_transformers) (1.3.0)\n",
+ "Defaulting to user installation because normal site-packages is not writeable\n",
+ "Requirement already satisfied: tiktoken in /home/vscode/.local/lib/python3.10/site-packages (0.5.2)\n",
+ "Requirement already satisfied: regex>=2022.1.18 in /home/vscode/.local/lib/python3.10/site-packages (from tiktoken) (2023.12.25)\n",
+ "Requirement already satisfied: requests>=2.26.0 in /usr/local/lib/python3.10/site-packages (from tiktoken) (2.31.0)\n",
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/site-packages (from requests>=2.26.0->tiktoken) (3.3.2)\n",
+ "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/site-packages (from requests>=2.26.0->tiktoken) (3.6)\n",
+ "Requirement already satisfied: urllib3<3,>=1.21.1 in /home/vscode/.local/lib/python3.10/site-packages (from requests>=2.26.0->tiktoken) (1.26.18)\n",
+ "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/site-packages (from requests>=2.26.0->tiktoken) (2023.11.17)\n",
+ "Defaulting to user installation because normal site-packages is not writeable\n",
+ "Requirement already satisfied: pypdf in /home/vscode/.local/lib/python3.10/site-packages (3.17.4)\n"
+ ]
+ }
+ ],
+ "source": [
+ "!pip install \"pyautogen>=0.2.3\"\n",
+ "!pip install chromadb\n",
+ "!pip install sentence_transformers\n",
+ "!pip install tiktoken\n",
+ "!pip install pypdf"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "yU8zQPetsW28"
+ },
+ "outputs": [],
+ "source": [
+ "import asyncio\n",
+ "from typing import Any, Callable, Dict, List, Optional, Tuple, Union\n",
+ "\n",
+ "import nest_asyncio\n",
+ "\n",
+ "from autogen import AssistantAgent\n",
+ "from autogen.agentchat.contrib.retrieve_user_proxy_agent import RetrieveUserProxyAgent\n",
+ "from autogen.agentchat.user_proxy_agent import UserProxyAgent"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "R2ITRFQisgzI"
+ },
+ "outputs": [],
+ "source": [
+ "# Define an asynchronous function that simulates some asynchronous task (e.g., I/O operation)\n",
+ "\n",
+ "\n",
+ "async def my_asynchronous_function():\n",
+ " print(\"Start asynchronous function\")\n",
+ " await asyncio.sleep(2) # Simulate some asynchronous task (e.g., I/O operation)\n",
+ " print(\"End asynchronous function\")\n",
+ " return \"input\"\n",
+ "\n",
+ "\n",
+ "# Define a custom class `CustomisedUserProxyAgent` that extends `UserProxyAgent`\n",
+ "\n",
+ "\n",
+ "class CustomisedUserProxyAgent(UserProxyAgent):\n",
+ " # Asynchronous function to get human input\n",
+ " async def a_get_human_input(self, prompt: str) -> str:\n",
+ " # Call the asynchronous function to get user input asynchronously\n",
+ " user_input = await my_asynchronous_function()\n",
+ "\n",
+ " return user_input\n",
+ "\n",
+ " # Asynchronous function to receive a message\n",
+ "\n",
+ " async def a_receive(\n",
+ " self,\n",
+ " message: Union[Dict, str],\n",
+ " sender,\n",
+ " request_reply: Optional[bool] = None,\n",
+ " silent: Optional[bool] = False,\n",
+ " ):\n",
+ " # Call the superclass method to handle message reception asynchronously\n",
+ " await super().a_receive(message, sender, request_reply, silent)\n",
+ "\n",
+ "\n",
+ "class CustomisedAssistantAgent(AssistantAgent):\n",
+ " # Asynchronous function to get human input\n",
+ " async def a_get_human_input(self, prompt: str) -> str:\n",
+ " # Call the asynchronous function to get user input asynchronously\n",
+ " user_input = await my_asynchronous_function()\n",
+ "\n",
+ " return user_input\n",
+ "\n",
+ " # Asynchronous function to receive a message\n",
+ " async def a_receive(\n",
+ " self,\n",
+ " message: Union[Dict, str],\n",
+ " sender,\n",
+ " request_reply: Optional[bool] = None,\n",
+ " silent: Optional[bool] = False,\n",
+ " ):\n",
+ " # Call the superclass method to handle message reception asynchronously\n",
+ " await super().a_receive(message, sender, request_reply, silent)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "2v_DhqBFuA1O"
+ },
+ "outputs": [],
+ "source": [
+ "def create_llm_config(model, temperature, seed):\n",
+ " config_list = [\n",
+ " {\n",
+ " \"model\": \"<model_name>\",\n",
+ " \"api_key\": \"<api_key>\",\n",
+ " },\n",
+ " ]\n",
+ "\n",
+ " llm_config = {\n",
+ " \"seed\": int(seed),\n",
+ " \"config_list\": config_list,\n",
+ " \"temperature\": float(temperature),\n",
+ " }\n",
+ "\n",
+ " return llm_config"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
},
+ "id": "yHIUO2CiwfIp",
+ "outputId": "79ceccca-f91c-4dde-9e21-1f2129cdc1a8"
+ },
+ "outputs": [
{
- "cell_type": "code",
- "execution_count": 25,
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "ywFsRNMDteaH",
- "outputId": "5716adad-de80-4595-eec9-523b1f1cd313"
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "boss (to assistant):\n",
- "\n",
- "Resume Review, Technical Skills Assessment, Project Discussion, Job Role Expectations, Closing Remarks.\n",
- "\n",
- "--------------------------------------------------------------------------------\n",
- "assistant (to boss):\n",
- "\n",
- "1. Can you walk me through your resume, highlighting the most significant parts?\n",
- "2. What technical skills do you possess that make you a strong candidate for this position?\n",
- "3. Can you discuss a project you've worked on that you believe showcases your abilities?\n",
- "4. What are your expectations for this job role?\n",
- "5. Do you have any questions or concerns before we conclude this interview?\n",
- "\n",
- "Can you please provide your feedback on these questions?\n",
- "\n",
- "--------------------------------------------------------------------------------\n",
- "Provide feedback to assistant. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: exit\n"
- ]
- }
- ],
- "source": [
- "import nest_asyncio\n",
- "nest_asyncio.apply()\n",
- "\n",
- "async def main():\n",
- " boss = CustomisedUserProxyAgent(\n",
- " name=\"boss\",\n",
- " human_input_mode=\"ALWAYS\",\n",
- " max_consecutive_auto_reply=0,\n",
- " code_execution_config=False,\n",
- " )\n",
- "\n",
- " assistant = CustomisedAssistantAgent(\n",
- " name=\"assistant\",\n",
- " system_message=\"You will provide some agenda, and I will create questions for an interview meeting. Every time when you generate question then you have to ask user for feedback and if user provides the feedback then you have to incorporate that feedback and generate new set of questions and if user don't want to update then terminate the process and exit\",\n",
- " llm_config=create_llm_config(\"gpt-4\", \"0.4\", \"23\"),\n",
- " )\n",
- "\n",
- " await boss.a_initiate_chat(\n",
- " assistant,\n",
- " message=\"Resume Review, Technical Skills Assessment, Project Discussion, Job Role Expectations, Closing Remarks.\",\n",
- " n_results=3,\n",
- " )\n",
- "\n",
- "await main()"
- ]
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Requirement already satisfied: nest-asyncio in /usr/local/lib/python3.10/dist-packages (1.5.8)\n"
+ ]
}
- ],
- "metadata": {
+ ],
+ "source": [
+ "!pip install nest-asyncio"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 25,
+ "metadata": {
"colab": {
- "provenance": [],
- "toc_visible": true
- },
- "kernelspec": {
- "display_name": "Python 3",
- "name": "python3"
+ "base_uri": "https://localhost:8080/"
},
- "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.10.13"
+ "id": "ywFsRNMDteaH",
+ "outputId": "5716adad-de80-4595-eec9-523b1f1cd313"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "boss (to assistant):\n",
+ "\n",
+ "Resume Review, Technical Skills Assessment, Project Discussion, Job Role Expectations, Closing Remarks.\n",
+ "\n",
+ "--------------------------------------------------------------------------------\n",
+ "assistant (to boss):\n",
+ "\n",
+ "1. Can you walk me through your resume, highlighting the most significant parts?\n",
+ "2. What technical skills do you possess that make you a strong candidate for this position?\n",
+ "3. Can you discuss a project you've worked on that you believe showcases your abilities?\n",
+ "4. What are your expectations for this job role?\n",
+ "5. Do you have any questions or concerns before we conclude this interview?\n",
+ "\n",
+ "Can you please provide your feedback on these questions?\n",
+ "\n",
+ "--------------------------------------------------------------------------------\n",
+ "Provide feedback to assistant. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: exit\n"
+ ]
}
+ ],
+ "source": [
+ "nest_asyncio.apply()\n",
+ "\n",
+ "\n",
+ "async def main():\n",
+ " boss = CustomisedUserProxyAgent(\n",
+ " name=\"boss\",\n",
+ " human_input_mode=\"ALWAYS\",\n",
+ " max_consecutive_auto_reply=0,\n",
+ " code_execution_config=False,\n",
+ " )\n",
+ "\n",
+ " assistant = CustomisedAssistantAgent(\n",
+ " name=\"assistant\",\n",
+ " system_message=\"You will provide some agenda, and I will create questions for an interview meeting. Every time when you generate question then you have to ask user for feedback and if user provides the feedback then you have to incorporate that feedback and generate new set of questions and if user don't want to update then terminate the process and exit\",\n",
+ " llm_config=create_llm_config(\"gpt-4\", \"0.4\", \"23\"),\n",
+ " )\n",
+ "\n",
+ " await boss.a_initiate_chat(\n",
+ " assistant,\n",
+ " message=\"Resume Review, Technical Skills Assessment, Project Discussion, Job Role Expectations, Closing Remarks.\",\n",
+ " n_results=3,\n",
+ " )\n",
+ "\n",
+ "\n",
+ "await main() # noqa: F704"
+ ]
+ }
+ ],
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "toc_visible": true
+ },
+ "kernelspec": {
+ "display_name": "Python 3",
+ "name": "python3"
},
- "nbformat": 4,
- "nbformat_minor": 0
+ "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.10.13"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
}
diff --git a/notebook/agentchat_MathChat.ipynb b/notebook/agentchat_MathChat.ipynb
index 92a9b39cd..8ea4c1f1c 100644
--- a/notebook/agentchat_MathChat.ipynb
+++ b/notebook/agentchat_MathChat.ipynb
@@ -52,7 +52,10 @@
"metadata": {},
"outputs": [],
"source": [
+ "import os\n",
+ "\n",
"import autogen\n",
+ "from autogen.agentchat.contrib.math_user_proxy_agent import MathUserProxyAgent\n",
"\n",
"config_list = autogen.config_list_from_json(\n",
" \"OAI_CONFIG_LIST\",\n",
@@ -70,7 +73,7 @@
" \"gpt-35-turbo-v0301\",\n",
" \"gpt\",\n",
" }\n",
- " }\n",
+ " },\n",
")"
]
},
@@ -126,23 +129,21 @@
"metadata": {},
"outputs": [],
"source": [
- "from autogen.agentchat.contrib.math_user_proxy_agent import MathUserProxyAgent\n",
- "\n",
"# 1. create an AssistantAgent instance named \"assistant\"\n",
"assistant = autogen.AssistantAgent(\n",
- " name=\"assistant\", \n",
+ " name=\"assistant\",\n",
" system_message=\"You are a helpful assistant.\",\n",
" llm_config={\n",
" \"timeout\": 600,\n",
" \"seed\": 42,\n",
" \"config_list\": config_list,\n",
- " }\n",
+ " },\n",
")\n",
"\n",
"# 2. create the MathUserProxyAgent instance named \"mathproxyagent\"\n",
"# By default, the human_input_mode is \"NEVER\", which means the agent will not ask for human input.\n",
"mathproxyagent = MathUserProxyAgent(\n",
- " name=\"mathproxyagent\", \n",
+ " name=\"mathproxyagent\",\n",
" human_input_mode=\"NEVER\",\n",
" code_execution_config={\"use_docker\": False},\n",
")"
@@ -176,7 +177,9 @@
"# given a math problem, we use the mathproxyagent to generate a prompt to be sent to the assistant as the initial message.\n",
"# the assistant receives the message and generates a response. The response will be sent back to the mathproxyagent for processing.\n",
"# The conversation continues until the termination condition is met, in MathChat, the termination condition is the detect of \"\\boxed{}\" in the response.\n",
- "math_problem = \"Find all $x$ that satisfy the inequality $(2x+10)(x+3)<(3x+9)(x+8)$. Express your answer in interval notation.\"\n",
+ "math_problem = (\n",
+ " \"Find all $x$ that satisfy the inequality $(2x+10)(x+3)<(3x+9)(x+8)$. Express your answer in interval notation.\"\n",
+ ")\n",
"mathproxyagent.initiate_chat(assistant, problem=math_problem)"
]
},
@@ -291,7 +294,6 @@
"outputs": [],
"source": [
"# The wolfram alpha app id is required for this example (the assistant may choose to query Wolfram Alpha).\n",
- "import os\n",
"if \"WOLFRAM_ALPHA_APPID\" not in os.environ:\n",
" os.environ[\"WOLFRAM_ALPHA_APPID\"] = open(\"wolfram.txt\").read().strip()\n",
"\n",
diff --git a/notebook/agentchat_RetrieveChat.ipynb b/notebook/agentchat_RetrieveChat.ipynb
index b98872edc..d229f53da 100644
--- a/notebook/agentchat_RetrieveChat.ipynb
+++ b/notebook/agentchat_RetrieveChat.ipynb
@@ -79,7 +79,18 @@
}
],
"source": [
+ "import json\n",
+ "import os\n",
+ "\n",
+ "import chromadb\n",
+ "\n",
"import autogen\n",
+ "from autogen.agentchat.contrib.retrieve_assistant_agent import RetrieveAssistantAgent\n",
+ "from autogen.agentchat.contrib.retrieve_user_proxy_agent import RetrieveUserProxyAgent\n",
+ "\n",
+ "# Accepted file formats for that can be stored in\n",
+ "# a vector database instance\n",
+ "from autogen.retrieve_utils import TEXT_FORMATS\n",
"\n",
"config_list = autogen.config_list_from_json(\n",
" env_or_file=\"OAI_CONFIG_LIST\",\n",
@@ -161,10 +172,6 @@
}
],
"source": [
- "# Accepted file formats for that can be stored in \n",
- "# a vector database instance\n",
- "from autogen.retrieve_utils import TEXT_FORMATS\n",
- "\n",
"print(\"Accepted file formats for `docs_path`:\")\n",
"print(TEXT_FORMATS)"
]
@@ -175,11 +182,6 @@
"metadata": {},
"outputs": [],
"source": [
- "from autogen.agentchat.contrib.retrieve_assistant_agent import RetrieveAssistantAgent\n",
- "from autogen.agentchat.contrib.retrieve_user_proxy_agent import RetrieveUserProxyAgent\n",
- "import chromadb\n",
- "import os\n",
- "\n",
"# 1. create an RetrieveAssistantAgent instance named \"assistant\"\n",
"assistant = RetrieveAssistantAgent(\n",
" name=\"assistant\",\n",
@@ -210,7 +212,7 @@
" \"docs_path\": [\n",
" \"https://raw.githubusercontent.com/microsoft/FLAML/main/website/docs/Examples/Integrate%20-%20Spark.md\",\n",
" \"https://raw.githubusercontent.com/microsoft/FLAML/main/website/docs/Research.md\",\n",
- " os.path.join(os.path.abspath(''), \"..\", \"website\", \"docs\"),\n",
+ " os.path.join(os.path.abspath(\"\"), \"..\", \"website\", \"docs\"),\n",
" ],\n",
" \"custom_text_types\": [\"mdx\"],\n",
" \"chunk_token_size\": 2000,\n",
@@ -219,7 +221,7 @@
" \"embedding_model\": \"all-mpnet-base-v2\",\n",
" \"get_or_create\": True, # set to False if you don't want to reuse an existing collection, but you'll need to remove the collection manually\n",
" },\n",
- " code_execution_config=False, # set to False if you don't want to execute the code\n",
+ " code_execution_config=False, # set to False if you don't want to execute the code\n",
")"
]
},
@@ -525,7 +527,9 @@
"# The conversation continues until the termination condition is met, in RetrieveChat, the termination condition when no human-in-loop is no code block detected.\n",
"# With human-in-loop, the conversation will continue until the user says \"exit\".\n",
"code_problem = \"How can I use FLAML to perform a classification task and use spark to do parallel training. Train 30 seconds and force cancel jobs if time limit is reached.\"\n",
- "ragproxyagent.initiate_chat(assistant, problem=code_problem, search_string=\"spark\") # search_string is used as an extra filter for the embeddings search, in this case, we only want to search documents that contain \"spark\"."
+ "ragproxyagent.initiate_chat(\n",
+ " assistant, problem=code_problem, search_string=\"spark\"\n",
+ ") # search_string is used as an extra filter for the embeddings search, in this case, we only want to search documents that contain \"spark\"."
]
},
{
@@ -2122,8 +2126,6 @@
}
],
"source": [
- "import json\n",
- "\n",
"# queries_file = \"https://huggingface.co/datasets/thinkall/NaturalQuestionsQA/resolve/main/queries.jsonl\"\n",
"queries = \"\"\"{\"_id\": \"ce2342e1feb4e119cb273c05356b33309d38fa132a1cbeac2368a337e38419b8\", \"text\": \"what is non controlling interest on balance sheet\", \"metadata\": {\"answer\": [\"the portion of a subsidiary corporation 's stock that is not owned by the parent corporation\"]}}\n",
"{\"_id\": \"3a10ff0e520530c0aa33b2c7e8d989d78a8cd5d699201fc4b13d3845010994ee\", \"text\": \"how many episodes are in chicago fire season 4\", \"metadata\": {\"answer\": [\"23\"]}}\n",
@@ -2645,7 +2647,7 @@
"\n",
" # reset the assistant. Always reset the assistant before starting a new conversation.\n",
" assistant.reset()\n",
- " \n",
+ "\n",
" qa_problem = questions[i]\n",
" ragproxyagent.initiate_chat(assistant, problem=qa_problem, n_results=30)"
]
@@ -2724,7 +2726,7 @@
"metadata": {},
"outputs": [],
"source": [
- "#create the RetrieveUserProxyAgent instance named \"ragproxyagent\"\n",
+ "# create the RetrieveUserProxyAgent instance named \"ragproxyagent\"\n",
"corpus_file = \"https://huggingface.co/datasets/thinkall/2WikiMultihopQA/resolve/main/corpus.txt\"\n",
"\n",
"# Create a new collection for NaturalQuestions dataset\n",
@@ -2762,8 +2764,6 @@
}
],
"source": [
- "import json\n",
- "\n",
"# queries_file = \"https://huggingface.co/datasets/thinkall/2WikiMultihopQA/resolve/main/queries.jsonl\"\n",
"queries = \"\"\"{\"_id\": \"61a46987092f11ebbdaeac1f6bf848b6\", \"text\": \"Which film came out first, Blind Shaft or The Mask Of Fu Manchu?\", \"metadata\": {\"answer\": [\"The Mask Of Fu Manchu\"]}}\n",
"{\"_id\": \"a7b9672009c311ebbdb0ac1f6bf848b6\", \"text\": \"Are North Marion High School (Oregon) and Seoul High School both located in the same country?\", \"metadata\": {\"answer\": [\"no\"]}}\n",
@@ -3075,7 +3075,7 @@
"\n",
" # reset the assistant. Always reset the assistant before starting a new conversation.\n",
" assistant.reset()\n",
- " \n",
+ "\n",
" qa_problem = questions[i]\n",
" ragproxyagent.initiate_chat(assistant, problem=qa_problem, n_results=10)"
]
diff --git a/notebook/agentchat_agentoptimizer.ipynb b/notebook/agentchat_agentoptimizer.ipynb
index 110679192..78ea14fe9 100644
--- a/notebook/agentchat_agentoptimizer.ipynb
+++ b/notebook/agentchat_agentoptimizer.ipynb
@@ -32,16 +32,15 @@
"source": [
"import json\n",
"import os\n",
- "from typing import Any, Callable, Dict, List, Optional, Union, Tuple\n",
+ "from typing import Any, Callable, Dict, List, Optional, Tuple, Union\n",
+ "\n",
+ "from openai import AzureOpenAI, BadRequestError\n",
+ "\n",
"import autogen\n",
"from autogen.agentchat import Agent\n",
"from autogen.agentchat.contrib.math_user_proxy_agent import MathUserProxyAgent\n",
- "from autogen.code_utils import extract_code, execute_code\n",
- "from autogen.math_utils import get_answer\n",
- "from openai import AzureOpenAI\n",
- "from openai import (\n",
- " BadRequestError,\n",
- ")"
+ "from autogen.code_utils import execute_code, extract_code\n",
+ "from autogen.math_utils import get_answer"
]
},
{
@@ -63,10 +62,10 @@
"metadata": {},
"outputs": [],
"source": [
- "class AgentOptimizer():\n",
- " OPT_PROMPT = \"\"\"You are a function optimizer. Your task is to maintain a list of functions for the assistant according to the existing function list and conversation history that happens between the assistant and the user. \n",
- "You can perform one of the following four actions to manipulate the function list using the functions you have: \n",
- "1. Revise one existing function (using revise_function). \n",
+ "class AgentOptimizer:\n",
+ " OPT_PROMPT = \"\"\"You are a function optimizer. Your task is to maintain a list of functions for the assistant according to the existing function list and conversation history that happens between the assistant and the user.\n",
+ "You can perform one of the following four actions to manipulate the function list using the functions you have:\n",
+ "1. Revise one existing function (using revise_function).\n",
"2. Remove one existing function (using remove_function).\n",
"3. Add one new function (using add_function).\n",
"4. Directly return \"TERMINATE\" to me if no more actions are needed for the current function list.\n",
@@ -78,33 +77,33 @@
"3. Do not revise the function that you think works well and plays a critical role in solving the problems according to the conversation history. Only making revisions if needed.\n",
"4. Sometimes, a NameError may occur. To fix this error, you can either revise the name of the function in the code implementation or revise the name of the function call to make these two names consistent.\n",
"(2) Remove one existing function:\n",
- "1. Only remove the function that you think is not needed anymore in future tasks. \n",
+ "1. Only remove the function that you think is not needed anymore in future tasks.\n",
"(3) Add one new function:\n",
- "1. The added new function should solve a higher-level question that encompasses the original query and extend the code's functionality to make it more versatile and widely applicable. \n",
+ "1. The added new function should solve a higher-level question that encompasses the original query and extend the code's functionality to make it more versatile and widely applicable.\n",
"2. The added new function should solve queries of the same type, based on common reasoning steps without mentioning specific object names or entity terms.\n",
"3. Name the function and write the description concerning both the core reasoning pattern and data organization format, without referencing specific objects. The name of the function MUST be the same with the function name in the code you generated.\n",
- "4. Replace specific strings or variable names with general variables to enhance the tool's applicability to various queries. All names used inside the function should be passed in as arguments. \n",
+ "4. Replace specific strings or variable names with general variables to enhance the tool's applicability to various queries. All names used inside the function should be passed in as arguments.\n",
"(4) Directly return \"TERMINATE\":\n",
"If you think there is no need to perform any other actions for the current function list since the current list is optimal more actions will harm the performance in future tasks. Please directly reply to me with \"TERMINATE\".\n",
"\n",
"One function signature includes the following five elements:\n",
- "1. Function name \n",
- "2. Function description \n",
+ "1. Function name\n",
+ "2. Function description\n",
"3. JSON schema of arguments encoded as a string\n",
- "4. A list of package names imported by the function packages \n",
+ "4. A list of package names imported by the function packages\n",
"5. The code implementation\n",
"\n",
"Below are the signatures of the current functions:\n",
- "List A: {signiture}. \n",
+ "List A: {signiture}.\n",
"The success rate (performance) with this function list is {success_rate}.\n",
"The following list are the function signatures that you have after taking {actions_num} actions in our previous conversations:\n",
- "List B: {after_signiture}. \n",
+ "List B: {after_signiture}.\n",
"Here are {conversation_num} conversation histories of solving {conversation_num} tasks.\n",
"History:\n",
"{history}\n",
- "The following table shows the statistical information for solving each task in each conversation and indicates whether each task was successfully solved. \n",
+ "The following table shows the statistical information for solving each task in each conversation and indicates whether each task was successfully solved.\n",
"1 represents correct. 0 represents wrong.\n",
- "statistic: \n",
+ "statistic:\n",
"{statistic}\n",
"\n",
"According to the information I provide, please take one of four actions to manipulate list B using the functions you know.\n",
@@ -118,30 +117,24 @@
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
- " \"name\": {\n",
- " \"type\": \"string\",\n",
- " \"description\": \"The name of the function in the code implementation.\"\n",
- " },\n",
- " \"description\": {\n",
- " \"type\": \"string\",\n",
- " \"description\": \"A short description of the function.\"\n",
- " },\n",
+ " \"name\": {\"type\": \"string\", \"description\": \"The name of the function in the code implementation.\"},\n",
+ " \"description\": {\"type\": \"string\", \"description\": \"A short description of the function.\"},\n",
" \"arguments\": {\n",
" \"type\": \"string\",\n",
- " \"description\": \"JSON schema of arguments encoded as a string. Please note that the JSON schema only supports specific types including string, integer, object, array, boolean. (do not have float type) For example: { \\\"url\\\": { \\\"type\\\": \\\"string\\\", \\\"description\\\": \\\"The URL\\\", }}. Please avoid the error 'array schema missing items' when using array type.\"\n",
+ " \"description\": 'JSON schema of arguments encoded as a string. Please note that the JSON schema only supports specific types including string, integer, object, array, boolean. (do not have float type) For example: { \"url\": { \"type\": \"string\", \"description\": \"The URL\", }}. Please avoid the error \\'array schema missing items\\' when using array type.',\n",
" },\n",
" \"packages\": {\n",
" \"type\": \"string\",\n",
- " \"description\": \"A list of package names imported by the function, and that need to be installed with pip prior to invoking the function. This solves ModuleNotFoundError. It should be string, not list.\"\n",
+ " \"description\": \"A list of package names imported by the function, and that need to be installed with pip prior to invoking the function. This solves ModuleNotFoundError. It should be string, not list.\",\n",
" },\n",
" \"code\": {\n",
" \"type\": \"string\",\n",
- " \"description\": \"The implementation in Python. Do not include the function declaration.\"\n",
- " }\n",
+ " \"description\": \"The implementation in Python. Do not include the function declaration.\",\n",
+ " },\n",
" },\n",
- " \"required\": [\"name\", \"description\", \"arguments\", \"packages\", \"code\"]\n",
- " }\n",
- " }\n",
+ " \"required\": [\"name\", \"description\", \"arguments\", \"packages\", \"code\"],\n",
+ " },\n",
+ " },\n",
" }\n",
"\n",
" REVISE_FUNC = {\n",
@@ -152,30 +145,24 @@
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
- " \"name\": {\n",
- " \"type\": \"string\",\n",
- " \"description\": \"The name of the function in the code implementation.\"\n",
- " },\n",
- " \"description\": {\n",
- " \"type\": \"string\",\n",
- " \"description\": \"A short description of the function.\"\n",
- " },\n",
+ " \"name\": {\"type\": \"string\", \"description\": \"The name of the function in the code implementation.\"},\n",
+ " \"description\": {\"type\": \"string\", \"description\": \"A short description of the function.\"},\n",
" \"arguments\": {\n",
" \"type\": \"string\",\n",
- " \"description\": \"JSON schema of arguments encoded as a string. Please note that the JSON schema only supports specific types including string, integer, object, array, boolean. (do not have float type) For example: { \\\"url\\\": { \\\"type\\\": \\\"string\\\", \\\"description\\\": \\\"The URL\\\", }}. Please avoid the error 'array schema missing items' when using array type.\"\n",
+ " \"description\": 'JSON schema of arguments encoded as a string. Please note that the JSON schema only supports specific types including string, integer, object, array, boolean. (do not have float type) For example: { \"url\": { \"type\": \"string\", \"description\": \"The URL\", }}. Please avoid the error \\'array schema missing items\\' when using array type.',\n",
" },\n",
" \"packages\": {\n",
" \"type\": \"string\",\n",
- " \"description\": \"A list of package names imported by the function, and that need to be installed with pip prior to invoking the function. This solves ModuleNotFoundError. It should be string, not list.\"\n",
+ " \"description\": \"A list of package names imported by the function, and that need to be installed with pip prior to invoking the function. This solves ModuleNotFoundError. It should be string, not list.\",\n",
" },\n",
" \"code\": {\n",
" \"type\": \"string\",\n",
- " \"description\": \"The implementation in Python. Do not include the function declaration.\"\n",
- " }\n",
+ " \"description\": \"The implementation in Python. Do not include the function declaration.\",\n",
+ " },\n",
" },\n",
- " \"required\": [\"name\", \"description\", \"arguments\", \"packages\", \"code\"]\n",
- " }\n",
- " }\n",
+ " \"required\": [\"name\", \"description\", \"arguments\", \"packages\", \"code\"],\n",
+ " },\n",
+ " },\n",
" }\n",
"\n",
" REMOVE_FUNC = {\n",
@@ -186,14 +173,11 @@
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
- " \"name\": {\n",
- " \"type\": \"string\",\n",
- " \"description\": \"The name of the function in the code implementation.\"\n",
- " }\n",
+ " \"name\": {\"type\": \"string\", \"description\": \"The name of the function in the code implementation.\"}\n",
" },\n",
- " \"required\": [\"name\"]\n",
- " }\n",
- " }\n",
+ " \"required\": [\"name\"],\n",
+ " },\n",
+ " },\n",
" }\n",
"\n",
" def __init__(self, OAI_config, action_num=3, each_action_max_trials=10):\n",
@@ -214,7 +198,7 @@
" function_args = action.function.arguments\n",
" try:\n",
" function_args = json.loads(function_args.strip('\"'))\n",
- " if 'arguments' in function_args.keys():\n",
+ " if \"arguments\" in function_args.keys():\n",
" json.loads(function_args.get(\"arguments\").strip('\"'))\n",
" except Exception as e:\n",
" print(\"JSON is invalid:\", e)\n",
@@ -243,7 +227,7 @@
" function_args = json.loads(action.function.arguments.strip('\"'))\n",
" code = function_args.get(\"code\")\n",
" try:\n",
- " compile(code, '<string>', 'exec')\n",
+ " compile(code, \"<string>\", \"exec\")\n",
" print(\"successfully compiled\")\n",
" except SyntaxError as e:\n",
" print(\"Syntax is invalid:\", e)\n",
@@ -290,18 +274,18 @@
" for action in action_json:\n",
" action_name = action.get(\"action_name\")\n",
" if action_name != \"remove_function\":\n",
- " cur_functions = [\n",
- " item for item in cur_functions if item[\"name\"] != action.get(\"name\")]\n",
+ " cur_functions = [item for item in cur_functions if item[\"name\"] != action.get(\"name\")]\n",
" cur_functions.append(\n",
- " {\"name\": action.get(\"name\"),\n",
- " \"description\": action.get(\"description\"),\n",
- " \"arguments\": action.get(\"arguments\"),\n",
- " \"packages\": action.get(\"packages\"),\n",
- " \"code\": action.get(\"code\")}\n",
+ " {\n",
+ " \"name\": action.get(\"name\"),\n",
+ " \"description\": action.get(\"description\"),\n",
+ " \"arguments\": action.get(\"arguments\"),\n",
+ " \"packages\": action.get(\"packages\"),\n",
+ " \"code\": action.get(\"code\"),\n",
+ " }\n",
" )\n",
" else:\n",
- " cur_functions = [\n",
- " item for item in cur_functions if item[\"name\"] != action.get(\"name\")]\n",
+ " cur_functions = [item for item in cur_functions if item[\"name\"] != action.get(\"name\")]\n",
" return cur_functions\n",
"\n",
" def update_function_call(self, action, mathproxyagent, assistant):\n",
@@ -315,7 +299,7 @@
"import subprocess\n",
"{pip_install}\n",
"print(\"Result of {name} function execution:\")\n",
- "{code} \n",
+ "{code}\n",
"args={args}\n",
"result={name}(**args)\n",
"if result is not None: print(result)\n",
@@ -327,8 +311,14 @@
" print(f\"Result: {result[1]}\")\n",
" return result[1]\n",
"\n",
- " name, description, arguments, packages, code, action_name = action.get(\"name\"), action.get(\n",
- " \"description\"), action.get(\"arguments\"), action.get(\"packages\"), action.get(\"code\"), action.get(\"action_name\")\n",
+ " name, description, arguments, packages, code, action_name = (\n",
+ " action.get(\"name\"),\n",
+ " action.get(\"description\"),\n",
+ " action.get(\"arguments\"),\n",
+ " action.get(\"packages\"),\n",
+ " action.get(\"code\"),\n",
+ " action.get(\"action_name\"),\n",
+ " )\n",
"\n",
" if name in mathproxyagent._function_map.keys():\n",
" del mathproxyagent._function_map[name]\n",
@@ -339,7 +329,8 @@
" \"parameters\": {\"type\": \"object\", \"properties\": arguments},\n",
" }\n",
" mathproxyagent.register_function(\n",
- " function_map={name: lambda **args: execute_func(name, packages, code, **args)})\n",
+ " function_map={name: lambda **args: execute_func(name, packages, code, **args)}\n",
+ " )\n",
" assistant.update_function_signature(function_config, is_remove=False)\n",
" else:\n",
" assistant.update_function_signature(name, is_remove=True)\n",
@@ -369,13 +360,17 @@
" tool_choice=\"auto\",\n",
" )\n",
" actions = response.choices[0].message.tool_calls\n",
- " if self._val_json(actions) and self._val_syntax(actions) and self._val_remove(actions, modified_signiture):\n",
+ " if (\n",
+ " self._val_json(actions)\n",
+ " and self._val_syntax(actions)\n",
+ " and self._val_remove(actions, modified_signiture)\n",
+ " ):\n",
" break\n",
" if actions is not None:\n",
" action_result = self._format_actions(actions)\n",
" action_return = action_return + action_result\n",
" modified_signiture = self._modify_function_signiture(modified_signiture, action_result)\n",
- " return action_return, modified_signiture\n"
+ " return action_return, modified_signiture"
]
},
{
@@ -409,11 +404,7 @@
" if message.rstrip().find(\"TERMINATE\") >= 0:\n",
" return True\n",
"\n",
- " return (\n",
- " not contain_code\n",
- " and get_answer(message) is not None\n",
- " and get_answer(message) != \"\"\n",
- " )\n",
+ " return not contain_code and get_answer(message) is not None and get_answer(message) != \"\"\n",
"\n",
"\n",
"class MathUserProxyAgent(MathUserProxyAgent):\n",
@@ -433,7 +424,7 @@
"1. Solve the problem step by step (do not over-divide the steps).\n",
"2. Take out any queries that can be asked through Python code (for example, any calculations or equations that can be calculated) and functions you know in the context of this conversation.\n",
"\n",
- "Please \n",
+ "Please\n",
"(1) do not mix suggested Python codes and function calls in one step.\n",
"(2) You MUST remember that you don’t have a function named \"python\" available.\n",
"\n",
@@ -447,19 +438,17 @@
"\n",
"After all the queries are run and you get the answer, put the answer in \\\\boxed{}.\n",
"\n",
- "Problem: \n",
+ "Problem:\n",
"\"\"\"\n",
"\n",
" def __init__(\n",
- " self,\n",
- " name: Optional[str] = \"MathChatAgent\",\n",
- " is_termination_msg: Optional[\n",
- " Callable[[Dict], bool]\n",
- " ] = is_termination_msg_mathchat,\n",
- " human_input_mode: Optional[str] = \"NEVER\",\n",
- " default_auto_reply: Optional[Union[str, Dict, None]] = DEFAULT_REPLY,\n",
- " max_invalid_q_per_step=3,\n",
- " **kwargs,\n",
+ " self,\n",
+ " name: Optional[str] = \"MathChatAgent\",\n",
+ " is_termination_msg: Optional[Callable[[Dict], bool]] = is_termination_msg_mathchat,\n",
+ " human_input_mode: Optional[str] = \"NEVER\",\n",
+ " default_auto_reply: Optional[Union[str, Dict, None]] = DEFAULT_REPLY,\n",
+ " max_invalid_q_per_step=3,\n",
+ " **kwargs,\n",
" ):\n",
" super().__init__(\n",
" name=name,\n",
@@ -472,28 +461,29 @@
" del self._reply_func_list[2]\n",
" self.register_reply([Agent, None], MathUserProxyAgent._generate_math_reply, position=4)\n",
" del self._reply_func_list[3]\n",
- " self.register_reply(trigger=autogen.ConversableAgent,\n",
- " reply_func=MathUserProxyAgent.generate_function_call_reply, position=3)\n",
- " self.register_reply(trigger=autogen.ConversableAgent,\n",
- " reply_func=MathUserProxyAgent._check_final_result, position=0)\n",
+ " self.register_reply(\n",
+ " trigger=autogen.ConversableAgent, reply_func=MathUserProxyAgent.generate_function_call_reply, position=3\n",
+ " )\n",
+ " self.register_reply(\n",
+ " trigger=autogen.ConversableAgent, reply_func=MathUserProxyAgent._check_final_result, position=0\n",
+ " )\n",
"\n",
" self.max_function_call_trial = 3\n",
" self.query = None\n",
" self.answer = None\n",
"\n",
" def generate_function_call_reply(\n",
- " self,\n",
- " messages: Optional[List[Dict]] = None,\n",
- " sender: Optional[autogen.ConversableAgent] = None,\n",
- " config: Optional[Any] = None,\n",
+ " self,\n",
+ " messages: Optional[List[Dict]] = None,\n",
+ " sender: Optional[autogen.ConversableAgent] = None,\n",
+ " config: Optional[Any] = None,\n",
" ) -> Tuple[bool, Union[Dict, None]]:\n",
" \"\"\"Generate a reply using function call.\"\"\"\n",
" if messages is None:\n",
" messages = self._oai_messages[sender]\n",
" message = messages[-1]\n",
" if \"function_call\" in message:\n",
- " is_exec_success, func_return = self.execute_function(\n",
- " message[\"function_call\"])\n",
+ " is_exec_success, func_return = self.execute_function(message[\"function_call\"])\n",
" if is_exec_success:\n",
" self.max_function_call_trial = 3\n",
" return True, func_return\n",
@@ -502,7 +492,12 @@
" error_message = func_return[\"content\"]\n",
" self.logs[\"is_correct\"] = 0\n",
" self.max_function_call_trial = 3\n",
- " return True, \"The func is executed failed many times. \" + error_message + \". Please directly reply me with TERMINATE. We need to terminate the conversation.\"\n",
+ " return (\n",
+ " True,\n",
+ " \"The func is executed failed many times. \"\n",
+ " + error_message\n",
+ " + \". Please directly reply me with TERMINATE. We need to terminate the conversation.\",\n",
+ " )\n",
" else:\n",
" revise_prompt = \"You may make a wrong function call (It may due the arguments you provided doesn't fit the function arguments like missing required positional argument). \\\n",
" If you think this error occurs due to you make a wrong function arguments input and you could make it success, please try to call this function again using the correct arguments. \\\n",
@@ -512,17 +507,17 @@
" return False, None\n",
"\n",
" def initiate_chat(\n",
- " self,\n",
- " recipient,\n",
- " query: None,\n",
- " answer: None,\n",
- " silent: Optional[bool] = False,\n",
- " **context,\n",
+ " self,\n",
+ " recipient,\n",
+ " query: None,\n",
+ " answer: None,\n",
+ " silent: Optional[bool] = False,\n",
+ " **context,\n",
" ):\n",
" self.query = query\n",
" if not isinstance(answer, str):\n",
" answer = str(answer)\n",
- " if answer.endswith('.0'):\n",
+ " if answer.endswith(\".0\"):\n",
" answer = answer[:-2]\n",
" self._answer = answer\n",
" else:\n",
@@ -552,10 +547,11 @@
" return self.logs, chat_history\n",
"\n",
" def _check_final_result(\n",
- " self,\n",
- " messages: Optional[List[Dict]] = None,\n",
- " sender: Optional[autogen.Agent] = None,\n",
- " config: Optional[Any] = None):\n",
+ " self,\n",
+ " messages: Optional[List[Dict]] = None,\n",
+ " sender: Optional[autogen.Agent] = None,\n",
+ " config: Optional[Any] = None,\n",
+ " ):\n",
" messages = messages[-1]\n",
"\n",
" if isinstance(messages, dict):\n",
@@ -569,11 +565,7 @@
" if c[0] == \"python\" or c[0] == \"wolfram\":\n",
" contain_code = True\n",
" break\n",
- " if (\n",
- " not contain_code\n",
- " and get_answer(messages) is not None\n",
- " and get_answer(messages) != \"\"\n",
- " ):\n",
+ " if not contain_code and get_answer(messages) is not None and get_answer(messages) != \"\":\n",
" if get_answer(messages) == self._answer:\n",
" self.logs[\"is_correct\"] = 1\n",
" return True, \"The result is Correct. Please reply me with TERMINATE.\"\n",
@@ -643,8 +635,7 @@
"mathproxyagent = MathUserProxyAgent(\n",
" name=\"mathproxyagent\",\n",
" human_input_mode=\"NEVER\",\n",
- " code_execution_config={\n",
- " \"work_dir\": \"_output\", \"use_docker\": False},\n",
+ " code_execution_config={\"work_dir\": \"_output\", \"use_docker\": False},\n",
" is_termination_msg=is_termination_msg_mathchat,\n",
")\n",
"assistant = autogen.AssistantAgent(\n",
@@ -654,7 +645,7 @@
" \"timeout\": 600,\n",
" \"seed\": 42,\n",
" \"config_list\": config_list,\n",
- " }\n",
+ " },\n",
")"
]
},
@@ -678,7 +669,8 @@
"history_list, statistic_list = {}, {}\n",
"for index, query in enumerate(test_data):\n",
" single_statistic, chat_history = mathproxyagent.initiate_chat(\n",
- " recipient=assistant, answer=query[\"answer\"], query=[\"question\"], problem=query[\"question\"])\n",
+ " recipient=assistant, answer=query[\"answer\"], query=[\"question\"], problem=query[\"question\"]\n",
+ " )\n",
" history_list[\"conversation: {index}\".format(index=index + 1)] = chat_history\n",
" statistic_list[\"conversation: {index}\".format(index=index + 1)] = single_statistic\n",
"\n",
@@ -734,19 +726,18 @@
" history_list, statistic_list = {}, {}\n",
" for index, query in enumerate(train_data):\n",
" single_statistic, chat_history = mathproxyagent.initiate_chat(\n",
- " recipient=assistant, answer=query[\"answer\"], query=[\"question\"], problem=query[\"question\"])\n",
- " history_list[\"conversation: {index}\".format(\n",
- " index=index + 1)] = chat_history\n",
- " statistic_list[\"conversation: {index}\".format(\n",
- " index=index + 1)] = single_statistic\n",
- " \n",
+ " recipient=assistant, answer=query[\"answer\"], query=[\"question\"], problem=query[\"question\"]\n",
+ " )\n",
+ " history_list[\"conversation: {index}\".format(index=index + 1)] = chat_history\n",
+ " statistic_list[\"conversation: {index}\".format(index=index + 1)] = single_statistic\n",
+ "\n",
" sum = 0\n",
" for key, value in statistic_list.items():\n",
" if \"is_correct\" not in value.keys():\n",
" statistic_list[key][\"is_correct\"] = 0\n",
" for key, value in statistic_list.items():\n",
" sum += value[\"is_correct\"]\n",
- " print(\"Train_Epoch_{epoch_num}_Success_Rate: {average}%\".format(epoch_num=epoch, average=sum/len(statistic_list)))"
+ " print(\"Train_Epoch_{epoch_num}_Success_Rate: {average}%\".format(epoch_num=epoch, average=sum / len(statistic_list)))"
]
},
{
@@ -770,7 +761,8 @@
"history_list, statistic_list = {}, {}\n",
"for index, query in enumerate(test_data):\n",
" single_statistic, chat_history = mathproxyagent.initiate_chat(\n",
- " recipient=assistant, answer=query[\"answer\"], query=[\"question\"], problem=query[\"question\"])\n",
+ " recipient=assistant, answer=query[\"answer\"], query=[\"question\"], problem=query[\"question\"]\n",
+ " )\n",
" history_list[\"conversation: {index}\".format(index=index + 1)] = chat_history\n",
" statistic_list[\"conversation: {index}\".format(index=index + 1)] = single_statistic\n",
"\n",
@@ -781,7 +773,7 @@
"for key, value in statistic_list.items():\n",
" sum += value[\"is_correct\"]\n",
"\n",
- "success_rate_with_agent_training = sum / len(statistic_list.keys())\n"
+ "success_rate_with_agent_training = sum / len(statistic_list.keys())"
]
},
{
@@ -807,12 +799,14 @@
}
],
"source": [
- "print(\"------------------------------------------------Functions learned------------------------------------------------\")\n",
+ "print(\n",
+ " \"------------------------------------------------Functions learned------------------------------------------------\"\n",
+ ")\n",
"for function in function_json:\n",
" print(function)\n",
"print(\"------------------------------------------------Summary------------------------------------------------\\n\")\n",
- "print(\"success_rate_without_agent_training: {average}%\\n\".format(average = success_rate_without_agent_training*100))\n",
- "print(\"success_rate_with_agent_training: {average}%\\n\".format(average = success_rate_with_agent_training*100))"
+ "print(\"success_rate_without_agent_training: {average}%\\n\".format(average=success_rate_without_agent_training * 100))\n",
+ "print(\"success_rate_with_agent_training: {average}%\\n\".format(average=success_rate_with_agent_training * 100))"
]
}
],
diff --git a/notebook/agentchat_auto_feedback_from_code_execution.ipynb b/notebook/agentchat_auto_feedback_from_code_execution.ipynb
index 49bd66237..25207e54d 100644
--- a/notebook/agentchat_auto_feedback_from_code_execution.ipynb
+++ b/notebook/agentchat_auto_feedback_from_code_execution.ipynb
@@ -64,6 +64,11 @@
"metadata": {},
"outputs": [],
"source": [
+ "from typing import Dict, Union\n",
+ "\n",
+ "from IPython import get_ipython\n",
+ "from IPython.display import Image\n",
+ "\n",
"import autogen\n",
"\n",
"config_list = autogen.config_list_from_json(\n",
@@ -509,9 +514,7 @@
}
],
"source": [
- "from IPython.display import Image\n",
- "\n",
- "Image(filename='coding/stock_price_ytd.png')"
+ "Image(filename=\"coding/stock_price_ytd.png\")"
]
},
{
@@ -532,17 +535,17 @@
"metadata": {},
"outputs": [],
"source": [
- "from typing import Dict, Union\n",
- "from IPython import get_ipython\n",
- "\n",
"class IPythonUserProxyAgent(autogen.UserProxyAgent):\n",
" def __init__(self, name: str, **kwargs):\n",
" super().__init__(name, **kwargs)\n",
" self._ipython = get_ipython()\n",
"\n",
" def generate_init_message(self, *args, **kwargs) -> Union[str, Dict]:\n",
- " return super().generate_init_message(*args, **kwargs) + \"\"\"\n",
+ " return (\n",
+ " super().generate_init_message(*args, **kwargs)\n",
+ " + \"\"\"\n",
"If you suggest code, the code will be executed in IPython.\"\"\"\n",
+ " )\n",
"\n",
" def run_code(self, code, **kwargs):\n",
" result = self._ipython.run_cell(\"%%capture --no-display cap\\n\" + code)\n",
@@ -780,7 +783,8 @@
" \"ipython_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\") or x.get(\"content\", \"\").rstrip().endswith('\"TERMINATE\".'),\n",
+ " is_termination_msg=lambda x: x.get(\"content\", \"\").rstrip().endswith(\"TERMINATE\")\n",
+ " or x.get(\"content\", \"\").rstrip().endswith('\"TERMINATE\".'),\n",
")\n",
"# the assistant receives a message from the user, which contains the task description\n",
"ipy_user.initiate_chat(\n",
diff --git a/notebook/agentchat_chess.ipynb b/notebook/agentchat_chess.ipynb
index 9c3ca5c4e..acc1b978f 100644
--- a/notebook/agentchat_chess.ipynb
+++ b/notebook/agentchat_chess.ipynb
@@ -36,17 +36,15 @@
"source": [
"%%capture --no-stderr\n",
"# %pip install \"pyautogen>=0.2.3\"\n",
- "%pip install chess -U"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [],
- "source": [
+ "from collections import defaultdict\n",
+ "from typing import Any, Dict, List, Optional, Union\n",
+ "\n",
"import chess\n",
- "import chess.svg"
+ "import chess.svg\n",
+ "\n",
+ "import autogen\n",
+ "\n",
+ "%pip install chess -U"
]
},
{
@@ -65,8 +63,6 @@
"metadata": {},
"outputs": [],
"source": [
- "import autogen\n",
- "\n",
"config_list_gpt4 = autogen.config_list_from_json(\n",
" \"OAI_CONFIG_LIST\",\n",
" filter_dict={\n",
@@ -138,13 +134,11 @@
"metadata": {},
"outputs": [],
"source": [
- "from collections import defaultdict\n",
- "from typing import Any, Dict, List, Optional, Union\n",
- "\n",
"sys_msg = \"\"\"You are an AI-powered chess board agent.\n",
"You translate the user's natural language input into legal UCI moves.\n",
"You should only reply with a UCI move string extracted from the user's input.\"\"\"\n",
"\n",
+ "\n",
"class BoardAgent(autogen.AssistantAgent):\n",
" board: chess.Board\n",
" correct_move_messages: Dict[autogen.Agent, List[Dict]]\n",
@@ -168,7 +162,9 @@
" ) -> Union[str, Dict, None]:\n",
" message = messages[-1]\n",
" # extract a UCI move from player's message\n",
- " reply = self.generate_reply(self.correct_move_messages[sender] + [message], sender, exclude=[BoardAgent._generate_board_reply])\n",
+ " reply = self.generate_reply(\n",
+ " self.correct_move_messages[sender] + [message], sender, exclude=[BoardAgent._generate_board_reply]\n",
+ " )\n",
" uci_move = reply if isinstance(reply, str) else str(reply[\"content\"])\n",
" try:\n",
" self.board.push_uci(uci_move)\n",
@@ -178,10 +174,14 @@
" else:\n",
" # valid move\n",
" m = chess.Move.from_uci(uci_move)\n",
- " display(chess.svg.board(self.board, arrows=[(m.from_square, m.to_square)], fill={m.from_square: \"gray\"}, size=200))\n",
+ " display( # noqa: F821\n",
+ " chess.svg.board(\n",
+ " self.board, arrows=[(m.from_square, m.to_square)], fill={m.from_square: \"gray\"}, size=200\n",
+ " )\n",
+ " )\n",
" self.correct_move_messages[sender].extend([message, self._message_to_dict(uci_move)])\n",
" self.correct_move_messages[sender][-1][\"role\"] = \"assistant\"\n",
- " return True, uci_move\n"
+ " return True, uci_move"
]
},
{
@@ -190,9 +190,9 @@
"metadata": {},
"outputs": [],
"source": [
- "sys_msg_tmpl = \"\"\"Your name is {name} and you are a chess player. \n",
- "You are playing against {opponent_name}. \n",
- "You are playing as {color}. \n",
+ "sys_msg_tmpl = \"\"\"Your name is {name} and you are a chess player.\n",
+ "You are playing against {opponent_name}.\n",
+ "You are playing as {color}.\n",
"You communicate your move using universal chess interface language.\n",
"You also chit-chat with your opponent when you communicate a move to light up the mood.\n",
"You should ensure both you and the opponent are making legal moves.\n",
@@ -200,7 +200,6 @@
"\n",
"\n",
"class ChessPlayerAgent(autogen.AssistantAgent):\n",
- "\n",
" def __init__(\n",
" self,\n",
" color: str,\n",
@@ -241,7 +240,9 @@
" if last_message[\"content\"].startswith(\"Error\"):\n",
" # try again\n",
" last_message[\"role\"] = \"system\"\n",
- " return True, self.generate_reply(messages + board_state_msg, sender, exclude=[ChessPlayerAgent._generate_reply_for_board])\n",
+ " return True, self.generate_reply(\n",
+ " messages + board_state_msg, sender, exclude=[ChessPlayerAgent._generate_reply_for_board]\n",
+ " )\n",
" else:\n",
" return True, None\n",
"\n",
@@ -255,7 +256,9 @@
" # add a system message about the current state of the board.\n",
" board_state_msg = [{\"role\": \"system\", \"content\": f\"Current board:\\n{board_agent.board}\"}]\n",
" # propose a reply which will be sent to the board agent for verification.\n",
- " message = self.generate_reply(messages + board_state_msg, sender, exclude=[ChessPlayerAgent._generate_reply_for_player])\n",
+ " message = self.generate_reply(\n",
+ " messages + board_state_msg, sender, exclude=[ChessPlayerAgent._generate_reply_for_player]\n",
+ " )\n",
" if message is None:\n",
" return True, None\n",
" # converse with the board until a legal move is made or max allowed retries.\n",
@@ -267,7 +270,7 @@
" # didn't make a legal move after a limit times of retries.\n",
" print(f\"{self.name}: I yield.\")\n",
" return True, None\n",
- " return True, self._oai_messages[board_agent][-2]\n"
+ " return True, self._oai_messages[board_agent][-2]"
]
},
{
diff --git a/notebook/agentchat_compression.ipynb b/notebook/agentchat_compression.ipynb
index 26405da71..f11417ba5 100644
--- a/notebook/agentchat_compression.ipynb
+++ b/notebook/agentchat_compression.ipynb
@@ -82,7 +82,13 @@
"metadata": {},
"outputs": [],
"source": [
+ "# define functions according to the function description\n",
+ "from IPython import get_ipython\n",
+ "\n",
"import autogen\n",
+ "from autogen.agentchat.contrib.compressible_agent import CompressibleAgent\n",
+ "from autogen.agentchat.contrib.math_user_proxy_agent import MathUserProxyAgent\n",
+ "\n",
"config_list = autogen.config_list_from_json(\n",
" \"OAI_CONFIG_LIST\",\n",
" filter_dict={\n",
@@ -534,12 +540,9 @@
}
],
"source": [
- "from autogen.agentchat.contrib.math_user_proxy_agent import MathUserProxyAgent\n",
- "from autogen.agentchat.contrib.compressible_agent import CompressibleAgent\n",
- "\n",
"# 1. replace AssistantAgent with CompressibleAgent\n",
"assistant = CompressibleAgent(\n",
- " name=\"assistant\", \n",
+ " name=\"assistant\",\n",
" system_message=\"You are a helpful assistant.\",\n",
" llm_config={\n",
" \"timeout\": 600,\n",
@@ -548,20 +551,22 @@
" },\n",
" compress_config={\n",
" \"mode\": \"COMPRESS\",\n",
- " \"trigger_count\": 600, # set this to a large number for less frequent compression\n",
- " \"verbose\": True, # to allow printing of compression information: context before and after compression\n",
+ " \"trigger_count\": 600, # set this to a large number for less frequent compression\n",
+ " \"verbose\": True, # to allow printing of compression information: context before and after compression\n",
" \"leave_last_n\": 2,\n",
- " }\n",
+ " },\n",
")\n",
"\n",
"# 2. create the MathUserProxyAgent instance named \"mathproxyagent\"\n",
"mathproxyagent = MathUserProxyAgent(\n",
- " name=\"mathproxyagent\", \n",
+ " name=\"mathproxyagent\",\n",
" human_input_mode=\"NEVER\",\n",
" code_execution_config={\"use_docker\": False},\n",
" max_consecutive_auto_reply=5,\n",
")\n",
- "math_problem = \"Find all $x$ that satisfy the inequality $(2x+10)(x+3)<(3x+9)(x+8)$. Express your answer in interval notation.\"\n",
+ "math_problem = (\n",
+ " \"Find all $x$ that satisfy the inequality $(2x+10)(x+3)<(3x+9)(x+8)$. Express your answer in interval notation.\"\n",
+ ")\n",
"mathproxyagent.initiate_chat(assistant, problem=math_problem)"
]
},
@@ -811,8 +816,6 @@
" \"config_list\": config_list,\n",
" \"timeout\": 120,\n",
"}\n",
- "import autogen\n",
- "from autogen.agentchat.contrib.compressible_agent import CompressibleAgent\n",
"\n",
"chatbot = CompressibleAgent(\n",
" name=\"chatbot\",\n",
@@ -820,10 +823,10 @@
" llm_config=llm_config,\n",
" compress_config={\n",
" \"mode\": \"COMPRESS\",\n",
- " \"trigger_count\": 600, # set this to a large number for less frequent compression\n",
- " \"verbose\": True, # set this to False to suppress the compression log\n",
+ " \"trigger_count\": 600, # set this to a large number for less frequent compression\n",
+ " \"verbose\": True, # set this to False to suppress the compression log\n",
" \"leave_last_n\": 2,\n",
- " }\n",
+ " },\n",
")\n",
"\n",
"# create a UserProxyAgent instance named \"user_proxy\"\n",
@@ -835,8 +838,6 @@
" code_execution_config={\"work_dir\": \"coding\"},\n",
")\n",
"\n",
- "# define functions according to the function description\n",
- "from IPython import get_ipython\n",
"\n",
"def exec_python(cell):\n",
" ipython = get_ipython()\n",
@@ -848,9 +849,11 @@
" log += f\"\\n{result.error_in_exec}\"\n",
" return log\n",
"\n",
+ "\n",
"def exec_sh(script):\n",
" return user_proxy.execute_code_blocks([(\"sh\", script)])\n",
"\n",
+ "\n",
"# register the functions\n",
"user_proxy.register_function(\n",
" function_map={\n",
@@ -863,7 +866,7 @@
"user_proxy.initiate_chat(\n",
" chatbot,\n",
" message=\"Draw two agents chatting with each other with an example dialog. Don't add plt.show().\",\n",
- ")\n"
+ ")"
]
},
{
@@ -1217,13 +1220,9 @@
}
],
"source": [
- "import autogen\n",
- "from autogen.agentchat.contrib.math_user_proxy_agent import MathUserProxyAgent\n",
- "from autogen.agentchat.contrib.compressible_agent import CompressibleAgent\n",
- "\n",
"def constrain_num_messages(messages):\n",
" \"\"\"Constrain the number of messages to 3.\n",
- " \n",
+ "\n",
" This is an example of a customized compression function.\n",
"\n",
" Returns:\n",
@@ -1233,10 +1232,11 @@
" if len(messages) <= 3:\n",
" # do nothing\n",
" return False, None\n",
- " \n",
+ "\n",
" # save the first and last two messages\n",
" return True, messages[:1] + messages[-2:]\n",
"\n",
+ "\n",
"# create a CompressibleAgent instance named \"assistant\"\n",
"assistant = CompressibleAgent(\n",
" name=\"assistant\",\n",
@@ -1248,7 +1248,7 @@
" compress_config={\n",
" \"mode\": \"CUSTOMIZED\",\n",
" \"compress_function\": constrain_num_messages, # this is required for customized compression\n",
- " \"trigger_count\": 1600, \n",
+ " \"trigger_count\": 1600,\n",
" },\n",
")\n",
"\n",
@@ -1257,16 +1257,17 @@
" name=\"user_proxy\",\n",
" human_input_mode=\"TERMINATE\",\n",
" max_consecutive_auto_reply=10,\n",
- " is_termination_msg=lambda x: x.get(\"content\", \"\").rstrip().endswith(\"TERMINATE\") or x.get(\"content\", \"\").rstrip().endswith(\"TERMINATE.\"),\n",
+ " is_termination_msg=lambda x: x.get(\"content\", \"\").rstrip().endswith(\"TERMINATE\")\n",
+ " or x.get(\"content\", \"\").rstrip().endswith(\"TERMINATE.\"),\n",
" code_execution_config={\"work_dir\": \"web\"},\n",
" system_message=\"\"\"Reply TERMINATE if the task has been solved at full satisfaction.\n",
- "Otherwise, reply CONTINUE, or the reason why the task is not solved yet.\"\"\"\n",
+ "Otherwise, reply CONTINUE, or the reason why the task is not solved yet.\"\"\",\n",
")\n",
"\n",
"user_proxy.initiate_chat(\n",
" assistant,\n",
" message=\"\"\"Show me the YTD gain of 10 largest technology companies as of today.\"\"\",\n",
- ")\n"
+ ")"
]
}
],
diff --git a/notebook/agentchat_dalle_and_gpt4v.ipynb b/notebook/agentchat_dalle_and_gpt4v.ipynb
index 3cdb051be..b63ae2ee6 100644
--- a/notebook/agentchat_dalle_and_gpt4v.ipynb
+++ b/notebook/agentchat_dalle_and_gpt4v.ipynb
@@ -28,33 +28,26 @@
"metadata": {},
"outputs": [],
"source": [
- "import requests\n",
"import json\n",
- "import pdb\n",
"import os\n",
+ "import pdb\n",
+ "import random\n",
"import re\n",
- "\n",
+ "import time\n",
"from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union\n",
"\n",
- "import autogen\n",
- "from autogen import AssistantAgent, Agent, UserProxyAgent, ConversableAgent\n",
- "\n",
- "from autogen.agentchat.contrib.img_utils import get_image_data, _to_pil\n",
- "from termcolor import colored\n",
- "import random"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "id": "c8591de9",
- "metadata": {},
- "outputs": [],
- "source": [
- "from openai import OpenAI\n",
- "import os\n",
+ "import matplotlib.pyplot as plt\n",
"import PIL\n",
- "import matplotlib.pyplot as plt"
+ "import requests\n",
+ "from diskcache import Cache\n",
+ "from openai import OpenAI\n",
+ "from PIL import Image\n",
+ "from termcolor import colored\n",
+ "\n",
+ "import autogen\n",
+ "from autogen import Agent, AssistantAgent, ConversableAgent, UserProxyAgent\n",
+ "from autogen.agentchat.contrib.img_utils import _to_pil, get_image_data\n",
+ "from autogen.agentchat.contrib.multimodal_conversable_agent import MultimodalConversableAgent"
]
},
{
@@ -64,8 +57,6 @@
"metadata": {},
"outputs": [],
"source": [
- "from autogen.agentchat.contrib.multimodal_conversable_agent import MultimodalConversableAgent\n",
- "\n",
"config_list_4v = autogen.config_list_from_json(\n",
" \"OAI_CONFIG_LIST\",\n",
" filter_dict={\n",
@@ -125,8 +116,6 @@
"metadata": {},
"outputs": [],
"source": [
- "from diskcache import Cache\n",
- "\n",
"def dalle_call(client: OpenAI, model: str, prompt: str, size: str, quality: str, n: int) -> str:\n",
" \"\"\"\n",
" Generate an image using OpenAI's DALL-E model and cache the result.\n",
@@ -152,25 +141,24 @@
" - The image data is obtained by making a secondary request to the URL provided by the DALL-E API response.\n",
" \"\"\"\n",
" # Function implementation...\n",
- " cache = Cache('.cache/') # Create a cache directory\n",
+ " cache = Cache(\".cache/\") # Create a cache directory\n",
" key = (model, prompt, size, quality, n)\n",
" if key in cache:\n",
" return cache[key]\n",
"\n",
" # If not in cache, compute and store the result\n",
" response = client.images.generate(\n",
- " model=model,\n",
- " prompt=prompt,\n",
- " size=size,\n",
- " quality=quality,\n",
- " n=n,\n",
- " )\n",
+ " model=model,\n",
+ " prompt=prompt,\n",
+ " size=size,\n",
+ " quality=quality,\n",
+ " n=n,\n",
+ " )\n",
" image_url = response.data[0].url\n",
" img_data = get_image_data(image_url)\n",
" cache[key] = img_data\n",
"\n",
- " return img_data\n",
- "\n"
+ " return img_data"
]
},
{
@@ -188,7 +176,6 @@
"metadata": {},
"outputs": [],
"source": [
- "\n",
"def extract_img(agent: Agent) -> PIL.Image:\n",
" \"\"\"\n",
" Extracts an image from the last message of an agent and converts it to a PIL image.\n",
@@ -212,7 +199,7 @@
" # Function implementation...\n",
" img_data = re.findall(\"<img (.*)>\", agent.last_message()[\"content\"])[0]\n",
" pil_img = _to_pil(img_data)\n",
- " return pil_img\n"
+ " return pil_img"
]
},
{
@@ -230,20 +217,19 @@
"metadata": {},
"outputs": [],
"source": [
- "\n",
"class DALLEAgent(ConversableAgent):\n",
" def __init__(self, name, llm_config: dict, **kwargs):\n",
" super().__init__(name, llm_config=llm_config, **kwargs)\n",
- " \n",
+ "\n",
" try:\n",
" config_list = llm_config[\"config_list\"]\n",
" api_key = config_list[0][\"api_key\"]\n",
" except Exception as e:\n",
" print(\"Unable to fetch API Key, because\", e)\n",
- " api_key = os.getenv('OPENAI_API_KEY')\n",
+ " api_key = os.getenv(\"OPENAI_API_KEY\")\n",
" self.client = OpenAI(api_key=api_key)\n",
" self.register_reply([Agent, None], DALLEAgent.generate_dalle_reply)\n",
- " \n",
+ "\n",
" def send(\n",
" self,\n",
" message: Union[Dict, str],\n",
@@ -251,10 +237,10 @@
" request_reply: Optional[bool] = None,\n",
" silent: Optional[bool] = False,\n",
" ):\n",
- " # override and always \"silent\" the send out message; \n",
+ " # override and always \"silent\" the send out message;\n",
" # otherwise, the print log would be super long!\n",
" super().send(message, recipient, request_reply, silent=True)\n",
- " \n",
+ "\n",
" def generate_dalle_reply(self, messages: Optional[List[Dict]], sender: \"Agent\", config):\n",
" \"\"\"Generate a reply using OpenAI DALLE call.\"\"\"\n",
" client = self.client if config is None else config\n",
@@ -269,12 +255,12 @@
" client=self.client,\n",
" model=\"dall-e-3\",\n",
" prompt=prompt,\n",
- " size=\"1024x1024\", # TODO: the size should be flexible, deciding landscape, square, or portrait mode.\n",
+ " size=\"1024x1024\", # TODO: the size should be flexible, deciding landscape, square, or portrait mode.\n",
" quality=\"standard\",\n",
" n=1,\n",
" )\n",
" out_message = f\"<img {img_data}>\"\n",
- " return True, out_message\n"
+ " return True, out_message"
]
},
{
@@ -308,17 +294,15 @@
"source": [
"dalle = DALLEAgent(name=\"Dalle\", llm_config={\"config_list\": config_list_dalle})\n",
"\n",
- "user_proxy = autogen.UserProxyAgent(\n",
- " name=\"User_proxy\",\n",
- " system_message=\"A human admin.\",\n",
- " human_input_mode=\"NEVER\",\n",
- " max_consecutive_auto_reply=0\n",
+ "user_proxy = UserProxyAgent(\n",
+ " name=\"User_proxy\", system_message=\"A human admin.\", human_input_mode=\"NEVER\", max_consecutive_auto_reply=0\n",
")\n",
"\n",
"# Ask the question with an image\n",
- "user_proxy.initiate_chat(dalle,\n",
- " message=\"\"\"Create an image with black background, a happy robot is showing a sign with \"I Love AutoGen\".\"\"\", \n",
- " )"
+ "user_proxy.initiate_chat(\n",
+ " dalle,\n",
+ " message=\"\"\"Create an image with black background, a happy robot is showing a sign with \"I Love AutoGen\".\"\"\",\n",
+ ")"
]
},
{
@@ -344,7 +328,7 @@
"img = extract_img(dalle)\n",
"\n",
"plt.imshow(img)\n",
- "plt.axis('off') # Turn off axis numbers\n",
+ "plt.axis(\"off\") # Turn off axis numbers\n",
"plt.show()"
]
},
@@ -364,28 +348,25 @@
"outputs": [],
"source": [
"class DalleCreator(AssistantAgent):\n",
- "\n",
" def __init__(self, n_iters=2, **kwargs):\n",
" \"\"\"\n",
" Initializes a DalleCreator instance.\n",
- " \n",
- " This agent facilitates the creation of visualizations through a collaborative effort among \n",
+ "\n",
+ " This agent facilitates the creation of visualizations through a collaborative effort among\n",
" its child agents: dalle and critics.\n",
- " \n",
+ "\n",
" Parameters:\n",
" - n_iters (int, optional): The number of \"improvement\" iterations to run. Defaults to 2.\n",
" - **kwargs: keyword arguments for the parent AssistantAgent.\n",
" \"\"\"\n",
" super().__init__(**kwargs)\n",
- " self.register_reply([Agent, None],\n",
- " reply_func=DalleCreator._reply_user,\n",
- " position=0)\n",
+ " self.register_reply([Agent, None], reply_func=DalleCreator._reply_user, position=0)\n",
" self._n_iters = n_iters\n",
"\n",
" def _reply_user(self, messages=None, sender=None, config=None):\n",
" if all((messages is None, sender is None)):\n",
" error_msg = f\"Either {messages=} or {sender=} must be provided.\"\n",
- " logger.error(error_msg)\n",
+ " logger.error(error_msg) # noqa: F821\n",
" raise AssertionError(error_msg)\n",
"\n",
" if messages is None:\n",
@@ -396,8 +377,7 @@
" ## Define the agents\n",
" self.critics = MultimodalConversableAgent(\n",
" name=\"Critics\",\n",
- " system_message=\n",
- " \"\"\"You need to improve the prompt of the figures you saw.\n",
+ " system_message=\"\"\"You need to improve the prompt of the figures you saw.\n",
"How to create a figure that is better in terms of color, shape, text (clarity), and other things.\n",
"Reply with the following format:\n",
"\n",
@@ -410,43 +390,37 @@
" max_consecutive_auto_reply=3,\n",
" )\n",
"\n",
- " self.dalle = DALLEAgent(name=\"Dalle\", llm_config={\"config_list\": config_list_dalle}, \n",
- " max_consecutive_auto_reply=0)\n",
+ " self.dalle = DALLEAgent(\n",
+ " name=\"Dalle\", llm_config={\"config_list\": config_list_dalle}, max_consecutive_auto_reply=0\n",
+ " )\n",
"\n",
" # Data flow begins\n",
" self.send(message=img_prompt, recipient=self.dalle, request_reply=True)\n",
" img = extract_img(self.dalle)\n",
" plt.imshow(img)\n",
- " plt.axis('off') # Turn off axis numbers\n",
+ " plt.axis(\"off\") # Turn off axis numbers\n",
" plt.show()\n",
" print(\"Image PLOTTED\")\n",
- " \n",
- " \n",
+ "\n",
" for i in range(self._n_iters):\n",
" # Downsample the image s.t. GPT-4V can take\n",
" img = extract_img(self.dalle)\n",
" smaller_image = img.resize((128, 128), Image.Resampling.LANCZOS)\n",
" smaller_image.save(\"result.png\")\n",
- " \n",
"\n",
" self.msg_to_critics = f\"\"\"Here is the prompt: {img_prompt}.\n",
" Here is the figure <img result.png>.\n",
" Now, critic and create a prompt so that DALLE can give me a better image.\n",
" Show me both \"CRITICS\" and \"PROMPT\"!\n",
" \"\"\"\n",
- " self.send(message=self.msg_to_critics,\n",
- " recipient=self.critics,\n",
- " request_reply=True)\n",
+ " self.send(message=self.msg_to_critics, recipient=self.critics, request_reply=True)\n",
" feedback = self._oai_messages[self.critics][-1][\"content\"]\n",
" img_prompt = re.findall(\"PROMPT: (.*)\", feedback)[0]\n",
- " \n",
- " self.send(\n",
- " message=img_prompt,\n",
- " recipient=self.dalle,\n",
- " request_reply=True)\n",
+ "\n",
+ " self.send(message=img_prompt, recipient=self.dalle, request_reply=True)\n",
" img = extract_img(self.dalle)\n",
" plt.imshow(img)\n",
- " plt.axis('off') # Turn off axis numbers\n",
+ " plt.axis(\"off\") # Turn off axis numbers\n",
" plt.show()\n",
" print(f\"Image {i} PLOTTED\")\n",
"\n",
@@ -577,25 +551,18 @@
}
],
"source": [
- "import matplotlib.pyplot as plt\n",
- "import time\n",
- "from PIL import Image\n",
- "\n",
"creator = DalleCreator(\n",
" name=\"DALLE Creator!\",\n",
" max_consecutive_auto_reply=0,\n",
" system_message=\"Help me coordinate generating image\",\n",
- " llm_config=gpt4_llm_config\n",
+ " llm_config=gpt4_llm_config,\n",
")\n",
"\n",
- "user_proxy = autogen.UserProxyAgent(\n",
- " name=\"User\",\n",
- " human_input_mode=\"NEVER\",\n",
- " max_consecutive_auto_reply=0\n",
- ")\n",
+ "user_proxy = UserProxyAgent(name=\"User\", human_input_mode=\"NEVER\", max_consecutive_auto_reply=0)\n",
"\n",
- "user_proxy.initiate_chat(creator, \n",
- " message=\"\"\"Create an image with black background, a happy robot is showing a sign with \"I Love AutoGen\".\"\"\")\n"
+ "user_proxy.initiate_chat(\n",
+ " creator, message=\"\"\"Create an image with black background, a happy robot is showing a sign with \"I Love AutoGen\".\"\"\"\n",
+ ")"
]
}
],
diff --git a/notebook/agentchat_function_call.ipynb b/notebook/agentchat_function_call.ipynb
index 1585877cc..b6e077cba 100644
--- a/notebook/agentchat_function_call.ipynb
+++ b/notebook/agentchat_function_call.ipynb
@@ -57,6 +57,9 @@
"metadata": {},
"outputs": [],
"source": [
+ "from IPython import get_ipython\n",
+ "from typing_extensions import Annotated\n",
+ "\n",
"import autogen\n",
"\n",
"config_list = autogen.config_list_from_json(\n",
@@ -211,9 +214,8 @@
" code_execution_config={\"work_dir\": \"coding\"},\n",
")\n",
"\n",
+ "\n",
"# define functions according to the function description\n",
- "from IPython import get_ipython\n",
- "from typing_extensions import Annotated\n",
"\n",
"\n",
"@user_proxy.register_for_execution()\n",
diff --git a/notebook/agentchat_function_call_async.ipynb b/notebook/agentchat_function_call_async.ipynb
index 581022e44..098457792 100644
--- a/notebook/agentchat_function_call_async.ipynb
+++ b/notebook/agentchat_function_call_async.ipynb
@@ -66,6 +66,10 @@
"metadata": {},
"outputs": [],
"source": [
+ "import time\n",
+ "\n",
+ "from typing_extensions import Annotated\n",
+ "\n",
"import autogen\n",
"\n",
"config_list = autogen.config_list_from_json(\n",
@@ -171,9 +175,6 @@
}
],
"source": [
- "# define functions according to the function description\n",
- "import time\n",
- "\n",
"llm_config = {\n",
" \"config_list\": config_list,\n",
"}\n",
@@ -194,9 +195,11 @@
" code_execution_config={\"work_dir\": \"coding\"},\n",
")\n",
"\n",
- "from typing_extensions import Annotated\n",
+ "# define functions according to the function description\n",
"\n",
"# An example async function\n",
+ "\n",
+ "\n",
"@user_proxy.register_for_execution()\n",
"@coder.register_for_llm(description=\"create a timer for N seconds\")\n",
"async def timer(num_seconds: Annotated[str, \"Number of seconds in the timer.\"]) -> str:\n",
@@ -219,7 +222,7 @@
"# 'await' is used to pause and resume code execution for async IO operations.\n",
"# Without 'await', an async function returns a coroutine object but doesn't execute the function.\n",
"# With 'await', the async function is executed and the current function is paused until the awaited function returns a result.\n",
- "await user_proxy.a_initiate_chat(\n",
+ "await user_proxy.a_initiate_chat( # noqa: F704\n",
" coder,\n",
" message=\"Create a timer for 5 seconds and then a stopwatch for 5 seconds.\",\n",
")"
@@ -248,6 +251,8 @@
")\n",
"\n",
"# Add a function for robust group chat termination\n",
+ "\n",
+ "\n",
"@user_proxy.register_for_execution()\n",
"@markdownagent.register_for_llm()\n",
"@coder.register_for_llm(description=\"terminate the group chat\")\n",
@@ -360,7 +365,7 @@
}
],
"source": [
- "await user_proxy.a_initiate_chat(\n",
+ "await user_proxy.a_initiate_chat( # noqa: F704\n",
" manager,\n",
" message=\"\"\"\n",
"1) Create a timer for 5 seconds.\n",
diff --git a/notebook/agentchat_function_call_currency_calculator.ipynb b/notebook/agentchat_function_call_currency_calculator.ipynb
index 8e5be9928..cdde6ec67 100644
--- a/notebook/agentchat_function_call_currency_calculator.ipynb
+++ b/notebook/agentchat_function_call_currency_calculator.ipynb
@@ -57,6 +57,11 @@
"metadata": {},
"outputs": [],
"source": [
+ "from typing import Literal\n",
+ "\n",
+ "from pydantic import BaseModel, Field\n",
+ "from typing_extensions import Annotated\n",
+ "\n",
"import autogen\n",
"\n",
"config_list = autogen.config_list_from_json(\n",
@@ -139,9 +144,6 @@
" max_consecutive_auto_reply=10,\n",
")\n",
"\n",
- "from typing import Literal\n",
- "\n",
- "from typing_extensions import Annotated\n",
"\n",
"CurrencySymbol = Literal[\"USD\", \"EUR\"]\n",
"\n",
@@ -347,15 +349,12 @@
" max_consecutive_auto_reply=10,\n",
")\n",
"\n",
- "from typing import Literal\n",
- "\n",
- "from pydantic import BaseModel, Field\n",
- "from typing_extensions import Annotated\n",
"\n",
"class Currency(BaseModel):\n",
" currency: Annotated[CurrencySymbol, Field(..., description=\"Currency symbol\")]\n",
" amount: Annotated[float, Field(0, description=\"Amount of currency\", ge=0)]\n",
"\n",
+ "\n",
"@user_proxy.register_for_execution()\n",
"@chatbot.register_for_llm(description=\"Currency exchange calculator.\")\n",
"def currency_calculator(\n",
diff --git a/notebook/agentchat_graph_modelling_language_using_select_speaker.ipynb b/notebook/agentchat_graph_modelling_language_using_select_speaker.ipynb
index 72a98bb88..894232bbb 100644
--- a/notebook/agentchat_graph_modelling_language_using_select_speaker.ipynb
+++ b/notebook/agentchat_graph_modelling_language_using_select_speaker.ipynb
@@ -33,7 +33,7 @@
},
{
"cell_type": "code",
- "execution_count": 2,
+ "execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@@ -43,6 +43,22 @@
"%pip install matplotlib~=3.8.1"
]
},
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import random # noqa E402\n",
+ "\n",
+ "import matplotlib.pyplot as plt # noqa E402\n",
+ "import networkx as nx # noqa E402\n",
+ "\n",
+ "import autogen # noqa E402\n",
+ "from autogen.agentchat.assistant_agent import AssistantAgent # noqa E402\n",
+ "from autogen.agentchat.groupchat import GroupChat # noqa E402"
+ ]
+ },
{
"cell_type": "code",
"execution_count": 3,
@@ -57,15 +73,6 @@
}
],
"source": [
- "import autogen\n",
- "import networkx as nx\n",
- "import matplotlib.pyplot as plt\n",
- "from autogen.agentchat.groupchat import GroupChat\n",
- "from autogen.agentchat.assistant_agent import AssistantAgent\n",
- "\n",
- "import random\n",
- "from typing import List, Dict\n",
- "\n",
"print(autogen.__version__)"
]
},
@@ -110,7 +117,7 @@
" if source_node != target_node: # To avoid self-loops\n",
" graph.add_edge(source_node, target_node)\n",
"\n",
- "nx.draw(graph, with_labels=True, font_weight='bold')"
+ "nx.draw(graph, with_labels=True, font_weight=\"bold\")"
]
},
{
@@ -137,7 +144,6 @@
}
],
"source": [
- "\n",
"# Hub and Spoke\n",
"# Create an empty directed graph\n",
"graph = nx.DiGraph()\n",
@@ -154,7 +160,7 @@
" graph.add_edge(target_node, source_node)\n",
"\n",
"\n",
- "nx.draw(graph, with_labels=True, font_weight='bold')"
+ "nx.draw(graph, with_labels=True, font_weight=\"bold\")"
]
},
{
@@ -179,7 +185,7 @@
"graph = nx.DiGraph()\n",
"\n",
"# Outer loop for prefixes 'A', 'B', 'C'\n",
- "for prefix in ['A', 'B', 'C']:\n",
+ "for prefix in [\"A\", \"B\", \"C\"]:\n",
" # Add 5 nodes with each prefix to the graph using a for loop\n",
" for i in range(5):\n",
" node_id = f\"{prefix}{i}\"\n",
@@ -192,12 +198,12 @@
" target_id = f\"{prefix}{target_node}\"\n",
" if source_node != target_node: # To avoid self-loops\n",
" graph.add_edge(source_id, target_id)\n",
- " \n",
- "graph.add_edge('A0', 'B0')\n",
- "graph.add_edge('B0', 'C0')\n",
+ "\n",
+ "graph.add_edge(\"A0\", \"B0\")\n",
+ "graph.add_edge(\"B0\", \"C0\")\n",
"\n",
"# Draw the graph\n",
- "nx.draw(graph, with_labels=True, font_weight='bold')"
+ "nx.draw(graph, with_labels=True, font_weight=\"bold\")"
]
},
{
@@ -228,8 +234,7 @@
" for target_node in range(2):\n",
" graph.add_edge(source_node, target_node)\n",
"\n",
- "nx.draw(graph, with_labels=True, font_weight='bold')\n",
- " "
+ "nx.draw(graph, with_labels=True, font_weight=\"bold\")"
]
},
{
@@ -317,71 +322,82 @@
" self.previous_speaker = None # Keep track of the previous speaker\n",
" self.graph = graph # The graph depicting who are the next speakers available\n",
"\n",
- " def select_speaker(self, last_speaker, selector): \n",
+ " def select_speaker(self, last_speaker, selector):\n",
" self.previous_speaker = last_speaker\n",
"\n",
" # Check if last message suggests a next speaker or termination\n",
" last_message = self.messages[-1] if self.messages else None\n",
" suggested_next = None\n",
- " \n",
+ "\n",
" if last_message:\n",
- " if 'NEXT:' in last_message['content']:\n",
- " suggested_next = last_message['content'].split('NEXT: ')[-1].strip()\n",
+ " if \"NEXT:\" in last_message[\"content\"]:\n",
+ " suggested_next = last_message[\"content\"].split(\"NEXT: \")[-1].strip()\n",
" # Strip full stop and comma\n",
- " suggested_next = suggested_next.replace('.', '').replace(',', '')\n",
+ " suggested_next = suggested_next.replace(\".\", \"\").replace(\",\", \"\")\n",
" print(f\"Suggested next speaker from the last message: {suggested_next}\")\n",
- " \n",
- " elif 'TERMINATE' in last_message['content']:\n",
+ "\n",
+ " elif \"TERMINATE\" in last_message[\"content\"]:\n",
" try:\n",
- " return self.agent_by_name('User_proxy')\n",
+ " return self.agent_by_name(\"User_proxy\")\n",
" except ValueError:\n",
" print(f\"agent_by_name failed suggested_next: {suggested_next}\")\n",
- " \n",
+ "\n",
" # Debugging print for the current previous speaker\n",
" if self.previous_speaker is not None:\n",
- " print('Current previous speaker:', self.previous_speaker.name)\n",
+ " print(\"Current previous speaker:\", self.previous_speaker.name)\n",
"\n",
" # Selecting first round speaker\n",
" if self.previous_speaker is None and self.graph is not None:\n",
- " eligible_speakers = [agent for agent in agents if self.graph.nodes[agent.name].get('first_round_speaker', False)]\n",
- " print('First round eligible speakers:', [speaker.name for speaker in eligible_speakers])\n",
+ " eligible_speakers = [\n",
+ " agent for agent in agents if self.graph.nodes[agent.name].get(\"first_round_speaker\", False)\n",
+ " ]\n",
+ " print(\"First round eligible speakers:\", [speaker.name for speaker in eligible_speakers])\n",
"\n",
" # Selecting successors of the previous speaker\n",
" elif self.previous_speaker is not None and self.graph is not None:\n",
" eligible_speaker_names = [target for target in self.graph.successors(self.previous_speaker.name)]\n",
" eligible_speakers = [agent for agent in agents if agent.name in eligible_speaker_names]\n",
- " print('Eligible speakers based on previous speaker:', eligible_speaker_names)\n",
+ " print(\"Eligible speakers based on previous speaker:\", eligible_speaker_names)\n",
"\n",
" else:\n",
" eligible_speakers = agents\n",
"\n",
" # Debugging print for the next potential speakers\n",
- " print(f\"Eligible speakers based on graph and previous speaker {self.previous_speaker.name if self.previous_speaker else 'None'}: {[speaker.name for speaker in eligible_speakers]}\")\n",
+ " print(\n",
+ " f\"Eligible speakers based on graph and previous speaker {self.previous_speaker.name if self.previous_speaker else 'None'}: {[speaker.name for speaker in eligible_speakers]}\"\n",
+ " )\n",
"\n",
" # Three attempts at getting the next_speaker\n",
" # 1. Using suggested_next if suggested_next is in the eligible_speakers.name\n",
" # 2. Using LLM to pick from eligible_speakers, given that there is some context in self.message\n",
" # 3. Random (catch-all)\n",
" next_speaker = None\n",
- " \n",
+ "\n",
" if eligible_speakers:\n",
" print(\"Selecting from eligible speakers:\", [speaker.name for speaker in eligible_speakers])\n",
" # 1. Using suggested_next if suggested_next is in the eligible_speakers.name\n",
" if suggested_next in [speaker.name for speaker in eligible_speakers]:\n",
" print(\"suggested_next is in eligible_speakers\")\n",
" next_speaker = self.agent_by_name(suggested_next)\n",
- " \n",
+ "\n",
" else:\n",
" msgs_len = len(self.messages)\n",
" print(f\"msgs_len is now {msgs_len}\")\n",
" if len(self.messages) > 1:\n",
" # 2. Using LLM to pick from eligible_speakers, given that there is some context in self.message\n",
- " print(f\"Using LLM to pick from eligible_speakers: {[speaker.name for speaker in eligible_speakers]}\")\n",
+ " print(\n",
+ " f\"Using LLM to pick from eligible_speakers: {[speaker.name for speaker in eligible_speakers]}\"\n",
+ " )\n",
" selector.update_system_message(self.select_speaker_msg(eligible_speakers))\n",
- " _, name = selector.generate_oai_reply(self.messages + [{\n",
- " \"role\": \"system\",\n",
- " \"content\": f\"Read the above conversation. Then select the next role from {[agent.name for agent in eligible_speakers]} to play. Only return the role.\",\n",
- " }])\n",
+ " _, name = selector.generate_oai_reply(\n",
+ " self.messages\n",
+ " + [\n",
+ " {\n",
+ " \"role\": \"system\",\n",
+ " \"content\": f\"Read the above conversation. Then select the next role from {[agent.name for agent in eligible_speakers]} to play. Only return the role.\",\n",
+ " }\n",
+ " ]\n",
+ " )\n",
"\n",
" # If exactly one agent is mentioned, use it. Otherwise, leave the OAI response unmodified\n",
" mentions = self._mentioned_agents(name, eligible_speakers)\n",
@@ -392,8 +408,7 @@
" if next_speaker is None:\n",
" # 3. Random (catch-all)\n",
" next_speaker = random.choice(eligible_speakers)\n",
- " \n",
- " \n",
+ "\n",
" print(f\"Selected next speaker: {next_speaker.name}\")\n",
"\n",
" return next_speaker\n",
@@ -434,7 +449,7 @@
],
"source": [
"# llm config\n",
- "llm_config = {\"config_list\": config_list_gpt4, \"cache_seed\": 100} \n",
+ "llm_config = {\"config_list\": config_list_gpt4, \"cache_seed\": 100}\n",
"\n",
"# Create an empty directed graph\n",
"graph = nx.DiGraph()\n",
@@ -442,41 +457,45 @@
"agents = []\n",
"\n",
"# Outer loop for prefixes 'A', 'B', 'C'\n",
- "for prefix in ['A', 'B', 'C']:\n",
+ "for prefix in [\"A\", \"B\", \"C\"]:\n",
" # Add 3 nodes with each prefix to the graph using a for loop\n",
" for i in range(3):\n",
" node_id = f\"{prefix}{i}\"\n",
" secret_value = random.randint(1, 5) # Generate a random secret value\n",
" graph.add_node(node_id, label=node_id, secret_value=secret_value)\n",
- " \n",
+ "\n",
" # Create an AssistantAgent for each node (assuming AssistantAgent is a defined class)\n",
- " agents.append(AssistantAgent(name=node_id, \n",
- " system_message=f\"\"\"Your name is {node_id}. \n",
+ " agents.append(\n",
+ " AssistantAgent(\n",
+ " name=node_id,\n",
+ " system_message=f\"\"\"Your name is {node_id}.\n",
" Do not respond as the speaker named in the NEXT tag if your name is not in the NEXT tag. Instead, suggest a relevant team leader to handle the mis-tag, with the NEXT: tag.\n",
- " \n",
+ "\n",
" You have {secret_value} chocolates.\n",
- " \n",
+ "\n",
" The list of players are [A0, A1, A2, B0, B1, B2, C0, C1, C2].\n",
- " \n",
- " Your first character of your name is your team, and your second character denotes that you are a team leader if it is 0. \n",
+ "\n",
+ " Your first character of your name is your team, and your second character denotes that you are a team leader if it is 0.\n",
" CONSTRAINTS: Team members can only talk within the team, whilst team leader can talk to team leaders of other teams but not team members of other teams.\n",
- " \n",
+ "\n",
" You can use NEXT: to suggest the next speaker. You have to respect the CONSTRAINTS, and can only suggest one player from the list of players, i.e., do not suggest A3 because A3 is not from the list of players.\n",
" Team leaders must make sure that they know the sum of the individual chocolate count of all three players in their own team, i.e., A0 is responsible for team A only.\n",
- " \n",
+ "\n",
" Keep track of the player's tally using a JSON format so that others can check the total tally. Use\n",
" A0:?, A1:?, A2:?,\n",
" B0:?, B1:?, B2:?,\n",
" C0:?, C1:?, C2:?\n",
- " \n",
+ "\n",
" If you are the team leader, you should aggregate your team's total chocolate count to cooperate.\n",
" Once the team leader know their team's tally, they can suggest another team leader for them to find their team tally, because we need all three team tallys to succeed.\n",
" Use NEXT: to suggest the next speaker, e.g., NEXT: A0.\n",
- " \n",
+ "\n",
" Once we have the total tally from all nine players, sum up all three teams' tally, then terminate the discussion using TERMINATE.\n",
- " \n",
+ "\n",
" \"\"\",\n",
- " llm_config=llm_config))\n",
+ " llm_config=llm_config,\n",
+ " )\n",
+ " )\n",
"\n",
" # Add edges between nodes with the same prefix using a nested for loop\n",
" for source_node in range(3):\n",
@@ -487,34 +506,36 @@
" graph.add_edge(source_id, target_id)\n",
"\n",
"# Adding edges between teams\n",
- "graph.add_edge('A0', 'B0')\n",
- "graph.add_edge('A0', 'C0')\n",
- "graph.add_edge('B0', 'A0')\n",
- "graph.add_edge('B0', 'C0')\n",
- "graph.add_edge('C0', 'A0')\n",
- "graph.add_edge('C0', 'B0')\n",
+ "graph.add_edge(\"A0\", \"B0\")\n",
+ "graph.add_edge(\"A0\", \"C0\")\n",
+ "graph.add_edge(\"B0\", \"A0\")\n",
+ "graph.add_edge(\"B0\", \"C0\")\n",
+ "graph.add_edge(\"C0\", \"A0\")\n",
+ "graph.add_edge(\"C0\", \"B0\")\n",
"\n",
"\n",
"# Updating node A0\n",
- "graph.nodes['A0']['first_round_speaker'] = True\n",
+ "graph.nodes[\"A0\"][\"first_round_speaker\"] = True\n",
+ "\n",
"\n",
"def get_node_color(node):\n",
- " if graph.nodes[node].get('first_round_speaker', False):\n",
- " return 'red'\n",
+ " if graph.nodes[node].get(\"first_round_speaker\", False):\n",
+ " return \"red\"\n",
" else:\n",
- " return 'green'\n",
+ " return \"green\"\n",
+ "\n",
"\n",
"# Draw the graph with secret values annotated\n",
"plt.figure(figsize=(12, 10))\n",
"pos = nx.spring_layout(graph) # positions for all nodes\n",
"\n",
"# Draw nodes with their colors\n",
- "nx.draw(graph, pos, with_labels=True, font_weight='bold', node_color=[get_node_color(node) for node in graph])\n",
+ "nx.draw(graph, pos, with_labels=True, font_weight=\"bold\", node_color=[get_node_color(node) for node in graph])\n",
"\n",
"# Annotate secret values\n",
"for node, (x, y) in pos.items():\n",
- " secret_value = graph.nodes[node]['secret_value']\n",
- " plt.text(x, y + 0.1, s=f\"Secret: {secret_value}\", horizontalalignment='center')\n",
+ " secret_value = graph.nodes[node][\"secret_value\"]\n",
+ " plt.text(x, y + 0.1, s=f\"Secret: {secret_value}\", horizontalalignment=\"center\")\n",
"\n",
"plt.show()"
]
@@ -526,19 +547,23 @@
"outputs": [],
"source": [
"# Termination message detection\n",
+ "\n",
+ "\n",
"def is_termination_msg(content) -> bool:\n",
" have_content = content.get(\"content\", None) is not None\n",
" if have_content and \"TERMINATE\" in content[\"content\"]:\n",
" return True\n",
" return False\n",
"\n",
+ "\n",
"# Terminates the conversation when TERMINATE is detected.\n",
"user_proxy = autogen.UserProxyAgent(\n",
- " name=\"User_proxy\",\n",
- " system_message=\"Terminator admin.\",\n",
- " code_execution_config=False,\n",
- " is_termination_msg=is_termination_msg,\n",
- " human_input_mode=\"NEVER\")\n",
+ " name=\"User_proxy\",\n",
+ " system_message=\"Terminator admin.\",\n",
+ " code_execution_config=False,\n",
+ " is_termination_msg=is_termination_msg,\n",
+ " human_input_mode=\"NEVER\",\n",
+ ")\n",
"\n",
"agents.append(user_proxy)"
]
@@ -740,12 +765,7 @@
}
],
"source": [
- "group_chat = CustomGroupChat(\n",
- " agents=agents, # Include all agents\n",
- " messages=[],\n",
- " max_round=20,\n",
- " graph=graph\n",
- ")\n",
+ "group_chat = CustomGroupChat(agents=agents, messages=[], max_round=20, graph=graph) # Include all agents\n",
"\n",
"\n",
"# Create the manager\n",
@@ -753,11 +773,13 @@
"\n",
"\n",
"# Initiates the chat with Alice\n",
- "agents[0].initiate_chat(manager, message=\"\"\"\n",
- " There are 9 players in this game, split equally into Teams A, B, C. Therefore each team has 3 players, including the team leader. \n",
- " The task is to find out the sum of chocolate count from all nine players. I will now start with my team. \n",
- " NEXT: A1\"\"\")\n",
- "\n"
+ "agents[0].initiate_chat(\n",
+ " manager,\n",
+ " message=\"\"\"\n",
+ " There are 9 players in this game, split equally into Teams A, B, C. Therefore each team has 3 players, including the team leader.\n",
+ " The task is to find out the sum of chocolate count from all nine players. I will now start with my team.\n",
+ " NEXT: A1\"\"\",\n",
+ ")"
]
}
],
diff --git a/notebook/agentchat_groupchat.ipynb b/notebook/agentchat_groupchat.ipynb
index ff152a4fc..5a928cc06 100644
--- a/notebook/agentchat_groupchat.ipynb
+++ b/notebook/agentchat_groupchat.ipynb
@@ -126,10 +126,10 @@
"source": [
"llm_config = {\"config_list\": config_list_gpt4, \"cache_seed\": 42}\n",
"user_proxy = autogen.UserProxyAgent(\n",
- " name=\"User_proxy\",\n",
- " system_message=\"A human admin.\",\n",
- " code_execution_config={\"last_n_messages\": 2, \"work_dir\": \"groupchat\"},\n",
- " human_input_mode=\"TERMINATE\"\n",
+ " name=\"User_proxy\",\n",
+ " system_message=\"A human admin.\",\n",
+ " code_execution_config={\"last_n_messages\": 2, \"work_dir\": \"groupchat\"},\n",
+ " human_input_mode=\"TERMINATE\",\n",
")\n",
"coder = autogen.AssistantAgent(\n",
" name=\"Coder\",\n",
@@ -259,7 +259,9 @@
}
],
"source": [
- "user_proxy.initiate_chat(manager, message=\"Find a latest paper about gpt-4 on arxiv and find its potential applications in software.\")\n",
+ "user_proxy.initiate_chat(\n",
+ " manager, message=\"Find a latest paper about gpt-4 on arxiv and find its potential applications in software.\"\n",
+ ")\n",
"# type exit to terminate the chat"
]
}
diff --git a/notebook/agentchat_groupchat_RAG.ipynb b/notebook/agentchat_groupchat_RAG.ipynb
index f683987b0..1f5c20d95 100644
--- a/notebook/agentchat_groupchat_RAG.ipynb
+++ b/notebook/agentchat_groupchat_RAG.ipynb
@@ -60,7 +60,11 @@
}
],
"source": [
+ "import chromadb\n",
+ "\n",
"import autogen\n",
+ "from autogen import AssistantAgent\n",
+ "from autogen.agentchat.contrib.retrieve_user_proxy_agent import RetrieveUserProxyAgent\n",
"\n",
"config_list = autogen.config_list_from_json(\n",
" \"OAI_CONFIG_LIST\",\n",
@@ -125,10 +129,6 @@
}
],
"source": [
- "from autogen.agentchat.contrib.retrieve_user_proxy_agent import RetrieveUserProxyAgent\n",
- "from autogen import AssistantAgent\n",
- "import chromadb\n",
- "\n",
"llm_config = {\n",
" \"timeout\": 60,\n",
" \"cache_seed\": 42,\n",
@@ -137,7 +137,11 @@
"}\n",
"\n",
"# autogen.ChatCompletion.start_logging()\n",
- "termination_msg = lambda x: isinstance(x, dict) and \"TERMINATE\" == str(x.get(\"content\", \"\"))[-9:].upper()\n",
+ "\n",
+ "\n",
+ "def termination_msg(x):\n",
+ " return isinstance(x, dict) and \"TERMINATE\" == str(x.get(\"content\", \"\"))[-9:].upper()\n",
+ "\n",
"\n",
"boss = autogen.UserProxyAgent(\n",
" name=\"Boss\",\n",
diff --git a/notebook/agentchat_groupchat_research.ipynb b/notebook/agentchat_groupchat_research.ipynb
index 74c957fb5..23056703c 100644
--- a/notebook/agentchat_groupchat_research.ipynb
+++ b/notebook/agentchat_groupchat_research.ipynb
@@ -117,29 +117,29 @@
" \"timeout\": 120,\n",
"}\n",
"user_proxy = autogen.UserProxyAgent(\n",
- " name=\"Admin\",\n",
- " system_message=\"A human admin. Interact with the planner to discuss the plan. Plan execution needs to be approved by this admin.\",\n",
- " code_execution_config=False,\n",
+ " name=\"Admin\",\n",
+ " system_message=\"A human admin. Interact with the planner to discuss the plan. Plan execution needs to be approved by this admin.\",\n",
+ " code_execution_config=False,\n",
")\n",
"engineer = autogen.AssistantAgent(\n",
" name=\"Engineer\",\n",
" llm_config=gpt4_config,\n",
- " system_message='''Engineer. You follow an approved plan. You write python/shell code to solve tasks. Wrap the code in a code block that specifies the script type. The user can't modify your code. So do not suggest incomplete code which requires others to modify. Don't use a code block if it's not intended to be executed by the executor.\n",
+ " system_message=\"\"\"Engineer. You follow an approved plan. You write python/shell code to solve tasks. Wrap the code in a code block that specifies the script type. The user can't modify your code. So do not suggest incomplete code which requires others to modify. Don't use a code block if it's not intended to be executed by the executor.\n",
"Don't include multiple code blocks in one response. Do not ask others to copy and paste the result. Check the execution result returned by the executor.\n",
"If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code or code changes. If the error can't be fixed or if the task is not solved even after the code is executed successfully, analyze the problem, revisit your assumption, collect additional info you need, and think of a different approach to try.\n",
- "''',\n",
+ "\"\"\",\n",
")\n",
"scientist = autogen.AssistantAgent(\n",
" name=\"Scientist\",\n",
" llm_config=gpt4_config,\n",
- " system_message=\"\"\"Scientist. You follow an approved plan. You are able to categorize papers after seeing their abstracts printed. You don't write code.\"\"\"\n",
+ " system_message=\"\"\"Scientist. You follow an approved plan. You are able to categorize papers after seeing their abstracts printed. You don't write code.\"\"\",\n",
")\n",
"planner = autogen.AssistantAgent(\n",
" name=\"Planner\",\n",
- " system_message='''Planner. Suggest a plan. Revise the plan based on feedback from admin and critic, until admin approval.\n",
+ " system_message=\"\"\"Planner. Suggest a plan. Revise the plan based on feedback from admin and critic, until admin approval.\n",
"The plan may involve an engineer who can write code and a scientist who doesn't write code.\n",
"Explain the plan first. Be clear which step is performed by an engineer, and which step is performed by a scientist.\n",
- "''',\n",
+ "\"\"\",\n",
" llm_config=gpt4_config,\n",
")\n",
"executor = autogen.UserProxyAgent(\n",
@@ -153,7 +153,9 @@
" system_message=\"Critic. Double check plan, claims, code from other agents and provide feedback. Check whether the plan includes adding verifiable info such as source URL.\",\n",
" llm_config=gpt4_config,\n",
")\n",
- "groupchat = autogen.GroupChat(agents=[user_proxy, engineer, scientist, planner, executor, critic], messages=[], max_round=50)\n",
+ "groupchat = autogen.GroupChat(\n",
+ " agents=[user_proxy, engineer, scientist, planner, executor, critic], messages=[], max_round=50\n",
+ ")\n",
"manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=gpt4_config)"
]
},
@@ -526,7 +528,9 @@
}
],
"source": [
- "groupchat_nocritic = autogen.GroupChat(agents=[user_proxy, engineer, scientist, planner, executor], messages=[], max_round=50)\n",
+ "groupchat_nocritic = autogen.GroupChat(\n",
+ " agents=[user_proxy, engineer, scientist, planner, executor], messages=[], max_round=50\n",
+ ")\n",
"for agent in groupchat.agents:\n",
" agent.reset()\n",
"manager_nocritic = autogen.GroupChatManager(groupchat=groupchat_nocritic, llm_config=gpt4_config)\n",
diff --git a/notebook/agentchat_groupchat_vis.ipynb b/notebook/agentchat_groupchat_vis.ipynb
index 78f32914c..5d0549bd7 100644
--- a/notebook/agentchat_groupchat_vis.ipynb
+++ b/notebook/agentchat_groupchat_vis.ipynb
@@ -52,6 +52,11 @@
"metadata": {},
"outputs": [],
"source": [
+ "import matplotlib.pyplot as plt\n",
+ "import pandas as pd\n",
+ "import seaborn as sns\n",
+ "from IPython.display import Image\n",
+ "\n",
"import autogen\n",
"\n",
"config_list_gpt4 = autogen.config_list_from_json(\n",
@@ -134,10 +139,10 @@
"source": [
"llm_config = {\"config_list\": config_list_gpt4, \"cache_seed\": 42}\n",
"user_proxy = autogen.UserProxyAgent(\n",
- " name=\"User_proxy\",\n",
- " system_message=\"A human admin.\",\n",
- " code_execution_config={\"last_n_messages\": 3, \"work_dir\": \"groupchat\"},\n",
- " human_input_mode=\"NEVER\",\n",
+ " name=\"User_proxy\",\n",
+ " system_message=\"A human admin.\",\n",
+ " code_execution_config={\"last_n_messages\": 3, \"work_dir\": \"groupchat\"},\n",
+ " human_input_mode=\"NEVER\",\n",
")\n",
"coder = autogen.AssistantAgent(\n",
" name=\"Coder\", # the default assistant agent is capable of solving problems with code\n",
@@ -155,7 +160,7 @@
"\n",
"YOU MUST PROVIDE A SCORE for each of the above dimensions.\n",
"{bugs: 0, transformation: 0, compliance: 0, type: 0, encoding: 0, aesthetics: 0}\n",
- "Do not suggest code. \n",
+ "Do not suggest code.\n",
"Finally, based on the critique above, suggest a concrete list of actions that the coder should take to improve the code.\n",
"\"\"\",\n",
" llm_config=llm_config,\n",
@@ -670,7 +675,10 @@
}
],
"source": [
- "user_proxy.initiate_chat(manager, message=\"download data from https://raw.githubusercontent.com/uwdata/draco/master/data/cars.csv and plot a visualization that tells us about the relationship between weight and horsepower. Save the plot to a file. Print the fields in a dataset before visualizing it.\")\n",
+ "user_proxy.initiate_chat(\n",
+ " manager,\n",
+ " message=\"download data from https://raw.githubusercontent.com/uwdata/draco/master/data/cars.csv and plot a visualization that tells us about the relationship between weight and horsepower. Save the plot to a file. Print the fields in a dataset before visualizing it.\",\n",
+ ")\n",
"# type exit to terminate the chat"
]
},
@@ -700,9 +708,7 @@
}
],
"source": [
- "from IPython.display import Image\n",
- "\n",
- "Image(filename='groupchat/weight_vs_horsepower.png')"
+ "Image(filename=\"groupchat/weight_vs_horsepower.png\")"
]
},
{
@@ -928,7 +934,10 @@
"critic.reset()\n",
"groupchat = autogen.GroupChat(agents=[user_proxy, coder, critic], messages=[], max_round=20)\n",
"manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)\n",
- "user_proxy.initiate_chat(manager, message=\"download data from https://raw.githubusercontent.com/vega/vega/main/docs/data/seattle-weather.csv and show me a plot that tells me about the amount of each weather . Save the plot to a file. Print the fields in a dataset before visualizing it. Take the feedback from the critic to improve the code.\")\n"
+ "user_proxy.initiate_chat(\n",
+ " manager,\n",
+ " message=\"download data from https://raw.githubusercontent.com/vega/vega/main/docs/data/seattle-weather.csv and show me a plot that tells me about the amount of each weather . Save the plot to a file. Print the fields in a dataset before visualizing it. Take the feedback from the critic to improve the code.\",\n",
+ ")"
]
},
{
@@ -965,26 +974,22 @@
}
],
"source": [
- "import pandas as pd\n",
- "import seaborn as sns\n",
- "import matplotlib.pyplot as plt\n",
- "\n",
"url = \"https://raw.githubusercontent.com/vega/vega/main/docs/data/seattle-weather.csv\"\n",
"data = pd.read_csv(url)\n",
"print(\"Fields in the dataset:\")\n",
"print(data.columns)\n",
"\n",
"# Count the occurrences of each weather type\n",
- "weather_counts = data['weather'].value_counts()\n",
+ "weather_counts = data[\"weather\"].value_counts()\n",
"\n",
"# Create a bar plot of weather occurrences\n",
- "sns.set(style='whitegrid', font_scale=1.2)\n",
+ "sns.set(style=\"whitegrid\", font_scale=1.2)\n",
"plt.figure(figsize=(10, 6))\n",
"weather_plot = sns.barplot(x=weather_counts.index, y=weather_counts.values)\n",
"\n",
"# Add labels and title\n",
- "weather_plot.set(xlabel='Weather Types', ylabel='Number of Days', title='Seattle Weather Types Frequency')\n",
- "plt.savefig('weather_plot.png')\n",
+ "weather_plot.set(xlabel=\"Weather Types\", ylabel=\"Number of Days\", title=\"Seattle Weather Types Frequency\")\n",
+ "plt.savefig(\"weather_plot.png\")\n",
"\n",
"print(\"Plot has been saved to 'weather_plot.png'.\")"
]
@@ -1015,9 +1020,7 @@
}
],
"source": [
- "from IPython.display import Image\n",
- "\n",
- "Image(filename='groupchat/improved_weather_plot.png')"
+ "Image(filename=\"groupchat/improved_weather_plot.png\")"
]
}
],
diff --git a/notebook/agentchat_guidance.ipynb b/notebook/agentchat_guidance.ipynb
index edbed9df1..efd8aa5de 100644
--- a/notebook/agentchat_guidance.ipynb
+++ b/notebook/agentchat_guidance.ipynb
@@ -15,9 +15,12 @@
"metadata": {},
"outputs": [],
"source": [
- "from guidance import models, gen, user, assistant, system\n",
- "from autogen import AssistantAgent, UserProxyAgent, Agent\n",
- "from autogen import config_list_from_json\n",
+ "import re\n",
+ "\n",
+ "from guidance import assistant, gen, models, system, user\n",
+ "from pydantic import BaseModel\n",
+ "\n",
+ "from autogen import Agent, AssistantAgent, UserProxyAgent, config_list_from_json\n",
"\n",
"llm_config = config_list_from_json(\"OAI_CONFIG_LIST\")[0] # use the first config\n",
"gpt = models.OpenAI(\"gpt-4\", api_key=llm_config.get(\"api_key\"))"
@@ -119,8 +122,6 @@
}
],
"source": [
- "import re\n",
- "\n",
"def is_valid_code_block(code):\n",
" pattern = r\"```[\\w\\s]*\\n([\\s\\S]*?)\\n```\"\n",
" match = re.search(pattern, code)\n",
@@ -132,11 +133,11 @@
"\n",
"def generate_structured_response(recipient, messages, sender, config):\n",
" gpt = models.OpenAI(\"gpt-4\", api_key=llm_config.get(\"api_key\"), echo=False)\n",
- " \n",
+ "\n",
" # populate the recipient with the messages from the history\n",
" with system():\n",
" lm = gpt + recipient.system_message\n",
- " \n",
+ "\n",
" for message in messages:\n",
" if message.get(\"role\") == \"user\":\n",
" with user():\n",
@@ -160,22 +161,26 @@
" with assistant():\n",
" lm += \"```\" + gen(name=\"code\")\n",
" response = \"```\" + lm[\"code\"]\n",
- " \n",
+ "\n",
" is_valid = is_valid_code_block(response)\n",
" if not is_valid:\n",
" raise ValueError(f\"Failed to generate a valid code block\\n {response}\")\n",
- " \n",
+ "\n",
" # otherwise, just use the initial response\n",
" else:\n",
" response = lm[\"initial_response\"]\n",
- " \n",
+ "\n",
" return True, response\n",
"\n",
"\n",
"guidance_agent = AssistantAgent(\"guidance_coder\", llm_config=llm_config)\n",
"guidance_agent.register_reply(Agent, generate_structured_response, 1)\n",
- "user_proxy = UserProxyAgent(\"user\", human_input_mode=\"TERMINATE\", code_execution_config={\"work_dir\": \"coding\"},\n",
- " is_termination_msg=lambda msg: \"TERMINATE\" in msg.get(\"content\"))\n",
+ "user_proxy = UserProxyAgent(\n",
+ " \"user\",\n",
+ " human_input_mode=\"TERMINATE\",\n",
+ " code_execution_config={\"work_dir\": \"coding\"},\n",
+ " is_termination_msg=lambda msg: \"TERMINATE\" in msg.get(\"content\"),\n",
+ ")\n",
"user_proxy.initiate_chat(guidance_agent, message=\"Plot and save a chart of nvidia and tsla stock price change YTD.\")"
]
},
@@ -220,25 +225,25 @@
}
],
"source": [
- "from pydantic import BaseModel\n",
- "\n",
"class Response(BaseModel):\n",
" label: str\n",
" explanation: str\n",
"\n",
+ "\n",
"response_prompt_instructions = \"\"\"The label must be a JSON of the format:\n",
"{\n",
" \"label\": str,\n",
" \"explanation\": str\n",
"}\"\"\"\n",
"\n",
+ "\n",
"def generate_structured_response(recipient, messages, sender, config):\n",
" gpt = models.OpenAI(\"gpt-4\", api_key=llm_config.get(\"api_key\"), echo=False)\n",
- " \n",
+ "\n",
" # populate the recipient with the messages from the history\n",
" with system():\n",
" lm = gpt + recipient.system_message\n",
- " \n",
+ "\n",
" for message in messages:\n",
" if message.get(\"role\") == \"user\":\n",
" with user():\n",
@@ -258,7 +263,9 @@
" # if the response contains code, ask the agent to generate a proper code block\n",
" if \"yes\" in lm[\"contains_json\"].lower():\n",
" with user():\n",
- " lm += \"What was that JSON object? Only respond with that valid JSON string. A valid JSON string starts with {\"\n",
+ " lm += (\n",
+ " \"What was that JSON object? Only respond with that valid JSON string. A valid JSON string starts with {\"\n",
+ " )\n",
" with assistant():\n",
" lm += \"{\" + gen(name=\"json\")\n",
" response = \"{\" + lm[\"json\"]\n",
@@ -274,17 +281,21 @@
"\n",
" return True, response\n",
"\n",
+ "\n",
"guidance_agent = AssistantAgent(\"guidance_labeler\", llm_config=llm_config, system_message=\"You are a helpful assistant\")\n",
"guidance_agent.register_reply(Agent, generate_structured_response, 1)\n",
"user_proxy = UserProxyAgent(\"user\", human_input_mode=\"ALWAYS\", code_execution_config=False)\n",
- "user_proxy.initiate_chat(guidance_agent, message=f\"\"\"\n",
+ "user_proxy.initiate_chat(\n",
+ " guidance_agent,\n",
+ " message=f\"\"\"\n",
"Label the TEXT via the following instructions:\n",
- " \n",
+ "\n",
"{response_prompt_instructions}\n",
- " \n",
+ "\n",
"TEXT: what did the fish say when it bumped into a wall? Dam!\n",
"\n",
- "\"\"\")"
+ "\"\"\",\n",
+ ")"
]
}
],
diff --git a/notebook/agentchat_hierarchy_flow_using_select_speaker.ipynb b/notebook/agentchat_hierarchy_flow_using_select_speaker.ipynb
index bcd10fc02..f14b9e769 100644
--- a/notebook/agentchat_hierarchy_flow_using_select_speaker.ipynb
+++ b/notebook/agentchat_hierarchy_flow_using_select_speaker.ipynb
@@ -77,7 +77,13 @@
}
],
"source": [
+ "import random\n",
+ "from typing import Dict, List\n",
+ "\n",
"import autogen\n",
+ "from autogen.agentchat.agent import Agent\n",
+ "from autogen.agentchat.assistant_agent import AssistantAgent\n",
+ "from autogen.agentchat.groupchat import GroupChat\n",
"\n",
"print(autogen.__version__)\n",
"\n",
@@ -91,11 +97,11 @@
"\n",
"# Contributor's config - Please replace with your own, I have replaced mine with an Azure OpenAI endpoint.\n",
"config_list_gpt4 = autogen.config_list_from_json(\n",
- " \"OAI_CONFIG_LIST\",\n",
- " filter_dict={\n",
- " \"model\": [\"gpt-4\"],\n",
- " },\n",
- " )"
+ " \"OAI_CONFIG_LIST\",\n",
+ " filter_dict={\n",
+ " \"model\": [\"gpt-4\"],\n",
+ " },\n",
+ ")"
]
},
{
@@ -166,61 +172,51 @@
"llm_config = {\"config_list\": config_list_gpt4, \"cache_seed\": 42}"
]
},
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [],
- "source": [
- "from autogen.agentchat.groupchat import GroupChat\n",
- "from autogen.agentchat.agent import Agent\n",
- "from autogen.agentchat.assistant_agent import AssistantAgent"
- ]
- },
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
- "import random\n",
- "from typing import List, Dict\n",
- "\n",
"class CustomGroupChat(GroupChat):\n",
" def __init__(self, agents, messages, max_round=10):\n",
" super().__init__(agents, messages, max_round)\n",
" self.previous_speaker = None # Keep track of the previous speaker\n",
- " \n",
+ "\n",
" def select_speaker(self, last_speaker: Agent, selector: AssistantAgent):\n",
" # Check if last message suggests a next speaker or termination\n",
" last_message = self.messages[-1] if self.messages else None\n",
" if last_message:\n",
- " if 'NEXT:' in last_message['content']:\n",
- " suggested_next = last_message['content'].split('NEXT: ')[-1].strip()\n",
- " print(f'Extracted suggested_next = {suggested_next}')\n",
+ " if \"NEXT:\" in last_message[\"content\"]:\n",
+ " suggested_next = last_message[\"content\"].split(\"NEXT: \")[-1].strip()\n",
+ " print(f\"Extracted suggested_next = {suggested_next}\")\n",
" try:\n",
" return self.agent_by_name(suggested_next)\n",
" except ValueError:\n",
" pass # If agent name is not valid, continue with normal selection\n",
- " elif 'TERMINATE' in last_message['content']:\n",
+ " elif \"TERMINATE\" in last_message[\"content\"]:\n",
" try:\n",
- " return self.agent_by_name('User_proxy')\n",
+ " return self.agent_by_name(\"User_proxy\")\n",
" except ValueError:\n",
" pass # If 'User_proxy' is not a valid name, continue with normal selection\n",
- " \n",
- " team_leader_names = [agent.name for agent in self.agents if agent.name.endswith('1')]\n",
+ "\n",
+ " team_leader_names = [agent.name for agent in self.agents if agent.name.endswith(\"1\")]\n",
"\n",
" if last_speaker.name in team_leader_names:\n",
" team_letter = last_speaker.name[0]\n",
" possible_next_speakers = [\n",
- " agent for agent in self.agents if (agent.name.startswith(team_letter) or agent.name in team_leader_names) \n",
- " and agent != last_speaker and agent != self.previous_speaker\n",
+ " agent\n",
+ " for agent in self.agents\n",
+ " if (agent.name.startswith(team_letter) or agent.name in team_leader_names)\n",
+ " and agent != last_speaker\n",
+ " and agent != self.previous_speaker\n",
" ]\n",
" else:\n",
" team_letter = last_speaker.name[0]\n",
" possible_next_speakers = [\n",
- " agent for agent in self.agents if agent.name.startswith(team_letter) \n",
- " and agent != last_speaker and agent != self.previous_speaker\n",
+ " agent\n",
+ " for agent in self.agents\n",
+ " if agent.name.startswith(team_letter) and agent != last_speaker and agent != self.previous_speaker\n",
" ]\n",
"\n",
" self.previous_speaker = last_speaker\n",
@@ -229,7 +225,7 @@
" next_speaker = random.choice(possible_next_speakers)\n",
" return next_speaker\n",
" else:\n",
- " return None\n"
+ " return None"
]
},
{
@@ -239,6 +235,8 @@
"outputs": [],
"source": [
"# Termination message detection\n",
+ "\n",
+ "\n",
"def is_termination_msg(content) -> bool:\n",
" have_content = content.get(\"content\", None) is not None\n",
" if have_content and \"TERMINATE\" in content[\"content\"]:\n",
@@ -319,33 +317,44 @@
"source": [
"# Initialization\n",
"agents_A = [\n",
- " AssistantAgent(name='A1', \n",
- " system_message=\"You are a team leader A1, your team consists of A2, A3. You can talk to the other team leader B1, whose team member is B2.\",\n",
- " llm_config=llm_config),\n",
- " AssistantAgent(name='A2', \n",
- " system_message=\"You are team member A2, you know the secret value of x but not y, x = 9. Tell others x to cooperate.\",\n",
- " llm_config=llm_config),\n",
- " AssistantAgent(name='A3', \n",
- " system_message=\"You are team member A3, You know the secret value of y but not x, y = 5. Tell others y to cooperate.\",\n",
- " llm_config=llm_config)\n",
+ " AssistantAgent(\n",
+ " name=\"A1\",\n",
+ " system_message=\"You are a team leader A1, your team consists of A2, A3. You can talk to the other team leader B1, whose team member is B2.\",\n",
+ " llm_config=llm_config,\n",
+ " ),\n",
+ " AssistantAgent(\n",
+ " name=\"A2\",\n",
+ " system_message=\"You are team member A2, you know the secret value of x but not y, x = 9. Tell others x to cooperate.\",\n",
+ " llm_config=llm_config,\n",
+ " ),\n",
+ " AssistantAgent(\n",
+ " name=\"A3\",\n",
+ " system_message=\"You are team member A3, You know the secret value of y but not x, y = 5. Tell others y to cooperate.\",\n",
+ " llm_config=llm_config,\n",
+ " ),\n",
"]\n",
"\n",
"agents_B = [\n",
- " AssistantAgent(name='B1', \n",
- " system_message=\"You are a team leader B1, your team consists of B2. You can talk to the other team leader A1, whose team member is A2, A3. Use NEXT: A1 to suggest talking to A1.\",\n",
- " llm_config=llm_config),\n",
- " AssistantAgent(name='B2', \n",
- " system_message=\"You are team member B2. Your task is to find out the value of x and y and compute the product. Once you have the answer, say out the answer and append a new line with TERMINATE.\",\n",
- " llm_config=llm_config)\n",
+ " AssistantAgent(\n",
+ " name=\"B1\",\n",
+ " system_message=\"You are a team leader B1, your team consists of B2. You can talk to the other team leader A1, whose team member is A2, A3. Use NEXT: A1 to suggest talking to A1.\",\n",
+ " llm_config=llm_config,\n",
+ " ),\n",
+ " AssistantAgent(\n",
+ " name=\"B2\",\n",
+ " system_message=\"You are team member B2. Your task is to find out the value of x and y and compute the product. Once you have the answer, say out the answer and append a new line with TERMINATE.\",\n",
+ " llm_config=llm_config,\n",
+ " ),\n",
"]\n",
"\n",
"# Terminates the conversation when TERMINATE is detected.\n",
"user_proxy = autogen.UserProxyAgent(\n",
- " name=\"User_proxy\",\n",
- " system_message=\"Terminator admin.\",\n",
- " code_execution_config=False,\n",
- " is_termination_msg=is_termination_msg,\n",
- " human_input_mode=\"NEVER\")\n",
+ " name=\"User_proxy\",\n",
+ " system_message=\"Terminator admin.\",\n",
+ " code_execution_config=False,\n",
+ " is_termination_msg=is_termination_msg,\n",
+ " human_input_mode=\"NEVER\",\n",
+ ")\n",
"\n",
"list_of_agents = agents_A + agents_B\n",
"list_of_agents.append(user_proxy)\n",
@@ -353,13 +362,18 @@
"# Create CustomGroupChat\n",
"group_chat = CustomGroupChat(\n",
" agents=list_of_agents, # Include all agents\n",
- " messages=['Everyone cooperate and help agent B2 in his task. Team A has A1, A2, A3. Team B has B1, B2. Only members of the same team can talk to one another. Only team leaders (names ending with 1) can talk amongst themselves. You must use \"NEXT: B1\" to suggest talking to B1 for example; You can suggest only one person, you cannot suggest yourself or the previous speaker; You can also dont suggest anyone.'],\n",
- " max_round=30\n",
+ " messages=[\n",
+ " 'Everyone cooperate and help agent B2 in his task. Team A has A1, A2, A3. Team B has B1, B2. Only members of the same team can talk to one another. Only team leaders (names ending with 1) can talk amongst themselves. You must use \"NEXT: B1\" to suggest talking to B1 for example; You can suggest only one person, you cannot suggest yourself or the previous speaker; You can also dont suggest anyone.'\n",
+ " ],\n",
+ " max_round=30,\n",
")\n",
"\n",
"\n",
"# Create the manager\n",
- "llm_config = {\"config_list\": config_list_gpt4, \"cache_seed\": None} # cache_seed is None because we want to observe if there is any communication pattern difference if we reran the group chat.\n",
+ "llm_config = {\n",
+ " \"config_list\": config_list_gpt4,\n",
+ " \"cache_seed\": None,\n",
+ "} # cache_seed is None because we want to observe if there is any communication pattern difference if we reran the group chat.\n",
"manager = autogen.GroupChatManager(groupchat=group_chat, llm_config=llm_config)\n",
"\n",
"\n",
diff --git a/notebook/agentchat_human_feedback.ipynb b/notebook/agentchat_human_feedback.ipynb
index 79b72c581..4bf073dea 100644
--- a/notebook/agentchat_human_feedback.ipynb
+++ b/notebook/agentchat_human_feedback.ipynb
@@ -64,6 +64,8 @@
"metadata": {},
"outputs": [],
"source": [
+ "import json\n",
+ "\n",
"import autogen\n",
"\n",
"config_list = autogen.config_list_from_json(\"OAI_CONFIG_LIST\")"
@@ -125,14 +127,14 @@
" llm_config={\n",
" \"cache_seed\": 41,\n",
" \"config_list\": config_list,\n",
- " }\n",
+ " },\n",
")\n",
"# create a UserProxyAgent instance named \"user_proxy\"\n",
"user_proxy = autogen.UserProxyAgent(\n",
" name=\"user_proxy\",\n",
" human_input_mode=\"ALWAYS\",\n",
" is_termination_msg=lambda x: x.get(\"content\", \"\").rstrip().endswith(\"TERMINATE\"),\n",
- ")\n"
+ ")"
]
},
{
@@ -328,7 +330,7 @@
],
"source": [
"math_problem_to_solve = \"\"\"\n",
- "Find $a + b + c$, given that $x+y \\\\neq -1$ and \n",
+ "Find $a + b + c$, given that $x+y \\\\neq -1$ and\n",
"\\\\begin{align}\n",
"\tax + by + c & = x + 7,\\\\\n",
"\ta + bx + cy & = 2x + 6y,\\\\\n",
@@ -367,8 +369,6 @@
"metadata": {},
"outputs": [],
"source": [
- "import json\n",
- "\n",
"json.dump(user_proxy.chat_messages[assistant], open(\"conversations.json\", \"w\"), indent=2)"
]
}
diff --git a/notebook/agentchat_inception_function.ipynb b/notebook/agentchat_inception_function.ipynb
index 1a3bfb8c8..f968ebdc9 100644
--- a/notebook/agentchat_inception_function.ipynb
+++ b/notebook/agentchat_inception_function.ipynb
@@ -174,9 +174,10 @@
}
],
"source": [
+ "import json\n",
+ "\n",
"from autogen import AssistantAgent, UserProxyAgent, config_list_from_json\n",
"from autogen.code_utils import execute_code\n",
- "import json\n",
"\n",
"config_list = config_list_from_json(\n",
" \"OAI_CONFIG_LIST\",\n",
@@ -203,7 +204,7 @@
" },\n",
" \"arguments\": {\n",
" \"type\": \"string\",\n",
- " \"description\": \"JSON schema of arguments encoded as a string. For example: { \\\"url\\\": { \\\"type\\\": \\\"string\\\", \\\"description\\\": \\\"The URL\\\", }}\",\n",
+ " \"description\": 'JSON schema of arguments encoded as a string. For example: { \"url\": { \"type\": \"string\", \"description\": \"The URL\", }}',\n",
" },\n",
" \"packages\": {\n",
" \"type\": \"string\",\n",
@@ -222,26 +223,28 @@
" \"request_timeout\": 120,\n",
"}\n",
"\n",
+ "\n",
"def define_function(name, description, arguments, packages, code):\n",
" json_args = json.loads(arguments)\n",
" function_config = {\n",
" \"name\": name,\n",
" \"description\": description,\n",
- " \"parameters\": { \"type\": \"object\", \"properties\": json_args },\n",
+ " \"parameters\": {\"type\": \"object\", \"properties\": json_args},\n",
" # TODO Make all arguments required\n",
" \"required\": [\"url\"],\n",
" }\n",
" llm_config[\"functions\"] = llm_config[\"functions\"] + [function_config]\n",
- " user_proxy.register_function(\n",
- " function_map={\n",
- " name: lambda **args: execute_func(name, packages, code, **args)\n",
- " }\n",
- " )\n",
- " assistant.update_function_signature(function_config, is_remove= False)\n",
+ " user_proxy.register_function(function_map={name: lambda **args: execute_func(name, packages, code, **args)})\n",
+ " assistant.update_function_signature(function_config, is_remove=False)\n",
" return f\"A function has been added to the context of this conversation.\\nDescription: {description}\"\n",
"\n",
+ "\n",
"def execute_func(name, packages, code, **args):\n",
- " pip_install = f\"\"\"print(\"Installing package: {packages}\")\\nsubprocess.run([\"pip\", \"-qq\", \"install\", \"{packages}\"])\"\"\" if packages else ''\n",
+ " pip_install = (\n",
+ " f\"\"\"print(\"Installing package: {packages}\")\\nsubprocess.run([\"pip\", \"-qq\", \"install\", \"{packages}\"])\"\"\"\n",
+ " if packages\n",
+ " else \"\"\n",
+ " )\n",
" str = f\"\"\"\n",
"import subprocess\n",
"{pip_install}\n",
@@ -256,6 +259,7 @@
" print(f\"Result: {result}\")\n",
" return result\n",
"\n",
+ "\n",
"def _is_termination_msg(message):\n",
" \"\"\"Check if a message is a termination message.\"\"\"\n",
" if isinstance(message, dict):\n",
@@ -264,6 +268,7 @@
" return False\n",
" return message.rstrip().endswith(\"TERMINATE\")\n",
"\n",
+ "\n",
"assistant = AssistantAgent(\n",
" name=\"chatbot\",\n",
" system_message=\"\"\"You are an assistant.\n",
@@ -279,25 +284,25 @@
" code_execution_config=False,\n",
" is_termination_msg=_is_termination_msg,\n",
" default_auto_reply=\"Reply TERMINATE when the initial request has been fulfilled.\",\n",
- " human_input_mode=\"NEVER\")\n",
- "\n",
- "user_proxy.register_function(\n",
- " function_map={\n",
- " \"define_function\": define_function\n",
- " }\n",
+ " human_input_mode=\"NEVER\",\n",
")\n",
"\n",
+ "user_proxy.register_function(function_map={\"define_function\": define_function})\n",
+ "\n",
"# user_proxy.initiate_chat(\n",
"# assistant, message=\"What functions do you know about?\")\n",
"\n",
"user_proxy.initiate_chat(\n",
- " assistant, message=\"Define a function that gets a URL, then prints the response body.\\nReply TERMINATE when the function is defined.\")\n",
+ " assistant,\n",
+ " message=\"Define a function that gets a URL, then prints the response body.\\nReply TERMINATE when the function is defined.\",\n",
+ ")\n",
"\n",
"# user_proxy.initiate_chat(\n",
"# assistant, message=\"List functions do you know about.\")\n",
"\n",
"user_proxy.initiate_chat(\n",
- " assistant, message=\"Print the response body of https://echo.free.beeceptor.com/\\nUse the functions you know about.\")\n"
+ " assistant, message=\"Print the response body of https://echo.free.beeceptor.com/\\nUse the functions you know about.\"\n",
+ ")"
]
}
],
diff --git a/notebook/agentchat_langchain.ipynb b/notebook/agentchat_langchain.ipynb
index 116cce46b..14d749c0e 100644
--- a/notebook/agentchat_langchain.ipynb
+++ b/notebook/agentchat_langchain.ipynb
@@ -1,704 +1,708 @@
{
- "cells": [
- {
- "attachments": {},
- "cell_type": "markdown",
- "id": "ae1f50ec",
- "metadata": {
- "id": "ae1f50ec"
- },
- "source": [
- "<a href=\"https://colab.research.google.com/github/microsoft/autogen/blob/main/notebook/agentchat_function_call.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
- ]
- },
- {
- "attachments": {},
- "cell_type": "markdown",
- "id": "9a71fa36",
- "metadata": {
- "id": "9a71fa36"
- },
- "source": [
- "# Auto Generated Agent Chat: Task Solving with Langchain Provided Tools as Functions\n",
- "\n",
- "AutoGen offers conversable agents powered by LLM, tool, or human, which can be used to perform tasks collectively via automated chat. This framework allows tool use and human participants through multi-agent conversation. Please find documentation about this feature [here](https://microsoft.github.io/autogen/docs/Use-Cases/agent_chat).\n",
- "\n",
- "In this notebook, we demonstrate how to use `AssistantAgent` and `UserProxyAgent` to make function calls with the new feature of OpenAI models (in model version 0613) with a set of Langchain-provided tools and toolkits, to demonstrate how to leverage the 35+ tools available. \n",
- "A specified prompt and function configs must be passed to `AssistantAgent` to initialize the agent. The corresponding functions must be passed to `UserProxyAgent`, which will execute any function calls made by `AssistantAgent`. Besides this requirement of matching descriptions with functions, we recommend checking the system message in the `AssistantAgent` to ensure the instructions align with the function call descriptions.\n",
- "\n",
- "## Requirements\n",
- "\n",
- "AutoGen requires `Python>=3.8`. To run this notebook example, please install `pyautogen` and `Langchain`:\n",
- "```bash\n",
- "pip install pyautogen Langchain\n",
- "```"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "2b803c17",
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "2b803c17",
- "outputId": "2e12aa3f-e46c-4b82-cc2e-1495f70a2961"
- },
- "outputs": [],
- "source": [
- "%pip install \"pyautogen>=0.2.3\" Langchain"
- ]
- },
- {
- "attachments": {},
- "cell_type": "markdown",
- "id": "5ebd2397",
- "metadata": {
- "id": "5ebd2397"
- },
- "source": [
- "## Set your API Endpoint\n",
- "\n",
- "The [`config_list_from_models`](https://microsoft.github.io/autogen/docs/reference/oai/openai_utils#config_list_from_models) function tries to create a list of configurations using Azure OpenAI endpoints and OpenAI endpoints for the provided list of models. It assumes the api keys and api bases are stored in the corresponding environment variables or local txt files:\n",
- "\n",
- "- OpenAI API key: os.environ[\"OPENAI_API_KEY\"] or `openai_api_key_file=\"key_openai.txt\"`.\n",
- "- Azure OpenAI API key: os.environ[\"AZURE_OPENAI_API_KEY\"] or `aoai_api_key_file=\"key_aoai.txt\"`. Multiple keys can be stored, one per line.\n",
- "- Azure OpenAI API base: os.environ[\"AZURE_OPENAI_API_BASE\"] or `aoai_api_base_file=\"base_aoai.txt\"`. Multiple bases can be stored, one per line.\n",
- "\n",
- "It's OK to have only the OpenAI API key, or only the Azure OpenAI API key + base.\n",
- "If you open this notebook in google colab, you can upload your files by clicking the file icon on the left panel and then choosing \"upload file\" icon.\n",
- "\n",
- "The following code excludes Azure OpenAI endpoints from the config list because some endpoints don't support functions yet. Remove the `exclude` argument if they do."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "dca301a4",
- "metadata": {
- "id": "dca301a4"
- },
- "outputs": [],
- "source": [
- "import autogen\n",
- "\n",
- "config_list = autogen.config_list_from_json(\n",
- " \"OAI_CONFIG_LIST\",\n",
- " filter_dict={\n",
- " \"model\": [\"gpt-4\", \"gpt-3.5-turbo\", \"gpt-3.5-turbo-16k\"],\n",
- " },\n",
- ")"
- ]
- },
- {
- "attachments": {},
- "cell_type": "markdown",
- "id": "dd1cda81",
- "metadata": {},
- "source": [
- "It first looks for environment variable \"OAI_CONFIG_LIST\" which needs to be a valid json string. If that variable is not found, it then looks for a json file named \"OAI_CONFIG_LIST\". It filters the configs by models (you can filter by other keys as well). Only the models with matching names are kept in the list based on the filter condition.\n",
- "\n",
- "The config list looks like the following:\n",
- "```python\n",
- "config_list = [\n",
- " {\n",
- " 'model': 'gpt-4',\n",
- " 'api_key': '<your OpenAI API key here>',\n",
- " },\n",
- " {\n",
- " 'model': 'gpt-3.5-turbo',\n",
- " 'api_key': '<your Azure OpenAI API key here>',\n",
- " 'base_url': '<your Azure OpenAI API base here>',\n",
- " 'api_type': 'azure',\n",
- " 'api_version': '2023-08-01-preview',\n",
- " },\n",
- " {\n",
- " 'model': 'gpt-3.5-turbo-16k',\n",
- " 'api_key': '<your Azure OpenAI API key here>',\n",
- " 'base_url': '<your Azure OpenAI API base here>',\n",
- " 'api_type': 'azure',\n",
- " 'api_version': '2023-08-01-preview',\n",
- " },\n",
- "]\n",
- "```\n",
- "\n",
- "You can set the value of config_list in any way you prefer. Please refer to this [notebook](https://github.com/microsoft/autogen/blob/main/notebook/oai_openai_utils.ipynb) for full code examples of the different methods."
- ]
- },
- {
- "attachments": {},
- "cell_type": "markdown",
- "id": "2b9526e7",
- "metadata": {
- "id": "2b9526e7"
- },
- "source": [
- "## Making Function Calls\n",
- "\n",
- "In this example, we demonstrate function call execution with `AssistantAgent` and `UserProxyAgent`. With the default system prompt of `AssistantAgent`, we allow the LLM assistant to perform tasks with code, and the `UserProxyAgent` would extract code blocks from the LLM response and execute them. With the new \"function_call\" feature, we define functions and specify the description of the function in the OpenAI config for the `AssistantAgent`. Then we register the functions in `UserProxyAgent`.\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "qCzNbbVajvpc",
- "metadata": {
- "id": "qCzNbbVajvpc"
- },
- "outputs": [],
- "source": [
- "# Import things that are needed generically\n",
- "from langchain.pydantic_v1 import BaseModel, Field\n",
- "from langchain.tools import BaseTool\n",
- "from typing import Optional, Type\n",
- "import math\n",
- "import os\n",
- "\n",
- "class CircumferenceToolInput(BaseModel):\n",
- " radius: float = Field()\n",
- "\n",
- "class CircumferenceTool(BaseTool):\n",
- " name = \"circumference_calculator\"\n",
- " description = \"Use this tool when you need to calculate a circumference using the radius of a circle\"\n",
- " args_schema: Type[BaseModel] = CircumferenceToolInput\n",
- "\n",
- " def _run(self, radius: float):\n",
- " return float(radius) * 2.0 * math.pi\n",
- " \n",
- "def get_file_path_of_example():\n",
- " # Get the current working directory\n",
- " current_dir = os.getcwd()\n",
- "\n",
- " # Go one directory up\n",
- " parent_dir = os.path.dirname(current_dir)\n",
- "\n",
- " # Move to the target directory\n",
- " target_folder = os.path.join(parent_dir, \"test\")\n",
- "\n",
- " # Construct the path to your target file\n",
- " file_path = os.path.join(target_folder, \"test_files/radius.txt\")\n",
- " \n",
- " return file_path"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "COlL5_98atDs",
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "COlL5_98atDs",
- "outputId": "24ce236d-8993-4a69-99e2-65453574d61e"
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "user_proxy (to chatbot):\n",
- "\n",
- "Read the file with the path 'Test.txt', then calculate the circumference of a circle that has a radius of that files contents.\n",
- "\n",
- "--------------------------------------------------------------------------------\n",
- "chatbot (to user_proxy):\n",
- "\n",
- "***** Suggested function Call: read_file *****\n",
- "Arguments: \n",
- "{\n",
- " \"file_path\": \"Test.txt\"\n",
- "}\n",
- "**********************************************\n",
- "\n",
- "--------------------------------------------------------------------------------\n",
- "\n",
- ">>>>>>>> EXECUTING FUNCTION read_file...\n",
- "user_proxy (to chatbot):\n",
- "\n",
- "***** Response from calling function \"read_file\" *****\n",
- "7.81mm\n",
- "******************************************************\n",
- "\n",
- "--------------------------------------------------------------------------------\n",
- "chatbot (to user_proxy):\n",
- "\n",
- "***** Suggested function Call: circumference_calculator *****\n",
- "Arguments: \n",
- "{\n",
- " \"radius\": 7.81\n",
- "}\n",
- "*************************************************************\n",
- "\n",
- "--------------------------------------------------------------------------------\n",
- "\n",
- ">>>>>>>> EXECUTING FUNCTION circumference_calculator...\n",
- "user_proxy (to chatbot):\n",
- "\n",
- "***** Response from calling function \"circumference_calculator\" *****\n",
- "49.071677249072565\n",
- "*********************************************************************\n",
- "\n",
- "--------------------------------------------------------------------------------\n",
- "chatbot (to user_proxy):\n",
- "\n",
- "The circumference of a circle with a radius of 7.81mm is approximately 49.071mm.\n",
- "\n",
- "--------------------------------------------------------------------------------\n",
- "user_proxy (to chatbot):\n",
- "\n",
- "\n",
- "\n",
- "--------------------------------------------------------------------------------\n",
- "chatbot (to user_proxy):\n",
- "\n",
- "TERMINATE\n",
- "\n",
- "--------------------------------------------------------------------------------\n"
- ]
- }
- ],
- "source": [
- "from langchain.tools.file_management.read import ReadFileTool\n",
- "\n",
- "# Define a function to generate llm_config from a LangChain tool\n",
- "def generate_llm_config(tool):\n",
- " # Define the function schema based on the tool's args_schema\n",
- " function_schema = {\n",
- " \"name\": tool.name.lower().replace (' ', '_'),\n",
- " \"description\": tool.description,\n",
- " \"parameters\": {\n",
- " \"type\": \"object\",\n",
- " \"properties\": {},\n",
- " \"required\": [],\n",
- " },\n",
- " }\n",
- "\n",
- " if tool.args is not None:\n",
- " function_schema[\"parameters\"][\"properties\"] = tool.args\n",
- "\n",
- " return function_schema\n",
- "\n",
- "# Instantiate the ReadFileTool\n",
- "read_file_tool = ReadFileTool()\n",
- "custom_tool = CircumferenceTool()\n",
- "\n",
- "# Construct the llm_config\n",
- "llm_config = {\n",
- " #Generate functions config for the Tool\n",
- " \"functions\":[\n",
- " generate_llm_config(custom_tool),\n",
- " generate_llm_config(read_file_tool),\n",
- " ],\n",
- " \"config_list\": config_list, # Assuming you have this defined elsewhere\n",
- " \"timeout\": 120,\n",
- "}\n",
- "\n",
- "user_proxy = autogen.UserProxyAgent(\n",
- " name=\"user_proxy\",\n",
- " is_termination_msg=lambda x: x.get(\"content\", \"\") and x.get(\"content\", \"\").rstrip().endswith(\"TERMINATE\"),\n",
- " human_input_mode=\"NEVER\",\n",
- " max_consecutive_auto_reply=10,\n",
- " code_execution_config={\"work_dir\": \"coding\"},\n",
- ")\n",
- "\n",
- "# Register the tool and start the conversation\n",
- "user_proxy.register_function(\n",
- " function_map={\n",
- " custom_tool.name: custom_tool._run,\n",
- " read_file_tool.name: read_file_tool._run,\n",
- " }\n",
- ")\n",
- "\n",
- "chatbot = autogen.AssistantAgent(\n",
- " name=\"chatbot\",\n",
- " system_message=\"For coding tasks, only use the functions you have been provided with. Reply TERMINATE when the task is done.\",\n",
- " llm_config=llm_config,\n",
- ")\n",
- "\n",
- "user_proxy.initiate_chat(\n",
- " chatbot,\n",
- " message=f\"Read the file with the path {get_file_path_of_example()}, then calculate the circumference of a circle that has a radius of that files contents.\", #7.81mm in the file\n",
- " llm_config=llm_config,\n",
- ")"
- ]
- },
- {
- "attachments": {},
- "cell_type": "markdown",
- "id": "11cc4e60",
- "metadata": {},
- "source": [
- "# A PySpark Example"
- ]
+ "cells": [
+ {
+ "attachments": {},
+ "cell_type": "markdown",
+ "id": "ae1f50ec",
+ "metadata": {
+ "id": "ae1f50ec"
+ },
+ "source": [
+ "<a href=\"https://colab.research.google.com/github/microsoft/autogen/blob/main/notebook/agentchat_function_call.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
+ ]
+ },
+ {
+ "attachments": {},
+ "cell_type": "markdown",
+ "id": "9a71fa36",
+ "metadata": {
+ "id": "9a71fa36"
+ },
+ "source": [
+ "# Auto Generated Agent Chat: Task Solving with Langchain Provided Tools as Functions\n",
+ "\n",
+ "AutoGen offers conversable agents powered by LLM, tool, or human, which can be used to perform tasks collectively via automated chat. This framework allows tool use and human participants through multi-agent conversation. Please find documentation about this feature [here](https://microsoft.github.io/autogen/docs/Use-Cases/agent_chat).\n",
+ "\n",
+ "In this notebook, we demonstrate how to use `AssistantAgent` and `UserProxyAgent` to make function calls with the new feature of OpenAI models (in model version 0613) with a set of Langchain-provided tools and toolkits, to demonstrate how to leverage the 35+ tools available. \n",
+ "A specified prompt and function configs must be passed to `AssistantAgent` to initialize the agent. The corresponding functions must be passed to `UserProxyAgent`, which will execute any function calls made by `AssistantAgent`. Besides this requirement of matching descriptions with functions, we recommend checking the system message in the `AssistantAgent` to ensure the instructions align with the function call descriptions.\n",
+ "\n",
+ "## Requirements\n",
+ "\n",
+ "AutoGen requires `Python>=3.8`. To run this notebook example, please install `pyautogen` and `Langchain`:\n",
+ "```bash\n",
+ "pip install pyautogen Langchain\n",
+ "```"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "2b803c17",
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
},
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "Y-ozf9EFCegw",
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "Y-ozf9EFCegw",
- "outputId": "db7b73a8-6129-4dfb-9d5c-ac3536f310d7"
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Requirement already satisfied: pyspark in /usr/local/lib/python3.10/dist-packages (3.5.0)\n",
- "Requirement already satisfied: py4j==0.10.9.7 in /usr/local/lib/python3.10/dist-packages (from pyspark) (0.10.9.7)\n"
- ]
- }
- ],
- "source": [
- "%pip install pyspark"
- ]
+ "id": "2b803c17",
+ "outputId": "2e12aa3f-e46c-4b82-cc2e-1495f70a2961"
+ },
+ "outputs": [],
+ "source": [
+ "%pip install \"pyautogen>=0.2.3\" Langchain"
+ ]
+ },
+ {
+ "attachments": {},
+ "cell_type": "markdown",
+ "id": "5ebd2397",
+ "metadata": {
+ "id": "5ebd2397"
+ },
+ "source": [
+ "## Set your API Endpoint\n",
+ "\n",
+ "The [`config_list_from_models`](https://microsoft.github.io/autogen/docs/reference/oai/openai_utils#config_list_from_models) function tries to create a list of configurations using Azure OpenAI endpoints and OpenAI endpoints for the provided list of models. It assumes the api keys and api bases are stored in the corresponding environment variables or local txt files:\n",
+ "\n",
+ "- OpenAI API key: os.environ[\"OPENAI_API_KEY\"] or `openai_api_key_file=\"key_openai.txt\"`.\n",
+ "- Azure OpenAI API key: os.environ[\"AZURE_OPENAI_API_KEY\"] or `aoai_api_key_file=\"key_aoai.txt\"`. Multiple keys can be stored, one per line.\n",
+ "- Azure OpenAI API base: os.environ[\"AZURE_OPENAI_API_BASE\"] or `aoai_api_base_file=\"base_aoai.txt\"`. Multiple bases can be stored, one per line.\n",
+ "\n",
+ "It's OK to have only the OpenAI API key, or only the Azure OpenAI API key + base.\n",
+ "If you open this notebook in google colab, you can upload your files by clicking the file icon on the left panel and then choosing \"upload file\" icon.\n",
+ "\n",
+ "The following code excludes Azure OpenAI endpoints from the config list because some endpoints don't support functions yet. Remove the `exclude` argument if they do."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "dca301a4",
+ "metadata": {
+ "id": "dca301a4"
+ },
+ "outputs": [],
+ "source": [
+ "import math\n",
+ "import os\n",
+ "from typing import Optional, Type\n",
+ "\n",
+ "# Starndard Langchain example\n",
+ "from langchain.agents import create_spark_sql_agent\n",
+ "from langchain.agents.agent_toolkits import SparkSQLToolkit\n",
+ "from langchain.chat_models import ChatOpenAI\n",
+ "\n",
+ "# Import things that are needed generically\n",
+ "from langchain.pydantic_v1 import BaseModel, Field\n",
+ "from langchain.tools import BaseTool\n",
+ "from langchain.tools.file_management.read import ReadFileTool\n",
+ "from langchain.utilities.spark_sql import SparkSQL\n",
+ "from pyspark.sql import SparkSession\n",
+ "\n",
+ "import autogen\n",
+ "\n",
+ "config_list = autogen.config_list_from_json(\n",
+ " \"OAI_CONFIG_LIST\",\n",
+ " filter_dict={\n",
+ " \"model\": [\"gpt-4\", \"gpt-3.5-turbo\", \"gpt-3.5-turbo-16k\"],\n",
+ " },\n",
+ ")"
+ ]
+ },
+ {
+ "attachments": {},
+ "cell_type": "markdown",
+ "id": "dd1cda81",
+ "metadata": {},
+ "source": [
+ "It first looks for environment variable \"OAI_CONFIG_LIST\" which needs to be a valid json string. If that variable is not found, it then looks for a json file named \"OAI_CONFIG_LIST\". It filters the configs by models (you can filter by other keys as well). Only the models with matching names are kept in the list based on the filter condition.\n",
+ "\n",
+ "The config list looks like the following:\n",
+ "```python\n",
+ "config_list = [\n",
+ " {\n",
+ " 'model': 'gpt-4',\n",
+ " 'api_key': '<your OpenAI API key here>',\n",
+ " },\n",
+ " {\n",
+ " 'model': 'gpt-3.5-turbo',\n",
+ " 'api_key': '<your Azure OpenAI API key here>',\n",
+ " 'base_url': '<your Azure OpenAI API base here>',\n",
+ " 'api_type': 'azure',\n",
+ " 'api_version': '2023-08-01-preview',\n",
+ " },\n",
+ " {\n",
+ " 'model': 'gpt-3.5-turbo-16k',\n",
+ " 'api_key': '<your Azure OpenAI API key here>',\n",
+ " 'base_url': '<your Azure OpenAI API base here>',\n",
+ " 'api_type': 'azure',\n",
+ " 'api_version': '2023-08-01-preview',\n",
+ " },\n",
+ "]\n",
+ "```\n",
+ "\n",
+ "You can set the value of config_list in any way you prefer. Please refer to this [notebook](https://github.com/microsoft/autogen/blob/main/notebook/oai_openai_utils.ipynb) for full code examples of the different methods."
+ ]
+ },
+ {
+ "attachments": {},
+ "cell_type": "markdown",
+ "id": "2b9526e7",
+ "metadata": {
+ "id": "2b9526e7"
+ },
+ "source": [
+ "## Making Function Calls\n",
+ "\n",
+ "In this example, we demonstrate function call execution with `AssistantAgent` and `UserProxyAgent`. With the default system prompt of `AssistantAgent`, we allow the LLM assistant to perform tasks with code, and the `UserProxyAgent` would extract code blocks from the LLM response and execute them. With the new \"function_call\" feature, we define functions and specify the description of the function in the OpenAI config for the `AssistantAgent`. Then we register the functions in `UserProxyAgent`.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "qCzNbbVajvpc",
+ "metadata": {
+ "id": "qCzNbbVajvpc"
+ },
+ "outputs": [],
+ "source": [
+ "class CircumferenceToolInput(BaseModel):\n",
+ " radius: float = Field()\n",
+ "\n",
+ "\n",
+ "class CircumferenceTool(BaseTool):\n",
+ " name = \"circumference_calculator\"\n",
+ " description = \"Use this tool when you need to calculate a circumference using the radius of a circle\"\n",
+ " args_schema: Type[BaseModel] = CircumferenceToolInput\n",
+ "\n",
+ " def _run(self, radius: float):\n",
+ " return float(radius) * 2.0 * math.pi\n",
+ "\n",
+ "\n",
+ "def get_file_path_of_example():\n",
+ " # Get the current working directory\n",
+ " current_dir = os.getcwd()\n",
+ "\n",
+ " # Go one directory up\n",
+ " parent_dir = os.path.dirname(current_dir)\n",
+ "\n",
+ " # Move to the target directory\n",
+ " target_folder = os.path.join(parent_dir, \"test\")\n",
+ "\n",
+ " # Construct the path to your target file\n",
+ " file_path = os.path.join(target_folder, \"test_files/radius.txt\")\n",
+ "\n",
+ " return file_path"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "COlL5_98atDs",
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
},
+ "id": "COlL5_98atDs",
+ "outputId": "24ce236d-8993-4a69-99e2-65453574d61e"
+ },
+ "outputs": [
{
- "cell_type": "code",
- "execution_count": null,
- "id": "7iFp-Sm4CYq_",
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "7iFp-Sm4CYq_",
- "outputId": "2e1a2a70-53e6-4896-9232-63db6d097d63"
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "+---------+--------+------------------+-----------+--------------+----------+----------+-------------+------------------+\n",
- "|longitude|latitude|housing_median_age|total_rooms|total_bedrooms|population|households|median_income|median_house_value|\n",
- "+---------+--------+------------------+-----------+--------------+----------+----------+-------------+------------------+\n",
- "| -114.31| 34.19| 15.0| 5612.0| 1283.0| 1015.0| 472.0| 1.4936| 66900.0|\n",
- "| -114.47| 34.4| 19.0| 7650.0| 1901.0| 1129.0| 463.0| 1.82| 80100.0|\n",
- "| -114.56| 33.69| 17.0| 720.0| 174.0| 333.0| 117.0| 1.6509| 85700.0|\n",
- "| -114.57| 33.64| 14.0| 1501.0| 337.0| 515.0| 226.0| 3.1917| 73400.0|\n",
- "| -114.57| 33.57| 20.0| 1454.0| 326.0| 624.0| 262.0| 1.925| 65500.0|\n",
- "| -114.58| 33.63| 29.0| 1387.0| 236.0| 671.0| 239.0| 3.3438| 74000.0|\n",
- "| -114.58| 33.61| 25.0| 2907.0| 680.0| 1841.0| 633.0| 2.6768| 82400.0|\n",
- "| -114.59| 34.83| 41.0| 812.0| 168.0| 375.0| 158.0| 1.7083| 48500.0|\n",
- "| -114.59| 33.61| 34.0| 4789.0| 1175.0| 3134.0| 1056.0| 2.1782| 58400.0|\n",
- "| -114.6| 34.83| 46.0| 1497.0| 309.0| 787.0| 271.0| 2.1908| 48100.0|\n",
- "| -114.6| 33.62| 16.0| 3741.0| 801.0| 2434.0| 824.0| 2.6797| 86500.0|\n",
- "| -114.6| 33.6| 21.0| 1988.0| 483.0| 1182.0| 437.0| 1.625| 62000.0|\n",
- "| -114.61| 34.84| 48.0| 1291.0| 248.0| 580.0| 211.0| 2.1571| 48600.0|\n",
- "| -114.61| 34.83| 31.0| 2478.0| 464.0| 1346.0| 479.0| 3.212| 70400.0|\n",
- "| -114.63| 32.76| 15.0| 1448.0| 378.0| 949.0| 300.0| 0.8585| 45000.0|\n",
- "| -114.65| 34.89| 17.0| 2556.0| 587.0| 1005.0| 401.0| 1.6991| 69100.0|\n",
- "| -114.65| 33.6| 28.0| 1678.0| 322.0| 666.0| 256.0| 2.9653| 94900.0|\n",
- "| -114.65| 32.79| 21.0| 44.0| 33.0| 64.0| 27.0| 0.8571| 25000.0|\n",
- "| -114.66| 32.74| 17.0| 1388.0| 386.0| 775.0| 320.0| 1.2049| 44000.0|\n",
- "| -114.67| 33.92| 17.0| 97.0| 24.0| 29.0| 15.0| 1.2656| 27500.0|\n",
- "+---------+--------+------------------+-----------+--------------+----------+----------+-------------+------------------+\n",
- "only showing top 20 rows\n",
- "\n"
- ]
- }
- ],
- "source": [
- "#Starndard Langchain example\n",
- "from langchain.agents import create_spark_sql_agent\n",
- "from langchain.agents.agent_toolkits import SparkSQLToolkit\n",
- "from langchain.chat_models import ChatOpenAI\n",
- "from langchain.utilities.spark_sql import SparkSQL\n",
- "from pyspark.sql import SparkSession\n",
- "\n",
- "spark = SparkSession.builder.getOrCreate()\n",
- "schema = \"langchain_example\"\n",
- "spark.sql(f\"CREATE DATABASE IF NOT EXISTS {schema}\")\n",
- "spark.sql(f\"USE {schema}\")\n",
- "csv_file_path = \"./sample_data/california_housing_train.csv\"\n",
- "table = \"california_housing_train\"\n",
- "spark.read.csv(csv_file_path, header=True, inferSchema=True).write.option(\"path\", \"file:/content/spark-warehouse/langchain_example.db/california_housing_train\").mode(\"overwrite\").saveAsTable(table)\n",
- "spark.table(table).show()"
- ]
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "user_proxy (to chatbot):\n",
+ "\n",
+ "Read the file with the path 'Test.txt', then calculate the circumference of a circle that has a radius of that files contents.\n",
+ "\n",
+ "--------------------------------------------------------------------------------\n",
+ "chatbot (to user_proxy):\n",
+ "\n",
+ "***** Suggested function Call: read_file *****\n",
+ "Arguments: \n",
+ "{\n",
+ " \"file_path\": \"Test.txt\"\n",
+ "}\n",
+ "**********************************************\n",
+ "\n",
+ "--------------------------------------------------------------------------------\n",
+ "\n",
+ ">>>>>>>> EXECUTING FUNCTION read_file...\n",
+ "user_proxy (to chatbot):\n",
+ "\n",
+ "***** Response from calling function \"read_file\" *****\n",
+ "7.81mm\n",
+ "******************************************************\n",
+ "\n",
+ "--------------------------------------------------------------------------------\n",
+ "chatbot (to user_proxy):\n",
+ "\n",
+ "***** Suggested function Call: circumference_calculator *****\n",
+ "Arguments: \n",
+ "{\n",
+ " \"radius\": 7.81\n",
+ "}\n",
+ "*************************************************************\n",
+ "\n",
+ "--------------------------------------------------------------------------------\n",
+ "\n",
+ ">>>>>>>> EXECUTING FUNCTION circumference_calculator...\n",
+ "user_proxy (to chatbot):\n",
+ "\n",
+ "***** Response from calling function \"circumference_calculator\" *****\n",
+ "49.071677249072565\n",
+ "*********************************************************************\n",
+ "\n",
+ "--------------------------------------------------------------------------------\n",
+ "chatbot (to user_proxy):\n",
+ "\n",
+ "The circumference of a circle with a radius of 7.81mm is approximately 49.071mm.\n",
+ "\n",
+ "--------------------------------------------------------------------------------\n",
+ "user_proxy (to chatbot):\n",
+ "\n",
+ "\n",
+ "\n",
+ "--------------------------------------------------------------------------------\n",
+ "chatbot (to user_proxy):\n",
+ "\n",
+ "TERMINATE\n",
+ "\n",
+ "--------------------------------------------------------------------------------\n"
+ ]
+ }
+ ],
+ "source": [
+ "# Define a function to generate llm_config from a LangChain tool\n",
+ "\n",
+ "\n",
+ "def generate_llm_config(tool):\n",
+ " # Define the function schema based on the tool's args_schema\n",
+ " function_schema = {\n",
+ " \"name\": tool.name.lower().replace(\" \", \"_\"),\n",
+ " \"description\": tool.description,\n",
+ " \"parameters\": {\n",
+ " \"type\": \"object\",\n",
+ " \"properties\": {},\n",
+ " \"required\": [],\n",
+ " },\n",
+ " }\n",
+ "\n",
+ " if tool.args is not None:\n",
+ " function_schema[\"parameters\"][\"properties\"] = tool.args\n",
+ "\n",
+ " return function_schema\n",
+ "\n",
+ "\n",
+ "# Instantiate the ReadFileTool\n",
+ "read_file_tool = ReadFileTool()\n",
+ "custom_tool = CircumferenceTool()\n",
+ "\n",
+ "# Construct the llm_config\n",
+ "llm_config = {\n",
+ " # Generate functions config for the Tool\n",
+ " \"functions\": [\n",
+ " generate_llm_config(custom_tool),\n",
+ " generate_llm_config(read_file_tool),\n",
+ " ],\n",
+ " \"config_list\": config_list, # Assuming you have this defined elsewhere\n",
+ " \"timeout\": 120,\n",
+ "}\n",
+ "\n",
+ "user_proxy = autogen.UserProxyAgent(\n",
+ " name=\"user_proxy\",\n",
+ " is_termination_msg=lambda x: x.get(\"content\", \"\") and x.get(\"content\", \"\").rstrip().endswith(\"TERMINATE\"),\n",
+ " human_input_mode=\"NEVER\",\n",
+ " max_consecutive_auto_reply=10,\n",
+ " code_execution_config={\"work_dir\": \"coding\"},\n",
+ ")\n",
+ "\n",
+ "# Register the tool and start the conversation\n",
+ "user_proxy.register_function(\n",
+ " function_map={\n",
+ " custom_tool.name: custom_tool._run,\n",
+ " read_file_tool.name: read_file_tool._run,\n",
+ " }\n",
+ ")\n",
+ "\n",
+ "chatbot = autogen.AssistantAgent(\n",
+ " name=\"chatbot\",\n",
+ " system_message=\"For coding tasks, only use the functions you have been provided with. Reply TERMINATE when the task is done.\",\n",
+ " llm_config=llm_config,\n",
+ ")\n",
+ "\n",
+ "user_proxy.initiate_chat(\n",
+ " chatbot,\n",
+ " message=f\"Read the file with the path {get_file_path_of_example()}, then calculate the circumference of a circle that has a radius of that files contents.\", # 7.81mm in the file\n",
+ " llm_config=llm_config,\n",
+ ")"
+ ]
+ },
+ {
+ "attachments": {},
+ "cell_type": "markdown",
+ "id": "11cc4e60",
+ "metadata": {},
+ "source": [
+ "# A PySpark Example"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "Y-ozf9EFCegw",
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
},
+ "id": "Y-ozf9EFCegw",
+ "outputId": "db7b73a8-6129-4dfb-9d5c-ac3536f310d7"
+ },
+ "outputs": [
{
- "cell_type": "code",
- "execution_count": null,
- "id": "iLtSTHoJD7Jn",
- "metadata": {
- "id": "iLtSTHoJD7Jn"
- },
- "outputs": [],
- "source": [
- "# Note, you can also connect to Spark via Spark connect. For example:\n",
- "# db = SparkSQL.from_uri(\"sc://localhost:15002\", schema=schema)\n",
- "spark_sql = SparkSQL(schema=schema)\n",
- "llm = ChatOpenAI(temperature=0, model=\"gpt-3.5-turbo-16k\")\n",
- "toolkit = SparkSQLToolkit(db=spark_sql, llm=llm)\n",
- "agent_executor = create_spark_sql_agent(llm=llm, toolkit=toolkit, verbose=True)"
- ]
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Requirement already satisfied: pyspark in /usr/local/lib/python3.10/dist-packages (3.5.0)\n",
+ "Requirement already satisfied: py4j==0.10.9.7 in /usr/local/lib/python3.10/dist-packages (from pyspark) (0.10.9.7)\n"
+ ]
+ }
+ ],
+ "source": [
+ "%pip install pyspark"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "7iFp-Sm4CYq_",
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
},
+ "id": "7iFp-Sm4CYq_",
+ "outputId": "2e1a2a70-53e6-4896-9232-63db6d097d63"
+ },
+ "outputs": [
{
- "cell_type": "code",
- "execution_count": null,
- "id": "VzqNYlVjCqQa",
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/",
- "height": 608
- },
- "id": "VzqNYlVjCqQa",
- "outputId": "dd4de772-7b0c-4650-d106-c83d4593158e"
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "\n",
- "\n",
- "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
- "\u001b[32;1m\u001b[1;3mAction: list_tables_sql_db\n",
- "Action Input: \"\"\u001b[0m\n",
- "Observation: \u001b[38;5;200m\u001b[1;3mcalifornia_housing_train\u001b[0m\n",
- "Thought:\u001b[32;1m\u001b[1;3mI see that there is a table called \"california_housing_train\" in the database. I can use the \"schema_sql_db\" tool to get more information about this table.\n",
- "Action: schema_sql_db\n",
- "Action Input: \"california_housing_train\"\u001b[0m\n",
- "Observation: \u001b[33;1m\u001b[1;3mCREATE TABLE spark_catalog.langchain_example.california_housing_train (\n",
- " longitude DOUBLE,\n",
- " latitude DOUBLE,\n",
- " housing_median_age DOUBLE,\n",
- " total_rooms DOUBLE,\n",
- " total_bedrooms DOUBLE,\n",
- " population DOUBLE,\n",
- " households DOUBLE,\n",
- " median_income DOUBLE,\n",
- " median_house_value DOUBLE)\n",
- ";\n",
- "\n",
- "/*\n",
- "3 rows from california_housing_train table:\n",
- "longitude\tlatitude\thousing_median_age\ttotal_rooms\ttotal_bedrooms\tpopulation\thouseholds\tmedian_income\tmedian_house_value\n",
- "-114.31\t34.19\t15.0\t5612.0\t1283.0\t1015.0\t472.0\t1.4936\t66900.0\n",
- "-114.47\t34.4\t19.0\t7650.0\t1901.0\t1129.0\t463.0\t1.82\t80100.0\n",
- "-114.56\t33.69\t17.0\t720.0\t174.0\t333.0\t117.0\t1.6509\t85700.0\n",
- "*/\u001b[0m\n",
- "Thought:\u001b[32;1m\u001b[1;3mThe \"california_housing_train\" table has the following columns: longitude, latitude, housing_median_age, total_rooms, total_bedrooms, population, households, median_income, and median_house_value. It contains information about housing in California. I can now answer the question.\n",
- "Action: None\n",
- "Final Answer: The \"california_housing_train\" table has the following columns: longitude, latitude, housing_median_age, total_rooms, total_bedrooms, population, households, median_income, and median_house_value.\u001b[0m\n",
- "\n",
- "\u001b[1m> Finished chain.\u001b[0m\n"
- ]
- },
- {
- "data": {
- "application/vnd.google.colaboratory.intrinsic+json": {
- "type": "string"
- },
- "text/plain": [
- "'The \"california_housing_train\" table has the following columns: longitude, latitude, housing_median_age, total_rooms, total_bedrooms, population, households, median_income, and median_house_value.'"
- ]
- },
- "execution_count": 10,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "#Starndard Langchain example\n",
- "agent_executor.run(\"Describe the california_housing_train table\")"
- ]
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "+---------+--------+------------------+-----------+--------------+----------+----------+-------------+------------------+\n",
+ "|longitude|latitude|housing_median_age|total_rooms|total_bedrooms|population|households|median_income|median_house_value|\n",
+ "+---------+--------+------------------+-----------+--------------+----------+----------+-------------+------------------+\n",
+ "| -114.31| 34.19| 15.0| 5612.0| 1283.0| 1015.0| 472.0| 1.4936| 66900.0|\n",
+ "| -114.47| 34.4| 19.0| 7650.0| 1901.0| 1129.0| 463.0| 1.82| 80100.0|\n",
+ "| -114.56| 33.69| 17.0| 720.0| 174.0| 333.0| 117.0| 1.6509| 85700.0|\n",
+ "| -114.57| 33.64| 14.0| 1501.0| 337.0| 515.0| 226.0| 3.1917| 73400.0|\n",
+ "| -114.57| 33.57| 20.0| 1454.0| 326.0| 624.0| 262.0| 1.925| 65500.0|\n",
+ "| -114.58| 33.63| 29.0| 1387.0| 236.0| 671.0| 239.0| 3.3438| 74000.0|\n",
+ "| -114.58| 33.61| 25.0| 2907.0| 680.0| 1841.0| 633.0| 2.6768| 82400.0|\n",
+ "| -114.59| 34.83| 41.0| 812.0| 168.0| 375.0| 158.0| 1.7083| 48500.0|\n",
+ "| -114.59| 33.61| 34.0| 4789.0| 1175.0| 3134.0| 1056.0| 2.1782| 58400.0|\n",
+ "| -114.6| 34.83| 46.0| 1497.0| 309.0| 787.0| 271.0| 2.1908| 48100.0|\n",
+ "| -114.6| 33.62| 16.0| 3741.0| 801.0| 2434.0| 824.0| 2.6797| 86500.0|\n",
+ "| -114.6| 33.6| 21.0| 1988.0| 483.0| 1182.0| 437.0| 1.625| 62000.0|\n",
+ "| -114.61| 34.84| 48.0| 1291.0| 248.0| 580.0| 211.0| 2.1571| 48600.0|\n",
+ "| -114.61| 34.83| 31.0| 2478.0| 464.0| 1346.0| 479.0| 3.212| 70400.0|\n",
+ "| -114.63| 32.76| 15.0| 1448.0| 378.0| 949.0| 300.0| 0.8585| 45000.0|\n",
+ "| -114.65| 34.89| 17.0| 2556.0| 587.0| 1005.0| 401.0| 1.6991| 69100.0|\n",
+ "| -114.65| 33.6| 28.0| 1678.0| 322.0| 666.0| 256.0| 2.9653| 94900.0|\n",
+ "| -114.65| 32.79| 21.0| 44.0| 33.0| 64.0| 27.0| 0.8571| 25000.0|\n",
+ "| -114.66| 32.74| 17.0| 1388.0| 386.0| 775.0| 320.0| 1.2049| 44000.0|\n",
+ "| -114.67| 33.92| 17.0| 97.0| 24.0| 29.0| 15.0| 1.2656| 27500.0|\n",
+ "+---------+--------+------------------+-----------+--------------+----------+----------+-------------+------------------+\n",
+ "only showing top 20 rows\n",
+ "\n"
+ ]
+ }
+ ],
+ "source": [
+ "spark = SparkSession.builder.getOrCreate()\n",
+ "schema = \"langchain_example\"\n",
+ "spark.sql(f\"CREATE DATABASE IF NOT EXISTS {schema}\")\n",
+ "spark.sql(f\"USE {schema}\")\n",
+ "csv_file_path = \"./sample_data/california_housing_train.csv\"\n",
+ "table = \"california_housing_train\"\n",
+ "spark.read.csv(csv_file_path, header=True, inferSchema=True).write.option(\n",
+ " \"path\", \"file:/content/spark-warehouse/langchain_example.db/california_housing_train\"\n",
+ ").mode(\"overwrite\").saveAsTable(table)\n",
+ "spark.table(table).show()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "iLtSTHoJD7Jn",
+ "metadata": {
+ "id": "iLtSTHoJD7Jn"
+ },
+ "outputs": [],
+ "source": [
+ "# Note, you can also connect to Spark via Spark connect. For example:\n",
+ "# db = SparkSQL.from_uri(\"sc://localhost:15002\", schema=schema)\n",
+ "spark_sql = SparkSQL(schema=schema)\n",
+ "llm = ChatOpenAI(temperature=0, model=\"gpt-3.5-turbo-16k\")\n",
+ "toolkit = SparkSQLToolkit(db=spark_sql, llm=llm)\n",
+ "agent_executor = create_spark_sql_agent(llm=llm, toolkit=toolkit, verbose=True)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "VzqNYlVjCqQa",
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 608
},
+ "id": "VzqNYlVjCqQa",
+ "outputId": "dd4de772-7b0c-4650-d106-c83d4593158e"
+ },
+ "outputs": [
{
- "cell_type": "code",
- "execution_count": null,
- "id": "d94d45a9",
- "metadata": {},
- "outputs": [],
- "source": [
- "# LangChain direct tool usage instead of toolkit example\n",
- "# from langchain.tools.spark_sql.tool import (\n",
- "# InfoSparkSQLTool,\n",
- "# ListSparkSQLTool,\n",
- "# QueryCheckerTool,\n",
- "# QuerySparkSQLTool,\n",
- "# )\n",
- "# debug_toolkit = [\n",
- "# QuerySparkSQLTool(db=spark_sql),\n",
- "# InfoSparkSQLTool(db=spark_sql),\n",
- "# ListSparkSQLTool(db=spark_sql),\n",
- "# QueryCheckerTool(db=spark_sql, llm=llm),\n",
- "#]"
- ]
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\n",
+ "\n",
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
+ "\u001b[32;1m\u001b[1;3mAction: list_tables_sql_db\n",
+ "Action Input: \"\"\u001b[0m\n",
+ "Observation: \u001b[38;5;200m\u001b[1;3mcalifornia_housing_train\u001b[0m\n",
+ "Thought:\u001b[32;1m\u001b[1;3mI see that there is a table called \"california_housing_train\" in the database. I can use the \"schema_sql_db\" tool to get more information about this table.\n",
+ "Action: schema_sql_db\n",
+ "Action Input: \"california_housing_train\"\u001b[0m\n",
+ "Observation: \u001b[33;1m\u001b[1;3mCREATE TABLE spark_catalog.langchain_example.california_housing_train (\n",
+ " longitude DOUBLE,\n",
+ " latitude DOUBLE,\n",
+ " housing_median_age DOUBLE,\n",
+ " total_rooms DOUBLE,\n",
+ " total_bedrooms DOUBLE,\n",
+ " population DOUBLE,\n",
+ " households DOUBLE,\n",
+ " median_income DOUBLE,\n",
+ " median_house_value DOUBLE)\n",
+ ";\n",
+ "\n",
+ "/*\n",
+ "3 rows from california_housing_train table:\n",
+ "longitude\tlatitude\thousing_median_age\ttotal_rooms\ttotal_bedrooms\tpopulation\thouseholds\tmedian_income\tmedian_house_value\n",
+ "-114.31\t34.19\t15.0\t5612.0\t1283.0\t1015.0\t472.0\t1.4936\t66900.0\n",
+ "-114.47\t34.4\t19.0\t7650.0\t1901.0\t1129.0\t463.0\t1.82\t80100.0\n",
+ "-114.56\t33.69\t17.0\t720.0\t174.0\t333.0\t117.0\t1.6509\t85700.0\n",
+ "*/\u001b[0m\n",
+ "Thought:\u001b[32;1m\u001b[1;3mThe \"california_housing_train\" table has the following columns: longitude, latitude, housing_median_age, total_rooms, total_bedrooms, population, households, median_income, and median_house_value. It contains information about housing in California. I can now answer the question.\n",
+ "Action: None\n",
+ "Final Answer: The \"california_housing_train\" table has the following columns: longitude, latitude, housing_median_age, total_rooms, total_bedrooms, population, households, median_income, and median_house_value.\u001b[0m\n",
+ "\n",
+ "\u001b[1m> Finished chain.\u001b[0m\n"
+ ]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "r7PFvDS7Ev-E",
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "r7PFvDS7Ev-E",
- "outputId": "53d9c45d-058e-4e37-ba73-556591aaab42"
+ "data": {
+ "application/vnd.google.colaboratory.intrinsic+json": {
+ "type": "string"
},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{'name': 'query_sql_db', 'description': '\\n Input to this tool is a detailed and correct SQL query, output is a result from the Spark SQL.\\n If the query is not correct, an error message will be returned.\\n If an error is returned, rewrite the query, check the query, and try again.\\n ', 'parameters': {'type': 'object', 'properties': {'query': {'title': 'Query', 'type': 'string'}}, 'required': []}}\n",
- "{'name': 'schema_sql_db', 'description': '\\n Input to this tool is a comma-separated list of tables, output is the schema and sample rows for those tables.\\n Be sure that the tables actually exist by calling list_tables_sql_db first!\\n\\n Example Input: \"table1, table2, table3\"\\n ', 'parameters': {'type': 'object', 'properties': {'table_names': {'title': 'Table Names', 'type': 'string'}}, 'required': []}}\n",
- "{'name': 'list_tables_sql_db', 'description': 'Input is an empty string, output is a comma separated list of tables in the Spark SQL.', 'parameters': {'type': 'object', 'properties': {'tool_input': {'title': 'Tool Input', 'default': '', 'type': 'string'}}, 'required': []}}\n",
- "{'name': 'query_checker_sql_db', 'description': '\\n Use this tool to double check if your query is correct before executing it.\\n Always use this tool before executing a query with query_sql_db!\\n ', 'parameters': {'type': 'object', 'properties': {'query': {'title': 'Query', 'type': 'string'}}, 'required': []}}\n",
- "user_proxy (to chatbot):\n",
- "\n",
- "Describe the table names california_housing_train\n",
- "\n",
- "--------------------------------------------------------------------------------\n",
- "chatbot (to user_proxy):\n",
- "\n",
- "***** Suggested function Call: schema_sql_db *****\n",
- "Arguments: \n",
- "{\n",
- " \"table_names\": \"california_housing_train\"\n",
- "}\n",
- "**************************************************\n",
- "\n",
- "--------------------------------------------------------------------------------\n",
- "\n",
- ">>>>>>>> EXECUTING FUNCTION schema_sql_db...\n",
- "user_proxy (to chatbot):\n",
- "\n",
- "***** Response from calling function \"schema_sql_db\" *****\n",
- "CREATE TABLE spark_catalog.langchain_example.california_housing_train (\n",
- " longitude DOUBLE,\n",
- " latitude DOUBLE,\n",
- " housing_median_age DOUBLE,\n",
- " total_rooms DOUBLE,\n",
- " total_bedrooms DOUBLE,\n",
- " population DOUBLE,\n",
- " households DOUBLE,\n",
- " median_income DOUBLE,\n",
- " median_house_value DOUBLE)\n",
- ";\n",
- "\n",
- "/*\n",
- "3 rows from california_housing_train table:\n",
- "longitude\tlatitude\thousing_median_age\ttotal_rooms\ttotal_bedrooms\tpopulation\thouseholds\tmedian_income\tmedian_house_value\n",
- "-114.31\t34.19\t15.0\t5612.0\t1283.0\t1015.0\t472.0\t1.4936\t66900.0\n",
- "-114.47\t34.4\t19.0\t7650.0\t1901.0\t1129.0\t463.0\t1.82\t80100.0\n",
- "-114.56\t33.69\t17.0\t720.0\t174.0\t333.0\t117.0\t1.6509\t85700.0\n",
- "*/\n",
- "**********************************************************\n",
- "\n",
- "--------------------------------------------------------------------------------\n",
- "chatbot (to user_proxy):\n",
- "\n",
- "The california_housing_train table has the following schema:\n",
- "\n",
- "- longitude: DOUBLE\n",
- "- latitude: DOUBLE\n",
- "- housing_median_age: DOUBLE\n",
- "- total_rooms: DOUBLE\n",
- "- total_bedrooms: DOUBLE\n",
- "- population: DOUBLE\n",
- "- households: DOUBLE\n",
- "- median_income: DOUBLE\n",
- "- median_house_value: DOUBLE\n",
- "\n",
- "Here are sample rows from the table:\n",
- "\n",
- "| longitude | latitude | housing_median_age | total_rooms | total_bedrooms | population | households | median_income | median_house_value |\n",
- "|-----------|----------|--------------------|-------------|----------------|------------|------------|---------------|--------------------|\n",
- "| -114.31 | 34.19 | 15.0 | 5612.0 | 1283.0 | 1015.0 | 472.0 | 1.4936 | 66900.0 |\n",
- "| -114.47 | 34.4 | 19.0 | 7650.0 | 1901.0 | 1129.0 | 463.0 | 1.82 | 80100.0 |\n",
- "| -114.56 | 33.69 | 17.0 | 720.0 | 174.0 | 333.0 | 117.0 | 1.6509 | 85700.0 |\n",
- "\n",
- "--------------------------------------------------------------------------------\n",
- "user_proxy (to chatbot):\n",
- "\n",
- "\n",
- "\n",
- "--------------------------------------------------------------------------------\n",
- "chatbot (to user_proxy):\n",
- "\n",
- "TERMINATE\n",
- "\n",
- "--------------------------------------------------------------------------------\n"
- ]
- }
- ],
- "source": [
- "\n",
- "# Now use AutoGen with Langchain Tool Bridgre\n",
- "tools = []\n",
- "function_map = {}\n",
- "\n",
- "for tool in toolkit.get_tools(): #debug_toolkit if you want to use tools directly\n",
- " tool_schema = generate_llm_config(tool)\n",
- " print(tool_schema)\n",
- " tools.append(tool_schema)\n",
- " function_map[tool.name] = tool._run\n",
- "\n",
- "# Construct the llm_config\n",
- "llm_config = {\n",
- " \"functions\": tools,\n",
- " \"config_list\": config_list, # Assuming you have this defined elsewhere\n",
- " \"timeout\": 120,\n",
- "}\n",
- "\n",
- "user_proxy = autogen.UserProxyAgent(\n",
- " name=\"user_proxy\",\n",
- " is_termination_msg=lambda x: x.get(\"content\", \"\") and x.get(\"content\", \"\").rstrip().endswith(\"TERMINATE\"),\n",
- " human_input_mode=\"NEVER\",\n",
- " max_consecutive_auto_reply=10,\n",
- " code_execution_config={\"work_dir\": \"coding\"},\n",
- ")\n",
- "\n",
- "print(function_map)\n",
- "\n",
- "# Register the tool and start the conversation\n",
- "user_proxy.register_function(\n",
- " function_map = function_map\n",
- ")\n",
- "\n",
- "chatbot = autogen.AssistantAgent(\n",
- " name=\"chatbot\",\n",
- " system_message=\"For coding tasks, only use the functions you have been provided with. Reply TERMINATE when the task is done.\",\n",
- " llm_config=llm_config,\n",
- ")\n",
- "\n",
- "user_proxy.initiate_chat(\n",
- " chatbot,\n",
- " message=\"Describe the table names california_housing_train\",\n",
- " llm_config=llm_config,\n",
- ")"
+ "text/plain": [
+ "'The \"california_housing_train\" table has the following columns: longitude, latitude, housing_median_age, total_rooms, total_bedrooms, population, households, median_income, and median_house_value.'"
]
+ },
+ "execution_count": 10,
+ "metadata": {},
+ "output_type": "execute_result"
}
- ],
- "metadata": {
+ ],
+ "source": [
+ "# Starndard Langchain example\n",
+ "agent_executor.run(\"Describe the california_housing_train table\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "d94d45a9",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# LangChain direct tool usage instead of toolkit example\n",
+ "# from langchain.tools.spark_sql.tool import (\n",
+ "# InfoSparkSQLTool,\n",
+ "# ListSparkSQLTool,\n",
+ "# QueryCheckerTool,\n",
+ "# QuerySparkSQLTool,\n",
+ "# )\n",
+ "# debug_toolkit = [\n",
+ "# QuerySparkSQLTool(db=spark_sql),\n",
+ "# InfoSparkSQLTool(db=spark_sql),\n",
+ "# ListSparkSQLTool(db=spark_sql),\n",
+ "# QueryCheckerTool(db=spark_sql, llm=llm),\n",
+ "# ]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "r7PFvDS7Ev-E",
+ "metadata": {
"colab": {
- "provenance": []
+ "base_uri": "https://localhost:8080/"
},
- "kernelspec": {
- "display_name": "flaml_dev",
- "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.9.17"
+ "id": "r7PFvDS7Ev-E",
+ "outputId": "53d9c45d-058e-4e37-ba73-556591aaab42"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "{'name': 'query_sql_db', 'description': '\\n Input to this tool is a detailed and correct SQL query, output is a result from the Spark SQL.\\n If the query is not correct, an error message will be returned.\\n If an error is returned, rewrite the query, check the query, and try again.\\n ', 'parameters': {'type': 'object', 'properties': {'query': {'title': 'Query', 'type': 'string'}}, 'required': []}}\n",
+ "{'name': 'schema_sql_db', 'description': '\\n Input to this tool is a comma-separated list of tables, output is the schema and sample rows for those tables.\\n Be sure that the tables actually exist by calling list_tables_sql_db first!\\n\\n Example Input: \"table1, table2, table3\"\\n ', 'parameters': {'type': 'object', 'properties': {'table_names': {'title': 'Table Names', 'type': 'string'}}, 'required': []}}\n",
+ "{'name': 'list_tables_sql_db', 'description': 'Input is an empty string, output is a comma separated list of tables in the Spark SQL.', 'parameters': {'type': 'object', 'properties': {'tool_input': {'title': 'Tool Input', 'default': '', 'type': 'string'}}, 'required': []}}\n",
+ "{'name': 'query_checker_sql_db', 'description': '\\n Use this tool to double check if your query is correct before executing it.\\n Always use this tool before executing a query with query_sql_db!\\n ', 'parameters': {'type': 'object', 'properties': {'query': {'title': 'Query', 'type': 'string'}}, 'required': []}}\n",
+ "user_proxy (to chatbot):\n",
+ "\n",
+ "Describe the table names california_housing_train\n",
+ "\n",
+ "--------------------------------------------------------------------------------\n",
+ "chatbot (to user_proxy):\n",
+ "\n",
+ "***** Suggested function Call: schema_sql_db *****\n",
+ "Arguments: \n",
+ "{\n",
+ " \"table_names\": \"california_housing_train\"\n",
+ "}\n",
+ "**************************************************\n",
+ "\n",
+ "--------------------------------------------------------------------------------\n",
+ "\n",
+ ">>>>>>>> EXECUTING FUNCTION schema_sql_db...\n",
+ "user_proxy (to chatbot):\n",
+ "\n",
+ "***** Response from calling function \"schema_sql_db\" *****\n",
+ "CREATE TABLE spark_catalog.langchain_example.california_housing_train (\n",
+ " longitude DOUBLE,\n",
+ " latitude DOUBLE,\n",
+ " housing_median_age DOUBLE,\n",
+ " total_rooms DOUBLE,\n",
+ " total_bedrooms DOUBLE,\n",
+ " population DOUBLE,\n",
+ " households DOUBLE,\n",
+ " median_income DOUBLE,\n",
+ " median_house_value DOUBLE)\n",
+ ";\n",
+ "\n",
+ "/*\n",
+ "3 rows from california_housing_train table:\n",
+ "longitude\tlatitude\thousing_median_age\ttotal_rooms\ttotal_bedrooms\tpopulation\thouseholds\tmedian_income\tmedian_house_value\n",
+ "-114.31\t34.19\t15.0\t5612.0\t1283.0\t1015.0\t472.0\t1.4936\t66900.0\n",
+ "-114.47\t34.4\t19.0\t7650.0\t1901.0\t1129.0\t463.0\t1.82\t80100.0\n",
+ "-114.56\t33.69\t17.0\t720.0\t174.0\t333.0\t117.0\t1.6509\t85700.0\n",
+ "*/\n",
+ "**********************************************************\n",
+ "\n",
+ "--------------------------------------------------------------------------------\n",
+ "chatbot (to user_proxy):\n",
+ "\n",
+ "The california_housing_train table has the following schema:\n",
+ "\n",
+ "- longitude: DOUBLE\n",
+ "- latitude: DOUBLE\n",
+ "- housing_median_age: DOUBLE\n",
+ "- total_rooms: DOUBLE\n",
+ "- total_bedrooms: DOUBLE\n",
+ "- population: DOUBLE\n",
+ "- households: DOUBLE\n",
+ "- median_income: DOUBLE\n",
+ "- median_house_value: DOUBLE\n",
+ "\n",
+ "Here are sample rows from the table:\n",
+ "\n",
+ "| longitude | latitude | housing_median_age | total_rooms | total_bedrooms | population | households | median_income | median_house_value |\n",
+ "|-----------|----------|--------------------|-------------|----------------|------------|------------|---------------|--------------------|\n",
+ "| -114.31 | 34.19 | 15.0 | 5612.0 | 1283.0 | 1015.0 | 472.0 | 1.4936 | 66900.0 |\n",
+ "| -114.47 | 34.4 | 19.0 | 7650.0 | 1901.0 | 1129.0 | 463.0 | 1.82 | 80100.0 |\n",
+ "| -114.56 | 33.69 | 17.0 | 720.0 | 174.0 | 333.0 | 117.0 | 1.6509 | 85700.0 |\n",
+ "\n",
+ "--------------------------------------------------------------------------------\n",
+ "user_proxy (to chatbot):\n",
+ "\n",
+ "\n",
+ "\n",
+ "--------------------------------------------------------------------------------\n",
+ "chatbot (to user_proxy):\n",
+ "\n",
+ "TERMINATE\n",
+ "\n",
+ "--------------------------------------------------------------------------------\n"
+ ]
}
+ ],
+ "source": [
+ "# Now use AutoGen with Langchain Tool Bridgre\n",
+ "tools = []\n",
+ "function_map = {}\n",
+ "\n",
+ "for tool in toolkit.get_tools(): # debug_toolkit if you want to use tools directly\n",
+ " tool_schema = generate_llm_config(tool)\n",
+ " print(tool_schema)\n",
+ " tools.append(tool_schema)\n",
+ " function_map[tool.name] = tool._run\n",
+ "\n",
+ "# Construct the llm_config\n",
+ "llm_config = {\n",
+ " \"functions\": tools,\n",
+ " \"config_list\": config_list, # Assuming you have this defined elsewhere\n",
+ " \"timeout\": 120,\n",
+ "}\n",
+ "\n",
+ "user_proxy = autogen.UserProxyAgent(\n",
+ " name=\"user_proxy\",\n",
+ " is_termination_msg=lambda x: x.get(\"content\", \"\") and x.get(\"content\", \"\").rstrip().endswith(\"TERMINATE\"),\n",
+ " human_input_mode=\"NEVER\",\n",
+ " max_consecutive_auto_reply=10,\n",
+ " code_execution_config={\"work_dir\": \"coding\"},\n",
+ ")\n",
+ "\n",
+ "print(function_map)\n",
+ "\n",
+ "# Register the tool and start the conversation\n",
+ "user_proxy.register_function(function_map=function_map)\n",
+ "\n",
+ "chatbot = autogen.AssistantAgent(\n",
+ " name=\"chatbot\",\n",
+ " system_message=\"For coding tasks, only use the functions you have been provided with. Reply TERMINATE when the task is done.\",\n",
+ " llm_config=llm_config,\n",
+ ")\n",
+ "\n",
+ "user_proxy.initiate_chat(\n",
+ " chatbot,\n",
+ " message=\"Describe the table names california_housing_train\",\n",
+ " llm_config=llm_config,\n",
+ ")"
+ ]
+ }
+ ],
+ "metadata": {
+ "colab": {
+ "provenance": []
+ },
+ "kernelspec": {
+ "display_name": "flaml_dev",
+ "language": "python",
+ "name": "python3"
},
- "nbformat": 4,
- "nbformat_minor": 5
+ "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.9.17"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
}
diff --git a/notebook/agentchat_lmm_gpt-4v.ipynb b/notebook/agentchat_lmm_gpt-4v.ipynb
index d1b478ea1..5e074e494 100644
--- a/notebook/agentchat_lmm_gpt-4v.ipynb
+++ b/notebook/agentchat_lmm_gpt-4v.ipynb
@@ -26,16 +26,20 @@
"metadata": {},
"outputs": [],
"source": [
- "import requests\n",
"import json\n",
"import os\n",
- "\n",
+ "import random\n",
+ "import time\n",
"from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union\n",
"\n",
- "import autogen\n",
- "from autogen import AssistantAgent, Agent, UserProxyAgent, ConversableAgent\n",
+ "import matplotlib.pyplot as plt\n",
+ "import requests\n",
+ "from PIL import Image\n",
"from termcolor import colored\n",
- "import random"
+ "\n",
+ "import autogen\n",
+ "from autogen import Agent, AssistantAgent, ConversableAgent, UserProxyAgent\n",
+ "from autogen.agentchat.contrib.multimodal_conversable_agent import MultimodalConversableAgent"
]
},
{
@@ -68,8 +72,6 @@
"metadata": {},
"outputs": [],
"source": [
- "from autogen.agentchat.contrib.multimodal_conversable_agent import MultimodalConversableAgent\n",
- "\n",
"config_list_4v = autogen.config_list_from_json(\n",
" \"OAI_CONFIG_LIST\",\n",
" filter_dict={\n",
@@ -120,20 +122,22 @@
"image_agent = MultimodalConversableAgent(\n",
" name=\"image-explainer\",\n",
" max_consecutive_auto_reply=10,\n",
- " llm_config={\"config_list\": config_list_4v, \"temperature\": 0.5, \"max_tokens\": 300}\n",
+ " llm_config={\"config_list\": config_list_4v, \"temperature\": 0.5, \"max_tokens\": 300},\n",
")\n",
"\n",
"user_proxy = autogen.UserProxyAgent(\n",
" name=\"User_proxy\",\n",
" system_message=\"A human admin.\",\n",
- " human_input_mode=\"NEVER\", # Try between ALWAYS or NEVER\n",
- " max_consecutive_auto_reply=0\n",
+ " human_input_mode=\"NEVER\", # Try between ALWAYS or NEVER\n",
+ " max_consecutive_auto_reply=0,\n",
")\n",
"\n",
"# Ask the question with an image\n",
- "user_proxy.initiate_chat(image_agent, \n",
- " message=\"\"\"What's the breed of this dog? \n",
- "<img https://th.bing.com/th/id/R.422068ce8af4e15b0634fe2540adea7a?rik=y4OcXBE%2fqutDOw&pid=ImgRaw&r=0>.\"\"\")"
+ "user_proxy.initiate_chat(\n",
+ " image_agent,\n",
+ " message=\"\"\"What's the breed of this dog?\n",
+ "<img https://th.bing.com/th/id/R.422068ce8af4e15b0634fe2540adea7a?rik=y4OcXBE%2fqutDOw&pid=ImgRaw&r=0>.\"\"\",\n",
+ ")"
]
},
{
@@ -180,11 +184,13 @@
],
"source": [
"# Ask the question with an image\n",
- "user_proxy.send(message=\"\"\"What is this breed? \n",
+ "user_proxy.send(\n",
+ " message=\"\"\"What is this breed?\n",
"<img https://th.bing.com/th/id/OIP.29Mi2kJmcHHyQVGe_0NG7QHaEo?pid=ImgDet&rs=1>\n",
"\n",
- "Among the breeds, which one barks less?\"\"\", \n",
- " recipient=image_agent)"
+ "Among the breeds, which one barks less?\"\"\",\n",
+ " recipient=image_agent,\n",
+ ")"
]
},
{
@@ -210,27 +216,24 @@
"outputs": [],
"source": [
"class FigureCreator(AssistantAgent):\n",
- "\n",
" def __init__(self, n_iters=2, **kwargs):\n",
" \"\"\"\n",
" Initializes a FigureCreator instance.\n",
- " \n",
+ "\n",
" This agent facilitates the creation of visualizations through a collaborative effort among its child agents: commander, coder, and critics.\n",
- " \n",
+ "\n",
" Parameters:\n",
" - n_iters (int, optional): The number of \"improvement\" iterations to run. Defaults to 2.\n",
" - **kwargs: keyword arguments for the parent AssistantAgent.\n",
" \"\"\"\n",
" super().__init__(**kwargs)\n",
- " self.register_reply([Agent, None],\n",
- " reply_func=FigureCreator._reply_user,\n",
- " position=0)\n",
+ " self.register_reply([Agent, None], reply_func=FigureCreator._reply_user, position=0)\n",
" self._n_iters = n_iters\n",
"\n",
" def _reply_user(self, messages=None, sender=None, config=None):\n",
" if all((messages is None, sender is None)):\n",
" error_msg = f\"Either {messages=} or {sender=} must be provided.\"\n",
- " logger.error(error_msg)\n",
+ " logger.error(error_msg) # noqa: F821\n",
" raise AssertionError(error_msg)\n",
"\n",
" if messages is None:\n",
@@ -243,22 +246,15 @@
" name=\"Commander\",\n",
" human_input_mode=\"NEVER\",\n",
" max_consecutive_auto_reply=10,\n",
- " system_message=\n",
- " \"Help me run the code, and tell other agents it is in the <img result.jpg> file location.\",\n",
- " is_termination_msg=lambda x: x.get(\"content\", \"\").rstrip().endswith(\n",
- " \"TERMINATE\"),\n",
- " code_execution_config={\n",
- " \"last_n_messages\": 3,\n",
- " \"work_dir\": \".\",\n",
- " \"use_docker\": False\n",
- " },\n",
+ " system_message=\"Help me run the code, and tell other agents it is in the <img result.jpg> file location.\",\n",
+ " is_termination_msg=lambda x: x.get(\"content\", \"\").rstrip().endswith(\"TERMINATE\"),\n",
+ " code_execution_config={\"last_n_messages\": 3, \"work_dir\": \".\", \"use_docker\": False},\n",
" llm_config=self.llm_config,\n",
" )\n",
"\n",
" critics = MultimodalConversableAgent(\n",
" name=\"Critics\",\n",
- " system_message=\n",
- " \"\"\"Criticize the input figure. How to replot the figure so it will be better? Find bugs and issues for the figure. \n",
+ " system_message=\"\"\"Criticize the input figure. How to replot the figure so it will be better? Find bugs and issues for the figure.\n",
" Pay attention to the color, format, and presentation. Keep in mind of the reader-friendliness.\n",
" If you think the figures is good enough, then simply say NO_ISSUES\"\"\",\n",
" llm_config={\"config_list\": config_list_4v, \"max_tokens\": 300},\n",
@@ -273,22 +269,20 @@
" )\n",
"\n",
" coder.update_system_message(\n",
- " coder.system_message +\n",
- " \"ALWAYS save the figure in `result.jpg` file. Tell other agents it is in the <img result.jpg> file location.\"\n",
+ " coder.system_message\n",
+ " + \"ALWAYS save the figure in `result.jpg` file. Tell other agents it is in the <img result.jpg> file location.\"\n",
" )\n",
"\n",
" # Data flow begins\n",
" commander.initiate_chat(coder, message=user_question)\n",
" img = Image.open(\"result.jpg\")\n",
" plt.imshow(img)\n",
- " plt.axis('off') # Hide the axes\n",
+ " plt.axis(\"off\") # Hide the axes\n",
" plt.show()\n",
- " \n",
+ "\n",
" for i in range(self._n_iters):\n",
- " commander.send(message=\"Improve <img result.jpg>\",\n",
- " recipient=critics,\n",
- " request_reply=True)\n",
- " \n",
+ " commander.send(message=\"Improve <img result.jpg>\", recipient=critics, request_reply=True)\n",
+ "\n",
" feedback = commander._oai_messages[critics][-1][\"content\"]\n",
" if feedback.find(\"NO_ISSUES\") >= 0:\n",
" break\n",
@@ -296,12 +290,13 @@
" message=\"Here is the feedback to your figure. Please improve! Save the result to `result.jpg`\\n\"\n",
" + feedback,\n",
" recipient=coder,\n",
- " request_reply=True)\n",
+ " request_reply=True,\n",
+ " )\n",
" img = Image.open(\"result.jpg\")\n",
" plt.imshow(img)\n",
- " plt.axis('off') # Hide the axes\n",
+ " plt.axis(\"off\") # Hide the axes\n",
" plt.show()\n",
- " \n",
+ "\n",
" return True, \"result.jpg\""
]
},
@@ -655,11 +650,6 @@
}
],
"source": [
- "import matplotlib.pyplot as plt\n",
- "import time\n",
- "from PIL import Image\n",
- "\n",
- "\n",
"# config_list_gpt35 = autogen.config_list_from_json(\n",
"# \"OAI_CONFIG_LIST\",\n",
"# filter_dict={\n",
@@ -670,24 +660,19 @@
"# gpt35_llm_config = {\"config_list\": config_list_gpt35, \"cache_seed\": 42}\n",
"\n",
"\n",
- "creator = FigureCreator(\n",
- " name=\"Figure Creator~\",\n",
- " llm_config=gpt4_llm_config\n",
- " \n",
- ")\n",
+ "creator = FigureCreator(name=\"Figure Creator~\", llm_config=gpt4_llm_config)\n",
"\n",
- "user_proxy = autogen.UserProxyAgent(\n",
- " name=\"User\",\n",
- " human_input_mode=\"NEVER\",\n",
- " max_consecutive_auto_reply=0\n",
- ")\n",
+ "user_proxy = autogen.UserProxyAgent(name=\"User\", human_input_mode=\"NEVER\", max_consecutive_auto_reply=0)\n",
"\n",
- "user_proxy.initiate_chat(creator, message=\"\"\"\n",
+ "user_proxy.initiate_chat(\n",
+ " creator,\n",
+ " message=\"\"\"\n",
"Plot a figure by using the data from:\n",
"https://raw.githubusercontent.com/vega/vega/main/docs/data/seattle-weather.csv\n",
"\n",
"I want to show both temperature high and low.\n",
- "\"\"\")\n"
+ "\"\"\",\n",
+ ")"
]
},
{
@@ -698,7 +683,7 @@
"outputs": [],
"source": [
"if os.path.exists(\"result.jpg\"):\n",
- " os.remove(\"result.jpg\") # clean up"
+ " os.remove(\"result.jpg\") # clean up"
]
},
{
@@ -788,15 +773,14 @@
")\n",
"\n",
"# We set max_round to 5\n",
- "groupchat = autogen.GroupChat(agents=[agent1, agent2, user_proxy], \n",
- " messages=[], \n",
- " max_round=5)\n",
- "group_chat_manager = autogen.GroupChatManager(groupchat=groupchat, \n",
- " llm_config=gpt4_llm_config)\n",
- "\n",
- "user_proxy.initiate_chat(group_chat_manager,\n",
- " message=f\"\"\"Describe the image:\n",
- " <img https://th.bing.com/th/id/R.422068ce8af4e15b0634fe2540adea7a?rik=y4OcXBE%2fqutDOw&pid=ImgRaw&r=0>.\"\"\")"
+ "groupchat = autogen.GroupChat(agents=[agent1, agent2, user_proxy], messages=[], max_round=5)\n",
+ "group_chat_manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=gpt4_llm_config)\n",
+ "\n",
+ "user_proxy.initiate_chat(\n",
+ " group_chat_manager,\n",
+ " message=\"\"\"Describe the image:\n",
+ " <img https://th.bing.com/th/id/R.422068ce8af4e15b0634fe2540adea7a?rik=y4OcXBE%2fqutDOw&pid=ImgRaw&r=0>.\"\"\",\n",
+ ")"
]
}
],
diff --git a/notebook/agentchat_lmm_llava.ipynb b/notebook/agentchat_lmm_llava.ipynb
index 324eab810..061d84e24 100644
--- a/notebook/agentchat_lmm_llava.ipynb
+++ b/notebook/agentchat_lmm_llava.ipynb
@@ -39,27 +39,23 @@
"source": [
"# We use this variable to control where you want to host LLaVA, locally or remotely?\n",
"# More details in the two setup options below.\n",
- "LLAVA_MODE = \"remote\" # Either \"local\" or \"remote\"\n",
- "assert LLAVA_MODE in [\"local\", \"remote\"]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "id": "67d45964",
- "metadata": {},
- "outputs": [],
- "source": [
- "import requests\n",
"import json\n",
"import os\n",
- "\n",
+ "import random\n",
+ "import time\n",
"from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union\n",
"\n",
- "import autogen\n",
- "from autogen import AssistantAgent, Agent, UserProxyAgent, ConversableAgent\n",
+ "import matplotlib.pyplot as plt\n",
+ "import requests\n",
+ "from PIL import Image\n",
"from termcolor import colored\n",
- "import random"
+ "\n",
+ "import autogen\n",
+ "from autogen import Agent, AssistantAgent, ConversableAgent, UserProxyAgent\n",
+ "from autogen.agentchat.contrib.llava_agent import LLaVAAgent, llava_call\n",
+ "\n",
+ "LLAVA_MODE = \"remote\" # Either \"local\" or \"remote\"\n",
+ "assert LLAVA_MODE in [\"local\", \"remote\"]"
]
},
{
@@ -101,11 +97,11 @@
"source": [
"if LLAVA_MODE == \"remote\":\n",
" import replicate\n",
- " \n",
+ "\n",
" llava_config_list = [\n",
" {\n",
- " \"model\": \"whatever, will be ignored for remote\", # The model name doesn't matter here right now.\n",
- " \"api_key\": \"None\", # Note that you have to setup the API key with os.environ[\"REPLICATE_API_TOKEN\"] \n",
+ " \"model\": \"whatever, will be ignored for remote\", # The model name doesn't matter here right now.\n",
+ " \"api_key\": \"None\", # Note that you have to setup the API key with os.environ[\"REPLICATE_API_TOKEN\"]\n",
" \"base_url\": \"yorickvp/llava-13b:2facb4a474a0462c15041b78b1ad70952ea46b5ec6ad29583c0b29dbd4249591\",\n",
" }\n",
" ]"
@@ -169,7 +165,7 @@
"outputs": [],
"source": [
"# Run this code block only if you want to run LlaVA locally\n",
- "if LLAVA_MODE == \"local\": \n",
+ "if LLAVA_MODE == \"local\":\n",
" llava_config_list = [\n",
" {\n",
" \"model\": \"llava-v1.5-13b\",\n",
@@ -190,16 +186,6 @@
"![](https://raw.githubusercontent.com/microsoft/autogen/main/website/static/img/autogen_agentchat.png)\n"
]
},
- {
- "cell_type": "code",
- "execution_count": 6,
- "id": "e2ff1607",
- "metadata": {},
- "outputs": [],
- "source": [
- "from autogen.agentchat.contrib.llava_agent import llava_call"
- ]
- },
{
"cell_type": "code",
"execution_count": 7,
@@ -223,11 +209,10 @@
}
],
"source": [
- "rst = llava_call(\"Describe this AutoGen framework <img https://raw.githubusercontent.com/microsoft/autogen/main/website/static/img/autogen_agentchat.png> with bullet points.\",\n",
- " llm_config={\n",
- " \"config_list\": llava_config_list,\n",
- " \"temperature\": 0\n",
- " })\n",
+ "rst = llava_call(\n",
+ " \"Describe this AutoGen framework <img https://raw.githubusercontent.com/microsoft/autogen/main/website/static/img/autogen_agentchat.png> with bullet points.\",\n",
+ " llm_config={\"config_list\": llava_config_list, \"temperature\": 0},\n",
+ ")\n",
"\n",
"print(rst)"
]
@@ -255,16 +240,6 @@
"Within the user proxy agent, we can decide to activate the human input mode or not (for here, we use human_input_mode=\"NEVER\" for conciseness). This allows you to interact with LLaVA in a multi-round dialogue, enabling you to provide feedback as the conversation unfolds."
]
},
- {
- "cell_type": "code",
- "execution_count": 8,
- "id": "b1db6f5d",
- "metadata": {},
- "outputs": [],
- "source": [
- "from autogen.agentchat.contrib.llava_agent import LLaVAAgent"
- ]
- },
{
"cell_type": "code",
"execution_count": 9,
@@ -302,24 +277,23 @@
"image_agent = LLaVAAgent(\n",
" name=\"image-explainer\",\n",
" max_consecutive_auto_reply=10,\n",
- " llm_config={\"config_list\": llava_config_list, \"temperature\": 0.5, \"max_new_tokens\": 1000}\n",
+ " llm_config={\"config_list\": llava_config_list, \"temperature\": 0.5, \"max_new_tokens\": 1000},\n",
")\n",
"\n",
"user_proxy = autogen.UserProxyAgent(\n",
" name=\"User_proxy\",\n",
" system_message=\"A human admin.\",\n",
- " code_execution_config={\n",
- " \"last_n_messages\": 3,\n",
- " \"work_dir\": \"groupchat\"\n",
- " },\n",
- " human_input_mode=\"NEVER\", # Try between ALWAYS or NEVER\n",
- " max_consecutive_auto_reply=0\n",
+ " code_execution_config={\"last_n_messages\": 3, \"work_dir\": \"groupchat\"},\n",
+ " human_input_mode=\"NEVER\", # Try between ALWAYS or NEVER\n",
+ " max_consecutive_auto_reply=0,\n",
")\n",
"\n",
"# Ask the question with an image\n",
- "user_proxy.initiate_chat(image_agent, \n",
- " message=\"\"\"What's the breed of this dog? \n",
- "<img https://th.bing.com/th/id/R.422068ce8af4e15b0634fe2540adea7a?rik=y4OcXBE%2fqutDOw&pid=ImgRaw&r=0>.\"\"\")"
+ "user_proxy.initiate_chat(\n",
+ " image_agent,\n",
+ " message=\"\"\"What's the breed of this dog?\n",
+ "<img https://th.bing.com/th/id/R.422068ce8af4e15b0634fe2540adea7a?rik=y4OcXBE%2fqutDOw&pid=ImgRaw&r=0>.\"\"\",\n",
+ ")"
]
},
{
@@ -374,11 +348,13 @@
],
"source": [
"# Ask the question with an image\n",
- "user_proxy.send(message=\"\"\"What is this breed? \n",
+ "user_proxy.send(\n",
+ " message=\"\"\"What is this breed?\n",
"<img https://th.bing.com/th/id/OIP.29Mi2kJmcHHyQVGe_0NG7QHaEo?pid=ImgDet&rs=1>\n",
"\n",
- "Among the breeds, which one barks less?\"\"\", \n",
- " recipient=image_agent)"
+ "Among the breeds, which one barks less?\"\"\",\n",
+ " recipient=image_agent,\n",
+ ")"
]
},
{
@@ -404,27 +380,24 @@
"outputs": [],
"source": [
"class FigureCreator(AssistantAgent):\n",
- "\n",
" def __init__(self, n_iters=2, **kwargs):\n",
" \"\"\"\n",
" Initializes a FigureCreator instance.\n",
- " \n",
+ "\n",
" This agent facilitates the creation of visualizations through a collaborative effort among its child agents: commander, coder, and critics.\n",
- " \n",
+ "\n",
" Parameters:\n",
" - n_iters (int, optional): The number of \"improvement\" iterations to run. Defaults to 2.\n",
" - **kwargs: keyword arguments for the parent AssistantAgent.\n",
" \"\"\"\n",
" super().__init__(**kwargs)\n",
- " self.register_reply([Agent, None],\n",
- " reply_func=FigureCreator._reply_user,\n",
- " position=0)\n",
+ " self.register_reply([Agent, None], reply_func=FigureCreator._reply_user, position=0)\n",
" self._n_iters = n_iters\n",
"\n",
" def _reply_user(self, messages=None, sender=None, config=None):\n",
" if all((messages is None, sender is None)):\n",
" error_msg = f\"Either {messages=} or {sender=} must be provided.\"\n",
- " logger.error(error_msg)\n",
+ " logger.error(error_msg) # noqa: F821\n",
" raise AssertionError(error_msg)\n",
"\n",
" if messages is None:\n",
@@ -437,22 +410,15 @@
" name=\"Commander\",\n",
" human_input_mode=\"NEVER\",\n",
" max_consecutive_auto_reply=10,\n",
- " system_message=\n",
- " \"Help me run the code, and tell other agents it is in the <img result.jpg> file location.\",\n",
- " is_termination_msg=lambda x: x.get(\"content\", \"\").rstrip().endswith(\n",
- " \"TERMINATE\"),\n",
- " code_execution_config={\n",
- " \"last_n_messages\": 3,\n",
- " \"work_dir\": \".\",\n",
- " \"use_docker\": False\n",
- " },\n",
+ " system_message=\"Help me run the code, and tell other agents it is in the <img result.jpg> file location.\",\n",
+ " is_termination_msg=lambda x: x.get(\"content\", \"\").rstrip().endswith(\"TERMINATE\"),\n",
+ " code_execution_config={\"last_n_messages\": 3, \"work_dir\": \".\", \"use_docker\": False},\n",
" llm_config=self.llm_config,\n",
" )\n",
"\n",
" critics = LLaVAAgent(\n",
" name=\"Critics\",\n",
- " system_message=\n",
- " \"\"\"Criticize the input figure. How to replot the figure so it will be better? Find bugs and issues for the figure. \n",
+ " system_message=\"\"\"Criticize the input figure. How to replot the figure so it will be better? Find bugs and issues for the figure.\n",
" Pay attention to the color, format, and presentation. Keep in mind of the reader-friendliness.\n",
" If you think the figures is good enough, then simply say NO_ISSUES\"\"\",\n",
" llm_config={\"config_list\": llava_config_list},\n",
@@ -467,22 +433,20 @@
" )\n",
"\n",
" coder.update_system_message(\n",
- " coder.system_message +\n",
- " \"ALWAYS save the figure in `result.jpg` file. Tell other agents it is in the <img result.jpg> file location.\"\n",
+ " coder.system_message\n",
+ " + \"ALWAYS save the figure in `result.jpg` file. Tell other agents it is in the <img result.jpg> file location.\"\n",
" )\n",
"\n",
" # Data flow begins\n",
" commander.initiate_chat(coder, message=user_question)\n",
" img = Image.open(\"result.jpg\")\n",
" plt.imshow(img)\n",
- " plt.axis('off') # Hide the axes\n",
+ " plt.axis(\"off\") # Hide the axes\n",
" plt.show()\n",
- " \n",
+ "\n",
" for i in range(self._n_iters):\n",
- " commander.send(message=\"Improve <img result.jpg>\",\n",
- " recipient=critics,\n",
- " request_reply=True)\n",
- " \n",
+ " commander.send(message=\"Improve <img result.jpg>\", recipient=critics, request_reply=True)\n",
+ "\n",
" feedback = commander._oai_messages[critics][-1][\"content\"]\n",
" if feedback.find(\"NO_ISSUES\") >= 0:\n",
" break\n",
@@ -490,12 +454,13 @@
" message=\"Here is the feedback to your figure. Please improve! Save the result to `result.jpg`\\n\"\n",
" + feedback,\n",
" recipient=coder,\n",
- " request_reply=True)\n",
+ " request_reply=True,\n",
+ " )\n",
" img = Image.open(\"result.jpg\")\n",
" plt.imshow(img)\n",
- " plt.axis('off') # Hide the axes\n",
+ " plt.axis(\"off\") # Hide the axes\n",
" plt.show()\n",
- " \n",
+ "\n",
" return True, \"result.jpg\""
]
},
@@ -837,10 +802,6 @@
}
],
"source": [
- "import matplotlib.pyplot as plt\n",
- "import time\n",
- "from PIL import Image\n",
- "\n",
"config_list_gpt4 = autogen.config_list_from_json(\n",
" \"OAI_CONFIG_LIST\",\n",
" filter_dict={\n",
@@ -860,24 +821,19 @@
"# gpt35_llm_config = {\"config_list\": config_list_gpt35, \"cache_seed\": 42}\n",
"\n",
"\n",
- "creator = FigureCreator(\n",
- " name=\"Figure Creator~\",\n",
- " llm_config=gpt4_llm_config\n",
- " \n",
- ")\n",
+ "creator = FigureCreator(name=\"Figure Creator~\", llm_config=gpt4_llm_config)\n",
"\n",
- "user_proxy = autogen.UserProxyAgent(\n",
- " name=\"User\",\n",
- " human_input_mode=\"NEVER\",\n",
- " max_consecutive_auto_reply=0\n",
- ")\n",
+ "user_proxy = autogen.UserProxyAgent(name=\"User\", human_input_mode=\"NEVER\", max_consecutive_auto_reply=0)\n",
"\n",
- "user_proxy.initiate_chat(creator, message=\"\"\"\n",
+ "user_proxy.initiate_chat(\n",
+ " creator,\n",
+ " message=\"\"\"\n",
"Plot a figure by using the data from:\n",
"https://raw.githubusercontent.com/vega/vega/main/docs/data/seattle-weather.csv\n",
"\n",
"I want to show both temperature high and low.\n",
- "\"\"\")\n"
+ "\"\"\",\n",
+ ")"
]
},
{
@@ -888,7 +844,7 @@
"outputs": [],
"source": [
"if os.path.exists(\"result.jpg\"):\n",
- " os.remove(\"result.jpg\") # clean up"
+ " os.remove(\"result.jpg\") # clean up"
]
}
],
diff --git a/notebook/agentchat_microsoft_fabric.ipynb b/notebook/agentchat_microsoft_fabric.ipynb
index f6e20d86c..84e43dbb8 100644
--- a/notebook/agentchat_microsoft_fabric.ipynb
+++ b/notebook/agentchat_microsoft_fabric.ipynb
@@ -176,15 +176,19 @@
}
],
"source": [
+ "from synapse.ml.mlflow import get_mlflow_env_config\n",
+ "\n",
+ "import autogen\n",
+ "\n",
"# Choose different models\n",
"config_list = [\n",
" {\n",
- " 'model': 'gpt-4-turbo',\n",
+ " \"model\": \"gpt-4-turbo\",\n",
" },\n",
"]\n",
"\n",
"# Set temperature, timeout and other LLM configurations\n",
- "llm_config={\n",
+ "llm_config = {\n",
" \"config_list\": config_list,\n",
" \"temperature\": 0,\n",
"}"
@@ -309,8 +313,6 @@
}
],
"source": [
- "import autogen\n",
- "\n",
"# create an AssistantAgent instance named \"assistant\"\n",
"assistant = autogen.AssistantAgent(\n",
" name=\"assistant\",\n",
@@ -329,7 +331,7 @@
" },\n",
" llm_config=llm_config,\n",
" system_message=\"\"\"Reply TERMINATE if the task has been solved at full satisfaction.\n",
- "Otherwise, reply CONTINUE, or the reason why the task is not solved yet.\"\"\"\n",
+ "Otherwise, reply CONTINUE, or the reason why the task is not solved yet.\"\"\",\n",
")\n",
"\n",
"# the assistant receives a message from the user, which contains the task description\n",
@@ -526,8 +528,6 @@
}
],
"source": [
- "from synapse.ml.mlflow import get_mlflow_env_config\n",
- "\n",
"mlflow_env_configs = get_mlflow_env_config()\n",
"access_token = mlflow_env_configs.driver_aad_token\n",
"prebuilt_AI_base_url = mlflow_env_configs.workload_endpoint + \"cognitive/openai/\""
@@ -574,11 +574,11 @@
"source": [
"config_list = [\n",
" {\n",
- " 'model': 'gpt-4-turbo',\n",
- " 'api_key': access_token,\n",
- " 'base_url': prebuilt_AI_base_url,\n",
- " 'api_type': 'azure',\n",
- " 'api_version': '2023-08-01-preview',\n",
+ " \"model\": \"gpt-4-turbo\",\n",
+ " \"api_key\": access_token,\n",
+ " \"base_url\": prebuilt_AI_base_url,\n",
+ " \"api_type\": \"azure\",\n",
+ " \"api_version\": \"2023-08-01-preview\",\n",
" },\n",
"]"
]
@@ -751,8 +751,6 @@
}
],
"source": [
- "import autogen\n",
- "\n",
"# create an AssistantAgent named \"assistant\"\n",
"assistant = autogen.AssistantAgent(\n",
" name=\"assistant\",\n",
diff --git a/notebook/agentchat_oai_assistant_function_call.ipynb b/notebook/agentchat_oai_assistant_function_call.ipynb
index dc1ec6f31..64393be15 100644
--- a/notebook/agentchat_oai_assistant_function_call.ipynb
+++ b/notebook/agentchat_oai_assistant_function_call.ipynb
@@ -49,41 +49,41 @@
"source": [
"import logging\n",
"import os\n",
+ "\n",
"import requests\n",
"\n",
+ "from autogen import UserProxyAgent, config_list_from_json\n",
+ "from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent\n",
+ "\n",
"logger = logging.getLogger(__name__)\n",
"logger.setLevel(logging.WARNING)\n",
"\n",
"ossinsight_api_schema = {\n",
- " \"name\": \"ossinsight_data_api\",\n",
- " \"parameters\": {\n",
- " \"type\": \"object\",\n",
- " \"properties\": {\n",
- " \"question\": {\n",
- " \"type\": \"string\",\n",
- " \"description\": (\n",
- " \"Enter your GitHub data question in the form of a clear and specific question to ensure the returned data is accurate and valuable. \"\n",
- " \"For optimal results, specify the desired format for the data table in your request.\"\n",
- " ),\n",
- " }\n",
+ " \"name\": \"ossinsight_data_api\",\n",
+ " \"parameters\": {\n",
+ " \"type\": \"object\",\n",
+ " \"properties\": {\n",
+ " \"question\": {\n",
+ " \"type\": \"string\",\n",
+ " \"description\": (\n",
+ " \"Enter your GitHub data question in the form of a clear and specific question to ensure the returned data is accurate and valuable. \"\n",
+ " \"For optimal results, specify the desired format for the data table in your request.\"\n",
+ " ),\n",
+ " }\n",
+ " },\n",
+ " \"required\": [\"question\"],\n",
" },\n",
- " \"required\": [\n",
- " \"question\"\n",
- " ]\n",
- " },\n",
- " \"description\": \"This is an API endpoint allowing users (analysts) to input question about GitHub in text format to retrieve the related and structured data.\"\n",
+ " \"description\": \"This is an API endpoint allowing users (analysts) to input question about GitHub in text format to retrieve the related and structured data.\",\n",
"}\n",
"\n",
+ "\n",
"def get_ossinsight(question):\n",
" \"\"\"\n",
" Retrieve the top 10 developers with the most followers on GitHub.\n",
" \"\"\"\n",
" url = \"https://api.ossinsight.io/explorer/answer\"\n",
" headers = {\"Content-Type\": \"application/json\"}\n",
- " data = {\n",
- " \"question\": question,\n",
- " \"ignoreCache\": True\n",
- " }\n",
+ " data = {\"question\": question, \"ignoreCache\": True}\n",
"\n",
" response = requests.post(url, headers=headers, json=data)\n",
" if response.status_code == 200:\n",
@@ -93,16 +93,16 @@
"\n",
" report_components = []\n",
" report_components.append(f\"Question: {answer['question']['title']}\")\n",
- " if answer['query']['sql'] != \"\":\n",
+ " if answer[\"query\"][\"sql\"] != \"\":\n",
" report_components.append(f\"querySQL: {answer['query']['sql']}\")\n",
"\n",
- " if answer.get('result', None) is None or len(answer['result']['rows']) == 0:\n",
+ " if answer.get(\"result\", None) is None or len(answer[\"result\"][\"rows\"]) == 0:\n",
" result = \"Result: N/A\"\n",
" else:\n",
- " result = \"Result:\\n \" + \"\\n \".join([str(row) for row in answer['result']['rows']])\n",
+ " result = \"Result:\\n \" + \"\\n \".join([str(row) for row in answer[\"result\"][\"rows\"]])\n",
" report_components.append(result)\n",
"\n",
- " if answer.get('error', None) is not None:\n",
+ " if answer.get(\"error\", None) is not None:\n",
" report_components.append(f\"Error: {answer['error']}\")\n",
" return \"\\n\\n\".join(report_components) + \"\\n\\n\""
]
@@ -122,25 +122,21 @@
"metadata": {},
"outputs": [],
"source": [
- "from autogen import config_list_from_json\n",
- "from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent\n",
- "from autogen import UserProxyAgent\n",
- "\n",
"assistant_id = os.environ.get(\"ASSISTANT_ID\", None)\n",
"config_list = config_list_from_json(\"OAI_CONFIG_LIST\")\n",
"llm_config = {\n",
" \"config_list\": config_list,\n",
" \"assistant_id\": assistant_id,\n",
- " \"tools\": [\n",
+ " \"tools\": [\n",
" {\n",
" \"type\": \"function\",\n",
" \"function\": ossinsight_api_schema,\n",
" }\n",
- " ]\n",
+ " ],\n",
"}\n",
"\n",
"oss_analyst = GPTAssistantAgent(\n",
- " name=\"OSS Analyst\", \n",
+ " name=\"OSS Analyst\",\n",
" instructions=(\n",
" \"Hello, Open Source Project Analyst. You'll conduct comprehensive evaluations of open source projects or organizations on the GitHub platform, \"\n",
" \"analyzing project trajectories, contributor engagements, open source trends, and other vital parameters. \"\n",
@@ -235,13 +231,13 @@
}
],
"source": [
- "user_proxy = UserProxyAgent(name=\"user_proxy\",\n",
- " code_execution_config={\n",
- " \"work_dir\": \"coding\"\n",
- " },\n",
+ "user_proxy = UserProxyAgent(\n",
+ " name=\"user_proxy\",\n",
+ " code_execution_config={\"work_dir\": \"coding\"},\n",
" is_termination_msg=lambda msg: \"TERMINATE\" in msg[\"content\"],\n",
" human_input_mode=\"NEVER\",\n",
- " max_consecutive_auto_reply=1)\n",
+ " max_consecutive_auto_reply=1,\n",
+ ")\n",
"\n",
"user_proxy.initiate_chat(oss_analyst, message=\"Top 10 developers with the most followers\")"
]
diff --git a/notebook/agentchat_oai_assistant_groupchat.ipynb b/notebook/agentchat_oai_assistant_groupchat.ipynb
index ad98f21dc..9a0bf89f9 100644
--- a/notebook/agentchat_oai_assistant_groupchat.ipynb
+++ b/notebook/agentchat_oai_assistant_groupchat.ipynb
@@ -35,6 +35,8 @@
"outputs": [],
"source": [
"import autogen\n",
+ "from autogen.agentchat import AssistantAgent\n",
+ "from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent\n",
"\n",
"config_list_gpt4 = autogen.config_list_from_json(\n",
" \"OAI_CONFIG_LIST\",\n",
@@ -85,16 +87,13 @@
}
],
"source": [
- "from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent\n",
- "from autogen.agentchat import AssistantAgent\n",
- "\n",
"# Define user proxy agent\n",
"llm_config = {\"config_list\": config_list_gpt4, \"cache_seed\": 45}\n",
"user_proxy = autogen.UserProxyAgent(\n",
- " name=\"User_proxy\",\n",
- " system_message=\"A human admin.\",\n",
- " code_execution_config={\"last_n_messages\": 2, \"work_dir\": \"groupchat\"},\n",
- " human_input_mode=\"TERMINATE\"\n",
+ " name=\"User_proxy\",\n",
+ " system_message=\"A human admin.\",\n",
+ " code_execution_config={\"last_n_messages\": 2, \"work_dir\": \"groupchat\"},\n",
+ " human_input_mode=\"TERMINATE\",\n",
")\n",
"\n",
"# define two GPTAssistants\n",
@@ -103,7 +102,7 @@
" llm_config={\n",
" \"config_list\": config_list_gpt4,\n",
" },\n",
- " instructions=AssistantAgent.DEFAULT_SYSTEM_MESSAGE\n",
+ " instructions=AssistantAgent.DEFAULT_SYSTEM_MESSAGE,\n",
")\n",
"\n",
"analyst = GPTAssistantAgent(\n",
@@ -463,7 +462,10 @@
}
],
"source": [
- "user_proxy.initiate_chat(manager, message=\"Get the number of issues and pull requests for the repository 'microsoft/autogen' over the past three weeks and offer analysis to the data. You should print the data in csv format grouped by weeks.\")\n",
+ "user_proxy.initiate_chat(\n",
+ " manager,\n",
+ " message=\"Get the number of issues and pull requests for the repository 'microsoft/autogen' over the past three weeks and offer analysis to the data. You should print the data in csv format grouped by weeks.\",\n",
+ ")\n",
"# type exit to terminate the chat"
]
},
diff --git a/notebook/agentchat_oai_assistant_retrieval.ipynb b/notebook/agentchat_oai_assistant_retrieval.ipynb
index 01c6b0024..ba73e9f04 100644
--- a/notebook/agentchat_oai_assistant_retrieval.ipynb
+++ b/notebook/agentchat_oai_assistant_retrieval.ipynb
@@ -87,10 +87,9 @@
"source": [
"import logging\n",
"import os\n",
- " \n",
- "from autogen import config_list_from_json\n",
+ "\n",
+ "from autogen import UserProxyAgent, config_list_from_json\n",
"from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent\n",
- "from autogen import UserProxyAgent\n",
"\n",
"logger = logging.getLogger(__name__)\n",
"logger.setLevel(logging.WARNING)\n",
@@ -101,24 +100,22 @@
"llm_config = {\n",
" \"config_list\": config_list,\n",
" \"assistant_id\": assistant_id,\n",
- " \"tools\": [\n",
- " {\n",
- " \"type\": \"retrieval\"\n",
- " }\n",
- " ],\n",
- " \"file_ids\": [\"file-CmlT0YKLB3ZCdHmslF9FOv69\"] \n",
+ " \"tools\": [{\"type\": \"retrieval\"}],\n",
+ " \"file_ids\": [\"file-CmlT0YKLB3ZCdHmslF9FOv69\"]\n",
" # add id of an existing file in your openai account\n",
" # in this case I added the implementation of conversable_agent.py\n",
"}\n",
"\n",
- "gpt_assistant = GPTAssistantAgent(name=\"assistant\",\n",
- " instructions=\"You are adapt at question answering\",\n",
- " llm_config=llm_config)\n",
+ "gpt_assistant = GPTAssistantAgent(\n",
+ " name=\"assistant\", instructions=\"You are adapt at question answering\", llm_config=llm_config\n",
+ ")\n",
"\n",
- "user_proxy = UserProxyAgent(name=\"user_proxy\",\n",
+ "user_proxy = UserProxyAgent(\n",
+ " name=\"user_proxy\",\n",
" code_execution_config=False,\n",
" is_termination_msg=lambda msg: \"TERMINATE\" in msg[\"content\"],\n",
- " human_input_mode=\"ALWAYS\")\n",
+ " human_input_mode=\"ALWAYS\",\n",
+ ")\n",
"user_proxy.initiate_chat(gpt_assistant, message=\"What is the name of the class of agents I gave you?\")\n",
"\n",
"gpt_assistant.delete_assistant()"
diff --git a/notebook/agentchat_oai_assistant_twoagents_basic.ipynb b/notebook/agentchat_oai_assistant_twoagents_basic.ipynb
index 44660bf96..3db0e937a 100644
--- a/notebook/agentchat_oai_assistant_twoagents_basic.ipynb
+++ b/notebook/agentchat_oai_assistant_twoagents_basic.ipynb
@@ -78,11 +78,9 @@
"source": [
"import logging\n",
"import os\n",
- " \n",
- "from autogen import config_list_from_json\n",
- "from autogen import AssistantAgent\n",
+ "\n",
+ "from autogen import AssistantAgent, UserProxyAgent, config_list_from_json\n",
"from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent\n",
- "from autogen import UserProxyAgent\n",
"\n",
"logger = logging.getLogger(__name__)\n",
"logger.setLevel(logging.WARNING)\n",
@@ -90,22 +88,19 @@
"assistant_id = os.environ.get(\"ASSISTANT_ID\", None)\n",
"\n",
"config_list = config_list_from_json(\"OAI_CONFIG_LIST\")\n",
- "llm_config = {\n",
- " \"config_list\": config_list,\n",
- " \"assistant_id\": assistant_id\n",
- "}\n",
+ "llm_config = {\"config_list\": config_list, \"assistant_id\": assistant_id}\n",
"\n",
- "gpt_assistant = GPTAssistantAgent(name=\"assistant\",\n",
- " instructions=AssistantAgent.DEFAULT_SYSTEM_MESSAGE,\n",
- " llm_config=llm_config)\n",
+ "gpt_assistant = GPTAssistantAgent(\n",
+ " name=\"assistant\", instructions=AssistantAgent.DEFAULT_SYSTEM_MESSAGE, llm_config=llm_config\n",
+ ")\n",
"\n",
- "user_proxy = UserProxyAgent(name=\"user_proxy\",\n",
- " code_execution_config={\n",
- " \"work_dir\": \"coding\"\n",
- " },\n",
+ "user_proxy = UserProxyAgent(\n",
+ " name=\"user_proxy\",\n",
+ " code_execution_config={\"work_dir\": \"coding\"},\n",
" is_termination_msg=lambda msg: \"TERMINATE\" in msg[\"content\"],\n",
" human_input_mode=\"NEVER\",\n",
- " max_consecutive_auto_reply=1)\n",
+ " max_consecutive_auto_reply=1,\n",
+ ")\n",
"user_proxy.initiate_chat(gpt_assistant, message=\"Print hello world\")"
]
},
diff --git a/notebook/agentchat_oai_code_interpreter.ipynb b/notebook/agentchat_oai_code_interpreter.ipynb
index f023260b2..c8c97fe47 100644
--- a/notebook/agentchat_oai_code_interpreter.ipynb
+++ b/notebook/agentchat_oai_code_interpreter.ipynb
@@ -30,7 +30,14 @@
"metadata": {},
"outputs": [],
"source": [
+ "import io\n",
+ "\n",
+ "from IPython.display import display\n",
+ "from PIL import Image\n",
+ "\n",
"import autogen\n",
+ "from autogen.agentchat import AssistantAgent, UserProxyAgent\n",
+ "from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent\n",
"\n",
"config_list = autogen.config_list_from_json(\n",
" \"OAI_CONFIG_LIST\",\n",
@@ -105,18 +112,11 @@
}
],
"source": [
- "from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent\n",
- "from autogen.agentchat import AssistantAgent, UserProxyAgent\n",
- "\n",
"# Initiate an agent equipped with code interpreter\n",
"gpt_assistant = GPTAssistantAgent(\n",
" name=\"Coder Assistant\",\n",
" llm_config={\n",
- " \"tools\": [\n",
- " {\n",
- " \"type\": \"code_interpreter\"\n",
- " }\n",
- " ],\n",
+ " \"tools\": [{\"type\": \"code_interpreter\"}],\n",
" \"config_list\": config_list,\n",
" },\n",
" instructions=\"You are an expert at solving math questions. Write code and run it to solve math problems. Reply TERMINATE when the task is solved and there is no problem.\",\n",
@@ -129,11 +129,13 @@
" \"work_dir\": \"coding\",\n",
" \"use_docker\": False, # set to True or image name like \"python:3\" to use docker\n",
" },\n",
- " human_input_mode=\"NEVER\"\n",
+ " human_input_mode=\"NEVER\",\n",
")\n",
"\n",
"# When all is set, initiate the chat.\n",
- "user_proxy.initiate_chat(gpt_assistant, message=\"If $725x + 727y = 1500$ and $729x+ 731y = 1508$, what is the value of $x - y$ ?\")\n"
+ "user_proxy.initiate_chat(\n",
+ " gpt_assistant, message=\"If $725x + 727y = 1500$ and $729x+ 731y = 1508$, what is the value of $x - y$ ?\"\n",
+ ")"
]
},
{
@@ -205,17 +207,17 @@
"gpt_assistant = GPTAssistantAgent(\n",
" name=\"Coder Assistant\",\n",
" llm_config={\n",
- " \"tools\": [\n",
- " {\n",
- " \"type\": \"code_interpreter\"\n",
- " }\n",
- " ],\n",
+ " \"tools\": [{\"type\": \"code_interpreter\"}],\n",
" \"config_list\": config_list,\n",
" },\n",
" instructions=\"You are an expert at writing python code to solve problems. Reply TERMINATE when the task is solved and there is no problem.\",\n",
")\n",
"\n",
- "user_proxy.initiate_chat(gpt_assistant, message=\"Draw a line chart to show the population trend in US. Show how you solved it with code.\", clear_history=True)"
+ "user_proxy.initiate_chat(\n",
+ " gpt_assistant,\n",
+ " message=\"Draw a line chart to show the population trend in US. Show how you solved it with code.\",\n",
+ " clear_history=True,\n",
+ ")"
]
},
{
@@ -242,10 +244,6 @@
}
],
"source": [
- "from PIL import Image\n",
- "import io\n",
- "from IPython.display import display\n",
- "\n",
"api_response = gpt_assistant.openai_client.files.with_raw_response.retrieve_content(\"file-sAG7r763XR9jiuKYiQeoHRFU\")\n",
"\n",
"if api_response.status_code == 200:\n",
diff --git a/notebook/agentchat_planning.ipynb b/notebook/agentchat_planning.ipynb
index 43a7a22ac..84b8c05b2 100644
--- a/notebook/agentchat_planning.ipynb
+++ b/notebook/agentchat_planning.ipynb
@@ -122,7 +122,7 @@
" name=\"planner\",\n",
" llm_config={\"config_list\": config_list},\n",
" # the default system message of the AssistantAgent is overwritten here\n",
- " system_message=\"You are a helpful AI assistant. You suggest coding and reasoning steps for another AI assistant to accomplish a task. Do not suggest concrete code. For any action beyond writing code or reasoning, convert it to a step that can be implemented by writing code. For example, browsing the web can be implemented by writing code that reads and prints the content of a web page. Finally, inspect the execution result. If the plan is not good, suggest a better plan. If the execution is wrong, analyze the error and suggest a fix.\"\n",
+ " system_message=\"You are a helpful AI assistant. You suggest coding and reasoning steps for another AI assistant to accomplish a task. Do not suggest concrete code. For any action beyond writing code or reasoning, convert it to a step that can be implemented by writing code. For example, browsing the web can be implemented by writing code that reads and prints the content of a web page. Finally, inspect the execution result. If the plan is not good, suggest a better plan. If the execution is wrong, analyze the error and suggest a fix.\",\n",
")\n",
"planner_user = autogen.UserProxyAgent(\n",
" name=\"planner_user\",\n",
@@ -130,10 +130,11 @@
" human_input_mode=\"NEVER\",\n",
")\n",
"\n",
+ "\n",
"def ask_planner(message):\n",
" planner_user.initiate_chat(planner, message=message)\n",
" # return the last message received from the planner\n",
- " return planner_user.last_message()[\"content\"]\n"
+ " return planner_user.last_message()[\"content\"]"
]
},
{
@@ -174,7 +175,7 @@
" },\n",
" },\n",
" ],\n",
- " }\n",
+ " },\n",
")\n",
"\n",
"# create a UserProxyAgent instance named \"user_proxy\"\n",
diff --git a/notebook/agentchat_qdrant_RetrieveChat.ipynb b/notebook/agentchat_qdrant_RetrieveChat.ipynb
index 2dda59087..99aabdf08 100644
--- a/notebook/agentchat_qdrant_RetrieveChat.ipynb
+++ b/notebook/agentchat_qdrant_RetrieveChat.ipynb
@@ -219,7 +219,15 @@
}
],
"source": [
+ "from qdrant_client import QdrantClient\n",
+ "\n",
"import autogen\n",
+ "from autogen.agentchat.contrib.qdrant_retrieve_user_proxy_agent import QdrantRetrieveUserProxyAgent\n",
+ "from autogen.agentchat.contrib.retrieve_assistant_agent import RetrieveAssistantAgent\n",
+ "\n",
+ "# Accepted file formats for that can be stored in\n",
+ "# a vector database instance\n",
+ "from autogen.retrieve_utils import TEXT_FORMATS\n",
"\n",
"config_list = autogen.config_list_from_json(\n",
" env_or_file=\"OAI_CONFIG_LIST\",\n",
@@ -291,10 +299,6 @@
}
],
"source": [
- "# Accepted file formats for that can be stored in \n",
- "# a vector database instance\n",
- "from autogen.retrieve_utils import TEXT_FORMATS\n",
- "\n",
"print(\"Accepted file formats for `docs_path`:\")\n",
"print(TEXT_FORMATS)"
]
@@ -317,13 +321,9 @@
"metadata": {},
"outputs": [],
"source": [
- "from autogen.agentchat.contrib.retrieve_assistant_agent import RetrieveAssistantAgent\n",
- "from autogen.agentchat.contrib.qdrant_retrieve_user_proxy_agent import QdrantRetrieveUserProxyAgent\n",
- "from qdrant_client import QdrantClient\n",
- "\n",
"# 1. create an RetrieveAssistantAgent instance named \"assistant\"\n",
"assistant = RetrieveAssistantAgent(\n",
- " name=\"assistant\", \n",
+ " name=\"assistant\",\n",
" system_message=\"You are a helpful assistant.\",\n",
" llm_config={\n",
" \"timeout\": 600,\n",
@@ -334,9 +334,9 @@
"\n",
"# 2. create the QdrantRetrieveUserProxyAgent instance named \"ragproxyagent\"\n",
"# By default, the human_input_mode is \"ALWAYS\", which means the agent will ask for human input at every step. We set it to \"NEVER\" here.\n",
- "# `docs_path` is the path to the docs directory. It can also be the path to a single file, or the url to a single file. By default, \n",
+ "# `docs_path` is the path to the docs directory. It can also be the path to a single file, or the url to a single file. By default,\n",
"# it is set to None, which works only if the collection is already created.\n",
- "# \n",
+ "#\n",
"# Here we generated the documentations from FLAML's docstrings. Not needed if you just want to try this notebook but not to reproduce the\n",
"# outputs. Clone the FLAML (https://github.com/microsoft/FLAML) repo and navigate to its website folder. Pip install and run `pydoc-markdown`\n",
"# and it will generate folder `reference` under `website/docs`.\n",
diff --git a/notebook/agentchat_stream.ipynb b/notebook/agentchat_stream.ipynb
index d71459822..9a36f4b7b 100644
--- a/notebook/agentchat_stream.ipynb
+++ b/notebook/agentchat_stream.ipynb
@@ -64,6 +64,8 @@
"metadata": {},
"outputs": [],
"source": [
+ "import asyncio\n",
+ "\n",
"import autogen\n",
"\n",
"config_list = autogen.config_list_from_json(\"OAI_CONFIG_LIST\")"
@@ -121,11 +123,11 @@
"metadata": {},
"outputs": [],
"source": [
- "import asyncio\n",
- "\n",
"def get_market_news(ind, ind_upper):\n",
- " import requests\n",
" import json\n",
+ "\n",
+ " import requests\n",
+ "\n",
" # replace the \"demo\" apikey below with your own key from https://www.alphavantage.co/support/#api-key\n",
" # url = 'https://www.alphavantage.co/query?function=NEWS_SENTIMENT&tickers=AAPL&sort=LATEST&limit=5&apikey=demo'\n",
" # r = requests.get(url)\n",
@@ -172,8 +174,10 @@
" )\n",
" return feeds_summary\n",
"\n",
+ "\n",
"data = asyncio.Future()\n",
"\n",
+ "\n",
"async def add_stock_price_data():\n",
" # simulating the data stream\n",
" for i in range(0, 5, 1):\n",
@@ -185,7 +189,8 @@
" # print(data.result())\n",
" await asyncio.sleep(5)\n",
"\n",
- "data_task = asyncio.create_task(add_stock_price_data())\n"
+ "\n",
+ "data_task = asyncio.create_task(add_stock_price_data())"
]
},
{
@@ -202,8 +207,6 @@
"metadata": {},
"outputs": [],
"source": [
- "import autogen\n",
- "\n",
"# create an AssistantAgent instance named \"assistant\"\n",
"assistant = autogen.AssistantAgent(\n",
" name=\"assistant\",\n",
@@ -224,6 +227,7 @@
" default_auto_reply=None,\n",
")\n",
"\n",
+ "\n",
"async def add_data_reply(recipient, messages, sender, config):\n",
" await asyncio.sleep(0.1)\n",
" data = config[\"news_stream\"]\n",
@@ -238,6 +242,7 @@
" )\n",
" return False, None\n",
"\n",
+ "\n",
"user_proxy.register_reply(autogen.AssistantAgent, add_data_reply, position=2, config={\"news_stream\": data})"
]
},
@@ -353,14 +358,14 @@
}
],
"source": [
- "await user_proxy.a_initiate_chat(\n",
+ "await user_proxy.a_initiate_chat( # noqa: F704\n",
" assistant,\n",
" message=\"\"\"Give me investment suggestion in 3 bullet points.\"\"\",\n",
")\n",
"while not data_task.done() and not data_task.cancelled():\n",
- " reply = await user_proxy.a_generate_reply(sender=assistant)\n",
+ " reply = await user_proxy.a_generate_reply(sender=assistant) # noqa: F704\n",
" if reply is not None:\n",
- " await user_proxy.a_send(reply, assistant)"
+ " await user_proxy.a_send(reply, assistant) # noqa: F704"
]
}
],
diff --git a/notebook/agentchat_teachability.ipynb b/notebook/agentchat_teachability.ipynb
index 20f7cdf2f..7b2f6c63f 100644
--- a/notebook/agentchat_teachability.ipynb
+++ b/notebook/agentchat_teachability.ipynb
@@ -66,6 +66,8 @@
],
"source": [
"import autogen\n",
+ "from autogen import ConversableAgent, UserProxyAgent\n",
+ "from autogen.agentchat.contrib.capabilities.teachability import Teachability\n",
"\n",
"config_list = autogen.config_list_from_json(\n",
" env_or_file=\"OAI_CONFIG_LIST\",\n",
@@ -142,9 +144,6 @@
}
],
"source": [
- "from autogen.agentchat.contrib.capabilities.teachability import Teachability\n",
- "from autogen import ConversableAgent, UserProxyAgent\n",
- "\n",
"# Start by instantiating any agent that inherits from ConversableAgent.\n",
"teachable_agent = ConversableAgent(\n",
" name=\"teachable_agent\", # The name is flexible, but should not contain spaces to work in group chat.\n",
@@ -169,6 +168,7 @@
" def colored(x, *args, **kwargs):\n",
" return x\n",
"\n",
+ "\n",
"# Instantiate a UserProxyAgent to represent the user. But in this notebook, all user input will be simulated.\n",
"user = UserProxyAgent(\n",
" name=\"user\",\n",
@@ -454,7 +454,7 @@
}
],
"source": [
- "text = \"\"\"Please summarize this abstract. \n",
+ "text = \"\"\"Please summarize this abstract.\n",
"When I'm summarizing an abstract, I try to make the summary contain just three short bullet points: the title, the innovation, and the key empirical results.\n",
"\n",
"AutoGen: Enabling Next-Gen LLM Applications via Multi-Agent Conversation\n",
@@ -583,7 +583,7 @@
}
],
"source": [
- "text = \"\"\"Consider the identity: \n",
+ "text = \"\"\"Consider the identity:\n",
"9 * 4 + 6 * 6 = 72\n",
"Can you modify exactly one integer (and not more than that!) on the left hand side of the equation so the right hand side becomes 99?\n",
"-Let's think step-by-step, write down a plan, and then write down your solution as: \"The solution is: A * B + C * D\".\n",
@@ -676,7 +676,7 @@
}
],
"source": [
- "text = \"\"\"Consider the identity: \n",
+ "text = \"\"\"Consider the identity:\n",
"9 * 4 + 6 * 6 = 72\n",
"Can you modify exactly one integer (and not more than that!) on the left hand side of the equation so the right hand side becomes 99?\n",
"-Let's think step-by-step, write down a plan, and then write down your solution as: \"The solution is: A * B + C * D\".\n",
@@ -760,7 +760,7 @@
}
],
"source": [
- "text = \"\"\"Consider the identity: \n",
+ "text = \"\"\"Consider the identity:\n",
"9 * 4 + 6 * 6 = 72\n",
"Can you modify exactly one integer (and not more than that!) on the left hand side of the equation so the right hand side becomes 99?\n",
"-Let's think step-by-step, write down a plan, and then write down your solution as: \"The solution is: A * B + C * D\".\n",
@@ -828,7 +828,7 @@
}
],
"source": [
- "text = \"\"\"Consider the identity: \n",
+ "text = \"\"\"Consider the identity:\n",
"8 * 3 + 7 * 9 = 87\n",
"Can you modify exactly one integer (and not more than that!) on the left hand side of the equation so the right hand side becomes 59?\n",
"-Let's think step-by-step, write down a plan, and then write down your solution as: \"The solution is: A * B + C * D\".\n",
diff --git a/notebook/agentchat_teachable_oai_assistants.ipynb b/notebook/agentchat_teachable_oai_assistants.ipynb
index 582efb50d..931ac10fd 100644
--- a/notebook/agentchat_teachable_oai_assistants.ipynb
+++ b/notebook/agentchat_teachable_oai_assistants.ipynb
@@ -66,8 +66,17 @@
}
],
"source": [
+ "import logging\n",
+ "import os\n",
+ "\n",
+ "import requests\n",
+ "\n",
"import autogen\n",
+ "from autogen import UserProxyAgent, config_list_from_json\n",
+ "\n",
+ "# from autogen.agentchat import UserProxyAgent\n",
"from autogen.agentchat.contrib.capabilities.teachability import Teachability\n",
+ "from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent\n",
"\n",
"config_list = autogen.config_list_from_json(\n",
" env_or_file=\"OAI_CONFIG_LIST\",\n",
@@ -149,43 +158,35 @@
"metadata": {},
"outputs": [],
"source": [
- "import logging\n",
- "import os\n",
- "import requests\n",
- "\n",
"logger = logging.getLogger(__name__)\n",
"logger.setLevel(logging.WARNING)\n",
"\n",
"ossinsight_api_schema = {\n",
- " \"name\": \"ossinsight_data_api\",\n",
- " \"parameters\": {\n",
- " \"type\": \"object\",\n",
- " \"properties\": {\n",
- " \"question\": {\n",
- " \"type\": \"string\",\n",
- " \"description\": (\n",
- " \"Enter your GitHub data question in the form of a clear and specific question to ensure the returned data is accurate and valuable. \"\n",
- " \"For optimal results, specify the desired format for the data table in your request.\"\n",
- " ),\n",
- " }\n",
+ " \"name\": \"ossinsight_data_api\",\n",
+ " \"parameters\": {\n",
+ " \"type\": \"object\",\n",
+ " \"properties\": {\n",
+ " \"question\": {\n",
+ " \"type\": \"string\",\n",
+ " \"description\": (\n",
+ " \"Enter your GitHub data question in the form of a clear and specific question to ensure the returned data is accurate and valuable. \"\n",
+ " \"For optimal results, specify the desired format for the data table in your request.\"\n",
+ " ),\n",
+ " }\n",
+ " },\n",
+ " \"required\": [\"question\"],\n",
" },\n",
- " \"required\": [\n",
- " \"question\"\n",
- " ]\n",
- " },\n",
- " \"description\": \"This is an API endpoint allowing users (analysts) to input question about GitHub in text format to retrieve the realted and structured data.\"\n",
+ " \"description\": \"This is an API endpoint allowing users (analysts) to input question about GitHub in text format to retrieve the realted and structured data.\",\n",
"}\n",
"\n",
+ "\n",
"def get_ossinsight(question):\n",
" \"\"\"\n",
" Retrieve the top 10 developers with the most followers on GitHub.\n",
" \"\"\"\n",
" url = \"https://api.ossinsight.io/explorer/answer\"\n",
" headers = {\"Content-Type\": \"application/json\"}\n",
- " data = {\n",
- " \"question\": question,\n",
- " \"ignoreCache\": True\n",
- " }\n",
+ " data = {\"question\": question, \"ignoreCache\": True}\n",
"\n",
" response = requests.post(url, headers=headers, json=data)\n",
" if response.status_code == 200:\n",
@@ -195,16 +196,16 @@
"\n",
" report_components = []\n",
" report_components.append(f\"Question: {answer['question']['title']}\")\n",
- " if answer['query']['sql'] != \"\":\n",
+ " if answer[\"query\"][\"sql\"] != \"\":\n",
" report_components.append(f\"querySQL: {answer['query']['sql']}\")\n",
"\n",
- " if answer.get('result', None) is None or len(answer['result']['rows']) == 0:\n",
+ " if answer.get(\"result\", None) is None or len(answer[\"result\"][\"rows\"]) == 0:\n",
" result = \"Result: N/A\"\n",
" else:\n",
- " result = \"Result:\\n \" + \"\\n \".join([str(row) for row in answer['result']['rows']])\n",
+ " result = \"Result:\\n \" + \"\\n \".join([str(row) for row in answer[\"result\"][\"rows\"]])\n",
" report_components.append(result)\n",
"\n",
- " if answer.get('error', None) is not None:\n",
+ " if answer.get(\"error\", None) is not None:\n",
" report_components.append(f\"Error: {answer['error']}\")\n",
" return \"\\n\\n\".join(report_components) + \"\\n\\n\""
]
@@ -231,25 +232,21 @@
}
],
"source": [
- "from autogen import config_list_from_json\n",
- "from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent\n",
- "from autogen import UserProxyAgent\n",
- "\n",
"assistant_id = os.environ.get(\"ASSISTANT_ID\", None)\n",
"config_list = config_list_from_json(\"OAI_CONFIG_LIST\")\n",
"llm_config = {\n",
" \"config_list\": config_list,\n",
" \"assistant_id\": assistant_id,\n",
- " \"tools\": [\n",
+ " \"tools\": [\n",
" {\n",
" \"type\": \"function\",\n",
" \"function\": ossinsight_api_schema,\n",
" }\n",
- " ]\n",
+ " ],\n",
"}\n",
"\n",
"oss_analyst = GPTAssistantAgent(\n",
- " name=\"OSS_Analyst\", \n",
+ " name=\"OSS_Analyst\",\n",
" instructions=(\n",
" \"Hello, Open Source Project Analyst. You'll conduct comprehensive evaluations of open source projects or organizations on the GitHub platform, \"\n",
" \"analyzing project trajectories, contributor engagements, open source trends, and other vital parameters. \"\n",
@@ -331,13 +328,13 @@
}
],
"source": [
- "user_proxy = UserProxyAgent(name=\"user_proxy\",\n",
- " code_execution_config={\n",
- " \"work_dir\": \"coding\"\n",
- " },\n",
+ "user_proxy = UserProxyAgent(\n",
+ " name=\"user_proxy\",\n",
+ " code_execution_config={\"work_dir\": \"coding\"},\n",
" is_termination_msg=lambda msg: \"TERMINATE\" in msg[\"content\"],\n",
" human_input_mode=\"NEVER\",\n",
- " max_consecutive_auto_reply=0)\n",
+ " max_consecutive_auto_reply=0,\n",
+ ")\n",
"\n",
"user_proxy.initiate_chat(oss_analyst, message=\"Top 10 developers with the most followers\")"
]
@@ -436,7 +433,10 @@
}
],
"source": [
- "user_proxy.initiate_chat(oss_analyst, message=\"List the top 10 developers with the most followers. When listing things, please put them in a table.\")"
+ "user_proxy.initiate_chat(\n",
+ " oss_analyst,\n",
+ " message=\"List the top 10 developers with the most followers. When listing things, please put them in a table.\",\n",
+ ")"
]
},
{
@@ -572,18 +572,11 @@
}
],
"source": [
- "from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent\n",
- "from autogen.agentchat import UserProxyAgent\n",
- "\n",
"# Initiate an agent equipped with code interpreter\n",
"coder_assistant = GPTAssistantAgent(\n",
" name=\"Coder_Assistant\",\n",
" llm_config={\n",
- " \"tools\": [\n",
- " {\n",
- " \"type\": \"code_interpreter\"\n",
- " }\n",
- " ],\n",
+ " \"tools\": [{\"type\": \"code_interpreter\"}],\n",
" \"config_list\": config_list,\n",
" },\n",
" instructions=\"You are an expert at solving math questions. Write code and run it to solve math problems. Reply TERMINATE when the task is solved and there is no problem.\",\n",
@@ -597,7 +590,7 @@
" \"use_docker\": False, # set to True or image name like \"python:3\" to use docker\n",
" },\n",
" human_input_mode=\"NEVER\",\n",
- " max_consecutive_auto_reply=0\n",
+ " max_consecutive_auto_reply=0,\n",
")"
]
},
@@ -634,7 +627,9 @@
}
],
"source": [
- "user_proxy.initiate_chat(coder_assistant, message=\"If $725x + 727y = 1500$ and $729x+ 731y = 1508$, what is the value of $x - y$ ?\")\n"
+ "user_proxy.initiate_chat(\n",
+ " coder_assistant, message=\"If $725x + 727y = 1500$ and $729x+ 731y = 1508$, what is the value of $x - y$ ?\"\n",
+ ")"
]
},
{
@@ -732,8 +727,10 @@
}
],
"source": [
- "user_proxy.initiate_chat(coder_assistant, message=\"If $725x + 727y = 1500$ and $729x+ 731y = 1508$, what is the value of $x - y$ ? After finding the values of variables, always explain how to find the solution.\")\n",
- "\n"
+ "user_proxy.initiate_chat(\n",
+ " coder_assistant,\n",
+ " message=\"If $725x + 727y = 1500$ and $729x+ 731y = 1508$, what is the value of $x - y$ ? After finding the values of variables, always explain how to find the solution.\",\n",
+ ")"
]
},
{
@@ -808,8 +805,11 @@
}
],
"source": [
- "user_proxy.initiate_chat(coder_assistant, message=\"If $725x + 727y = 1500$ and $729x+ 731y = 1508$, what is the value of $x - y$ ?\", clear_history=True)\n",
- "\n"
+ "user_proxy.initiate_chat(\n",
+ " coder_assistant,\n",
+ " message=\"If $725x + 727y = 1500$ and $729x+ 731y = 1508$, what is the value of $x - y$ ?\",\n",
+ " clear_history=True,\n",
+ ")"
]
},
{
@@ -875,13 +875,6 @@
}
],
"source": [
- "import logging\n",
- "import os\n",
- " \n",
- "from autogen import config_list_from_json\n",
- "from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent\n",
- "from autogen import UserProxyAgent\n",
- "\n",
"logger = logging.getLogger(__name__)\n",
"logger.setLevel(logging.WARNING)\n",
"\n",
@@ -891,22 +884,20 @@
"llm_config = {\n",
" \"config_list\": config_list,\n",
" \"assistant_id\": assistant_id,\n",
- " \"tools\": [\n",
- " {\n",
- " \"type\": \"retrieval\"\n",
- " }\n",
- " ],\n",
- " \"file_ids\": [\"file-HPDOsp8k4dz95QRx9bnfNJHp\"]\n",
+ " \"tools\": [{\"type\": \"retrieval\"}],\n",
+ " \"file_ids\": [\"file-HPDOsp8k4dz95QRx9bnfNJHp\"],\n",
"}\n",
"\n",
- "rag_assistant = GPTAssistantAgent(name=\"RAG_Assistant\",\n",
- " instructions=\"You are adapt at question answering\",\n",
- " llm_config=llm_config)\n",
+ "rag_assistant = GPTAssistantAgent(\n",
+ " name=\"RAG_Assistant\", instructions=\"You are adapt at question answering\", llm_config=llm_config\n",
+ ")\n",
"\n",
- "user_proxy = UserProxyAgent(name=\"user_proxy\",\n",
+ "user_proxy = UserProxyAgent(\n",
+ " name=\"user_proxy\",\n",
" code_execution_config=False,\n",
" is_termination_msg=lambda msg: \"TERMINATE\" in msg[\"content\"],\n",
- " human_input_mode=\"ALWAYS\")"
+ " human_input_mode=\"ALWAYS\",\n",
+ ")"
]
},
{
@@ -1040,7 +1031,10 @@
}
],
"source": [
- "user_proxy.initiate_chat(rag_assistant, message=\"What is the name of the class of agents in the file I gave you? When you answer a question based on a file, always report your confidence in the answer as a percentage.\")\n"
+ "user_proxy.initiate_chat(\n",
+ " rag_assistant,\n",
+ " message=\"What is the name of the class of agents in the file I gave you? When you answer a question based on a file, always report your confidence in the answer as a percentage.\",\n",
+ ")"
]
},
{
@@ -1091,7 +1085,9 @@
}
],
"source": [
- "user_proxy.initiate_chat(rag_assistant, message=\"What is the name of the class of agents in the file I gave you?\", clear_history=True)\n"
+ "user_proxy.initiate_chat(\n",
+ " rag_assistant, message=\"What is the name of the class of agents in the file I gave you?\", clear_history=True\n",
+ ")"
]
},
{
diff --git a/notebook/agentchat_teaching.ipynb b/notebook/agentchat_teaching.ipynb
index 61dac0688..6d7d34d15 100644
--- a/notebook/agentchat_teaching.ipynb
+++ b/notebook/agentchat_teaching.ipynb
@@ -83,7 +83,7 @@
"source": [
"import autogen\n",
"\n",
- "llm_config={\n",
+ "llm_config = {\n",
" \"timeout\": 600,\n",
" \"cache_seed\": 44, # change the seed for different trials\n",
" \"config_list\": autogen.config_list_from_json(\n",
@@ -282,7 +282,7 @@
],
"source": [
"task1 = \"\"\"\n",
- "Find arxiv papers that show how are people studying trust calibration in AI based systems \n",
+ "Find arxiv papers that show how are people studying trust calibration in AI based systems\n",
"\"\"\"\n",
"\n",
"user_proxy.initiate_chat(assistant, message=task1)"
@@ -671,7 +671,7 @@
}
],
"source": [
- "task3 = \"\"\"Use this data to generate a bar chart of domains and number of papers in that domain and save to a file \n",
+ "task3 = \"\"\"Use this data to generate a bar chart of domains and number of papers in that domain and save to a file\n",
"\"\"\"\n",
"user_proxy.initiate_chat(assistant, message=task3, clear_history=False)"
]
@@ -819,9 +819,9 @@
}
],
"source": [
- "task4 = \"\"\"Reflect on the sequence and create a recipe containing all the steps \n",
+ "task4 = \"\"\"Reflect on the sequence and create a recipe containing all the steps\n",
"necessary and name for it. Suggest well-documented, generalized python function(s)\n",
- " to perform similar tasks for coding steps in future. Make sure coding steps and \n",
+ " to perform similar tasks for coding steps in future. Make sure coding steps and\n",
" non-coding steps are never mixed in one function. In the docstr of the function(s),\n",
" clarify what non-coding steps are needed to use the language skill of the assistant.\n",
"\"\"\"\n",
@@ -889,7 +889,7 @@
"def search_arxiv(query: str, max_results: int = 10) -> List[Dict[str, str]]:\n",
" \"\"\"\n",
" Search arxiv for papers related to a specific query.\n",
- " \n",
+ "\n",
" :param query: The search query for arxiv papers.\n",
" :param max_results: The maximum number of results to return. Default is 10.\n",
" :return: A list of dictionaries containing the title, link, and summary of each paper.\n",
@@ -901,14 +901,14 @@
" url = f\"{base_url}{search_query}&start={start}&{max_results}\"\n",
" response = requests.get(url)\n",
" feed = feedparser.parse(response.content)\n",
- " \n",
+ "\n",
" papers = [{\"title\": entry.title, \"link\": entry.link, \"summary\": entry.summary} for entry in feed.entries]\n",
" return papers\n",
"\n",
"def generate_bar_chart(domains: Dict[str, int], output_file: str) -> None:\n",
" \"\"\"\n",
" Generate a bar chart of application domains and the number of papers in each domain, and save it as an image file.\n",
- " \n",
+ "\n",
" :param domains: A dictionary containing application domains as keys and the number of papers as values.\n",
" :param output_file: The name of the output image file.\n",
" \"\"\"\n",
diff --git a/notebook/agentchat_two_users.ipynb b/notebook/agentchat_two_users.ipynb
index 6ab0c9363..54fe9f4d0 100644
--- a/notebook/agentchat_two_users.ipynb
+++ b/notebook/agentchat_two_users.ipynb
@@ -137,7 +137,7 @@
" # final message sent from the expert\n",
" expert.send(\"summarize the solution and explain the answer in an easy-to-understand way\", assistant_for_expert)\n",
" # return the last message the expert received\n",
- " return expert.last_message()[\"content\"]\n"
+ " return expert.last_message()[\"content\"]"
]
},
{
@@ -180,7 +180,7 @@
" },\n",
" }\n",
" ],\n",
- " }\n",
+ " },\n",
")\n",
"\n",
"student = autogen.UserProxyAgent(\n",
@@ -465,7 +465,7 @@
"# the assistant receives a message from the student, which contains the task description\n",
"student.initiate_chat(\n",
" assistant_for_student,\n",
- " message=\"\"\"Find $a + b + c$, given that $x+y \\\\neq -1$ and \n",
+ " message=\"\"\"Find $a + b + c$, given that $x+y \\\\neq -1$ and\n",
"\\\\begin{align}\n",
"\tax + by + c & = x + 7,\\\\\n",
"\ta + bx + cy & = 2x + 6y,\\\\\n",
diff --git a/notebook/agentchat_video_transcript_translate_with_whisper.ipynb b/notebook/agentchat_video_transcript_translate_with_whisper.ipynb
index 2a34ee6ac..62d30b7f7 100644
--- a/notebook/agentchat_video_transcript_translate_with_whisper.ipynb
+++ b/notebook/agentchat_video_transcript_translate_with_whisper.ipynb
@@ -64,16 +64,18 @@
"metadata": {},
"outputs": [],
"source": [
- "from openai import OpenAI\n",
+ "import os\n",
+ "\n",
"import whisper\n",
- "import autogen\n",
"from moviepy.editor import VideoFileClip\n",
- "import os\n",
+ "from openai import OpenAI\n",
+ "\n",
+ "import autogen\n",
"\n",
"config_list = [\n",
" {\n",
- " 'model': 'gpt-4',\n",
- " 'api_key': os.getenv(\"OPENAI_API_KEY\"),\n",
+ " \"model\": \"gpt-4\",\n",
+ " \"api_key\": os.getenv(\"OPENAI_API_KEY\"),\n",
" }\n",
"]"
]
@@ -218,27 +220,31 @@
" start_time = 0\n",
"\n",
" # Iterate through the segments in the result\n",
- " for segment in result['segments']:\n",
+ " for segment in result[\"segments\"]:\n",
" # If new sentence starts, save the previous one and reset variables\n",
- " if segment['start'] != start_time and sentence:\n",
- " transcript.append({\n",
- " \"sentence\": sentence.strip() + \".\",\n",
- " \"timestamp_start\": start_time,\n",
- " \"timestamp_end\": segment['start']\n",
- " })\n",
+ " if segment[\"start\"] != start_time and sentence:\n",
+ " transcript.append(\n",
+ " {\n",
+ " \"sentence\": sentence.strip() + \".\",\n",
+ " \"timestamp_start\": start_time,\n",
+ " \"timestamp_end\": segment[\"start\"],\n",
+ " }\n",
+ " )\n",
" sentence = \"\"\n",
- " start_time = segment['start']\n",
+ " start_time = segment[\"start\"]\n",
"\n",
" # Add the word to the current sentence\n",
- " sentence += segment['text'] + \" \"\n",
+ " sentence += segment[\"text\"] + \" \"\n",
"\n",
" # Add the final sentence\n",
" if sentence:\n",
- " transcript.append({\n",
- " \"sentence\": sentence.strip() + \".\",\n",
- " \"timestamp_start\": start_time,\n",
- " \"timestamp_end\": result['segments'][-1]['end']\n",
- " })\n",
+ " transcript.append(\n",
+ " {\n",
+ " \"sentence\": sentence.strip() + \".\",\n",
+ " \"timestamp_start\": start_time,\n",
+ " \"timestamp_end\": result[\"segments\"][-1][\"end\"],\n",
+ " }\n",
+ " )\n",
"\n",
" # Save the transcript to a file\n",
" with open(\"transcription.txt\", \"w\") as file:\n",
@@ -255,7 +261,6 @@
" return f\"An unexpected error occurred: {str(e)}\"\n",
"\n",
"\n",
- "\n",
"def translate_text(input_text, source_language, target_language):\n",
" client = OpenAI(api_key=key)\n",
"\n",
@@ -263,11 +268,13 @@
" model=\"gpt-3.5-turbo\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
- " {\"role\": \"user\",\n",
- " \"content\": f\"Directly translate the following {source_language} text to a pure {target_language} \"\n",
- " f\"video subtitle text without additional explanation.: '{input_text}'\"},\n",
+ " {\n",
+ " \"role\": \"user\",\n",
+ " \"content\": f\"Directly translate the following {source_language} text to a pure {target_language} \"\n",
+ " f\"video subtitle text without additional explanation.: '{input_text}'\",\n",
+ " },\n",
" ],\n",
- " max_tokens=1500\n",
+ " max_tokens=1500,\n",
" )\n",
"\n",
" # Correctly accessing the response content\n",
@@ -283,7 +290,7 @@
"\n",
" for line in lines:\n",
" # Split each line into timestamp and text parts\n",
- " parts = line.strip().split(': ')\n",
+ " parts = line.strip().split(\": \")\n",
" if len(parts) == 2:\n",
" timestamp, text = parts[0], parts[1]\n",
" # Translate only the text part\n",
@@ -295,7 +302,7 @@
" # If the line doesn't contain a timestamp, add it as is\n",
" translated_transcript.append(line.strip())\n",
"\n",
- " return '\\n'.join(translated_transcript)\n",
+ " return \"\\n\".join(translated_transcript)\n",
"\n",
"\n",
"llm_config = {\n",
@@ -327,7 +334,7 @@
" \"target_language\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"target language\",\n",
- " }\n",
+ " },\n",
" },\n",
" \"required\": [\"source_language\", \"target_language\"],\n",
" },\n",
@@ -364,7 +371,7 @@
"user_proxy.initiate_chat(\n",
" chatbot,\n",
" message=f\"For the video located in {target_video}, recognize the speech and transfer it into a script file, \"\n",
- " f\"then translate from {source_language} text to a {target_language} video subtitle text. \",\n",
+ " f\"then translate from {source_language} text to a {target_language} video subtitle text. \",\n",
")"
]
}
diff --git a/notebook/agentchat_web_info.ipynb b/notebook/agentchat_web_info.ipynb
index 6bf5d1d3e..abf527c3e 100644
--- a/notebook/agentchat_web_info.ipynb
+++ b/notebook/agentchat_web_info.ipynb
@@ -77,7 +77,7 @@
" },\n",
")\n",
"\n",
- "llm_config={\n",
+ "llm_config = {\n",
" \"timeout\": 600,\n",
" \"cache_seed\": 42,\n",
" \"config_list\": config_list,\n",
@@ -149,7 +149,7 @@
" code_execution_config={\"work_dir\": \"web\"},\n",
" llm_config=llm_config,\n",
" system_message=\"\"\"Reply TERMINATE if the task has been solved at full satisfaction.\n",
- "Otherwise, reply CONTINUE, or the reason why the task is not solved yet.\"\"\"\n",
+ "Otherwise, reply CONTINUE, or the reason why the task is not solved yet.\"\"\",\n",
")"
]
},
diff --git a/notebook/agenteval_cq_math.ipynb b/notebook/agenteval_cq_math.ipynb
index 36cb0fe40..613d2b753 100644
--- a/notebook/agenteval_cq_math.ipynb
+++ b/notebook/agenteval_cq_math.ipynb
@@ -81,6 +81,15 @@
},
"outputs": [],
"source": [
+ "import json\n",
+ "\n",
+ "import os\n",
+ "from pathlib import Path\n",
+ "\n",
+ "import matplotlib.pyplot as plt\n",
+ "import numpy as np\n",
+ "import scipy.stats as stats\n",
+ "\n",
"import autogen\n",
"\n",
"config_list = autogen.config_list_from_json(\n",
@@ -389,8 +398,6 @@
},
"outputs": [],
"source": [
- "import json\n",
- "\n",
"criteria_file = f\"../test/test_files/agenteval-in-out/{current_task_name}_criteria.json\"\n",
"quantifier = autogen.AssistantAgent(\n",
" name=\"quantifier\",\n",
@@ -429,9 +436,6 @@
},
"outputs": [],
"source": [
- "import os\n",
- "\n",
- "\n",
"def get_quantifier(file, criteria_file):\n",
" \"\"\"\n",
" Running quantifier agent on individual log.\n",
@@ -447,7 +451,7 @@
"\n",
" test_case, actual_label = read_without_groundtruth(file)\n",
" print(\"actual label for this case: \", actual_label)\n",
- " cq_results = quantifier_user.initiate_chat(\n",
+ " cq_results = quantifier_user.initiate_chat( # noqa: F841\n",
" quantifier,\n",
" message=sys_msg\n",
" + \"Evaluation dictionary: \"\n",
@@ -738,7 +742,8 @@
}
],
"source": [
- "from pathlib import Path\n",
+ "# You can set your own log path - we also limited the number of samples to avoid additional costs.\n",
+ "# By removing the condition about limitations on the number of samples per category, you can run it on all 120 problems\n",
"\n",
"log_path = \"../test/test_files/agenteval-in-out/agentchat_results/\"\n",
"\n",
@@ -3421,10 +3426,6 @@
}
],
"source": [
- "# You can set your own log path - we also limited the number of samples to avoid additional costs.\n",
- "# By removing the condition about limitations on the number of samples per category, you can run it on all 120 problems\n",
- "import os\n",
- "\n",
"# log_path = \"../test/test_files/agenteval-in-out/agentchat_results/\"\n",
"criteria_file = \"../test/test_files/agenteval-in-out/samples/sample_math_criteria.json\"\n",
"outcome = {}\n",
@@ -3486,15 +3487,11 @@
}
],
"source": [
- "import matplotlib.pyplot as plt\n",
- "import numpy as np\n",
- "import scipy.stats as stats\n",
- "\n",
"# computing average and 95% interval for failed and successful cases on all criteria\n",
"try:\n",
" # convert the criteria to dict type if it is already not\n",
" dictionary_for_eval = eval(open(criteria_file, \"r\").read())\n",
- "except:\n",
+ "except: # noqa: E722\n",
" pass\n",
"\n",
"criteria = list(dictionary_for_eval.keys())\n",
@@ -3522,7 +3519,7 @@
" task[\"f\"].append(nl2int[tmp_dic[criterion]])\n",
" else:\n",
" task[\"s\"].append(nl2int[tmp_dic[criterion]])\n",
- " except:\n",
+ " except: # noqa: E722\n",
" pass\n",
"\n",
" average_f[criterion] = np.mean(task[\"f\"])\n",
diff --git a/notebook/autobuild_agent_library.ipynb b/notebook/autobuild_agent_library.ipynb
index 07bf9b48e..e16c1ebe9 100644
--- a/notebook/autobuild_agent_library.ipynb
+++ b/notebook/autobuild_agent_library.ipynb
@@ -63,20 +63,19 @@
},
"outputs": [],
"source": [
+ "import json\n",
+ "\n",
"import autogen\n",
+ "from autogen.agentchat.contrib.agent_builder import AgentBuilder\n",
"\n",
+ "config_file_or_env = \"OAI_CONFIG_LIST\" # modify path\n",
+ "llm_config = {\"temperature\": 0}\n",
+ "config_list = autogen.config_list_from_json(config_file_or_env, filter_dict={\"model\": [\"gpt-4-1106-preview\", \"gpt-4\"]})\n",
"\n",
- "config_file_or_env = 'OAI_CONFIG_LIST' # modify path\n",
- "llm_config = {\n",
- " 'temperature': 0\n",
- "}\n",
- "config_list = autogen.config_list_from_json(config_file_or_env, filter_dict={\"model\": [\"gpt-4-1106-preview\", 'gpt-4']})\n",
"\n",
"def start_task(execution_task: str, agent_list: list):\n",
" group_chat = autogen.GroupChat(agents=agent_list, messages=[], max_round=12)\n",
- " manager = autogen.GroupChatManager(\n",
- " groupchat=group_chat, llm_config={\"config_list\": config_list, **llm_config}\n",
- " )\n",
+ " manager = autogen.GroupChatManager(groupchat=group_chat, llm_config={\"config_list\": config_list, **llm_config})\n",
" agent_list[0].initiate_chat(manager, message=execution_task)"
]
},
@@ -141,7 +140,7 @@
" \"IT_Specialist\",\n",
" \"Cybersecurity_Expert\",\n",
" \"Artificial_Intelligence_Engineer\",\n",
- " \"Financial_Analyst\"\n",
+ " \"Financial_Analyst\",\n",
"]"
]
},
@@ -164,17 +163,21 @@
"sys_msg_list = []\n",
"\n",
"for pos in position_list:\n",
- " resp_agent_sys_msg = build_manager.create(\n",
- " messages=[\n",
- " {\n",
- " \"role\": \"user\",\n",
- " \"content\": AGENT_SYS_MSG_PROMPT.format(\n",
- " position=pos,\n",
- " default_sys_msg=autogen.AssistantAgent.DEFAULT_SYSTEM_MESSAGE,\n",
- " ),\n",
- " }\n",
- " ]\n",
- " ).choices[0].message.content\n",
+ " resp_agent_sys_msg = (\n",
+ " build_manager.create(\n",
+ " messages=[\n",
+ " {\n",
+ " \"role\": \"user\",\n",
+ " \"content\": AGENT_SYS_MSG_PROMPT.format(\n",
+ " position=pos,\n",
+ " default_sys_msg=autogen.AssistantAgent.DEFAULT_SYSTEM_MESSAGE,\n",
+ " ),\n",
+ " }\n",
+ " ]\n",
+ " )\n",
+ " .choices[0]\n",
+ " .message.content\n",
+ " )\n",
" sys_msg_list.append({\"name\": pos, \"profile\": resp_agent_sys_msg})"
],
"metadata": {
@@ -236,9 +239,7 @@
"execution_count": 6,
"outputs": [],
"source": [
- "import json\n",
- "\n",
- "json.dump(sys_msg_list, open('./agent_library_example.json', 'w'), indent=4)"
+ "json.dump(sys_msg_list, open(\"./agent_library_example.json\", \"w\"), indent=4)"
],
"metadata": {
"collapsed": false,
@@ -267,7 +268,7 @@
"execution_count": 8,
"outputs": [],
"source": [
- "library_path_or_json = './agent_library_example.json'\n",
+ "library_path_or_json = \"./agent_library_example.json\"\n",
"building_task = \"Find a paper on arxiv by programming, and analyze its application in some domain. For example, find a recent paper about gpt-4 on arxiv and find its potential applications in software.\""
],
"metadata": {
@@ -306,11 +307,11 @@
"Creating agent Programmer with backbone gpt-4-1106-preview...\n",
"Creating agent Artificial_Intelligence_Engineer with backbone gpt-4-1106-preview...\n",
"Adding user console proxy...\n",
- "\u001B[33mUser_console_and_Python_code_interpreter\u001B[0m (to chat_manager):\n",
+ "\u001b[33mUser_console_and_Python_code_interpreter\u001b[0m (to chat_manager):\n",
"Find a recent paper about explainable AI on arxiv and find its potential applications in medical.\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[33mArtificial_Intelligence_Engineer\u001B[0m (to chat_manager):\n",
+ "\u001b[33mArtificial_Intelligence_Engineer\u001b[0m (to chat_manager):\n",
"\n",
"To find a recent paper about explainable AI on arXiv and explore its potential applications in the medical field, I will perform the following steps:\n",
"\n",
@@ -356,9 +357,9 @@
"Once we have the paper, we can move on to step 2 and analyze its content for potential medical applications. Since I cannot execute Python code directly, you would need to run the script on your local machine or development environment. After running the script, you can share the paper's title and abstract here, and we can discuss its potential applications in the medical field.\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[31m\n",
- ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001B[0m\n",
- "\u001B[33mUser_console_and_Python_code_interpreter\u001B[0m (to chat_manager):\n",
+ "\u001b[31m\n",
+ ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
+ "\u001b[33mUser_console_and_Python_code_interpreter\u001b[0m (to chat_manager):\n",
"\n",
"exitcode: 0 (execution succeeded)\n",
"Code output: \n",
@@ -396,7 +397,7 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[33mArtificial_Intelligence_Engineer\u001B[0m (to chat_manager):\n",
+ "\u001b[33mArtificial_Intelligence_Engineer\u001b[0m (to chat_manager):\n",
"\n",
"The paper you've found, titled \"Transparency and Privacy: The Role of Explainable AI and Federated Learning in Financial Fraud Detection,\" discusses the use of Explainable AI (XAI) and Federated Learning (FL) in the context of financial fraud detection. While the paper's primary focus is on the financial industry, the concepts of XAI and FL can be applied to the medical field as well.\n",
"\n",
@@ -421,11 +422,11 @@
"To discuss these findings with your team, you can highlight the parallels between the financial and medical domains in terms of data privacy and the need for transparency in AI systems. The paper's approach can be a starting point for developing similar systems in healthcare that benefit from the privacy-preserving and explainable nature of the technologies discussed.\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[33mUser_console_and_Python_code_interpreter\u001B[0m (to chat_manager):\n",
+ "\u001b[33mUser_console_and_Python_code_interpreter\u001b[0m (to chat_manager):\n",
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[33mSoftware_Developer\u001B[0m (to chat_manager):\n",
+ "\u001b[33mSoftware_Developer\u001b[0m (to chat_manager):\n",
"\n",
"TERMINATE\n",
"\n",
@@ -435,13 +436,13 @@
}
],
"source": [
- "from autogen.agentchat.contrib.agent_builder import AgentBuilder\n",
- "\n",
- "new_builder = AgentBuilder(config_file_or_env=config_file_or_env, builder_model='gpt-4-1106-preview', agent_model='gpt-4-1106-preview')\n",
+ "new_builder = AgentBuilder(\n",
+ " config_file_or_env=config_file_or_env, builder_model=\"gpt-4-1106-preview\", agent_model=\"gpt-4-1106-preview\"\n",
+ ")\n",
"agent_list, _ = new_builder.build_from_library(building_task, library_path_or_json, llm_config)\n",
"start_task(\n",
" execution_task=\"Find a recent paper about explainable AI on arxiv and find its potential applications in medical.\",\n",
- " agent_list=agent_list\n",
+ " agent_list=agent_list,\n",
")\n",
"new_builder.clear_all_agents()"
],
@@ -485,11 +486,11 @@
"Creating agent Physicist with backbone gpt-4-1106-preview...\n",
"Creating agent Data_Analyst with backbone gpt-4-1106-preview...\n",
"Adding user console proxy...\n",
- "\u001B[33mUser_console_and_Python_code_interpreter\u001B[0m (to chat_manager):\n",
+ "\u001b[33mUser_console_and_Python_code_interpreter\u001b[0m (to chat_manager):\n",
"Find a recent paper about gpt-4 on arxiv and find its potential applications in software.\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[33mProgrammer\u001B[0m (to chat_manager):\n",
+ "\u001b[33mProgrammer\u001b[0m (to chat_manager):\n",
"\n",
"To find a recent paper about GPT-4 on arXiv and analyze its potential applications in software, I would typically write a Python script that uses the arXiv API to search for papers related to GPT-4. However, as an AI, I can't execute scripts or make live API calls. Instead, I can guide you through the process of writing such a script.\n",
"\n",
@@ -541,11 +542,11 @@
"Remember, the actual applications will depend on the content of the papers, which might include but are not limited to natural language processing, content generation, automation, decision support systems, and more.\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[31m\n",
- ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001B[0m\n",
- "\u001B[31m\n",
- ">>>>>>>> EXECUTING CODE BLOCK 1 (inferred language is bash)...\u001B[0m\n",
- "\u001B[33mUser_console_and_Python_code_interpreter\u001B[0m (to chat_manager):\n",
+ "\u001b[31m\n",
+ ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
+ "\u001b[31m\n",
+ ">>>>>>>> EXECUTING CODE BLOCK 1 (inferred language is bash)...\u001b[0m\n",
+ "\u001b[33mUser_console_and_Python_code_interpreter\u001b[0m (to chat_manager):\n",
"\n",
"exitcode: 0 (execution succeeded)\n",
"Code output: \n",
@@ -793,7 +794,7 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[33mSoftware_Developer\u001B[0m (to chat_manager):\n",
+ "\u001b[33mSoftware_Developer\u001b[0m (to chat_manager):\n",
"\n",
"It appears that the code has been executed and the output lists several papers related to GPT-4 from arXiv. Let's analyze the potential applications in software based on the abstracts provided:\n",
"\n",
@@ -820,7 +821,7 @@
"In summary, the potential applications of GPT-4 in software are vast, ranging from healthcare diagnostics to educational tools, data analysis, and NLP applications. However, limitations in reasoning, robustness, and novel problem-solving must be considered when integrating GPT-4 into software solutions.\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[33mMathematician\u001B[0m (to chat_manager):\n",
+ "\u001b[33mMathematician\u001b[0m (to chat_manager):\n",
"\n",
"TERMINATE\n",
"\n",
@@ -830,13 +831,15 @@
}
],
"source": [
- "from autogen.agentchat.contrib.agent_builder import AgentBuilder\n",
- "\n",
- "new_builder = AgentBuilder(config_file_or_env=config_file_or_env, builder_model='gpt-4-1106-preview', agent_model='gpt-4-1106-preview')\n",
- "agent_list, _ = new_builder.build_from_library(building_task, library_path_or_json, llm_config, embedding_model=\"all-mpnet-base-v2\")\n",
+ "new_builder = AgentBuilder(\n",
+ " config_file_or_env=config_file_or_env, builder_model=\"gpt-4-1106-preview\", agent_model=\"gpt-4-1106-preview\"\n",
+ ")\n",
+ "agent_list, _ = new_builder.build_from_library(\n",
+ " building_task, library_path_or_json, llm_config, embedding_model=\"all-mpnet-base-v2\"\n",
+ ")\n",
"start_task(\n",
" execution_task=\"Find a recent paper about gpt-4 on arxiv and find its potential applications in software.\",\n",
- " agent_list=agent_list\n",
+ " agent_list=agent_list,\n",
")\n",
"new_builder.clear_all_agents()"
],
diff --git a/notebook/autobuild_basic.ipynb b/notebook/autobuild_basic.ipynb
index 416150baf..f1350083f 100644
--- a/notebook/autobuild_basic.ipynb
+++ b/notebook/autobuild_basic.ipynb
@@ -68,17 +68,14 @@
"import autogen\n",
"from autogen.agentchat.contrib.agent_builder import AgentBuilder\n",
"\n",
- "config_file_or_env = 'OAI_CONFIG_LIST'\n",
- "llm_config = {\n",
- " 'temperature': 0\n",
- "}\n",
+ "config_file_or_env = \"OAI_CONFIG_LIST\"\n",
+ "llm_config = {\"temperature\": 0}\n",
"config_list = autogen.config_list_from_json(config_file_or_env, filter_dict={\"model\": [\"gpt-4-1106-preview\", \"gpt-4\"]})\n",
"\n",
+ "\n",
"def start_task(execution_task: str, agent_list: list):\n",
" group_chat = autogen.GroupChat(agents=agent_list, messages=[], max_round=12)\n",
- " manager = autogen.GroupChatManager(\n",
- " groupchat=group_chat, llm_config={\"config_list\": config_list, **llm_config}\n",
- " )\n",
+ " manager = autogen.GroupChatManager(groupchat=group_chat, llm_config={\"config_list\": config_list, **llm_config})\n",
" agent_list[0].initiate_chat(manager, message=execution_task)"
]
},
@@ -107,7 +104,9 @@
},
"outputs": [],
"source": [
- "builder = AgentBuilder(config_file_or_env=config_file_or_env, builder_model='gpt-4-1106-preview', agent_model='gpt-4-1106-preview')"
+ "builder = AgentBuilder(\n",
+ " config_file_or_env=config_file_or_env, builder_model=\"gpt-4-1106-preview\", agent_model=\"gpt-4-1106-preview\"\n",
+ ")"
]
},
{
@@ -224,11 +223,11 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "\u001B[33mUser_console_and_code_interpreter\u001B[0m (to chat_manager):\n",
+ "\u001b[33mUser_console_and_code_interpreter\u001b[0m (to chat_manager):\n",
"Find a recent paper about gpt-4 on arxiv and find its potential applications in software.\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[33mArXiv_Data_Scraper_Developer\u001B[0m (to chat_manager):\n",
+ "\u001b[33mArXiv_Data_Scraper_Developer\u001b[0m (to chat_manager):\n",
"\n",
"To find a recent paper about GPT-4 on arXiv and its potential applications in software, we'll need to perform a few steps:\n",
"\n",
@@ -267,9 +266,9 @@
"Please note that the search query might need to be adjusted based on the actual terminology used in the papers and the specificity of the results you're looking for. If you encounter any issues or need further assistance, let me know!\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[31m\n",
- ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001B[0m\n",
- "\u001B[33mUser_console_and_code_interpreter\u001B[0m (to chat_manager):\n",
+ "\u001b[31m\n",
+ ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
+ "\u001b[33mUser_console_and_code_interpreter\u001b[0m (to chat_manager):\n",
"\n",
"exitcode: 0 (execution succeeded)\n",
"Code output: \n",
@@ -519,7 +518,7 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[33mML_Paper_Summarization_Specialist\u001B[0m (to chat_manager):\n",
+ "\u001b[33mML_Paper_Summarization_Specialist\u001b[0m (to chat_manager):\n",
"\n",
"Based on the recent papers found on arXiv, here are the potential applications of GPT-4 in software:\n",
"\n",
@@ -574,7 +573,7 @@
"source": [
"start_task(\n",
" execution_task=\"Find a recent paper about gpt-4 on arxiv and find its potential applications in software.\",\n",
- " agent_list=agent_list\n",
+ " agent_list=agent_list,\n",
")"
]
},
@@ -731,11 +730,11 @@
"Creating agent Data_Analysis_Engineer with backbone gpt-4-1106-preview...\n",
"Creating agent ML_Paper_Summarization_Specialist with backbone gpt-4-1106-preview...\n",
"Adding user console proxy...\n",
- "\u001B[33mUser_console_and_code_interpreter\u001B[0m (to chat_manager):\n",
+ "\u001b[33mUser_console_and_code_interpreter\u001b[0m (to chat_manager):\n",
"Find a recent paper about LLaVA on arxiv and find its potential applications in computer vision.\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[33mArXiv_Data_Scraper_Developer\u001B[0m (to chat_manager):\n",
+ "\u001b[33mArXiv_Data_Scraper_Developer\u001b[0m (to chat_manager):\n",
"\n",
"To find a recent paper about LLaVA on arXiv and its potential applications in computer vision, we'll need to perform a search on the arXiv API. I'll write a Python script that uses the `arxiv` library to query the arXiv database for papers related to \"LLaVA\" and \"computer vision\". If the `arxiv` library is not available, we can use the `requests` library to make a direct HTTP request to the arXiv API.\n",
"\n",
@@ -772,9 +771,9 @@
"If you encounter any issues with the script or if you need further assistance, please let me know.\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[31m\n",
- ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001B[0m\n",
- "\u001B[33mUser_console_and_code_interpreter\u001B[0m (to chat_manager):\n",
+ "\u001b[31m\n",
+ ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
+ "\u001b[33mUser_console_and_code_interpreter\u001b[0m (to chat_manager):\n",
"\n",
"exitcode: 0 (execution succeeded)\n",
"Code output: \n",
@@ -1049,7 +1048,7 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[33mML_Paper_Summarization_Specialist\u001B[0m (to chat_manager):\n",
+ "\u001b[33mML_Paper_Summarization_Specialist\u001b[0m (to chat_manager):\n",
"\n",
"Based on the recent papers extracted from arXiv, here are the potential applications in computer vision for the LLaVA framework and related technologies:\n",
"\n",
@@ -1076,7 +1075,7 @@
"These applications demonstrate the versatility of LLaVA and related technologies in enhancing computer vision tasks, from medical imaging to interactive systems and efficient model deployment on resource-constrained devices.\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[33mML_Paper_Summarization_Specialist\u001B[0m (to chat_manager):\n",
+ "\u001b[33mML_Paper_Summarization_Specialist\u001b[0m (to chat_manager):\n",
"\n",
"TERMINATE\n",
"\n",
@@ -1087,10 +1086,12 @@
],
"source": [
"new_builder = AgentBuilder(config_file_or_env=config_file_or_env)\n",
- "agent_list, agent_configs = new_builder.load('./save_config_c52224ebd16a2e60b348f3f04ac15e79.json') # load previous agent configs\n",
+ "agent_list, agent_configs = new_builder.load(\n",
+ " \"./save_config_c52224ebd16a2e60b348f3f04ac15e79.json\"\n",
+ ") # load previous agent configs\n",
"start_task(\n",
" execution_task=\"Find a recent paper about LLaVA on arxiv and find its potential applications in computer vision.\",\n",
- " agent_list=agent_list\n",
+ " agent_list=agent_list,\n",
")\n",
"new_builder.clear_all_agents()"
]
@@ -1145,11 +1146,11 @@
"Creating agent Machine_Learning_Model_Architect with backbone gpt-4-1106-preview...\n",
"Creating agent Data_Extraction_Scripting_Engineer with backbone gpt-4-1106-preview...\n",
"Adding user console proxy...\n",
- "\u001B[33mUser_console_and_code_interpreter\u001B[0m (to chat_manager):\n",
+ "\u001b[33mUser_console_and_code_interpreter\u001b[0m (to chat_manager):\n",
"Find a recent paper about explainable AI on arxiv and find its potential applications in medical.\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[33mArXiv_CS_Medical_Paper_Finder_Developer\u001B[0m (to chat_manager):\n",
+ "\u001b[33mArXiv_CS_Medical_Paper_Finder_Developer\u001b[0m (to chat_manager):\n",
"\n",
"To perform this task, we will first write a Python script to fetch papers related to \"explainable AI\" from arXiv that are also relevant to the medical field. We will use the `arxiv` library, which is a Python wrapper for the arXiv API. If you don't have the `arxiv` library installed, you can install it using the following command:\n",
"\n",
@@ -1201,11 +1202,11 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[31m\n",
- ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is bash)...\u001B[0m\n",
- "\u001B[31m\n",
- ">>>>>>>> EXECUTING CODE BLOCK 1 (inferred language is python)...\u001B[0m\n",
- "\u001B[33mUser_console_and_code_interpreter\u001B[0m (to chat_manager):\n",
+ "\u001b[31m\n",
+ ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is bash)...\u001b[0m\n",
+ "\u001b[31m\n",
+ ">>>>>>>> EXECUTING CODE BLOCK 1 (inferred language is python)...\u001b[0m\n",
+ "\u001b[33mUser_console_and_code_interpreter\u001b[0m (to chat_manager):\n",
"\n",
"exitcode: 1 (execution failed)\n",
"Code output: \n",
@@ -1225,7 +1226,7 @@
"AttributeError: 'Search' object has no attribute 'get'\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[33mArXiv_CS_Medical_Paper_Finder_Developer\u001B[0m (to chat_manager):\n",
+ "\u001b[33mArXiv_CS_Medical_Paper_Finder_Developer\u001b[0m (to chat_manager):\n",
"\n",
"It seems the script encountered an error because the `Search` object from the `arxiv` library does not have a `get` attribute, which is inconsistent with the usual usage of this library. Let's correct that.\n",
"\n",
@@ -1269,9 +1270,9 @@
"Run the above script, and it should now correctly fetch and print the details of the most recent paper related to explainable AI that has potential applications in the medical field. This fix should address the AttributeError by correctly utilizing the `results()` method provided by the `arxiv` library to obtain search results.\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[31m\n",
- ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001B[0m\n",
- "\u001B[33mUser_console_and_code_interpreter\u001B[0m (to chat_manager):\n",
+ "\u001b[31m\n",
+ ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
+ "\u001b[33mUser_console_and_code_interpreter\u001b[0m (to chat_manager):\n",
"\n",
"exitcode: 0 (execution succeeded)\n",
"Code output: \n",
@@ -1307,7 +1308,7 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[33mComputational_Biology_Research_Analyst\u001B[0m (to chat_manager):\n",
+ "\u001b[33mComputational_Biology_Research_Analyst\u001b[0m (to chat_manager):\n",
"\n",
"Based on the output, we have identified a recent paper from arXiv:\n",
"\n",
@@ -1343,7 +1344,7 @@
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
- "\u001B[33mComputer_Science_Literature_Review_Specialist\u001B[0m (to chat_manager):\n",
+ "\u001b[33mComputer_Science_Literature_Review_Specialist\u001b[0m (to chat_manager):\n",
"TERMINATE\n",
"\n",
"\n",
@@ -1353,11 +1354,15 @@
}
],
"source": [
- "new_builder = AgentBuilder(config_file_or_env=config_file_or_env, builder_model='gpt-4-1106-preview', agent_model='gpt-4-1106-preview')\n",
- "agent_list, agent_configs = new_builder.build(building_task, llm_config, use_oai_assistant=True) # Transfer to OpenAI assistant API.\n",
+ "new_builder = AgentBuilder(\n",
+ " config_file_or_env=config_file_or_env, builder_model=\"gpt-4-1106-preview\", agent_model=\"gpt-4-1106-preview\"\n",
+ ")\n",
+ "agent_list, agent_configs = new_builder.build(\n",
+ " building_task, llm_config, use_oai_assistant=True\n",
+ ") # Transfer to OpenAI assistant API.\n",
"start_task(\n",
" execution_task=\"Find a recent paper about explainable AI on arxiv and find its potential applications in medical.\",\n",
- " agent_list=agent_list\n",
+ " agent_list=agent_list,\n",
")\n",
"new_builder.clear_all_agents()"
]
diff --git a/notebook/oai_chatgpt_gpt4.ipynb b/notebook/oai_chatgpt_gpt4.ipynb
index c3c99ead5..23d68ed84 100644
--- a/notebook/oai_chatgpt_gpt4.ipynb
+++ b/notebook/oai_chatgpt_gpt4.ipynb
@@ -78,7 +78,12 @@
},
"outputs": [],
"source": [
- "import autogen"
+ "import logging\n",
+ "\n",
+ "import datasets\n",
+ "\n",
+ "import autogen\n",
+ "from autogen.math_utils import eval_math_responses"
]
},
{
@@ -171,8 +176,6 @@
}
],
"source": [
- "import datasets\n",
- "\n",
"seed = 41\n",
"data = datasets.load_dataset(\"competition_math\")\n",
"train_data = data[\"train\"].shuffle(seed=seed)\n",
@@ -183,16 +186,18 @@
" \"problem\": train_data[x][\"problem\"],\n",
" \"solution\": train_data[x][\"solution\"],\n",
" }\n",
- " for x in range(len(train_data)) if train_data[x][\"level\"] == \"Level 2\" and train_data[x][\"type\"] == \"Algebra\"\n",
+ " for x in range(len(train_data))\n",
+ " if train_data[x][\"level\"] == \"Level 2\" and train_data[x][\"type\"] == \"Algebra\"\n",
"][:n_tune_data]\n",
"test_data = [\n",
" {\n",
" \"problem\": test_data[x][\"problem\"],\n",
" \"solution\": test_data[x][\"solution\"],\n",
" }\n",
- " for x in range(len(test_data)) if test_data[x][\"level\"] == \"Level 2\" and test_data[x][\"type\"] == \"Algebra\"\n",
+ " for x in range(len(test_data))\n",
+ " if test_data[x][\"level\"] == \"Level 2\" and test_data[x][\"type\"] == \"Algebra\"\n",
"]\n",
- "print(len(tune_data), len(test_data))\n"
+ "print(len(tune_data), len(test_data))"
]
},
{
@@ -280,22 +285,6 @@
"Before we start tuning, we must define the success metric we want to optimize. For each math task, we use voting to select a response with the most common answers out of all the generated responses. We consider the task successfully solved if it has an equivalent answer to the canonical solution. Then we can optimize the mean success rate of a collection of tasks."
]
},
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {
- "execution": {
- "iopub.execute_input": "2023-02-13T23:40:54.626998Z",
- "iopub.status.busy": "2023-02-13T23:40:54.626593Z",
- "iopub.status.idle": "2023-02-13T23:40:54.631383Z",
- "shell.execute_reply": "2023-02-13T23:40:54.630770Z"
- }
- },
- "outputs": [],
- "source": [
- "from autogen.math_utils import eval_math_responses"
- ]
- },
{
"attachments": {},
"cell_type": "markdown",
@@ -387,9 +376,9 @@
},
"outputs": [],
"source": [
- "import logging\n",
- "\n",
- "prompts = [\"{problem} Solve the problem carefully. Simplify your answer as much as possible. Put the final answer in \\\\boxed{{}}.\"]\n",
+ "prompts = [\n",
+ " \"{problem} Solve the problem carefully. Simplify your answer as much as possible. Put the final answer in \\\\boxed{{}}.\"\n",
+ "]\n",
"config, analysis = autogen.ChatCompletion.tune(\n",
" data=tune_data, # the data for tuning\n",
" metric=\"success_vote\", # the metric to optimize\n",
@@ -407,7 +396,7 @@
" config_list=config_list, # the endpoint list\n",
" allow_format_str_template=True, # whether to allow format string template\n",
" # logging_level=logging.INFO, # the logging level\n",
- ")\n"
+ ")"
]
},
{
@@ -1671,7 +1660,7 @@
"response = autogen.ChatCompletion.create(context=tune_data[1], config_list=config_list, **config)\n",
"metric_results = eval_math_responses(autogen.ChatCompletion.extract_text(response), **tune_data[1])\n",
"print(\"response on an example data instance:\", response)\n",
- "print(\"metric_results on the example data instance:\", metric_results)\n"
+ "print(\"metric_results on the example data instance:\", metric_results)"
]
},
{
@@ -1767,7 +1756,7 @@
"\n",
"# config_n2 = {\"model\": 'gpt-4', \"prompt\": prompts[0], \"n\": 2, \"allow_format_str_template\": True}\n",
"# result_n2 = autogen.ChatCompletion.test(test_data, config_list=config_list, **config_n2)\n",
- "# print(\"performance on test data from gpt-4 with a default config and n=2:\", result_n2)\n"
+ "# print(\"performance on test data from gpt-4 with a default config and n=2:\", result_n2)"
]
},
{
diff --git a/notebook/oai_client_cost.ipynb b/notebook/oai_client_cost.ipynb
index 6ee5d7e4f..7798f315a 100644
--- a/notebook/oai_client_cost.ipynb
+++ b/notebook/oai_client_cost.ipynb
@@ -48,6 +48,7 @@
"outputs": [],
"source": [
"import autogen\n",
+ "from autogen import OpenAIWrapper\n",
"\n",
"# config_list = autogen.config_list_from_json(\n",
"# \"OAI_CONFIG_LIST\",\n",
@@ -112,10 +113,10 @@
}
],
"source": [
- "from autogen import OpenAIWrapper\n",
- "\n",
"client = OpenAIWrapper(config_list=config_list)\n",
- "messages = [{'role': 'user', 'content': 'Can you give me 3 useful tips on learning Python? Keep it simple and short.'},]\n",
+ "messages = [\n",
+ " {\"role\": \"user\", \"content\": \"Can you give me 3 useful tips on learning Python? Keep it simple and short.\"},\n",
+ "]\n",
"response = client.create(messages=messages, model=\"gpt-35-turbo-1106\", cache_seed=None)\n",
"print(response.cost)"
]
@@ -143,11 +144,11 @@
}
],
"source": [
- "from autogen import OpenAIWrapper\n",
- "\n",
"client = OpenAIWrapper(config_list=config_list)\n",
- "messages = [{'role': 'user', 'content': 'Can you give me 3 useful tips on learning Python? Keep it simple and short.'},]\n",
- "client.print_usage_summary() # print usage summary"
+ "messages = [\n",
+ " {\"role\": \"user\", \"content\": \"Can you give me 3 useful tips on learning Python? Keep it simple and short.\"},\n",
+ "]\n",
+ "client.print_usage_summary() # print usage summary"
]
},
{
@@ -184,9 +185,9 @@
"# The first creation\n",
"# By default, cache_seed is set to 41 and enabled. If you don't want to use cache, set cache_seed to None.\n",
"response = client.create(messages=messages, model=\"gpt-35-turbo-1106\", cache_seed=41)\n",
- "client.print_usage_summary() # default to [\"actual\", \"total\"]\n",
- "client.print_usage_summary(mode='actual') # print actual usage summary\n",
- "client.print_usage_summary(mode='total') # print total usage summary"
+ "client.print_usage_summary() # default to [\"actual\", \"total\"]\n",
+ "client.print_usage_summary(mode=\"actual\") # print actual usage summary\n",
+ "client.print_usage_summary(mode=\"total\") # print total usage summary"
]
},
{
@@ -232,7 +233,7 @@
}
],
"source": [
- "# Since cache is enabled, the same completion will be returned from cache, which will not incur any actual cost. \n",
+ "# Since cache is enabled, the same completion will be returned from cache, which will not incur any actual cost.\n",
"# So actual cost doesn't change but total cost doubles.\n",
"response = client.create(messages=messages, model=\"gpt-35-turbo-1106\", cache_seed=41)\n",
"client.print_usage_summary()"
@@ -253,7 +254,7 @@
],
"source": [
"# clear usage summary\n",
- "client.clear_usage_summary() \n",
+ "client.clear_usage_summary()\n",
"client.print_usage_summary()"
]
},
diff --git a/notebook/oai_completion.ipynb b/notebook/oai_completion.ipynb
index d3aaacb17..c6eb95622 100644
--- a/notebook/oai_completion.ipynb
+++ b/notebook/oai_completion.ipynb
@@ -81,6 +81,10 @@
},
"outputs": [],
"source": [
+ "from functools import partial\n",
+ "\n",
+ "import datasets\n",
+ "\n",
"import autogen\n",
"\n",
"endpoint_list = autogen.config_list_openai_aoai()\n",
@@ -136,7 +140,7 @@
"# 'api_type': 'azure',\n",
"# 'api_version': '2023-06-01-preview',\n",
"# }, # another Azure OpenAI API endpoint for gpt-3.5-turbo with deployment name gpt-35-turbo-v0301\n",
- "# ]\n"
+ "# ]"
]
},
{
@@ -193,8 +197,6 @@
}
],
"source": [
- "import datasets\n",
- "\n",
"seed = 41\n",
"data = datasets.load_dataset(\"openai_humaneval\")[\"test\"].shuffle(seed=seed)\n",
"n_tune_data = 20\n",
@@ -213,7 +215,7 @@
" \"entry_point\": data[x][\"entry_point\"],\n",
" }\n",
" for x in range(n_tune_data, len(data))\n",
- "]\n"
+ "]"
]
},
{
@@ -338,15 +340,13 @@
},
"outputs": [],
"source": [
- "from functools import partial\n",
- "\n",
"eval_with_generated_assertions = partial(\n",
" autogen.code_utils.eval_function_completions,\n",
" assertions=partial(autogen.code_utils.generate_assertions, config_list=config_list),\n",
" use_docker=False,\n",
" # Please set use_docker=True if docker is available to run the generated code.\n",
" # Using docker is safer than running the generated code directly.\n",
- ")\n"
+ ")"
]
},
{
@@ -488,7 +488,7 @@
" stop=[[\"\\nclass\", \"\\ndef\", \"\\nif\", \"\\nprint\"], None], # the stop sequences\n",
" config_list=endpoint_list, # optional: a list of endpoints to use\n",
" allow_format_str_template=True, # whether to allow format string template\n",
- ")\n"
+ ")"
]
},
{
@@ -747,7 +747,7 @@
"source": [
"response = autogen.Completion.create(context=tune_data[1], config_list=endpoint_list, **config)\n",
"print(response)\n",
- "print(eval_with_generated_assertions(autogen.Completion.extract_text(response), **tune_data[1]))\n"
+ "print(eval_with_generated_assertions(autogen.Completion.extract_text(response), **tune_data[1]))"
]
},
{
diff --git a/notebook/oai_openai_utils.ipynb b/notebook/oai_openai_utils.ipynb
index dadc43866..2040f570c 100644
--- a/notebook/oai_openai_utils.ipynb
+++ b/notebook/oai_openai_utils.ipynb
@@ -69,7 +69,7 @@
"metadata": {},
"outputs": [],
"source": [
- "import autogen "
+ "import autogen"
]
},
{
@@ -100,12 +100,7 @@
"api_type = \"openai\" # Type of API, e.g., \"openai\" or \"aoai\".\n",
"api_version = None # Specify API version if needed.\n",
"\n",
- "config_list = autogen.get_config_list(\n",
- " api_keys,\n",
- " base_urls=base_urls,\n",
- " api_type=api_type,\n",
- " api_version=api_version\n",
- ")\n",
+ "config_list = autogen.get_config_list(api_keys, base_urls=base_urls, api_type=api_type, api_version=api_version)\n",
"\n",
"print(config_list)"
]
@@ -141,7 +136,7 @@
" openai_api_key_file=\"key_openai.txt\",\n",
" aoai_api_key_file=\"key_aoai.txt\",\n",
" aoai_api_base_file=\"base_aoai.txt\",\n",
- " exclude=None # The API type to exclude, eg: \"openai\" or \"aoai\".\n",
+ " exclude=None, # The API type to exclude, eg: \"openai\" or \"aoai\".\n",
")"
]
},
@@ -191,7 +186,7 @@
" \"gpt-4\",\n",
" \"gpt-3.5-turbo\",\n",
" }\n",
- " }\n",
+ " },\n",
")"
]
},
@@ -213,21 +208,21 @@
"outputs": [],
"source": [
"cheap_config_list = autogen.config_list_from_json(\n",
- " env_or_file=\"OAI_CONFIG_LIST\", \n",
+ " env_or_file=\"OAI_CONFIG_LIST\",\n",
" filter_dict={\n",
" \"model\": {\n",
" \"gpt-3.5-turbo\",\n",
" }\n",
- " }\n",
+ " },\n",
")\n",
"\n",
"costly_config_list = autogen.config_list_from_json(\n",
- " env_or_file=\"OAI_CONFIG_LIST\", \n",
+ " env_or_file=\"OAI_CONFIG_LIST\",\n",
" filter_dict={\n",
" \"model\": {\n",
" \"gpt-4\",\n",
" }\n",
- " }\n",
+ " },\n",
")\n",
"\n",
"# Assistant using GPT 3.5 Turbo\n",
@@ -270,16 +265,7 @@
"source": [
"config_list = autogen.config_list_from_json(\n",
" env_or_file=\"OAI_CONFIG_LIST\",\n",
- " filter_dict = {\n",
- " \"model\": {\n",
- " \"gpt-4\"\n",
- " },\n",
- " \"api_version\": {\n",
- " \"2023-03-01-preview\"\n",
- " },\n",
- " \"api_type\": \n",
- " [\"aoai\"]\n",
- " },\n",
+ " filter_dict={\"model\": {\"gpt-4\"}, \"api_version\": {\"2023-03-01-preview\"}, \"api_type\": [\"aoai\"]},\n",
")"
]
},
@@ -302,10 +288,10 @@
"outputs": [],
"source": [
"config_list = autogen.config_list_from_models(\n",
- " key_file_path = \".\",\n",
- " openai_api_key_file = \"key_openai.txt\",\n",
- " aoai_api_key_file = \"key_aoai.txt\",\n",
- " aoai_api_base_file = \"base_aoai.txt\",\n",
+ " key_file_path=\".\",\n",
+ " openai_api_key_file=\"key_openai.txt\",\n",
+ " aoai_api_key_file=\"key_aoai.txt\",\n",
+ " aoai_api_base_file=\"base_aoai.txt\",\n",
" exclude=\"aoai\",\n",
" model_list=[\"gpt-4\", \"gpt-3.5-turbo\", \"gpt-3.5-turbo-16k\"],\n",
")"
@@ -358,16 +344,14 @@
}
],
"source": [
- "import autogen\n",
- "\n",
"config_list = autogen.config_list_from_dotenv(\n",
- " dotenv_file_path='.env', # If None the function will try to find in the working directory\n",
+ " dotenv_file_path=\".env\", # If None the function will try to find in the working directory\n",
" filter_dict={\n",
" \"model\": {\n",
" \"gpt-4\",\n",
" \"gpt-3.5-turbo\",\n",
" }\n",
- " }\n",
+ " },\n",
")\n",
"\n",
"config_list"
@@ -393,7 +377,7 @@
"source": [
"# gpt-3.5-turbo will default to OPENAI_API_KEY\n",
"config_list = autogen.config_list_from_dotenv(\n",
- " dotenv_file_path='.env', # If None the function will try to find in the working directory\n",
+ " dotenv_file_path=\".env\", # If None the function will try to find in the working directory\n",
" model_api_key_map={\n",
" \"gpt-4\": \"ANOTHER_API_KEY\", # String or dict accepted\n",
" },\n",
@@ -402,7 +386,7 @@
" \"gpt-4\",\n",
" \"gpt-3.5-turbo\",\n",
" }\n",
- " }\n",
+ " },\n",
")\n",
"\n",
"config_list"
@@ -428,7 +412,7 @@
"source": [
"# example using different environment variable names\n",
"config_list = autogen.config_list_from_dotenv(\n",
- " dotenv_file_path='.env',\n",
+ " dotenv_file_path=\".env\",\n",
" model_api_key_map={\n",
" \"gpt-4\": \"OPENAI_API_KEY\",\n",
" \"vicuna\": \"HUGGING_FACE_API_KEY\",\n",
@@ -438,7 +422,7 @@
" \"gpt-4\",\n",
" \"vicuna\",\n",
" }\n",
- " }\n",
+ " },\n",
")\n",
"\n",
"config_list"
@@ -474,22 +458,22 @@
],
"source": [
"config_list = autogen.config_list_from_dotenv(\n",
- " dotenv_file_path='.env',\n",
+ " dotenv_file_path=\".env\",\n",
" model_api_key_map={\n",
" \"gpt-4\": \"OPENAI_API_KEY\",\n",
" \"gpt-3.5-turbo\": {\n",
" \"api_key_env_var\": \"ANOTHER_API_KEY\",\n",
" \"api_type\": \"aoai\",\n",
" \"api_version\": \"v2\",\n",
- " \"base_url\": \"https://api.someotherapi.com\"\n",
- " }\n",
+ " \"base_url\": \"https://api.someotherapi.com\",\n",
+ " },\n",
" },\n",
" filter_dict={\n",
" \"model\": {\n",
" \"gpt-4\",\n",
" \"gpt-3.5-turbo\",\n",
" }\n",
- " }\n",
+ " },\n",
")\n",
"\n",
"config_list"
diff --git a/samples/apps/autogen-studio/notebooks/tutorial.ipynb b/samples/apps/autogen-studio/notebooks/tutorial.ipynb
index 032153fbc..36951bd32 100644
--- a/samples/apps/autogen-studio/notebooks/tutorial.ipynb
+++ b/samples/apps/autogen-studio/notebooks/tutorial.ipynb
@@ -39,17 +39,18 @@
}
],
"source": [
- "import json \n",
- "from autogenstudio import AutoGenWorkFlowManager, AgentWorkFlowConfig \n",
+ "import json\n",
"\n",
- "# load an agent specification in JSON \n",
- "agent_spec= json.load(open('agent_spec.json'))\n",
+ "from autogenstudio import AgentWorkFlowConfig, AutoGenWorkFlowManager\n",
+ "\n",
+ "# load an agent specification in JSON\n",
+ "agent_spec = json.load(open(\"agent_spec.json\"))\n",
"\n",
"# Creat a An AutoGen Workflow Configuration from the agent specification\n",
- "agent_work_flow_config = AgentWorkFlowConfig(**agent_spec)\n",
+ "agent_work_flow_config = AgentWorkFlowConfig(**agent_spec)\n",
"\n",
- "# Create a Workflow from the configuration \n",
- "agent_work_flow = AutoGenWorkFlowManager(agent_work_flow_config) \n",
+ "# Create a Workflow from the configuration\n",
+ "agent_work_flow = AutoGenWorkFlowManager(agent_work_flow_config)\n",
"\n",
"# Run the workflow on a task\n",
"task_query = \"What is the height of the Eiffel Tower?\"\n",