* Add isort

* Apply isort on py files

* Fix circular import

* Fix format for notebooks

* Fix format

---------

Co-authored-by: Chi Wang <wang.chi@microsoft.com>
This commit is contained in:
Li Jiang 2024-04-05 10:26:06 +08:00 committed by GitHub
parent 713cc9f6e6
commit 42b27b9a9d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
208 changed files with 1218 additions and 1018 deletions

View File

@ -1,10 +1,10 @@
import logging
from .version import __version__
from .oai import *
from .agentchat import *
from .exception_utils import *
from .code_utils import DEFAULT_MODEL, FAST_MODEL
from .agentchat import *
from .code_utils import DEFAULT_MODEL, FAST_MODEL
from .exception_utils import *
from .oai import *
from .version import __version__
# Set the root logger.
logger = logging.getLogger(__name__)

View File

@ -1,9 +1,9 @@
from .agent import Agent
from .assistant_agent import AssistantAgent
from .chat import ChatResult, initiate_chats
from .conversable_agent import ConversableAgent, register_function
from .groupchat import GroupChat, GroupChatManager
from .user_proxy_agent import UserProxyAgent
from .chat import initiate_chats, ChatResult
from .utils import gather_usage_summary
__all__ = (

View File

@ -1,7 +1,8 @@
from typing import Callable, Dict, Literal, Optional, Union
from autogen.runtime_logging import log_new_agent, logging_enabled
from .conversable_agent import ConversableAgent
from autogen.runtime_logging import logging_enabled, log_new_agent
class AssistantAgent(ConversableAgent):

View File

@ -1,15 +1,15 @@
import asyncio
from functools import partial
import logging
from collections import defaultdict, abc
from typing import Dict, List, Any, Set, Tuple
from dataclasses import dataclass
from .utils import consolidate_chat_info
import datetime
import logging
import warnings
from ..io.base import IOStream
from ..formatting_utils import colored
from collections import abc, defaultdict
from dataclasses import dataclass
from functools import partial
from typing import Any, Dict, List, Set, Tuple
from ..formatting_utils import colored
from ..io.base import IOStream
from .utils import consolidate_chat_info
logger = logging.getLogger(__name__)
Prerequisite = Tuple[int, int]

View File

@ -1,10 +1,11 @@
import autogen
import time
import subprocess as sp
import socket
import json
import hashlib
from typing import Optional, List, Dict, Tuple
import json
import socket
import subprocess as sp
import time
from typing import Dict, List, Optional, Tuple
import autogen
def _config_check(config: Dict):

View File

@ -1,8 +1,9 @@
from autogen.code_utils import execute_code
from typing import List, Dict, Optional
import json
import copy
import json
from typing import Dict, List, Optional
import autogen
from autogen.code_utils import execute_code
ADD_FUNC = {
"type": "function",

View File

@ -5,10 +5,10 @@ from openai import OpenAI
from PIL.Image import Image
from autogen import Agent, ConversableAgent, code_utils
from autogen.cache import AbstractCache
from autogen.agentchat.contrib import img_utils
from autogen.agentchat.contrib.capabilities.agent_capability import AgentCapability
from autogen.agentchat.contrib.text_analyzer_agent import TextAnalyzerAgent
from autogen.cache import AbstractCache
SYSTEM_MESSAGE = "You've been given the special ability to generate images."
DESCRIPTION_MESSAGE = "This agent has the ability to generate images."

View File

@ -1,11 +1,14 @@
import os
import pickle
from typing import Dict, Optional, Union
import chromadb
from chromadb.config import Settings
import pickle
from autogen.agentchat.assistant_agent import ConversableAgent
from autogen.agentchat.contrib.capabilities.agent_capability import AgentCapability
from autogen.agentchat.contrib.text_analyzer_agent import TextAnalyzerAgent
from ....formatting_utils import colored

View File

@ -4,6 +4,7 @@ from typing import Dict, List
from termcolor import colored
from autogen import ConversableAgent
from .transforms import MessageTransform

View File

@ -1,10 +1,10 @@
from typing import Callable, Dict, Optional, Union, Tuple, List, Any
from autogen import OpenAIWrapper
from autogen import Agent, ConversableAgent
import copy
import asyncio
import logging
import copy
import inspect
import logging
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from autogen import Agent, ConversableAgent, OpenAIWrapper
from autogen.token_count_utils import count_token, get_max_token_limit, num_tokens_from_functions
from ...formatting_utils import colored

View File

@ -1,16 +1,16 @@
from collections import defaultdict
import openai
import json
import time
import logging
import copy
import json
import logging
import time
from collections import defaultdict
from typing import Any, Dict, List, Optional, Tuple, Union
import openai
from autogen import OpenAIWrapper
from autogen.oai.openai_utils import retrieve_assistants_by_name
from autogen.agentchat.agent import Agent
from autogen.agentchat.assistant_agent import ConversableAgent
from autogen.agentchat.assistant_agent import AssistantAgent
from typing import Dict, Optional, Union, List, Tuple, Any
from autogen.agentchat.assistant_agent import AssistantAgent, ConversableAgent
from autogen.oai.openai_utils import retrieve_assistants_by_name
logger = logging.getLogger(__name__)

View File

@ -1,6 +1,7 @@
import json
import logging
from typing import List, Optional, Tuple
import replicate
import requests
@ -8,8 +9,8 @@ from autogen.agentchat.agent import Agent
from autogen.agentchat.contrib.img_utils import get_image_data, llava_formatter
from autogen.agentchat.contrib.multimodal_conversable_agent import MultimodalConversableAgent
from autogen.code_utils import content_str
from ...formatting_utils import colored
from ...formatting_utils import colored
logger = logging.getLogger(__name__)

View File

@ -1,15 +1,15 @@
import re
import os
from pydantic import BaseModel, Extra, root_validator
from typing import Any, Callable, Dict, List, Optional, Union, Tuple
import re
from time import sleep
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from pydantic import BaseModel, Extra, root_validator
from autogen._pydantic import PYDANTIC_V1
from autogen.agentchat import Agent, UserProxyAgent
from autogen.code_utils import UNKNOWN, extract_code, execute_code, infer_lang
from autogen.code_utils import UNKNOWN, execute_code, extract_code, infer_lang
from autogen.math_utils import get_answer
PROMPTS = {
# default
"default": """Let's use Python to solve a math problem.

View File

@ -11,7 +11,6 @@ from autogen.code_utils import content_str
from ..._pydantic import model_dump
DEFAULT_LMM_SYS_MSG = """You are a helpful AI assistant."""
DEFAULT_MODEL = "gpt-4-vision-preview"

View File

@ -1,15 +1,15 @@
import logging
from typing import Callable, Dict, List, Optional
from autogen.agentchat.contrib.retrieve_user_proxy_agent import RetrieveUserProxyAgent
from autogen.retrieve_utils import get_files_from_dir, split_files_to_chunks, TEXT_FORMATS
import logging
from autogen.retrieve_utils import TEXT_FORMATS, get_files_from_dir, split_files_to_chunks
logger = logging.getLogger(__name__)
try:
import fastembed
from qdrant_client import QdrantClient, models
from qdrant_client.fastembed_common import QueryResponse
import fastembed
except ImportError as e:
logging.fatal("Failed to import qdrant_client with fastembed. Try running 'pip install qdrant_client[fastembed]'")
raise e

View File

@ -1,6 +1,7 @@
from typing import Any, Dict, List, Optional, Tuple, Union
from autogen.agentchat.agent import Agent
from autogen.agentchat.assistant_agent import AssistantAgent
from typing import Dict, Optional, Union, List, Tuple, Any
class RetrieveAssistantAgent(AssistantAgent):

View File

@ -1,19 +1,20 @@
import re
from typing import Callable, Dict, Optional, Union, List, Tuple, Any
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from IPython import get_ipython
try:
import chromadb
except ImportError:
raise ImportError("Please install dependencies first. `pip install pyautogen[retrievechat]`")
from autogen.agentchat.agent import Agent
from autogen.agentchat import UserProxyAgent
from autogen.retrieve_utils import create_vector_db_from_dir, query_vector_db, TEXT_FORMATS
from autogen.token_count_utils import count_token
from autogen.code_utils import extract_code
from autogen import logger
from ...formatting_utils import colored
from autogen.agentchat import UserProxyAgent
from autogen.agentchat.agent import Agent
from autogen.code_utils import extract_code
from autogen.retrieve_utils import TEXT_FORMATS, create_vector_db_from_dir, query_vector_db
from autogen.token_count_utils import count_token
from ...formatting_utils import colored
PROMPT_DEFAULT = """You're a retrieve augmented chatbot. You answer user's questions based on your own knowledge and the
context provided by the user. You should follow the following steps to answer a question:

View File

@ -1,10 +1,11 @@
# ruff: noqa: E722
import copy
import json
import traceback
import copy
from dataclasses import dataclass
from typing import Dict, List, Optional, Union, Callable, Literal, Tuple
from autogen import Agent, ConversableAgent, GroupChatManager, GroupChat, OpenAIWrapper
from typing import Callable, Dict, List, Literal, Optional, Tuple, Union
from autogen import Agent, ConversableAgent, GroupChat, GroupChatManager, OpenAIWrapper
class SocietyOfMindAgent(ConversableAgent):

View File

@ -1,7 +1,8 @@
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from autogen import oai
from autogen.agentchat.agent import Agent
from autogen.agentchat.assistant_agent import ConversableAgent
from typing import Callable, Dict, Optional, Union, List, Tuple, Any
system_message = """You are an expert in text analysis.
The user will give you TEXT to analyze.

View File

@ -1,16 +1,18 @@
import json
import copy
import json
import logging
import re
from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Union, Callable, Literal, Tuple
from datetime import datetime
from typing import Any, Callable, Dict, List, Literal, Optional, Tuple, Union
from typing_extensions import Annotated
from ... import Agent, ConversableAgent, AssistantAgent, UserProxyAgent, GroupChatManager, GroupChat, OpenAIWrapper
from ... import Agent, AssistantAgent, ConversableAgent, GroupChat, GroupChatManager, OpenAIWrapper, UserProxyAgent
from ...browser_utils import SimpleTextBrowser
from ...code_utils import content_str
from datetime import datetime
from ...token_count_utils import count_token, get_max_token_limit
from ...oai.openai_utils import filter_config
from ...token_count_utils import count_token, get_max_token_limit
logger = logging.getLogger(__name__)

View File

@ -29,10 +29,10 @@ from ..coding.base import CodeExecutor
from ..coding.factory import CodeExecutorFactory
from ..formatting_utils import colored
from ..function_utils import get_function_schema, load_basemodels_if_needed, serialize_to_str
from ..io.base import IOStream
from ..oai.client import ModelClient, OpenAIWrapper
from ..runtime_logging import log_new_agent, logging_enabled
from .agent import Agent, LLMAgent
from ..io.base import IOStream
from .chat import ChatResult, a_initiate_chats, initiate_chats
from .utils import consolidate_chat_info, gather_usage_summary

View File

@ -5,13 +5,13 @@ import sys
from dataclasses import dataclass, field
from typing import Callable, Dict, List, Literal, Optional, Tuple, Union
from .agent import Agent
from .conversable_agent import ConversableAgent
from ..io.base import IOStream
from ..code_utils import content_str
from ..exception_utils import AgentNameConflict, NoEligibleSpeaker, UndefinedNextAgent
from ..graph_utils import check_graph_validity, invert_disallowed_to_allowed
from ..io.base import IOStream
from ..runtime_logging import log_new_agent, logging_enabled
from .agent import Agent
from .conversable_agent import ConversableAgent
logger = logging.getLogger(__name__)

View File

@ -1,7 +1,7 @@
from typing import Callable, Dict, List, Literal, Optional, Union
from ..runtime_logging import log_new_agent, logging_enabled
from .conversable_agent import ConversableAgent
from ..runtime_logging import logging_enabled, log_new_agent
class UserProxyAgent(ConversableAgent):

View File

@ -1,14 +1,15 @@
import json
import os
import requests
import re
import markdownify
import io
import uuid
import json
import mimetypes
import os
import re
import uuid
from typing import Any, Dict, List, Optional, Tuple, Union
from urllib.parse import urljoin, urlparse
import markdownify
import requests
from bs4 import BeautifulSoup
from typing import Any, Dict, List, Optional, Union, Tuple
# Optional PDF support
IS_PDF_CAPABLE = False

View File

@ -1,6 +1,6 @@
import sys
from types import TracebackType
from typing import Any, Optional, Protocol, Type
import sys
if sys.version_info >= (3, 11):
from typing import Self

View File

@ -1,12 +1,11 @@
from __future__ import annotations
from types import TracebackType
from typing import Dict, Any, Optional, Type, Union
from .abstract_cache_base import AbstractCache
from .cache_factory import CacheFactory
import sys
from types import TracebackType
from typing import Any, Dict, Optional, Type, Union
from .abstract_cache_base import AbstractCache
from .cache_factory import CacheFactory
if sys.version_info >= (3, 11):
from typing import Self

View File

@ -1,9 +1,9 @@
import logging
from typing import Optional, Union
from .abstract_cache_base import AbstractCache
from .disk_cache import DiskCache
import logging
class CacheFactory:
@staticmethod

View File

@ -1,8 +1,10 @@
import sys
from types import TracebackType
from typing import Any, Optional, Type, Union
import diskcache
from .abstract_cache_base import AbstractCache
import sys
if sys.version_info >= (3, 11):
from typing import Self

View File

@ -1,6 +1,7 @@
import sys
from types import TracebackType
from typing import Any, Dict, Optional, Type, Union
import sys
from .abstract_cache_base import AbstractCache
if sys.version_info >= (3, 11):

View File

@ -1,8 +1,10 @@
import pickle
import sys
from types import TracebackType
from typing import Any, Optional, Type, Union
import redis
import sys
from .abstract_cache_base import AbstractCache
if sys.version_info >= (3, 11):

View File

@ -10,10 +10,10 @@ from concurrent.futures import ThreadPoolExecutor, TimeoutError
from hashlib import md5
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from autogen import oai
import docker
from autogen import oai
from .types import UserMessageImageContentPart, UserMessageTextContentPart
SENTINEL = object()

View File

@ -1,8 +1,8 @@
from .base import CodeBlock, CodeExecutor, CodeExtractor, CodeResult
from .factory import CodeExecutorFactory
from .markdown_code_extractor import MarkdownCodeExtractor
from .local_commandline_code_executor import LocalCommandLineCodeExecutor
from .docker_commandline_code_executor import DockerCommandLineCodeExecutor
from .factory import CodeExecutorFactory
from .local_commandline_code_executor import LocalCommandLineCodeExecutor
from .markdown_code_extractor import MarkdownCodeExtractor
__all__ = (
"CodeBlock",

View File

@ -1,4 +1,5 @@
from __future__ import annotations
from typing import Any, List, Literal, Mapping, Optional, Protocol, TypedDict, Union, runtime_checkable
from pydantic import BaseModel, Field

View File

@ -1,22 +1,22 @@
from __future__ import annotations
import atexit
from hashlib import md5
import logging
import sys
import uuid
from hashlib import md5
from pathlib import Path
from time import sleep
from types import TracebackType
import uuid
from typing import Any, List, Optional, Type, Union
import docker
from docker.errors import ImageNotFound
from .utils import _get_file_name_from_content, silence_pip
from .base import CommandLineCodeResult
from ..code_utils import TIMEOUT_MSG, _cmd
from .base import CodeBlock, CodeExecutor, CodeExtractor
from .base import CodeBlock, CodeExecutor, CodeExtractor, CommandLineCodeResult
from .markdown_code_extractor import MarkdownCodeExtractor
import sys
from .utils import _get_file_name_from_content, silence_pip
if sys.version_info >= (3, 11):
from typing import Self

View File

@ -1,4 +1,4 @@
from .base import CodeExecutor, CodeExecutionConfig
from .base import CodeExecutionConfig, CodeExecutor
__all__ = ("CodeExecutorFactory",)

View File

@ -1,12 +1,14 @@
from __future__ import annotations
import inspect
import functools
from typing import Any, Callable, List, TypeVar, Generic, Union
from typing_extensions import ParamSpec
from textwrap import indent, dedent
import importlib
import inspect
from dataclasses import dataclass, field
from importlib.abc import SourceLoader
import importlib
from textwrap import dedent, indent
from typing import Any, Callable, Generic, List, TypeVar, Union
from typing_extensions import ParamSpec
T = TypeVar("T")
P = ParamSpec("P")

View File

@ -1,9 +1,9 @@
from .base import JupyterConnectable, JupyterConnectionInfo
from .jupyter_client import JupyterClient
from .local_jupyter_server import LocalJupyterServer
from .docker_jupyter_server import DockerJupyterServer
from .embedded_ipython_code_executor import EmbeddedIPythonCodeExecutor
from .jupyter_client import JupyterClient
from .jupyter_code_executor import JupyterCodeExecutor
from .local_jupyter_server import LocalJupyterServer
__all__ = [
"JupyterConnectable",

View File

@ -1,15 +1,16 @@
from __future__ import annotations
from pathlib import Path
import sys
from types import TracebackType
import uuid
from typing import Dict, Optional, Type, Union
import docker
import secrets
import io
import atexit
import io
import logging
import secrets
import sys
import uuid
from pathlib import Path
from types import TracebackType
from typing import Dict, Optional, Type, Union
import docker
from ..docker_commandline_code_executor import _wait_for_ready
@ -19,8 +20,8 @@ else:
from typing_extensions import Self
from .jupyter_client import JupyterClient
from .base import JupyterConnectable, JupyterConnectionInfo
from .jupyter_client import JupyterClient
class DockerJupyterServer(JupyterConnectable):

View File

@ -1,9 +1,9 @@
import base64
import json
import os
from pathlib import Path
import re
import uuid
from pathlib import Path
from queue import Empty
from typing import Any, ClassVar, List

View File

@ -1,22 +1,22 @@
from __future__ import annotations
import sys
from dataclasses import dataclass
from types import TracebackType
from typing import Any, Dict, List, Optional, Type, cast
import sys
if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self
import datetime
import json
import uuid
import datetime
import requests
from requests.adapters import HTTPAdapter, Retry
import requests
import websocket
from requests.adapters import HTTPAdapter, Retry
from websocket import WebSocket
from .base import JupyterConnectionInfo

View File

@ -1,12 +1,12 @@
import base64
import json
import os
from pathlib import Path
import re
from types import TracebackType
import uuid
from typing import Any, ClassVar, List, Optional, Type, Union
import sys
import uuid
from pathlib import Path
from types import TracebackType
from typing import Any, ClassVar, List, Optional, Type, Union
from autogen.coding.utils import silence_pip

View File

@ -1,14 +1,14 @@
from __future__ import annotations
from types import TracebackType
from typing import Optional, Type, Union, cast
import subprocess
import signal
import sys
import atexit
import json
import secrets
import signal
import socket
import atexit
import subprocess
import sys
from types import TracebackType
from typing import Optional, Type, Union, cast
if sys.version_info >= (3, 11):
from typing import Self

View File

@ -1,11 +1,15 @@
from hashlib import md5
from pathlib import Path
import logging
import re
from string import Template
import subprocess
import sys
import warnings
from hashlib import md5
from pathlib import Path
from string import Template
from typing import Any, Callable, ClassVar, List, TypeVar, Union, cast
from typing_extensions import ParamSpec
from autogen.coding.func_with_reqs import (
FunctionWithRequirements,
FunctionWithRequirementsStr,
@ -16,13 +20,8 @@ from autogen.coding.func_with_reqs import (
from ..code_utils import TIMEOUT_MSG, WIN32, _cmd
from .base import CodeBlock, CodeExecutor, CodeExtractor, CommandLineCodeResult
from .markdown_code_extractor import MarkdownCodeExtractor
from .utils import _get_file_name_from_content, silence_pip
import subprocess
import logging
__all__ = ("LocalCommandLineCodeExecutor",)
A = ParamSpec("A")

View File

@ -2,8 +2,8 @@ import re
from typing import Any, Dict, List, Optional, Union
from ..code_utils import CODE_BLOCK_PATTERN, UNKNOWN, content_str, infer_lang
from .base import CodeBlock, CodeExtractor
from ..types import UserMessageImageContentPart, UserMessageTextContentPart
from .base import CodeBlock, CodeExtractor
__all__ = ("MarkdownCodeExtractor",)

View File

@ -1,7 +1,7 @@
from typing import Dict, List
import logging
from typing import Dict, List
from autogen.agentchat.groupchat import Agent
from autogen.agentchat import Agent
def has_self_loops(allowed_speaker_transitions: Dict) -> bool:
@ -115,8 +115,8 @@ def visualize_speaker_transitions_dict(speaker_transitions_dict: dict, agents: L
Visualize the speaker_transitions_dict using networkx.
"""
try:
import networkx as nx
import matplotlib.pyplot as plt
import networkx as nx
except ImportError as e:
logging.fatal("Failed to import networkx or matplotlib. Try running 'pip install autogen[graphs]'")
raise e

View File

@ -1,6 +1,6 @@
import logging
from contextlib import contextmanager
from contextvars import ContextVar
import logging
from typing import Any, Iterator, Optional, Protocol, runtime_checkable
__all__ = ("OutputStream", "InputStream", "IOStream")

View File

@ -4,7 +4,7 @@ import threading
from contextlib import contextmanager
from functools import partial
from time import sleep
from typing import Any, Callable, Dict, Iterable, Iterator, Optional, TYPE_CHECKING, Protocol, Union
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, Iterator, Optional, Protocol, Union
from .base import IOStream

View File

@ -1,11 +1,11 @@
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import Any, Dict, List, TYPE_CHECKING, Union
import sqlite3
import uuid
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Any, Dict, List, Union
from openai import OpenAI, AzureOpenAI
from openai import AzureOpenAI, OpenAI
from openai.types.chat import ChatCompletion
if TYPE_CHECKING:

View File

@ -1,4 +1,5 @@
from typing import Any, Dict, Optional
from autogen.logger.base_logger import BaseLogger
from autogen.logger.sqlite_logger import SqliteLogger

View File

@ -6,16 +6,16 @@ import os
import sqlite3
import threading
import uuid
from typing import TYPE_CHECKING, Any, Dict, List, Tuple, Union
from openai import AzureOpenAI, OpenAI
from openai.types.chat import ChatCompletion
from autogen.logger.base_logger import BaseLogger
from autogen.logger.logger_utils import get_current_ts, to_dict
from openai import OpenAI, AzureOpenAI
from openai.types.chat import ChatCompletion
from typing import Any, Dict, List, TYPE_CHECKING, Tuple, Union
from .base_logger import LLMConfig
if TYPE_CHECKING:
from autogen import ConversableAgent, OpenAIWrapper

View File

@ -1,5 +1,6 @@
from typing import Optional
from autogen import oai, DEFAULT_MODEL
from autogen import DEFAULT_MODEL, oai
_MATH_PROMPT = "{problem} Solve the problem carefully. Simplify your answer as much as possible. Put the final answer in \\boxed{{}}."
_MATH_CONFIG = {

View File

@ -1,15 +1,15 @@
from autogen.oai.client import OpenAIWrapper, ModelClient
from autogen.oai.completion import Completion, ChatCompletion
from autogen.cache.cache import Cache
from autogen.oai.client import ModelClient, OpenAIWrapper
from autogen.oai.completion import ChatCompletion, Completion
from autogen.oai.openai_utils import (
get_config_list,
config_list_from_dotenv,
config_list_from_json,
config_list_from_models,
config_list_gpt4_gpt35,
config_list_openai_aoai,
config_list_from_models,
config_list_from_json,
config_list_from_dotenv,
filter_config,
get_config_list,
)
from autogen.cache.cache import Cache
__all__ = [
"OpenAIWrapper",

View File

@ -1,22 +1,20 @@
from __future__ import annotations
import sys
from typing import Any, List, Optional, Dict, Callable, Tuple, Union
import logging
import inspect
import logging
import sys
import uuid
from flaml.automl.logger import logger_formatter
from typing import Any, Callable, Dict, List, Optional, Protocol, Tuple, Union
from flaml.automl.logger import logger_formatter
from pydantic import BaseModel
from typing import Protocol
from autogen.cache import Cache
from autogen.io.base import IOStream
from autogen.oai.openai_utils import get_key, is_valid_api_key, OAI_PRICE1K
from autogen.token_count_utils import count_token
from autogen.runtime_logging import logging_enabled, log_chat_completion, log_new_client, log_new_wrapper
from autogen.logger.logger_utils import get_current_ts
from autogen.oai.openai_utils import OAI_PRICE1K, get_key, is_valid_api_key
from autogen.runtime_logging import log_chat_completion, log_new_client, log_new_wrapper, logging_enabled
from autogen.token_count_utils import count_token
TOOL_ENABLED = False
try:
@ -27,14 +25,15 @@ except ImportError:
AzureOpenAI = object
else:
# raises exception if openai>=1 is installed and something is wrong with imports
from openai import OpenAI, AzureOpenAI, APIError, APITimeoutError, __version__ as OPENAIVERSION
from openai import APIError, APITimeoutError, AzureOpenAI, OpenAI
from openai import __version__ as OPENAIVERSION
from openai.resources import Completions
from openai.types.chat import ChatCompletion
from openai.types.chat.chat_completion import ChatCompletionMessage, Choice # type: ignore [attr-defined]
from openai.types.chat.chat_completion_chunk import (
ChoiceDeltaFunctionCall,
ChoiceDeltaToolCall,
ChoiceDeltaToolCallFunction,
ChoiceDeltaFunctionCall,
)
from openai.types.completion import Completion
from openai.types.completion_usage import CompletionUsage

View File

@ -1,28 +1,30 @@
from time import sleep
import logging
import time
from typing import List, Optional, Dict, Callable, Union
import sys
import shutil
import numpy as np
from flaml import tune, BlendSearch
from flaml.tune.space import is_constant
from flaml.automl.logger import logger_formatter
from .openai_utils import get_key
import sys
import time
from collections import defaultdict
from time import sleep
from typing import Callable, Dict, List, Optional, Union
import numpy as np
from flaml import BlendSearch, tune
from flaml.automl.logger import logger_formatter
from flaml.tune.space import is_constant
from .openai_utils import get_key
try:
import diskcache
import openai
from openai import (
RateLimitError,
APIError,
BadRequestError,
APIConnectionError,
Timeout,
APIError,
AuthenticationError,
BadRequestError,
RateLimitError,
Timeout,
)
from openai import Completion as openai_Completion
import diskcache
ERROR = None
assert openai.__version__ < "1"

View File

@ -7,7 +7,6 @@ from pathlib import Path
from typing import Any, Dict, List, Optional, Set, Union
from dotenv import find_dotenv, load_dotenv
from openai import OpenAI
from openai.types.beta.assistant import Assistant

View File

@ -1,18 +1,21 @@
from typing import List, Union, Callable
import os
import requests
from urllib.parse import urlparse
import glob
import os
from typing import Callable, List, Union
from urllib.parse import urlparse
import chromadb
import requests
if chromadb.__version__ < "0.4.15":
from chromadb.api import API
else:
from chromadb.api import ClientAPI as API
from chromadb.api.types import QueryResult
import chromadb.utils.embedding_functions as ef
import logging
import chromadb.utils.embedding_functions as ef
import pypdf
from chromadb.api.types import QueryResult
from autogen.token_count_utils import count_token
try:

View File

@ -1,16 +1,16 @@
from __future__ import annotations
from autogen.logger.logger_factory import LoggerFactory
from autogen.logger.base_logger import LLMConfig
import logging
import sqlite3
from typing import Any, Dict, List, Optional, TYPE_CHECKING, Union
import uuid
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
from openai import OpenAI, AzureOpenAI
from openai import AzureOpenAI, OpenAI
from openai.types.chat import ChatCompletion
from autogen.logger.base_logger import LLMConfig
from autogen.logger.logger_factory import LoggerFactory
if TYPE_CHECKING:
from autogen import ConversableAgent, OpenAIWrapper

View File

@ -1,9 +1,9 @@
from typing import List, Union, Dict
import logging
import json
import tiktoken
import logging
import re
from typing import Dict, List, Union
import tiktoken
logger = logging.getLogger(__name__)

View File

@ -5,7 +5,10 @@
"metadata": {
"front_matter": {
"description": "AgentOptimizer is able to prompt LLMs to iteratively optimize function/skills of AutoGen agents according to the historical conversation and performance.",
"tags": ["optimization", "tool/function"]
"tags": [
"optimization",
"tool/function"
]
}
},
"source": [
@ -36,17 +39,19 @@
"metadata": {},
"outputs": [],
"source": [
"import copy\n",
"import json\n",
"from typing import Any, Callable, Dict, List, Optional, Tuple, Union\n",
"\n",
"from openai import BadRequestError\n",
"\n",
"import autogen\n",
"from autogen import config_list_from_json\n",
"from autogen.agentchat import Agent\n",
"from autogen.agentchat.contrib.agent_optimizer import AgentOptimizer\n",
"from autogen.agentchat.contrib.math_user_proxy_agent import MathUserProxyAgent\n",
"from autogen.agentchat import Agent\n",
"from openai import BadRequestError\n",
"from autogen.code_utils import extract_code\n",
"from autogen.math_utils import get_answer\n",
"from autogen import config_list_from_json\n",
"import autogen\n",
"import json\n",
"import copy"
"from autogen.math_utils import get_answer"
]
},
{

View File

@ -33,11 +33,11 @@
"metadata": {},
"outputs": [],
"source": [
"import csv\n",
"from typing import Dict, Union\n",
"\n",
"from IPython import get_ipython\n",
"from IPython.display import display, Image\n",
"import csv\n",
"from IPython.display import Image, display\n",
"\n",
"import autogen\n",
"\n",

View File

@ -31,6 +31,7 @@
"outputs": [],
"source": [
"import os\n",
"\n",
"import autogen\n",
"from autogen.agentchat.contrib.capabilities import context_handling"
]

View File

@ -60,9 +60,7 @@
"outputs": [],
"source": [
"import autogen\n",
"from autogen import OpenAIWrapper\n",
"from autogen import AssistantAgent, UserProxyAgent\n",
"from autogen import gather_usage_summary\n",
"from autogen import AssistantAgent, OpenAIWrapper, UserProxyAgent, gather_usage_summary\n",
"\n",
"# config_list = autogen.config_list_from_json(\n",
"# \"OAI_CONFIG_LIST\",\n",

View File

@ -36,10 +36,12 @@
"metadata": {},
"outputs": [],
"source": [
"from types import SimpleNamespace\n",
"\n",
"from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig\n",
"\n",
"import autogen\n",
"from autogen import AssistantAgent, UserProxyAgent\n",
"from transformers import AutoTokenizer, GenerationConfig, AutoModelForCausalLM\n",
"from types import SimpleNamespace"
"from autogen import AssistantAgent, UserProxyAgent"
]
},
{

View File

@ -46,7 +46,7 @@
"\n",
"import autogen\n",
"from autogen import Agent, AssistantAgent, ConversableAgent, UserProxyAgent\n",
"from autogen.agentchat.contrib.img_utils import _to_pil, get_image_data, gpt4v_formatter, get_pil_image\n",
"from autogen.agentchat.contrib.img_utils import _to_pil, get_image_data, get_pil_image, gpt4v_formatter\n",
"from autogen.agentchat.contrib.multimodal_conversable_agent import MultimodalConversableAgent"
]
},

View File

@ -65,7 +65,6 @@
"import autogen\n",
"from autogen.cache import Cache\n",
"\n",
"\n",
"config_list = autogen.config_list_from_json(\n",
" \"OAI_CONFIG_LIST\",\n",
" filter_dict={\n",

View File

@ -47,8 +47,8 @@
}
],
"source": [
"from typing_extensions import Annotated\n",
"import chromadb\n",
"from typing_extensions import Annotated\n",
"\n",
"import autogen\n",
"from autogen import AssistantAgent\n",

View File

@ -155,8 +155,9 @@
" }, # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.\n",
")\n",
"\n",
"from typing import Dict, List\n",
"\n",
"from autogen import Agent\n",
"from typing import List, Dict\n",
"\n",
"\n",
"def custom_speaker_selection_func(last_speaker: Agent, groupchat: autogen.GroupChat):\n",

View File

@ -40,14 +40,14 @@
"import re\n",
"from typing import Dict, Optional\n",
"\n",
"from IPython.display import display\n",
"from PIL.Image import Image\n",
"\n",
"import autogen\n",
"from autogen.cache import Cache\n",
"from autogen.agentchat.contrib import img_utils\n",
"from autogen.agentchat.contrib.capabilities import generate_images\n",
"from autogen.oai import openai_utils\n",
"from IPython.display import display"
"from autogen.cache import Cache\n",
"from autogen.oai import openai_utils"
]
},
{

View File

@ -43,17 +43,17 @@
"from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import requests\n",
"from PIL import Image\n",
"from termcolor import colored\n",
"import numpy as np\n",
"\n",
"import autogen\n",
"from autogen.code_utils import content_str\n",
"from autogen import Agent, AssistantAgent, ConversableAgent, UserProxyAgent\n",
"from autogen.agentchat.contrib.capabilities.vision_capability import VisionCapability\n",
"from autogen.agentchat.contrib.img_utils import get_pil_image, pil_to_data_uri\n",
"from autogen.agentchat.contrib.multimodal_conversable_agent import MultimodalConversableAgent\n",
"from autogen.agentchat.contrib.img_utils import get_pil_image, pil_to_data_uri"
"from autogen.code_utils import content_str"
]
},
{

View File

@ -38,9 +38,11 @@
],
"source": [
"import json\n",
"\n",
"import pandas as pd\n",
"\n",
"import autogen\n",
"from autogen import AssistantAgent, UserProxyAgent\n",
"import pandas as pd\n",
"\n",
"# Setup API key. Add your own API key to config file or environment variable\n",
"llm_config = {\n",

View File

@ -98,10 +98,11 @@
"metadata": {},
"outputs": [],
"source": [
"from typing import List\n",
"\n",
"import chess\n",
"import chess.svg\n",
"from IPython.display import display\n",
"from typing import List\n",
"from typing_extensions import Annotated\n",
"\n",
"# Initialize the board.\n",

View File

@ -31,9 +31,10 @@
"metadata": {},
"outputs": [],
"source": [
"import autogen\n",
"from typing_extensions import Annotated\n",
"\n",
"import autogen\n",
"\n",
"config_list = autogen.config_list_from_json(env_or_file=\"OAI_CONFIG_LIST\")\n",
"llm_config = {\"config_list\": config_list}"
]
@ -656,9 +657,10 @@
}
],
"metadata": {
"extra_files_to_copy": [
"nested_chat_1.png", "nested_chat_2.png"
],
"extra_files_to_copy": [
"nested_chat_1.png",
"nested_chat_2.png"
],
"front_matter": {
"description": "Solve complex tasks with a chat nested as inner monologue.",
"tags": [

View File

@ -36,18 +36,19 @@
"metadata": {},
"outputs": [],
"source": [
"import autogen\n",
"from typing import Union\n",
"\n",
"# test Gurobi installation\n",
"from gurobipy import GRB\n",
"from eventlet.timeout import Timeout\n",
"import re\n",
"from termcolor import colored\n",
"from autogen.code_utils import extract_code\n",
"from typing import Union\n",
"\n",
"# import auxiliary packages\n",
"import requests # for loading the example source code\n",
"from eventlet.timeout import Timeout\n",
"\n",
"# test Gurobi installation\n",
"from gurobipy import GRB\n",
"from termcolor import colored\n",
"\n",
"import autogen\n",
"from autogen.code_utils import extract_code\n",
"\n",
"config_list_gpt4 = autogen.config_list_from_json(\n",
" \"OAI_CONFIG_LIST\",\n",

View File

@ -45,12 +45,13 @@
],
"source": [
"# %pip install spider-env\n",
"from spider_env import SpiderEnv\n",
"\n",
"from autogen import UserProxyAgent, ConversableAgent, config_list_from_json\n",
"from typing import Annotated, Dict\n",
"import json\n",
"import os\n",
"from typing import Annotated, Dict\n",
"\n",
"from spider_env import SpiderEnv\n",
"\n",
"from autogen import ConversableAgent, UserProxyAgent, config_list_from_json\n",
"\n",
"gym = SpiderEnv()\n",
"\n",

View File

@ -29,14 +29,14 @@
"metadata": {},
"outputs": [],
"source": [
"import copy\n",
"import os\n",
"import pprint\n",
"import copy\n",
"import re\n",
"from typing import Dict, List\n",
"\n",
"import autogen\n",
"from autogen.agentchat.contrib.capabilities import transform_messages, transforms\n",
"from typing import Dict, List"
"from autogen.agentchat.contrib.capabilities import transform_messages, transforms"
]
},
{

View File

@ -84,8 +84,8 @@
"\n",
"import whisper\n",
"from openai import OpenAI\n",
"import autogen\n",
"\n",
"import autogen\n",
"\n",
"source_language = \"English\"\n",
"target_language = \"Chinese\"\n",

View File

@ -1,389 +1,389 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Web Scraping using Apify Tools\n",
"\n",
"This notebook shows how to use Apify tools with AutoGen agents to\n",
"scrape data from a website and formate the output."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First we need to install the Apify SDK and the AutoGen library."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"! pip install -qqq pyautogen apify-client"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Setting up the LLM configuration and the Apify API key is also required."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"config_list = [\n",
" {\"model\": \"gpt-4\", \"api_key\": os.getenv(\"OPENAI_API_KEY\")},\n",
"]\n",
"\n",
"apify_api_key = os.getenv(\"APIFY_API_KEY\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's define the tool for scraping data from the website using Apify actor.\n",
"Read more about tool use in this [tutorial chapter](/docs/tutorial/tool-use)."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"from typing_extensions import Annotated\n",
"from apify_client import ApifyClient\n",
"\n",
"\n",
"def scrape_page(url: Annotated[str, \"The URL of the web page to scrape\"]) -> Annotated[str, \"Scraped content\"]:\n",
" # Initialize the ApifyClient with your API token\n",
" client = ApifyClient(token=apify_api_key)\n",
"\n",
" # Prepare the Actor input\n",
" run_input = {\n",
" \"startUrls\": [{\"url\": url}],\n",
" \"useSitemaps\": False,\n",
" \"crawlerType\": \"playwright:firefox\",\n",
" \"includeUrlGlobs\": [],\n",
" \"excludeUrlGlobs\": [],\n",
" \"ignoreCanonicalUrl\": False,\n",
" \"maxCrawlDepth\": 0,\n",
" \"maxCrawlPages\": 1,\n",
" \"initialConcurrency\": 0,\n",
" \"maxConcurrency\": 200,\n",
" \"initialCookies\": [],\n",
" \"proxyConfiguration\": {\"useApifyProxy\": True},\n",
" \"maxSessionRotations\": 10,\n",
" \"maxRequestRetries\": 5,\n",
" \"requestTimeoutSecs\": 60,\n",
" \"dynamicContentWaitSecs\": 10,\n",
" \"maxScrollHeightPixels\": 5000,\n",
" \"removeElementsCssSelector\": \"\"\"nav, footer, script, style, noscript, svg,\n",
" [role=\\\"alert\\\"],\n",
" [role=\\\"banner\\\"],\n",
" [role=\\\"dialog\\\"],\n",
" [role=\\\"alertdialog\\\"],\n",
" [role=\\\"region\\\"][aria-label*=\\\"skip\\\" i],\n",
" [aria-modal=\\\"true\\\"]\"\"\",\n",
" \"removeCookieWarnings\": True,\n",
" \"clickElementsCssSelector\": '[aria-expanded=\"false\"]',\n",
" \"htmlTransformer\": \"readableText\",\n",
" \"readableTextCharThreshold\": 100,\n",
" \"aggressivePrune\": False,\n",
" \"debugMode\": True,\n",
" \"debugLog\": True,\n",
" \"saveHtml\": True,\n",
" \"saveMarkdown\": True,\n",
" \"saveFiles\": False,\n",
" \"saveScreenshots\": False,\n",
" \"maxResults\": 9999999,\n",
" \"clientSideMinChangePercentage\": 15,\n",
" \"renderingTypeDetectionPercentage\": 10,\n",
" }\n",
"\n",
" # Run the Actor and wait for it to finish\n",
" run = client.actor(\"aYG0l9s7dbB7j3gbS\").call(run_input=run_input)\n",
"\n",
" # Fetch and print Actor results from the run's dataset (if there are any)\n",
" text_data = \"\"\n",
" for item in client.dataset(run[\"defaultDatasetId\"]).iterate_items():\n",
" text_data += item.get(\"text\", \"\") + \"\\n\"\n",
"\n",
" average_token = 0.75\n",
" max_tokens = 20000 # slightly less than max to be safe 32k\n",
" text_data = text_data[: int(average_token * max_tokens)]\n",
" return text_data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create the agents and register the tool."
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"from autogen import ConversableAgent, register_function\n",
"\n",
"# Create web scrapper agent.\n",
"scraper_agent = ConversableAgent(\n",
" \"WebScraper\",\n",
" llm_config={\"config_list\": config_list},\n",
" system_message=\"You are a web scrapper and you can scrape any web page using the tools provided. \"\n",
" \"Returns 'TERMINATE' when the scraping is done.\",\n",
")\n",
"\n",
"# Create user proxy agent.\n",
"user_proxy_agent = ConversableAgent(\n",
" \"UserProxy\",\n",
" llm_config=False, # No LLM for this agent.\n",
" human_input_mode=\"NEVER\",\n",
" code_execution_config=False, # No code execution for this agent.\n",
" is_termination_msg=lambda x: x.get(\"content\", \"\") is not None and \"terminate\" in x[\"content\"].lower(),\n",
" default_auto_reply=\"Please continue if not finished, otherwise return 'TERMINATE'.\",\n",
")\n",
"\n",
"# Register the function with the agents.\n",
"register_function(\n",
" scrape_page,\n",
" caller=scraper_agent,\n",
" executor=user_proxy_agent,\n",
" name=\"scrape_page\",\n",
" description=\"Scrape a web page and return the content.\",\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Start the conversation for scraping web data. We used the\n",
"`reflection_with_llm` option for summary method\n",
"to perform the formatting of the output into a desired format.\n",
"The summary method is called after the conversation is completed\n",
"given the complete history of the conversation."
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[33mUserProxy\u001b[0m (to WebScraper):\n",
"\n",
"Can you scrape agentops.ai for me?\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[31m\n",
">>>>>>>> USING AUTO REPLY...\u001b[0m\n",
"\u001b[33mWebScraper\u001b[0m (to UserProxy):\n",
"\n",
"\u001b[32m***** Suggested tool call (call_0qok2jvCxOfv7HOA0oxPWneM): scrape_page *****\u001b[0m\n",
"Arguments: \n",
"{\n",
"\"url\": \"https://www.agentops.ai\"\n",
"}\n",
"\u001b[32m****************************************************************************\u001b[0m\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[35m\n",
">>>>>>>> EXECUTING FUNCTION scrape_page...\u001b[0m\n",
"\u001b[33mUserProxy\u001b[0m (to WebScraper):\n",
"\n",
"\u001b[33mUserProxy\u001b[0m (to WebScraper):\n",
"\n",
"\u001b[32m***** Response from calling tool (call_0qok2jvCxOfv7HOA0oxPWneM) *****\u001b[0m\n",
"START NOW\n",
"Take your business to the next level with our features \n",
"AI Agents Suck.\n",
"We're Fixing That. \n",
"Build compliant AI agents with observability, evals, and replay analytics. No more black boxes and prompt guessing.\n",
"New! Introducing AgentOps\n",
"Three Lines of Code. Unlimited Testing. \n",
"Instant Testing + Debugging = Compliant AI Agents That Work\n",
"5\n",
"# Beginning of program's code (i.e. main.py, __init__.py)\n",
"6\n",
"ao_client = agentops.Client(<INSERT YOUR API KEY HERE>)\n",
"9\n",
"# (optional: record specific functions)\n",
"10\n",
"@ao_client.record_action('sample function being record')\n",
"11\n",
"def sample_function(...):\n",
"15\n",
"ao_client.end_session('Success')\n",
"Prototype to Production\n",
"Generous free limits, upgrade only when you need it.\n",
"\n",
"\u001b[32m**********************************************************************\u001b[0m\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[31m\n",
">>>>>>>> USING AUTO REPLY...\u001b[0m\n",
"\u001b[33mWebScraper\u001b[0m (to UserProxy):\n",
"\n",
"Sure, here's the information from the website agentops.ai:\n",
"\n",
"- Their main value proposition is to fix bad AI Agents and replace black boxes and prompt guessing with compliant, observable AI agents that come with evals and replay analytics.\n",
"- Their latest product is AgentOps. The simple and instant testing & debugging offered promises better-performing compliant AI agents.\n",
"- Integration is easy with just three lines of code.\n",
"- They let you record specific functions.\n",
"- They provide generous free limits and you only need to upgrade when necessary.\n",
"\n",
"Here's a sample of their code:\n",
"```python\n",
"ao_client = agentops.Client(<INSERT YOUR API KEY HERE>)\n",
"\n",
"# optional: record specific functions\n",
"@ao_client.record_action('sample function being record')\n",
"def sample_function(...):\n",
" ...\n",
"\n",
"ao_client.end_session('Success')\n",
"```\n",
"This code is for sample usage of their libraries/functions.\n",
"\n",
"Let me know if you need more specific details.\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[33mUserProxy\u001b[0m (to WebScraper):\n",
"\n",
"Please continue if not finished, otherwise return 'TERMINATE'.\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[31m\n",
">>>>>>>> USING AUTO REPLY...\u001b[0m\n",
"\u001b[33mWebScraper\u001b[0m (to UserProxy):\n",
"\n",
"TERMINATE\n",
"\n",
"--------------------------------------------------------------------------------\n"
]
}
],
"source": [
"chat_result = user_proxy_agent.initiate_chat(\n",
" scraper_agent,\n",
" message=\"Can you scrape agentops.ai for me?\",\n",
" summary_method=\"reflection_with_llm\",\n",
" summary_args={\n",
" \"summary_prompt\": \"\"\"Summarize the scraped content and format summary EXACTLY as follows:\n",
"---\n",
"*Company name*:\n",
"`Acme Corp`\n",
"---\n",
"*Website*:\n",
"`acmecorp.com`\n",
"---\n",
"*Description*:\n",
"`Company that does things.`\n",
"---\n",
"*Tags*:\n",
"`Manufacturing. Retail. E-commerce.`\n",
"---\n",
"*Takeaways*:\n",
"`Provides shareholders with value by selling products.`\n",
"---\n",
"*Questions*:\n",
"`What products do they sell? How do they make money? What is their market share?`\n",
"---\n",
"\"\"\"\n",
" },\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The output is stored in the summary."
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"---\n",
"*Company name*:\n",
"`AgentOps`\n",
"---\n",
"*Website*:\n",
"`agentops.ai`\n",
"---\n",
"*Description*:\n",
"`Company that aims to improve AI agents. They offer observed and evaluable AI agents with replay analytics as an alternative to black box models and blind prompting.`\n",
"---\n",
"*Tags*:\n",
"`Artificial Intelligence, AI agents, Observability, Analytics.`\n",
"---\n",
"*Takeaways*:\n",
"`Their product, AgentOps, allows for easy and instant testing and debugging of AI agents. Integration is as simple as writing three lines of code. They also provide generous free limits and mandate upgrades only when necessary.`\n",
"---\n",
"*Questions*:\n",
"`What differentiates AgentOps from other, similar products? How does their pricing scale with usage? What are the details of their \"generous free limits\"?`\n",
"---\n"
]
}
],
"source": [
"print(chat_result.summary)"
]
}
],
"metadata": {
"front_matter": {
"description": "Scrapping web pages and summarizing the content using agents with tools.",
"tags": [
"web scraping",
"apify",
"tool use"
],
"title": "Web Scraper Agent using Apify Tools"
},
"kernelspec": {
"display_name": "autogen",
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Web Scraping using Apify Tools\n",
"\n",
"This notebook shows how to use Apify tools with AutoGen agents to\n",
"scrape data from a website and formate the output."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First we need to install the Apify SDK and the AutoGen library."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"! pip install -qqq pyautogen apify-client"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Setting up the LLM configuration and the Apify API key is also required."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"config_list = [\n",
" {\"model\": \"gpt-4\", \"api_key\": os.getenv(\"OPENAI_API_KEY\")},\n",
"]\n",
"\n",
"apify_api_key = os.getenv(\"APIFY_API_KEY\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's define the tool for scraping data from the website using Apify actor.\n",
"Read more about tool use in this [tutorial chapter](/docs/tutorial/tool-use)."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"from apify_client import ApifyClient\n",
"from typing_extensions import Annotated\n",
"\n",
"\n",
"def scrape_page(url: Annotated[str, \"The URL of the web page to scrape\"]) -> Annotated[str, \"Scraped content\"]:\n",
" # Initialize the ApifyClient with your API token\n",
" client = ApifyClient(token=apify_api_key)\n",
"\n",
" # Prepare the Actor input\n",
" run_input = {\n",
" \"startUrls\": [{\"url\": url}],\n",
" \"useSitemaps\": False,\n",
" \"crawlerType\": \"playwright:firefox\",\n",
" \"includeUrlGlobs\": [],\n",
" \"excludeUrlGlobs\": [],\n",
" \"ignoreCanonicalUrl\": False,\n",
" \"maxCrawlDepth\": 0,\n",
" \"maxCrawlPages\": 1,\n",
" \"initialConcurrency\": 0,\n",
" \"maxConcurrency\": 200,\n",
" \"initialCookies\": [],\n",
" \"proxyConfiguration\": {\"useApifyProxy\": True},\n",
" \"maxSessionRotations\": 10,\n",
" \"maxRequestRetries\": 5,\n",
" \"requestTimeoutSecs\": 60,\n",
" \"dynamicContentWaitSecs\": 10,\n",
" \"maxScrollHeightPixels\": 5000,\n",
" \"removeElementsCssSelector\": \"\"\"nav, footer, script, style, noscript, svg,\n",
" [role=\\\"alert\\\"],\n",
" [role=\\\"banner\\\"],\n",
" [role=\\\"dialog\\\"],\n",
" [role=\\\"alertdialog\\\"],\n",
" [role=\\\"region\\\"][aria-label*=\\\"skip\\\" i],\n",
" [aria-modal=\\\"true\\\"]\"\"\",\n",
" \"removeCookieWarnings\": True,\n",
" \"clickElementsCssSelector\": '[aria-expanded=\"false\"]',\n",
" \"htmlTransformer\": \"readableText\",\n",
" \"readableTextCharThreshold\": 100,\n",
" \"aggressivePrune\": False,\n",
" \"debugMode\": True,\n",
" \"debugLog\": True,\n",
" \"saveHtml\": True,\n",
" \"saveMarkdown\": True,\n",
" \"saveFiles\": False,\n",
" \"saveScreenshots\": False,\n",
" \"maxResults\": 9999999,\n",
" \"clientSideMinChangePercentage\": 15,\n",
" \"renderingTypeDetectionPercentage\": 10,\n",
" }\n",
"\n",
" # Run the Actor and wait for it to finish\n",
" run = client.actor(\"aYG0l9s7dbB7j3gbS\").call(run_input=run_input)\n",
"\n",
" # Fetch and print Actor results from the run's dataset (if there are any)\n",
" text_data = \"\"\n",
" for item in client.dataset(run[\"defaultDatasetId\"]).iterate_items():\n",
" text_data += item.get(\"text\", \"\") + \"\\n\"\n",
"\n",
" average_token = 0.75\n",
" max_tokens = 20000 # slightly less than max to be safe 32k\n",
" text_data = text_data[: int(average_token * max_tokens)]\n",
" return text_data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create the agents and register the tool."
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"from autogen import ConversableAgent, register_function\n",
"\n",
"# Create web scrapper agent.\n",
"scraper_agent = ConversableAgent(\n",
" \"WebScraper\",\n",
" llm_config={\"config_list\": config_list},\n",
" system_message=\"You are a web scrapper and you can scrape any web page using the tools provided. \"\n",
" \"Returns 'TERMINATE' when the scraping is done.\",\n",
")\n",
"\n",
"# Create user proxy agent.\n",
"user_proxy_agent = ConversableAgent(\n",
" \"UserProxy\",\n",
" llm_config=False, # No LLM for this agent.\n",
" human_input_mode=\"NEVER\",\n",
" code_execution_config=False, # No code execution for this agent.\n",
" is_termination_msg=lambda x: x.get(\"content\", \"\") is not None and \"terminate\" in x[\"content\"].lower(),\n",
" default_auto_reply=\"Please continue if not finished, otherwise return 'TERMINATE'.\",\n",
")\n",
"\n",
"# Register the function with the agents.\n",
"register_function(\n",
" scrape_page,\n",
" caller=scraper_agent,\n",
" executor=user_proxy_agent,\n",
" name=\"scrape_page\",\n",
" description=\"Scrape a web page and return the content.\",\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Start the conversation for scraping web data. We used the\n",
"`reflection_with_llm` option for summary method\n",
"to perform the formatting of the output into a desired format.\n",
"The summary method is called after the conversation is completed\n",
"given the complete history of the conversation."
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[33mUserProxy\u001b[0m (to WebScraper):\n",
"\n",
"Can you scrape agentops.ai for me?\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[31m\n",
">>>>>>>> USING AUTO REPLY...\u001b[0m\n",
"\u001b[33mWebScraper\u001b[0m (to UserProxy):\n",
"\n",
"\u001b[32m***** Suggested tool call (call_0qok2jvCxOfv7HOA0oxPWneM): scrape_page *****\u001b[0m\n",
"Arguments: \n",
"{\n",
"\"url\": \"https://www.agentops.ai\"\n",
"}\n",
"\u001b[32m****************************************************************************\u001b[0m\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[35m\n",
">>>>>>>> EXECUTING FUNCTION scrape_page...\u001b[0m\n",
"\u001b[33mUserProxy\u001b[0m (to WebScraper):\n",
"\n",
"\u001b[33mUserProxy\u001b[0m (to WebScraper):\n",
"\n",
"\u001b[32m***** Response from calling tool (call_0qok2jvCxOfv7HOA0oxPWneM) *****\u001b[0m\n",
"START NOW\n",
"Take your business to the next level with our features \n",
"AI Agents Suck.\n",
"We're Fixing That. \n",
"Build compliant AI agents with observability, evals, and replay analytics. No more black boxes and prompt guessing.\n",
"New! Introducing AgentOps\n",
"Three Lines of Code. Unlimited Testing. \n",
"Instant Testing + Debugging = Compliant AI Agents That Work\n",
"5\n",
"# Beginning of program's code (i.e. main.py, __init__.py)\n",
"6\n",
"ao_client = agentops.Client(<INSERT YOUR API KEY HERE>)\n",
"9\n",
"# (optional: record specific functions)\n",
"10\n",
"@ao_client.record_action('sample function being record')\n",
"11\n",
"def sample_function(...):\n",
"15\n",
"ao_client.end_session('Success')\n",
"Prototype to Production\n",
"Generous free limits, upgrade only when you need it.\n",
"\n",
"\u001b[32m**********************************************************************\u001b[0m\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[31m\n",
">>>>>>>> USING AUTO REPLY...\u001b[0m\n",
"\u001b[33mWebScraper\u001b[0m (to UserProxy):\n",
"\n",
"Sure, here's the information from the website agentops.ai:\n",
"\n",
"- Their main value proposition is to fix bad AI Agents and replace black boxes and prompt guessing with compliant, observable AI agents that come with evals and replay analytics.\n",
"- Their latest product is AgentOps. The simple and instant testing & debugging offered promises better-performing compliant AI agents.\n",
"- Integration is easy with just three lines of code.\n",
"- They let you record specific functions.\n",
"- They provide generous free limits and you only need to upgrade when necessary.\n",
"\n",
"Here's a sample of their code:\n",
"```python\n",
"ao_client = agentops.Client(<INSERT YOUR API KEY HERE>)\n",
"\n",
"# optional: record specific functions\n",
"@ao_client.record_action('sample function being record')\n",
"def sample_function(...):\n",
" ...\n",
"\n",
"ao_client.end_session('Success')\n",
"```\n",
"This code is for sample usage of their libraries/functions.\n",
"\n",
"Let me know if you need more specific details.\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[33mUserProxy\u001b[0m (to WebScraper):\n",
"\n",
"Please continue if not finished, otherwise return 'TERMINATE'.\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[31m\n",
">>>>>>>> USING AUTO REPLY...\u001b[0m\n",
"\u001b[33mWebScraper\u001b[0m (to UserProxy):\n",
"\n",
"TERMINATE\n",
"\n",
"--------------------------------------------------------------------------------\n"
]
}
],
"source": [
"chat_result = user_proxy_agent.initiate_chat(\n",
" scraper_agent,\n",
" message=\"Can you scrape agentops.ai for me?\",\n",
" summary_method=\"reflection_with_llm\",\n",
" summary_args={\n",
" \"summary_prompt\": \"\"\"Summarize the scraped content and format summary EXACTLY as follows:\n",
"---\n",
"*Company name*:\n",
"`Acme Corp`\n",
"---\n",
"*Website*:\n",
"`acmecorp.com`\n",
"---\n",
"*Description*:\n",
"`Company that does things.`\n",
"---\n",
"*Tags*:\n",
"`Manufacturing. Retail. E-commerce.`\n",
"---\n",
"*Takeaways*:\n",
"`Provides shareholders with value by selling products.`\n",
"---\n",
"*Questions*:\n",
"`What products do they sell? How do they make money? What is their market share?`\n",
"---\n",
"\"\"\"\n",
" },\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The output is stored in the summary."
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"---\n",
"*Company name*:\n",
"`AgentOps`\n",
"---\n",
"*Website*:\n",
"`agentops.ai`\n",
"---\n",
"*Description*:\n",
"`Company that aims to improve AI agents. They offer observed and evaluable AI agents with replay analytics as an alternative to black box models and blind prompting.`\n",
"---\n",
"*Tags*:\n",
"`Artificial Intelligence, AI agents, Observability, Analytics.`\n",
"---\n",
"*Takeaways*:\n",
"`Their product, AgentOps, allows for easy and instant testing and debugging of AI agents. Integration is as simple as writing three lines of code. They also provide generous free limits and mandate upgrades only when necessary.`\n",
"---\n",
"*Questions*:\n",
"`What differentiates AgentOps from other, similar products? How does their pricing scale with usage? What are the details of their \"generous free limits\"?`\n",
"---\n"
]
}
],
"source": [
"print(chat_result.summary)"
]
}
],
"metadata": {
"front_matter": {
"description": "Scrapping web pages and summarizing the content using agents with tools.",
"tags": [
"web scraping",
"apify",
"tool use"
],
"title": "Web Scraper Agent using Apify Tools"
},
"kernelspec": {
"display_name": "autogen",
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -82,7 +82,6 @@
"outputs": [],
"source": [
"import json\n",
"\n",
"import os\n",
"from pathlib import Path\n",
"\n",

View File

@ -29,6 +29,7 @@ select = [
# "D", # see: https://pypi.org/project/pydocstyle
# "N", # see: https://pypi.org/project/pep8-naming
# "S", # see: https://pypi.org/project/flake8-bandit
"I", # see: https://pypi.org/project/isort/
]
ignore = [

View File

@ -1,11 +1,10 @@
import os
import logging
import logging.handlers
import os
import discord
from discord.ext import commands
from agent_utils import solve_task
from discord.ext import commands
logger = logging.getLogger("anny")
logger.setLevel(logging.INFO)

View File

@ -1,4 +1,4 @@
from .chatmanager import *
from .workflowmanager import *
from .datamodel import *
from .version import __version__
from .workflowmanager import *

View File

@ -1,12 +1,14 @@
import asyncio
from datetime import datetime
import json
from queue import Queue
import time
from typing import Any, List, Dict, Optional, Tuple
import os
from fastapi import WebSocket, WebSocketDisconnect
import time
from datetime import datetime
from queue import Queue
from typing import Any, Dict, List, Optional, Tuple
import websockets
from fastapi import WebSocket, WebSocketDisconnect
from .datamodel import AgentWorkFlowConfig, Message, SocketMessage
from .utils import extract_successful_code_blocks, get_modified_files, summarize_chat_history
from .workflowmanager import AutoGenWorkFlowManager

View File

@ -1,10 +1,11 @@
import os
from typing_extensions import Annotated
import typer
import uvicorn
from typing_extensions import Annotated
from .version import VERSION
from .utils.dbutils import DBManager
from .version import VERSION
app = typer.Typer()

View File

@ -1,8 +1,9 @@
import uuid
from dataclasses import asdict, field
from datetime import datetime
from typing import Any, Callable, Dict, List, Literal, Optional, Union
from pydantic.dataclasses import dataclass
from dataclasses import asdict, field
@dataclass

View File

@ -1,13 +1,13 @@
import json
import logging
import os
import sqlite3
import threading
import os
from typing import Any, List, Dict, Optional, Tuple
from typing import Any, Dict, List, Optional, Tuple
from ..datamodel import AgentFlowSpec, AgentWorkFlowConfig, Gallery, Message, Model, Session, Skill
from ..version import __version__ as __db_version__
VERSION_TABLE_SQL = """
CREATE TABLE IF NOT EXISTS version (

View File

@ -1,14 +1,17 @@
import base64
import hashlib
from typing import List, Dict, Tuple, Union
import os
import re
import shutil
from pathlib import Path
import re
from typing import Dict, List, Tuple, Union
from dotenv import load_dotenv
import autogen
from autogen.oai.client import OpenAIWrapper
from ..datamodel import AgentConfig, AgentFlowSpec, AgentWorkFlowConfig, LLMConfig, Model, Skill
from dotenv import load_dotenv
from ..version import APP_NAME

View File

@ -1,26 +1,25 @@
import asyncio
from contextlib import asynccontextmanager
import json
import os
import queue
import threading
import traceback
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException, WebSocket, WebSocketDisconnect
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi import HTTPException
from openai import OpenAIError
from ..version import VERSION, APP_NAME
from ..chatmanager import AutoGenChatManager, WebSocketConnectionManager
from ..datamodel import (
DBWebRequestModel,
DeleteMessageWebRequestModel,
Message,
Session,
)
from ..utils import md5_hash, init_app_folders, DBManager, dbutils, test_model
from ..chatmanager import AutoGenChatManager, WebSocketConnectionManager
from ..utils import DBManager, dbutils, init_app_folders, md5_hash, test_model
from ..version import APP_NAME, VERSION
managers = {"chat": None} # manage calls to autogen
# Create thread-safe queue for messages between api thread and autogen threads

View File

@ -1,12 +1,13 @@
import os
from typing import List, Optional, Union, Dict
from datetime import datetime
from typing import Dict, List, Optional, Union
from requests import Session
import autogen
from .datamodel import AgentConfig, AgentFlowSpec, AgentWorkFlowConfig, Message, SocketMessage
from .utils import get_skills_from_prompt, clear_folder, sanitize_model
from datetime import datetime
from .utils import clear_folder, get_skills_from_prompt, sanitize_model
class AutoGenWorkFlowManager:

View File

@ -7,6 +7,7 @@
"outputs": [],
"source": [
"import json\n",
"\n",
"from autogenstudio import AgentWorkFlowConfig, AutoGenWorkFlowManager"
]
},

View File

@ -1,9 +1,11 @@
import zmq
import threading
import traceback
import time
from .DebugLog import Debug, Info, Error
import traceback
import zmq
from .Config import xpub_url
from .DebugLog import Debug, Error, Info
class Actor:

View File

@ -1,14 +1,16 @@
# Agent_Sender takes a zmq context, Topic and creates a
# socket that can publish to that topic. It exposes this functionality
# using send_msg method
import zmq
from zmq.utils.monitor import recv_monitor_message
import time
import uuid
from .DebugLog import Debug, Error, Info
from .Config import xsub_url, xpub_url, router_url
from typing import Any, Dict
import zmq
from zmq.utils.monitor import recv_monitor_message
from .Config import router_url, xpub_url, xsub_url
from .DebugLog import Debug, Error, Info
class ActorSender:
def __init__(self, context, topic):

View File

@ -1,8 +1,10 @@
import time
import zmq
import threading
import time
import zmq
from autogencap.Config import router_url, xpub_url, xsub_url
from autogencap.DebugLog import Debug, Info, Warn
from autogencap.Config import xsub_url, xpub_url, router_url
class Broker:

View File

@ -1,8 +1,10 @@
import threading
import datetime
import autogencap.Config as Config
import threading
from termcolor import colored
import autogencap.Config as Config
# Define log levels as constants
DEBUG = 0
INFO = 1

View File

@ -1,26 +1,29 @@
from autogencap.Constants import Directory_Svc_Topic
from autogencap.Config import xpub_url, xsub_url, router_url
from autogencap.DebugLog import Debug, Info, Error
from autogencap.ActorConnector import ActorConnector, ActorSender
from autogencap.Actor import Actor
from autogencap.Broker import Broker
from autogencap.proto.CAP_pb2 import (
ActorRegistration,
ActorInfo,
ActorLookup,
ActorLookupResponse,
Ping,
Pong,
ActorInfoCollection,
Error as ErrorMsg,
ErrorCode,
)
from autogencap.utility import report_error_msg
import zmq
import re
import threading
import time
import re
import zmq
from autogencap.Actor import Actor
from autogencap.ActorConnector import ActorConnector, ActorSender
from autogencap.Broker import Broker
from autogencap.Config import router_url, xpub_url, xsub_url
from autogencap.Constants import Directory_Svc_Topic
from autogencap.DebugLog import Debug, Error, Info
from autogencap.proto.CAP_pb2 import (
ActorInfo,
ActorInfoCollection,
ActorLookup,
ActorLookupResponse,
ActorRegistration,
ErrorCode,
Ping,
Pong,
)
from autogencap.proto.CAP_pb2 import (
Error as ErrorMsg,
)
from autogencap.utility import report_error_msg
# TODO (Future DirectorySv PR) use actor description, network_id, other properties to make directory
# service more generic and powerful

View File

@ -1,13 +1,15 @@
import time
from typing import List
import zmq
from .DebugLog import Debug, Warn
from .Actor import Actor
from .ActorConnector import ActorConnector
from .Broker import Broker
from .DirectorySvc import DirectorySvc
from .Constants import Termination_Topic
from .Actor import Actor
from .DebugLog import Debug, Warn
from .DirectorySvc import DirectorySvc
from .proto.CAP_pb2 import ActorInfo, ActorInfoCollection
from typing import List
import time
# TODO: remove time import

View File

@ -1,8 +1,10 @@
import time
from typing import Callable, Dict, List, Optional, Union
from autogen import Agent, ConversableAgent
from .AutoGenConnector import AutoGenConnector
from ..LocalActorNetwork import LocalActorNetwork
from .AutoGenConnector import AutoGenConnector
class AG2CAP(ConversableAgent):

View File

@ -1,4 +1,5 @@
import zmq
from autogencap.Actor import Actor
from autogencap.Constants import Termination_Topic
from autogencap.DebugLog import Debug

View File

@ -1,5 +1,7 @@
from typing import Dict, Optional, Union
from autogen import Agent
from ..ActorConnector import ActorConnector
from ..proto.Autogen_pb2 import GenReplyReq, GenReplyResp, PrepChat, ReceiveReq, Terminate

View File

@ -1,11 +1,13 @@
from enum import Enum
from typing import Optional
from autogen import ConversableAgent
from ..DebugLog import Debug, Error, Info, Warn, shorten
from ..LocalActorNetwork import LocalActorNetwork
from ..proto.Autogen_pb2 import GenReplyReq, GenReplyResp, PrepChat, ReceiveReq, Terminate
from .AGActor import AGActor
from .AG2CAP import AG2CAP
from autogen import ConversableAgent
from .AGActor import AGActor
class CAP2AG(AGActor):

View File

@ -1,8 +1,9 @@
from typing import List
from autogen import Agent, AssistantAgent, GroupChat
from autogencap.ag_adapter.AG2CAP import AG2CAP
from autogencap.ag_adapter.CAP2AG import CAP2AG
from autogencap.LocalActorNetwork import LocalActorNetwork
from typing import List
class CAPGroupChat(GroupChat):

Some files were not shown because too many files have changed in this diff Show More