diff --git a/.github/workflows/contrib-openai.yml b/.github/workflows/contrib-openai.yml index 7e8fb0033177..9a99caec9649 100644 --- a/.github/workflows/contrib-openai.yml +++ b/.github/workflows/contrib-openai.yml @@ -18,140 +18,141 @@ permissions: # contents: read # deployments: read jobs: - RetrieveChatTest: - strategy: - matrix: - os: [ubuntu-latest] - python-version: ["3.10"] - runs-on: ${{ matrix.os }} - environment: openai1 - services: - pgvector: - image: ankane/pgvector - env: - POSTGRES_DB: postgres - POSTGRES_USER: postgres - POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }} - POSTGRES_HOST_AUTH_METHOD: trust - options: >- - --health-cmd pg_isready - --health-interval 10s - --health-timeout 5s - --health-retries 5 - ports: - - 5432:5432 - steps: - # checkout to pr branch - - name: Checkout - uses: actions/checkout@v4 - with: - ref: ${{ github.event.pull_request.head.sha }} - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v5 - with: - python-version: ${{ matrix.python-version }} - - name: Install packages and dependencies - run: | - docker --version - python -m pip install --upgrade pip wheel - pip install -e . - python -c "import autogen" - pip install pytest-cov>=5 pytest-asyncio - - name: Install packages for test when needed - run: | - pip install docker - pip install -e .[retrievechat,retrievechat-qdrant,retrievechat-pgvector] - - name: Coverage - env: - OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} - AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }} - AZURE_OPENAI_API_BASE: ${{ secrets.AZURE_OPENAI_API_BASE }} - OAI_CONFIG_LIST: ${{ secrets.OAI_CONFIG_LIST }} - run: | - pytest test/agentchat/contrib/retrievechat/ test/agentchat/contrib/retrievechat - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v3 - with: - file: ./coverage.xml - flags: unittests - AgentEvalTest: - strategy: - matrix: - os: [ubuntu-latest] - python-version: ["3.10"] - runs-on: ${{ matrix.os }} - environment: openai1 - steps: - # checkout to pr branch - - name: Checkout - uses: actions/checkout@v4 - with: - ref: ${{ github.event.pull_request.head.sha }} - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v5 - with: - python-version: ${{ matrix.python-version }} - - name: Install packages and dependencies - run: | - docker --version - python -m pip install --upgrade pip wheel - pip install -e . - python -c "import autogen" - pip install pytest-cov>=5 pytest-asyncio - - name: Coverage - env: - OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} - AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }} - AZURE_OPENAI_API_BASE: ${{ secrets.AZURE_OPENAI_API_BASE }} - OAI_CONFIG_LIST: ${{ secrets.OAI_CONFIG_LIST }} - run: | - pytest test/agentchat/contrib/agent_eval/test_agent_eval.py - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v3 - with: - file: ./coverage.xml - flags: unittests + # RetrieveChatTest: + # strategy: + # matrix: + # os: [ubuntu-latest] + # python-version: ["3.10"] + # runs-on: ${{ matrix.os }} + # environment: openai1 + # services: + # pgvector: + # image: ankane/pgvector + # env: + # POSTGRES_DB: postgres + # POSTGRES_USER: postgres + # POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }} + # POSTGRES_HOST_AUTH_METHOD: trust + # options: >- + # --health-cmd pg_isready + # --health-interval 10s + # --health-timeout 5s + # --health-retries 5 + # ports: + # - 5432:5432 + # steps: + # # checkout to pr branch + # - name: Checkout + # uses: actions/checkout@v4 + # with: + # ref: ${{ github.event.pull_request.head.sha }} + # - name: Set up Python ${{ matrix.python-version }} + # uses: actions/setup-python@v5 + # with: + # python-version: ${{ matrix.python-version }} + # - name: Install packages and dependencies + # run: | + # docker --version + # python -m pip install --upgrade pip wheel + # pip install -e . + # python -c "import autogen" + # pip install pytest-cov>=5 pytest-asyncio + # - name: Install packages for test when needed + # run: | + # pip install docker + # pip install -e .[retrievechat,retrievechat-qdrant,retrievechat-pgvector] + # - name: Coverage + # env: + # OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + # AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }} + # AZURE_OPENAI_API_BASE: ${{ secrets.AZURE_OPENAI_API_BASE }} + # OAI_CONFIG_LIST: ${{ secrets.OAI_CONFIG_LIST }} + # run: | + # pytest test/agentchat/contrib/retrievechat/ test/agentchat/contrib/retrievechat + # - name: Upload coverage to Codecov + # uses: codecov/codecov-action@v3 + # with: + # file: ./coverage.xml + # flags: unittests - GPTAssistantAgent: - strategy: - matrix: - os: [ubuntu-latest] - python-version: ["3.11"] - runs-on: ${{ matrix.os }} - environment: openai1 - steps: - # checkout to pr branch - - name: Checkout - uses: actions/checkout@v4 - with: - ref: ${{ github.event.pull_request.head.sha }} - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v5 - with: - python-version: ${{ matrix.python-version }} - - name: Install packages and dependencies - run: | - docker --version - python -m pip install --upgrade pip wheel - pip install -e . - python -c "import autogen" - pip install pytest-cov>=5 pytest-asyncio - - name: Install packages for test when needed - run: | - pip install docker - - name: Coverage - env: - OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} - AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }} - AZURE_OPENAI_API_BASE: ${{ secrets.AZURE_OPENAI_API_BASE }} - OAI_CONFIG_LIST: ${{ secrets.OAI_CONFIG_LIST }} - run: | - pytest test/agentchat/contrib/test_gpt_assistant.py - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v3 - with: - file: ./coverage.xml - flags: unittests + # AgentEvalTest: + # strategy: + # matrix: + # os: [ubuntu-latest] + # python-version: ["3.10"] + # runs-on: ${{ matrix.os }} + # environment: openai1 + # steps: + # # checkout to pr branch + # - name: Checkout + # uses: actions/checkout@v4 + # with: + # ref: ${{ github.event.pull_request.head.sha }} + # - name: Set up Python ${{ matrix.python-version }} + # uses: actions/setup-python@v5 + # with: + # python-version: ${{ matrix.python-version }} + # - name: Install packages and dependencies + # run: | + # docker --version + # python -m pip install --upgrade pip wheel + # pip install -e . + # python -c "import autogen" + # pip install pytest-cov>=5 pytest-asyncio + # - name: Coverage + # env: + # OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + # AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }} + # AZURE_OPENAI_API_BASE: ${{ secrets.AZURE_OPENAI_API_BASE }} + # OAI_CONFIG_LIST: ${{ secrets.OAI_CONFIG_LIST }} + # run: | + # pytest test/agentchat/contrib/agent_eval/test_agent_eval.py + # - name: Upload coverage to Codecov + # uses: codecov/codecov-action@v3 + # with: + # file: ./coverage.xml + # flags: unittests + + # GPTAssistantAgent: + # strategy: + # matrix: + # os: [ubuntu-latest] + # python-version: ["3.11"] + # runs-on: ${{ matrix.os }} + # environment: openai1 + # steps: + # # checkout to pr branch + # - name: Checkout + # uses: actions/checkout@v4 + # with: + # ref: ${{ github.event.pull_request.head.sha }} + # - name: Set up Python ${{ matrix.python-version }} + # uses: actions/setup-python@v5 + # with: + # python-version: ${{ matrix.python-version }} + # - name: Install packages and dependencies + # run: | + # docker --version + # python -m pip install --upgrade pip wheel + # pip install -e . + # python -c "import autogen" + # pip install pytest-cov>=5 pytest-asyncio + # - name: Install packages for test when needed + # run: | + # pip install docker + # - name: Coverage + # env: + # OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + # AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }} + # AZURE_OPENAI_API_BASE: ${{ secrets.AZURE_OPENAI_API_BASE }} + # OAI_CONFIG_LIST: ${{ secrets.OAI_CONFIG_LIST }} + # run: | + # pytest test/agentchat/contrib/test_gpt_assistant.py + # - name: Upload coverage to Codecov + # uses: codecov/codecov-action@v3 + # with: + # file: ./coverage.xml + # flags: unittests TeachableAgent: strategy: matrix: @@ -229,44 +230,44 @@ jobs: with: file: ./coverage.xml flags: unittests - WebSurfer: - strategy: - matrix: - os: [ubuntu-latest] - python-version: ["3.11"] - runs-on: ${{ matrix.os }} - environment: openai1 - steps: - # checkout to pr branch - - name: Checkout - uses: actions/checkout@v4 - with: - ref: ${{ github.event.pull_request.head.sha }} - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v5 - with: - python-version: ${{ matrix.python-version }} - - name: Install packages and dependencies - run: | - docker --version - python -m pip install --upgrade pip wheel - pip install -e .[websurfer] - python -c "import autogen" - pip install pytest-cov>=5 - - name: Coverage - env: - OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} - AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }} - AZURE_OPENAI_API_BASE: ${{ secrets.AZURE_OPENAI_API_BASE }} - OAI_CONFIG_LIST: ${{ secrets.OAI_CONFIG_LIST }} - BING_API_KEY: ${{ secrets.BING_API_KEY }} - run: | - pytest test/agentchat/contrib/test_web_surfer.py - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v3 - with: - file: ./coverage.xml - flags: unittests + # WebSurfer: + # strategy: + # matrix: + # os: [ubuntu-latest] + # python-version: ["3.11"] + # runs-on: ${{ matrix.os }} + # environment: openai1 + # steps: + # # checkout to pr branch + # - name: Checkout + # uses: actions/checkout@v4 + # with: + # ref: ${{ github.event.pull_request.head.sha }} + # - name: Set up Python ${{ matrix.python-version }} + # uses: actions/setup-python@v5 + # with: + # python-version: ${{ matrix.python-version }} + # - name: Install packages and dependencies + # run: | + # docker --version + # python -m pip install --upgrade pip wheel + # pip install -e .[websurfer] + # python -c "import autogen" + # pip install pytest-cov>=5 + # - name: Coverage + # env: + # OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + # AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }} + # AZURE_OPENAI_API_BASE: ${{ secrets.AZURE_OPENAI_API_BASE }} + # OAI_CONFIG_LIST: ${{ secrets.OAI_CONFIG_LIST }} + # BING_API_KEY: ${{ secrets.BING_API_KEY }} + # run: | + # pytest test/agentchat/contrib/test_web_surfer.py + # - name: Upload coverage to Codecov + # uses: codecov/codecov-action@v3 + # with: + # file: ./coverage.xml + # flags: unittests ImageGen: strategy: @@ -303,40 +304,40 @@ jobs: file: ./coverage.xml flags: unittests - AgentOptimizer: - strategy: - matrix: - os: [ubuntu-latest] - python-version: ["3.11"] - runs-on: ${{ matrix.os }} - environment: openai1 - steps: - # checkout to pr branch - - name: Checkout - uses: actions/checkout@v4 - with: - ref: ${{ github.event.pull_request.head.sha }} - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v5 - with: - python-version: ${{ matrix.python-version }} - - name: Install packages and dependencies - run: | - docker --version - python -m pip install --upgrade pip wheel - pip install -e . - python -c "import autogen" - pip install pytest-cov>=5 - - name: Coverage - env: - OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} - AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }} - AZURE_OPENAI_API_BASE: ${{ secrets.AZURE_OPENAI_API_BASE }} - OAI_CONFIG_LIST: ${{ secrets.OAI_CONFIG_LIST }} - run: | - pytest test/agentchat/contrib/test_agent_optimizer.py - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v3 - with: - file: ./coverage.xml - flags: unittests + # AgentOptimizer: + # strategy: + # matrix: + # os: [ubuntu-latest] + # python-version: ["3.11"] + # runs-on: ${{ matrix.os }} + # environment: openai1 + # steps: + # # checkout to pr branch + # - name: Checkout + # uses: actions/checkout@v4 + # with: + # ref: ${{ github.event.pull_request.head.sha }} + # - name: Set up Python ${{ matrix.python-version }} + # uses: actions/setup-python@v5 + # with: + # python-version: ${{ matrix.python-version }} + # - name: Install packages and dependencies + # run: | + # docker --version + # python -m pip install --upgrade pip wheel + # pip install -e . + # python -c "import autogen" + # pip install pytest-cov>=5 + # - name: Coverage + # env: + # OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + # AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }} + # AZURE_OPENAI_API_BASE: ${{ secrets.AZURE_OPENAI_API_BASE }} + # OAI_CONFIG_LIST: ${{ secrets.OAI_CONFIG_LIST }} + # run: | + # pytest test/agentchat/contrib/test_agent_optimizer.py + # - name: Upload coverage to Codecov + # uses: codecov/codecov-action@v3 + # with: + # file: ./coverage.xml + # flags: unittests diff --git a/.github/workflows/contrib-tests.yml b/.github/workflows/contrib-tests.yml index 3abe257dfad6..7280c680d734 100644 --- a/.github/workflows/contrib-tests.yml +++ b/.github/workflows/contrib-tests.yml @@ -613,6 +613,46 @@ jobs: file: ./coverage.xml flags: unittests + OllamaTest: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest, windows-2019] + python-version: ["3.9", "3.10", "3.11", "3.12"] + exclude: + - os: macos-latest + python-version: "3.9" + steps: + - uses: actions/checkout@v4 + with: + lfs: true + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + - name: Install packages and dependencies for all tests + run: | + python -m pip install --upgrade pip wheel + pip install pytest-cov>=5 + - name: Install packages and dependencies for Ollama + run: | + pip install -e .[ollama,test] + - name: Set AUTOGEN_USE_DOCKER based on OS + shell: bash + run: | + if [[ ${{ matrix.os }} != ubuntu-latest ]]; then + echo "AUTOGEN_USE_DOCKER=False" >> $GITHUB_ENV + fi + - name: Coverage + run: | + pytest test/oai/test_ollama.py --skip-openai + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v3 + with: + file: ./coverage.xml + flags: unittests + BedrockTest: runs-on: ${{ matrix.os }} strategy: diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index f2967c13f5f0..09253b20b846 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -43,14 +43,24 @@ jobs: # - name: Conda list # shell: pwsh # run: conda list - - name: Build + - name: Build autogen shell: pwsh run: | pip install twine - python setup.py sdist bdist_wheel - - name: Publish to PyPI + python setup.py sdist bdist_wheel --name "autogen" + - name: Publish autogen to PyPI env: - TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} - TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} + TWINE_USERNAME: ${{ secrets.AUTOGEN_PYPI_USERNAME }} + TWINE_PASSWORD: ${{ secrets.AUTOGEN_PYPI_PASSWORD }} shell: pwsh - run: twine upload dist/* + run: twine upload dist/autogen* + - name: Build pyautogen + shell: pwsh + run: | + python setup.py sdist bdist_wheel --name "pyautogen" + - name: Publish pyautogen to PyPI + env: + TWINE_USERNAME: ${{ secrets.PYAUTOGEN_PYPI_USERNAME }} + TWINE_PASSWORD: ${{ secrets.PYAUTOGEN_PYPI_PASSWORD }} + shell: pwsh + run: twine upload dist/*pyautogen* diff --git a/autogen/agentchat/contrib/capabilities/transforms.py b/autogen/agentchat/contrib/capabilities/transforms.py index 4cdd0879e9bd..ab81f37b3e61 100644 --- a/autogen/agentchat/contrib/capabilities/transforms.py +++ b/autogen/agentchat/contrib/capabilities/transforms.py @@ -451,3 +451,111 @@ def _compress_text(self, text: str) -> Tuple[str, int]: def _validate_min_tokens(self, min_tokens: Optional[int]): if min_tokens is not None and min_tokens <= 0: raise ValueError("min_tokens must be greater than 0 or None") + + +class TextMessageContentName: + """A transform for including the agent's name in the content of a message. + + How to create and apply the transform: + # Imports + from autogen.agentchat.contrib.capabilities import transform_messages, transforms + + # Create Transform + name_transform = transforms.TextMessageContentName(position="start", format_string="'{name}' said:\n") + + # Create the TransformMessages + context_handling = transform_messages.TransformMessages( + transforms=[ + name_transform + ] + ) + + # Add it to an agent so when they run inference it will apply to the messages + context_handling.add_to_agent(my_agent) + """ + + def __init__( + self, + position: str = "start", + format_string: str = "{name}:\n", + deduplicate: bool = True, + filter_dict: Optional[Dict] = None, + exclude_filter: bool = True, + ): + """ + Args: + position (str): The position to add the name to the content. The possible options are 'start' or 'end'. Defaults to 'start'. + format_string (str): The f-string to format the message name with. Use '{name}' as a placeholder for the agent's name. Defaults to '{name}:\n' and must contain '{name}'. + deduplicate (bool): Whether to deduplicate the formatted string so it doesn't appear twice (sometimes the LLM will add it to new messages itself). Defaults to True. + filter_dict (None or dict): A dictionary to filter out messages that you want/don't want to compress. + If None, no filters will be applied. + exclude_filter (bool): If exclude filter is True (the default value), messages that match the filter will be + excluded from compression. If False, messages that match the filter will be compressed. + """ + + assert isinstance(position, str) and position in ["start", "end"] + assert isinstance(format_string, str) and "{name}" in format_string + assert isinstance(deduplicate, bool) and deduplicate is not None + + self._position = position + self._format_string = format_string + self._deduplicate = deduplicate + self._filter_dict = filter_dict + self._exclude_filter = exclude_filter + + # Track the number of messages changed for logging + self._messages_changed = 0 + + def apply_transform(self, messages: List[Dict]) -> List[Dict]: + """Applies the name change to the message based on the position and format string. + + Args: + messages (List[Dict]): A list of message dictionaries. + + Returns: + List[Dict]: A list of dictionaries with the message content updated with names. + """ + # Make sure there is at least one message + if not messages: + return messages + + messages_changed = 0 + processed_messages = copy.deepcopy(messages) + for message in processed_messages: + # Some messages may not have content. + if not transforms_util.is_content_right_type( + message.get("content") + ) or not transforms_util.is_content_right_type(message.get("name")): + continue + + if not transforms_util.should_transform_message(message, self._filter_dict, self._exclude_filter): + continue + + if transforms_util.is_content_text_empty(message["content"]) or transforms_util.is_content_text_empty( + message["name"] + ): + continue + + # Get and format the name in the content + content = message["content"] + formatted_name = self._format_string.format(name=message["name"]) + + if self._position == "start": + if not self._deduplicate or not content.startswith(formatted_name): + message["content"] = f"{formatted_name}{content}" + + messages_changed += 1 + else: + if not self._deduplicate or not content.endswith(formatted_name): + message["content"] = f"{content}{formatted_name}" + + messages_changed += 1 + + self._messages_changed = messages_changed + return processed_messages + + def get_logs(self, pre_transform_messages: List[Dict], post_transform_messages: List[Dict]) -> Tuple[str, bool]: + if self._messages_changed > 0: + return f"{self._messages_changed} message(s) changed to incorporate name.", True + else: + return "No messages changed to incorporate name.", False diff --git a/autogen/agentchat/contrib/graph_rag/__init__.py b/autogen/agentchat/contrib/graph_rag/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/autogen/agentchat/contrib/graph_rag/document.py b/autogen/agentchat/contrib/graph_rag/document.py new file mode 100644 index 000000000000..9730269c7ab6 --- /dev/null +++ b/autogen/agentchat/contrib/graph_rag/document.py @@ -0,0 +1,24 @@ +from dataclasses import dataclass +from enum import Enum, auto +from typing import Optional + + +class DocumentType(Enum): + """ + Enum for supporting document type. + """ + + TEXT = auto() + HTML = auto() + PDF = auto() + + +@dataclass +class Document: + """ + A wrapper of graph store query results. + """ + + doctype: DocumentType + data: Optional[object] = None + path_or_url: Optional[str] = "" diff --git a/autogen/agentchat/contrib/graph_rag/graph_query_engine.py b/autogen/agentchat/contrib/graph_rag/graph_query_engine.py new file mode 100644 index 000000000000..28ef6ede84a6 --- /dev/null +++ b/autogen/agentchat/contrib/graph_rag/graph_query_engine.py @@ -0,0 +1,51 @@ +from dataclasses import dataclass, field +from typing import List, Optional, Protocol + +from .document import Document + + +@dataclass +class GraphStoreQueryResult: + """ + A wrapper of graph store query results. + + answer: human readable answer to question/query. + results: intermediate results to question/query, e.g. node entities. + """ + + answer: Optional[str] = None + results: list = field(default_factory=list) + + +class GraphQueryEngine(Protocol): + """An abstract base class that represents a graph query engine on top of a underlying graph database. + + This interface defines the basic methods for graph rag. + """ + + def init_db(self, input_doc: List[Document] | None = None): + """ + This method initializes graph database with the input documents or records. + Usually, it takes the following steps, + 1. connecting to a graph database. + 2. extract graph nodes, edges based on input data, graph schema and etc. + 3. build indexes etc. + + Args: + input_doc: a list of input documents that are used to build the graph in database. + + Returns: GraphStore + """ + pass + + def add_records(self, new_records: List) -> bool: + """ + Add new records to the underlying database and add to the graph if required. + """ + pass + + def query(self, question: str, n_results: int = 1, **kwargs) -> GraphStoreQueryResult: + """ + This method transform a string format question into database query and return the result. + """ + pass diff --git a/autogen/agentchat/contrib/graph_rag/graph_rag_capability.py b/autogen/agentchat/contrib/graph_rag/graph_rag_capability.py new file mode 100644 index 000000000000..b6412305e069 --- /dev/null +++ b/autogen/agentchat/contrib/graph_rag/graph_rag_capability.py @@ -0,0 +1,56 @@ +from autogen.agentchat.contrib.capabilities.agent_capability import AgentCapability +from autogen.agentchat.conversable_agent import ConversableAgent + +from .graph_query_engine import GraphQueryEngine + + +class GraphRagCapability(AgentCapability): + """ + A graph rag capability uses a graph query engine to give a conversable agent the graph rag ability. + + An agent class with graph rag capability could + 1. create a graph in the underlying database with input documents. + 2. retrieved relevant information based on messages received by the agent. + 3. generate answers from retrieved information and send messages back. + + For example, + graph_query_engine = GraphQueryEngine(...) + graph_query_engine.init_db([Document(doc1), Document(doc2), ...]) + + graph_rag_agent = ConversableAgent( + name="graph_rag_agent", + max_consecutive_auto_reply=3, + ... + ) + graph_rag_capability = GraphRagCapbility(graph_query_engine) + graph_rag_capability.add_to_agent(graph_rag_agent) + + user_proxy = UserProxyAgent( + name="user_proxy", + code_execution_config=False, + is_termination_msg=lambda msg: "TERMINATE" in msg["content"], + human_input_mode="ALWAYS", + ) + user_proxy.initiate_chat(graph_rag_agent, message="Name a few actors who've played in 'The Matrix'") + + # ChatResult( + # chat_id=None, + # chat_history=[ + # {'content': 'Name a few actors who've played in \'The Matrix\'', 'role': 'graph_rag_agent'}, + # {'content': 'A few actors who have played in The Matrix are: + # - Keanu Reeves + # - Laurence Fishburne + # - Carrie-Anne Moss + # - Hugo Weaving', + # 'role': 'user_proxy'}, + # ...) + + """ + + def __init__(self, query_engine: GraphQueryEngine): + """ + initialize graph rag capability with a graph query engine + """ + ... + + def add_to_agent(self, agent: ConversableAgent): ... diff --git a/autogen/logger/file_logger.py b/autogen/logger/file_logger.py index 5309c38c71b5..fb0debb31aaf 100644 --- a/autogen/logger/file_logger.py +++ b/autogen/logger/file_logger.py @@ -29,6 +29,7 @@ from autogen.oai.gemini import GeminiClient from autogen.oai.groq import GroqClient from autogen.oai.mistral import MistralAIClient + from autogen.oai.ollama import OllamaClient from autogen.oai.together import TogetherClient logger = logging.getLogger(__name__) @@ -222,6 +223,7 @@ def log_new_client( | TogetherClient | GroqClient | CohereClient + | OllamaClient | BedrockClient ), wrapper: OpenAIWrapper, diff --git a/autogen/logger/sqlite_logger.py b/autogen/logger/sqlite_logger.py index 09616e054196..807fd0e69005 100644 --- a/autogen/logger/sqlite_logger.py +++ b/autogen/logger/sqlite_logger.py @@ -30,6 +30,7 @@ from autogen.oai.gemini import GeminiClient from autogen.oai.groq import GroqClient from autogen.oai.mistral import MistralAIClient + from autogen.oai.ollama import OllamaClient from autogen.oai.together import TogetherClient logger = logging.getLogger(__name__) @@ -409,6 +410,7 @@ def log_new_client( TogetherClient, GroqClient, CohereClient, + OllamaClient, BedrockClient, ], wrapper: OpenAIWrapper, diff --git a/autogen/oai/anthropic.py b/autogen/oai/anthropic.py index 4c9d33d776b8..4f8010fc3519 100644 --- a/autogen/oai/anthropic.py +++ b/autogen/oai/anthropic.py @@ -320,7 +320,7 @@ def oai_messages_to_anthropic_messages(params: Dict[str, Any]) -> list[dict[str, last_tool_result_index = -1 for message in params["messages"]: if message["role"] == "system": - params["system"] = message["content"] + params["system"] = params.get("system", "") + ("\n" if "system" in params else "") + message["content"] else: # New messages will be added here, manage role alternations expected_role = "user" if len(processed_messages) % 2 == 0 else "assistant" diff --git a/autogen/oai/client.py b/autogen/oai/client.py index 9943d1ab9ecd..8d4ff2e1b9b0 100644 --- a/autogen/oai/client.py +++ b/autogen/oai/client.py @@ -90,6 +90,13 @@ except ImportError as e: cohere_import_exception = e +try: + from autogen.oai.ollama import OllamaClient + + ollama_import_exception: Optional[ImportError] = None +except ImportError as e: + ollama_import_exception = e + try: from autogen.oai.bedrock import BedrockClient @@ -535,6 +542,11 @@ def _register_default_client(self, config: Dict[str, Any], openai_config: Dict[s raise ImportError("Please install `cohere` to use the Cohere API.") client = CohereClient(**openai_config) self._clients.append(client) + elif api_type is not None and api_type.startswith("ollama"): + if ollama_import_exception: + raise ImportError("Please install `ollama` to use the Ollama API.") + client = OllamaClient(**openai_config) + self._clients.append(client) elif api_type is not None and api_type.startswith("bedrock"): self._configure_openai_config_for_bedrock(config, openai_config) if bedrock_import_exception: diff --git a/autogen/oai/ollama.py b/autogen/oai/ollama.py new file mode 100644 index 000000000000..cf154f635da9 --- /dev/null +++ b/autogen/oai/ollama.py @@ -0,0 +1,579 @@ +"""Create an OpenAI-compatible client using Ollama's API. + +Example: + llm_config={ + "config_list": [{ + "api_type": "ollama", + "model": "mistral:7b-instruct-v0.3-q6_K" + } + ]} + + agent = autogen.AssistantAgent("my_agent", llm_config=llm_config) + +Install Ollama's python library using: pip install --upgrade ollama + +Resources: +- https://github.com/ollama/ollama-python +""" + +from __future__ import annotations + +import copy +import json +import random +import re +import time +import warnings +from typing import Any, Dict, List, Tuple + +import ollama +from fix_busted_json import repair_json +from ollama import Client +from openai.types.chat import ChatCompletion, ChatCompletionMessageToolCall +from openai.types.chat.chat_completion import ChatCompletionMessage, Choice +from openai.types.completion_usage import CompletionUsage + +from autogen.oai.client_utils import should_hide_tools, validate_parameter + + +class OllamaClient: + """Client for Ollama's API.""" + + # Defaults for manual tool calling + # Instruction is added to the first system message and provides directions to follow a two step + # process + # 1. (before tools have been called) Return JSON with the functions to call + # 2. (directly after tools have been called) Return Text describing the results of the function calls in text format + + # Override using "manual_tool_call_instruction" config parameter + TOOL_CALL_MANUAL_INSTRUCTION = ( + "You are to follow a strict two step process that will occur over " + "a number of interactions, so pay attention to what step you are in based on the full " + "conversation. We will be taking turns so only do one step at a time so don't perform step " + "2 until step 1 is complete and I've told you the result. The first step is to choose one " + "or more functions based on the request given and return only JSON with the functions and " + "arguments to use. The second step is to analyse the given output of the function and summarise " + "it returning only TEXT and not Python or JSON. " + "For argument values, be sure numbers aren't strings, they should not have double quotes around them. " + "In terms of your response format, for step 1 return only JSON and NO OTHER text, " + "for step 2 return only text and NO JSON/Python/Markdown. " + 'The format for running a function is [{"name": "function_name1", "arguments":{"argument_name": "argument_value"}},{"name": "function_name2", "arguments":{"argument_name": "argument_value"}}] ' + 'Make sure the keys "name" and "arguments" are as described. ' + "If you don't get the format correct, try again. " + "The following functions are available to you:[FUNCTIONS_LIST]" + ) + + # Appended to the last user message if no tools have been called + # Override using "manual_tool_call_step1" config parameter + TOOL_CALL_MANUAL_STEP1 = " (proceed with step 1)" + + # Appended to the user message after tools have been executed. Will create a 'user' message if one doesn't exist. + # Override using "manual_tool_call_step2" config parameter + TOOL_CALL_MANUAL_STEP2 = " (proceed with step 2)" + + def __init__(self, **kwargs): + """Note that no api_key or environment variable is required for Ollama. + + Args: + None + """ + + def message_retrieval(self, response) -> List: + """ + Retrieve and return a list of strings or a list of Choice.Message from the response. + + NOTE: if a list of Choice.Message is returned, it currently needs to contain the fields of OpenAI's ChatCompletion Message object, + since that is expected for function or tool calling in the rest of the codebase at the moment, unless a custom agent is being used. + """ + return [choice.message for choice in response.choices] + + def cost(self, response) -> float: + return response.cost + + @staticmethod + def get_usage(response) -> Dict: + """Return usage summary of the response using RESPONSE_USAGE_KEYS.""" + # ... # pragma: no cover + return { + "prompt_tokens": response.usage.prompt_tokens, + "completion_tokens": response.usage.completion_tokens, + "total_tokens": response.usage.total_tokens, + "cost": response.cost, + "model": response.model, + } + + def parse_params(self, params: Dict[str, Any]) -> Dict[str, Any]: + """Loads the parameters for Ollama API from the passed in parameters and returns a validated set. Checks types, ranges, and sets defaults""" + ollama_params = {} + + # Check that we have what we need to use Ollama's API + # https://github.com/ollama/ollama/blob/main/docs/api.md#generate-a-completion + + # The main parameters are model, prompt, stream, and options + # Options is a dictionary of parameters for the model + # There are other, advanced, parameters such as format, system (to override system message), template, raw, etc. - not used + + # We won't enforce the available models + ollama_params["model"] = params.get("model", None) + assert ollama_params[ + "model" + ], "Please specify the 'model' in your config list entry to nominate the Ollama model to use." + + ollama_params["stream"] = validate_parameter(params, "stream", bool, True, False, None, None) + + # Build up the options dictionary + # https://github.com/ollama/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values + options_dict = {} + + if "num_predict" in params: + # Maximum number of tokens to predict, note: -1 is infinite, -2 is fill context, 128 is default + ollama_params["num_predict"] = validate_parameter(params, "num_predict", int, False, 128, None, None) + + if "repeat_penalty" in params: + options_dict["repeat_penalty"] = validate_parameter( + params, "repeat_penalty", (int, float), False, 1.1, None, None + ) + + if "seed" in params: + options_dict["seed"] = validate_parameter(params, "seed", int, False, 42, None, None) + + if "temperature" in params: + ollama_params["temperature"] = validate_parameter( + params, "temperature", (int, float), False, 0.8, None, None + ) + + if "top_k" in params: + ollama_params["top_k"] = validate_parameter(params, "top_k", int, False, 40, None, None) + + if "top_p" in params: + ollama_params["top_p"] = validate_parameter(params, "top_p", (int, float), False, 0.9, None, None) + + if self._native_tool_calls and self._tools_in_conversation and not self._should_hide_tools: + ollama_params["tools"] = params["tools"] + + # Ollama doesn't support streaming with tools natively + if ollama_params["stream"] and self._native_tool_calls: + warnings.warn( + "Streaming is not supported when using tools and 'Native' tool calling, streaming will be disabled.", + UserWarning, + ) + + ollama_params["stream"] = False + + if not self._native_tool_calls and self._tools_in_conversation: + # For manual tool calling we have injected the available tools into the prompt + # and we don't want to force JSON mode + ollama_params["format"] = "" # Don't force JSON for manual tool calling mode + + if len(options_dict) != 0: + ollama_params["options"] = options_dict + + return ollama_params + + def create(self, params: Dict) -> ChatCompletion: + + messages = params.get("messages", []) + + # Are tools involved in this conversation? + self._tools_in_conversation = "tools" in params + + # We provide second-level filtering out of tools to avoid LLMs re-calling tools continuously + if self._tools_in_conversation: + hide_tools = validate_parameter( + params, "hide_tools", str, False, "never", None, ["if_all_run", "if_any_run", "never"] + ) + self._should_hide_tools = should_hide_tools(messages, params["tools"], hide_tools) + else: + self._should_hide_tools = False + + # Are we using native Ollama tool calling, otherwise we're doing manual tool calling + # We allow the user to decide if they want to use Ollama's tool calling + # or for tool calling to be handled manually through text messages + # Default is True = Ollama's tool calling + self._native_tool_calls = validate_parameter(params, "native_tool_calls", bool, False, True, None, None) + + if not self._native_tool_calls: + # Load defaults + self._manual_tool_call_instruction = validate_parameter( + params, "manual_tool_call_instruction", str, False, self.TOOL_CALL_MANUAL_INSTRUCTION, None, None + ) + self._manual_tool_call_step1 = validate_parameter( + params, "manual_tool_call_step1", str, False, self.TOOL_CALL_MANUAL_STEP1, None, None + ) + self._manual_tool_call_step2 = validate_parameter( + params, "manual_tool_call_step2", str, False, self.TOOL_CALL_MANUAL_STEP2, None, None + ) + + # Convert AutoGen messages to Ollama messages + ollama_messages = self.oai_messages_to_ollama_messages( + messages, + ( + params["tools"] + if (not self._native_tool_calls and self._tools_in_conversation) and not self._should_hide_tools + else None + ), + ) + + # Parse parameters to the Ollama API's parameters + ollama_params = self.parse_params(params) + + ollama_params["messages"] = ollama_messages + + # Token counts will be returned + prompt_tokens = 0 + completion_tokens = 0 + total_tokens = 0 + + ans = None + try: + if "client_host" in params: + client = Client(host=params["client_host"]) + response = client.chat(**ollama_params) + else: + response = ollama.chat(**ollama_params) + except Exception as e: + raise RuntimeError(f"Ollama exception occurred: {e}") + else: + + if ollama_params["stream"]: + # Read in the chunks as they stream, taking in tool_calls which may be across + # multiple chunks if more than one suggested + ans = "" + for chunk in response: + ans = ans + (chunk["message"]["content"] or "") + + if "done_reason" in chunk: + prompt_tokens = chunk["prompt_eval_count"] if "prompt_eval_count" in chunk else 0 + completion_tokens = chunk["eval_count"] if "eval_count" in chunk else 0 + total_tokens = prompt_tokens + completion_tokens + else: + # Non-streaming finished + ans: str = response["message"]["content"] + + prompt_tokens = response["prompt_eval_count"] if "prompt_eval_count" in response else 0 + completion_tokens = response["eval_count"] if "eval_count" in response else 0 + total_tokens = prompt_tokens + completion_tokens + + if response is not None: + + # Defaults + ollama_finish = "stop" + tool_calls = None + + # Id and streaming text into response + if ollama_params["stream"]: + response_content = ans + response_id = chunk["created_at"] + else: + response_content = response["message"]["content"] + response_id = response["created_at"] + + # Process tools in the response + if self._tools_in_conversation: + + if self._native_tool_calls: + + if not ollama_params["stream"]: + response_content = response["message"]["content"] + + # Native tool calling + if "tool_calls" in response["message"]: + ollama_finish = "tool_calls" + tool_calls = [] + random_id = random.randint(0, 10000) + for tool_call in response["message"]["tool_calls"]: + tool_calls.append( + ChatCompletionMessageToolCall( + id="ollama_func_{}".format(random_id), + function={ + "name": tool_call["function"]["name"], + "arguments": json.dumps(tool_call["function"]["arguments"]), + }, + type="function", + ) + ) + + random_id += 1 + + elif not self._native_tool_calls: + + # Try to convert the response to a tool call object + response_toolcalls = response_to_tool_call(ans) + + # If we can, then we've got tool call(s) + if response_toolcalls is not None: + ollama_finish = "tool_calls" + tool_calls = [] + random_id = random.randint(0, 10000) + + for json_function in response_toolcalls: + tool_calls.append( + ChatCompletionMessageToolCall( + id="ollama_manual_func_{}".format(random_id), + function={ + "name": json_function["name"], + "arguments": ( + json.dumps(json_function["arguments"]) + if "arguments" in json_function + else "{}" + ), + }, + type="function", + ) + ) + + random_id += 1 + + # Blank the message content + response_content = "" + + else: + raise RuntimeError("Failed to get response from Ollama.") + + # Convert response to AutoGen response + message = ChatCompletionMessage( + role="assistant", + content=response_content, + function_call=None, + tool_calls=tool_calls, + ) + choices = [Choice(finish_reason=ollama_finish, index=0, message=message)] + + response_oai = ChatCompletion( + id=response_id, + model=ollama_params["model"], + created=int(time.time()), + object="chat.completion", + choices=choices, + usage=CompletionUsage( + prompt_tokens=prompt_tokens, + completion_tokens=completion_tokens, + total_tokens=total_tokens, + ), + cost=0, # Local models, FREE! + ) + + return response_oai + + def oai_messages_to_ollama_messages(self, messages: list[Dict[str, Any]], tools: list) -> list[dict[str, Any]]: + """Convert messages from OAI format to Ollama's format. + We correct for any specific role orders and types, and convert tools to messages (as Ollama can't use tool messages) + """ + + ollama_messages = copy.deepcopy(messages) + + # Remove the name field + for message in ollama_messages: + if "name" in message: + message.pop("name", None) + + # Having a 'system' message on the end does not work well with Ollama, so we change it to 'user' + # 'system' messages on the end are typical of the summarisation message: summary_method="reflection_with_llm" + if len(ollama_messages) > 1 and ollama_messages[-1]["role"] == "system": + ollama_messages[-1]["role"] = "user" + + # Process messages for tool calling manually + if tools is not None and not self._native_tool_calls: + # 1. We need to append instructions to the starting system message on function calling + # 2. If we have not yet called tools we append "step 1 instruction" to the latest user message + # 3. If we have already called tools we append "step 2 instruction" to the latest user message + + have_tool_calls = False + have_tool_results = False + last_tool_result_index = -1 + + for i, message in enumerate(ollama_messages): + if "tool_calls" in message: + have_tool_calls = True + if "tool_call_id" in message: + have_tool_results = True + last_tool_result_index = i + + tool_result_is_last_msg = have_tool_results and last_tool_result_index == len(ollama_messages) - 1 + + if ollama_messages[0]["role"] == "system": + manual_instruction = self._manual_tool_call_instruction + + # Build a string of the functions available + functions_string = "" + for function in tools: + functions_string += f"""\n{function}\n""" + + # Replace single quotes with double questions - Not sure why this helps the LLM perform + # better, but it seems to. Monitor and remove if not necessary. + functions_string = functions_string.replace("'", '"') + + manual_instruction = manual_instruction.replace("[FUNCTIONS_LIST]", functions_string) + + # Update the system message with the instructions and functions + ollama_messages[0]["content"] = ollama_messages[0]["content"] + manual_instruction.rstrip() + + # If we are still in the function calling or evaluating process, append the steps instruction + if not have_tool_calls or tool_result_is_last_msg: + if ollama_messages[0]["role"] == "system": + # NOTE: we require a system message to exist for the manual steps texts + # Append the manual step instructions + content_to_append = ( + self._manual_tool_call_step1 if not have_tool_results else self._manual_tool_call_step2 + ) + + if content_to_append != "": + # Append the relevant tool call instruction to the latest user message + if ollama_messages[-1]["role"] == "user": + ollama_messages[-1]["content"] = ollama_messages[-1]["content"] + content_to_append + else: + ollama_messages.append({"role": "user", "content": content_to_append}) + + # Convert tool call and tool result messages to normal text messages for Ollama + for i, message in enumerate(ollama_messages): + if "tool_calls" in message: + # Recommended tool calls + content = "Run the following function(s):" + for tool_call in message["tool_calls"]: + content = content + "\n" + str(tool_call) + ollama_messages[i] = {"role": "assistant", "content": content} + if "tool_call_id" in message: + # Executed tool results + message["result"] = message["content"] + del message["content"] + del message["role"] + content = "The following function was run: " + str(message) + ollama_messages[i] = {"role": "user", "content": content} + + # As we are changing messages, let's merge if they have two user messages on the end and the last one is tool call step instructions + if ( + len(ollama_messages) >= 2 + and not self._native_tool_calls + and ollama_messages[-2]["role"] == "user" + and ollama_messages[-1]["role"] == "user" + and ( + ollama_messages[-1]["content"] == self._manual_tool_call_step1 + or ollama_messages[-1]["content"] == self._manual_tool_call_step2 + ) + ): + ollama_messages[-2]["content"] = ollama_messages[-2]["content"] + ollama_messages[-1]["content"] + del ollama_messages[-1] + + # Ensure the last message is a user / system message, if not, add a user message + if ollama_messages[-1]["role"] != "user" and ollama_messages[-1]["role"] != "system": + ollama_messages.append({"role": "user", "content": "Please continue."}) + + return ollama_messages + + +def response_to_tool_call(response_string: str) -> Any: + """Attempts to convert the response to an object, aimed to align with function format [{},{}]""" + + # We try and detect the list[dict] format: + # Pattern 1 is [{},{}] + # Pattern 2 is {} (without the [], so could be a single function call) + patterns = [r"\[[\s\S]*?\]", r"\{[\s\S]*\}"] + + for i, pattern in enumerate(patterns): + # Search for the pattern in the input string + matches = re.findall(pattern, response_string.strip()) + + for match in matches: + + # It has matched, extract it and load it + json_str = match.strip() + data_object = None + + try: + # Attempt to convert it as is + data_object = json.loads(json_str) + except Exception: + try: + # If that fails, attempt to repair it + + if i == 0: + # Enclose to a JSON object for repairing, which is restored upon fix + fixed_json = repair_json("{'temp':" + json_str + "}") + data_object = json.loads(fixed_json) + data_object = data_object["temp"] + else: + fixed_json = repair_json(json_str) + data_object = json.loads(fixed_json) + except json.JSONDecodeError as e: + if e.msg == "Invalid \\escape": + # Handle Mistral/Mixtral trying to escape underlines with \\ + try: + json_str = json_str.replace("\\_", "_") + if i == 0: + fixed_json = repair_json("{'temp':" + json_str + "}") + data_object = json.loads(fixed_json) + data_object = data_object["temp"] + else: + fixed_json = repair_json("{'temp':" + json_str + "}") + data_object = json.loads(fixed_json) + except Exception: + pass + except Exception: + pass + + if data_object is not None: + data_object = _object_to_tool_call(data_object) + + if data_object is not None: + return data_object + + # There's no tool call in the response + return None + + +def _object_to_tool_call(data_object: Any) -> List[Dict]: + """Attempts to convert an object to a valid tool call object List[Dict] and returns it, if it can, otherwise None""" + + # If it's a dictionary and not a list then wrap in a list + if isinstance(data_object, dict): + data_object = [data_object] + + # Validate that the data is a list of dictionaries + if isinstance(data_object, list) and all(isinstance(item, dict) for item in data_object): + # Perfect format, a list of dictionaries + + # Check that each dictionary has at least 'name', optionally 'arguments' and no other keys + is_invalid = False + for item in data_object: + if not is_valid_tool_call_item(item): + is_invalid = True + break + + # All passed, name and (optionally) arguments exist for all entries. + if not is_invalid: + return data_object + elif isinstance(data_object, list): + # If it's a list but the items are not dictionaries, check if they are strings that can be converted to dictionaries + data_copy = data_object.copy() + is_invalid = False + for i, item in enumerate(data_copy): + try: + new_item = eval(item) + if isinstance(new_item, dict): + if is_valid_tool_call_item(new_item): + data_object[i] = new_item + else: + is_invalid = True + break + else: + is_invalid = True + break + except Exception: + is_invalid = True + break + + if not is_invalid: + return data_object + + return None + + +def is_valid_tool_call_item(call_item: dict) -> bool: + """Check that a dictionary item has at least 'name', optionally 'arguments' and no other keys to match a tool call JSON""" + if "name" not in call_item or not isinstance(call_item["name"], str): + return False + + if set(call_item.keys()) - {"name", "arguments"}: + return False + + return True diff --git a/autogen/runtime_logging.py b/autogen/runtime_logging.py index 176e605dd1be..b55e4cfca342 100644 --- a/autogen/runtime_logging.py +++ b/autogen/runtime_logging.py @@ -25,6 +25,7 @@ from autogen.oai.gemini import GeminiClient from autogen.oai.groq import GroqClient from autogen.oai.mistral import MistralAIClient + from autogen.oai.ollama import OllamaClient from autogen.oai.together import TogetherClient logger = logging.getLogger(__name__) @@ -128,6 +129,7 @@ def log_new_client( TogetherClient, GroqClient, CohereClient, + OllamaClient, BedrockClient, ], wrapper: OpenAIWrapper, diff --git a/autogen/version.py b/autogen/version.py index cbe2b887fbeb..6cb97ca84ae9 100644 --- a/autogen/version.py +++ b/autogen/version.py @@ -4,4 +4,4 @@ # # Portions derived from https://github.com/microsoft/autogen are under the MIT License. # SPDX-License-Identifier: MIT -__version__ = "0.2.35" +__version__ = "0.3.0b4" diff --git a/setup.py b/setup.py index 5506b45127f0..b8e6a4616481 100644 --- a/setup.py +++ b/setup.py @@ -6,6 +6,7 @@ # SPDX-License-Identifier: MIT import os import platform +import sys import setuptools @@ -97,11 +98,21 @@ "mistral": ["mistralai>=1.0.1"], "groq": ["groq>=0.9.0"], "cohere": ["cohere>=5.5.8"], + "ollama": ["ollama>=0.3.2", "fix_busted_json>=0.0.18"], "bedrock": ["boto3>=1.34.149"], } + +if "--name" in sys.argv: + index = sys.argv.index("--name") + sys.argv.pop(index) # Removes --name + package_name = sys.argv.pop(index) # Removes the value after --name +else: + package_name = "autogen" + + setuptools.setup( - name="pyautogen", + name=package_name, version=__version__, author="Chi Wang & Qingyun Wu", author_email="auto-gen@outlook.com", @@ -114,8 +125,9 @@ extras_require=extra_require, classifiers=[ "Programming Language :: Python :: 3", - "License :: OSI Approved :: Apache License 2.0", + "License :: OSI Approved :: Apache Software License", "Operating System :: OS Independent", ], + license="Apache Software License 2.0", python_requires=">=3.8,<3.13", ) diff --git a/test/agentchat/contrib/capabilities/test_transforms.py b/test/agentchat/contrib/capabilities/test_transforms.py index 22a6ef1cc771..20233c77e456 100644 --- a/test/agentchat/contrib/capabilities/test_transforms.py +++ b/test/agentchat/contrib/capabilities/test_transforms.py @@ -15,6 +15,7 @@ MessageHistoryLimiter, MessageTokenLimiter, TextMessageCompressor, + TextMessageContentName, ) from autogen.agentchat.contrib.capabilities.transforms_util import count_text_tokens @@ -66,6 +67,42 @@ def get_tool_messages_kept() -> List[Dict]: ] +def get_messages_with_names() -> List[Dict]: + return [ + {"role": "system", "content": "I am the system."}, + {"role": "user", "name": "charlie", "content": "I think the sky is blue."}, + {"role": "user", "name": "mary", "content": "The sky is red."}, + {"role": "user", "name": "bob", "content": "The sky is crimson."}, + ] + + +def get_messages_with_names_post_start() -> List[Dict]: + return [ + {"role": "system", "content": "I am the system."}, + {"role": "user", "name": "charlie", "content": "'charlie' said:\nI think the sky is blue."}, + {"role": "user", "name": "mary", "content": "'mary' said:\nThe sky is red."}, + {"role": "user", "name": "bob", "content": "'bob' said:\nThe sky is crimson."}, + ] + + +def get_messages_with_names_post_end() -> List[Dict]: + return [ + {"role": "system", "content": "I am the system."}, + {"role": "user", "name": "charlie", "content": "I think the sky is blue.\n(said 'charlie')"}, + {"role": "user", "name": "mary", "content": "The sky is red.\n(said 'mary')"}, + {"role": "user", "name": "bob", "content": "The sky is crimson.\n(said 'bob')"}, + ] + + +def get_messages_with_names_post_filtered() -> List[Dict]: + return [ + {"role": "system", "content": "I am the system."}, + {"role": "user", "name": "charlie", "content": "I think the sky is blue."}, + {"role": "user", "name": "mary", "content": "'mary' said:\nThe sky is red."}, + {"role": "user", "name": "bob", "content": "'bob' said:\nThe sky is crimson."}, + ] + + def get_text_compressors() -> List[TextCompressor]: compressors: List[TextCompressor] = [_MockTextCompressor()] try: @@ -306,6 +343,63 @@ def test_text_compression_with_filter(messages, text_compressor): assert _filter_dict_test(post_transform, pre_transform, ["user"], exclude_filter=False) +@pytest.mark.parametrize("messages", [get_messages_with_names()]) +def test_message_content_name(messages): + # Test including content name in messages + + # Add name at the start with format: "'{name}' said:\n" + content_transform = TextMessageContentName(position="start", format_string="'{name}' said:\n") + transformed_messages = content_transform.apply_transform(messages=messages) + + assert transformed_messages == get_messages_with_names_post_start() + + # Add name at the end with format: "\n(said '{name}')" + content_transform = TextMessageContentName(position="end", format_string="\n(said '{name}')") + transformed_messages_end = content_transform.apply_transform(messages=messages) + + assert transformed_messages_end == get_messages_with_names_post_end() + + # Test filtering out exclusion + content_transform = TextMessageContentName( + position="start", + format_string="'{name}' said:\n", + filter_dict={"name": ["charlie"]}, + exclude_filter=True, # Exclude + ) + + transformed_messages_end = content_transform.apply_transform(messages=messages) + + assert transformed_messages_end == get_messages_with_names_post_filtered() + + # Test filtering (inclusion) + content_transform = TextMessageContentName( + position="start", + format_string="'{name}' said:\n", + filter_dict={"name": ["mary", "bob"]}, + exclude_filter=False, # Include + ) + + transformed_messages_end = content_transform.apply_transform(messages=messages) + + assert transformed_messages_end == get_messages_with_names_post_filtered() + + # Test instantiation + with pytest.raises(AssertionError): + TextMessageContentName(position=123) # Invalid type for position + + with pytest.raises(AssertionError): + TextMessageContentName(position="middle") # Invalid value for position + + with pytest.raises(AssertionError): + TextMessageContentName(format_string=123) # Invalid type for format_string + + with pytest.raises(AssertionError): + TextMessageContentName(format_string="Agent:\n") # Missing '{name}' in format_string + + with pytest.raises(AssertionError): + TextMessageContentName(deduplicate="yes") # Invalid type for deduplicate + + if __name__ == "__main__": long_messages = get_long_messages() short_messages = get_short_messages() diff --git a/test/agentchat/contrib/graph_rag/test_graph_rag_basic.py b/test/agentchat/contrib/graph_rag/test_graph_rag_basic.py new file mode 100644 index 000000000000..7c4a5094947c --- /dev/null +++ b/test/agentchat/contrib/graph_rag/test_graph_rag_basic.py @@ -0,0 +1,17 @@ +from unittest.mock import Mock + +from autogen.agentchat.contrib.graph_rag.graph_query_engine import GraphQueryEngine +from autogen.agentchat.contrib.graph_rag.graph_rag_capability import GraphRagCapability +from autogen.agentchat.conversable_agent import ConversableAgent + + +def test_dry_run(): + """Dry run for basic graph rag objects.""" + mock_graph_query_engine = Mock(spec=GraphQueryEngine) + + graph_rag_agent = ConversableAgent( + name="graph_rag_agent", + max_consecutive_auto_reply=3, + ) + graph_rag_capability = GraphRagCapability(mock_graph_query_engine) + graph_rag_capability.add_to_agent(graph_rag_agent) diff --git a/test/agentchat/contrib/test_agent_builder.py b/test/agentchat/contrib/test_agent_builder.py index b7f7ec888252..4612577c46ec 100755 --- a/test/agentchat/contrib/test_agent_builder.py +++ b/test/agentchat/contrib/test_agent_builder.py @@ -51,8 +51,8 @@ def test_build(): builder = AgentBuilder( config_file_or_env=OAI_CONFIG_LIST, config_file_location=KEY_LOC, - builder_model=["gpt-4", "gpt-4-1106-preview"], - agent_model=["gpt-4", "gpt-4-1106-preview"], + builder_model_tags=["gpt-4"], + agent_model_tags=["gpt-4"], ) building_task = ( "Find a paper on arxiv by programming, and analyze its application in some domain. " @@ -83,8 +83,8 @@ def test_build_from_library(): builder = AgentBuilder( config_file_or_env=OAI_CONFIG_LIST, config_file_location=KEY_LOC, - builder_model=["gpt-4", "gpt-4-1106-preview"], - agent_model=["gpt-4", "gpt-4-1106-preview"], + builder_model_tags=["gpt-4"], + agent_model_tags=["gpt-4"], ) building_task = ( "Find a paper on arxiv by programming, and analyze its application in some domain. " @@ -136,8 +136,8 @@ def test_save(): builder = AgentBuilder( config_file_or_env=OAI_CONFIG_LIST, config_file_location=KEY_LOC, - builder_model=["gpt-4", "gpt-4-1106-preview"], - agent_model=["gpt-4", "gpt-4-1106-preview"], + builder_model_tags=["gpt-4"], + agent_model_tags=["gpt-4"], ) building_task = ( "Find a paper on arxiv by programming, and analyze its application in some domain. " @@ -173,8 +173,10 @@ def test_load(): builder = AgentBuilder( config_file_or_env=OAI_CONFIG_LIST, config_file_location=KEY_LOC, - builder_model=["gpt-4", "gpt-4-1106-preview"], - agent_model=["gpt-4", "gpt-4-1106-preview"], + # builder_model=["gpt-4", "gpt-4-1106-preview"], + # agent_model=["gpt-4", "gpt-4-1106-preview"], + builder_model_tags=["gpt-4"], + agent_model_tags=["gpt-4"], ) config_save_path = f"{here}/example_test_agent_builder_config.json" @@ -202,8 +204,8 @@ def test_clear_agent(): builder = AgentBuilder( config_file_or_env=OAI_CONFIG_LIST, config_file_location=KEY_LOC, - builder_model=["gpt-4", "gpt-4-1106-preview"], - agent_model=["gpt-4", "gpt-4-1106-preview"], + builder_model_tags=["gpt-4"], + agent_model_tags=["gpt-4"], ) config_save_path = f"{here}/example_test_agent_builder_config.json" diff --git a/test/oai/test_ollama.py b/test/oai/test_ollama.py new file mode 100644 index 000000000000..729e1b95d816 --- /dev/null +++ b/test/oai/test_ollama.py @@ -0,0 +1,302 @@ +from unittest.mock import MagicMock, patch + +import pytest + +try: + from autogen.oai.ollama import OllamaClient, response_to_tool_call + + skip = False +except ImportError: + OllamaClient = object + InternalServerError = object + skip = True + + +# Fixtures for mock data +@pytest.fixture +def mock_response(): + class MockResponse: + def __init__(self, text, choices, usage, cost, model): + self.text = text + self.choices = choices + self.usage = usage + self.cost = cost + self.model = model + + return MockResponse + + +@pytest.fixture +def ollama_client(): + + # Set Ollama client with some default values + client = OllamaClient() + + client._native_tool_calls = True + client._tools_in_conversation = False + + return client + + +skip_reason = "Ollama dependency is not installed" + + +# Test initialization and configuration +@pytest.mark.skipif(skip, reason=skip_reason) +def test_initialization(): + + # Creation works without an api_key + OllamaClient() + + +# Test parameters +@pytest.mark.skipif(skip, reason=skip_reason) +def test_parsing_params(ollama_client): + # All parameters (with default values) + params = { + "model": "llama3.1:8b", + "temperature": 0.8, + "num_predict": 128, + "repeat_penalty": 1.1, + "seed": 42, + "top_k": 40, + "top_p": 0.9, + "stream": False, + } + expected_params = { + "model": "llama3.1:8b", + "temperature": 0.8, + "num_predict": 128, + "top_k": 40, + "top_p": 0.9, + "options": { + "repeat_penalty": 1.1, + "seed": 42, + }, + "stream": False, + } + result = ollama_client.parse_params(params) + assert result == expected_params + + # Incorrect types, defaults should be set, will show warnings but not trigger assertions + params = { + "model": "llama3.1:8b", + "temperature": "0.5", + "num_predict": "128", + "repeat_penalty": "1.1", + "seed": "42", + "top_k": "40", + "top_p": "0.9", + "stream": "True", + } + result = ollama_client.parse_params(params) + assert result == expected_params + + # Only model, others set as defaults if they are mandatory + params = { + "model": "llama3.1:8b", + } + expected_params = {"model": "llama3.1:8b", "stream": False} + result = ollama_client.parse_params(params) + assert result == expected_params + + # No model + params = { + "temperature": 0.8, + } + + with pytest.raises(AssertionError) as assertinfo: + result = ollama_client.parse_params(params) + + assert "Please specify the 'model' in your config list entry to nominate the Ollama model to use." in str( + assertinfo.value + ) + + +# Test text generation +@pytest.mark.skipif(skip, reason=skip_reason) +@patch("autogen.oai.ollama.OllamaClient.create") +def test_create_response(mock_chat, ollama_client): + # Mock OllamaClient.chat response + mock_ollama_response = MagicMock() + mock_ollama_response.choices = [ + MagicMock(finish_reason="stop", message=MagicMock(content="Example Ollama response", tool_calls=None)) + ] + mock_ollama_response.id = "mock_ollama_response_id" + mock_ollama_response.model = "llama3.1:8b" + mock_ollama_response.usage = MagicMock(prompt_tokens=10, completion_tokens=20) # Example token usage + + mock_chat.return_value = mock_ollama_response + + # Test parameters + params = { + "messages": [{"role": "user", "content": "Hello"}, {"role": "assistant", "content": "World"}], + "model": "llama3.1:8b", + } + + # Call the create method + response = ollama_client.create(params) + + # Assertions to check if response is structured as expected + assert ( + response.choices[0].message.content == "Example Ollama response" + ), "Response content should match expected output" + assert response.id == "mock_ollama_response_id", "Response ID should match the mocked response ID" + assert response.model == "llama3.1:8b", "Response model should match the mocked response model" + assert response.usage.prompt_tokens == 10, "Response prompt tokens should match the mocked response usage" + assert response.usage.completion_tokens == 20, "Response completion tokens should match the mocked response usage" + + +# Test functions/tools +@pytest.mark.skipif(skip, reason=skip_reason) +@patch("autogen.oai.ollama.OllamaClient.create") +def test_create_response_with_tool_call(mock_chat, ollama_client): + # Mock OllamaClient.chat response + mock_function = MagicMock(name="currency_calculator") + mock_function.name = "currency_calculator" + mock_function.arguments = '{"base_currency": "EUR", "quote_currency": "USD", "base_amount": 123.45}' + + mock_function_2 = MagicMock(name="get_weather") + mock_function_2.name = "get_weather" + mock_function_2.arguments = '{"location": "New York"}' + + mock_chat.return_value = MagicMock( + choices=[ + MagicMock( + finish_reason="tool_calls", + message=MagicMock( + content="Sample text about the functions", + tool_calls=[ + MagicMock(id="gdRdrvnHh", function=mock_function), + MagicMock(id="abRdrvnHh", function=mock_function_2), + ], + ), + ) + ], + id="mock_ollama_response_id", + model="llama3.1:8b", + usage=MagicMock(prompt_tokens=10, completion_tokens=20), + ) + + # Construct parameters + converted_functions = [ + { + "type": "function", + "function": { + "description": "Currency exchange calculator.", + "name": "currency_calculator", + "parameters": { + "type": "object", + "properties": { + "base_amount": {"type": "number", "description": "Amount of currency in base_currency"}, + }, + "required": ["base_amount"], + }, + }, + } + ] + ollama_messages = [ + {"role": "user", "content": "How much is 123.45 EUR in USD?"}, + {"role": "assistant", "content": "World"}, + ] + + # Call the create method + response = ollama_client.create({"messages": ollama_messages, "tools": converted_functions, "model": "llama3.1:8b"}) + + # Assertions to check if the functions and content are included in the response + assert response.choices[0].message.content == "Sample text about the functions" + assert response.choices[0].message.tool_calls[0].function.name == "currency_calculator" + assert response.choices[0].message.tool_calls[1].function.name == "get_weather" + + +# Test function parsing with manual tool calling +@pytest.mark.skipif(skip, reason=skip_reason) +def test_manual_tool_calling_parsing(ollama_client): + # Test the parsing of a tool call within the response content (fully correct) + response_content = """[{"name": "weather_forecast", "arguments":{"location": "New York"}},{"name": "currency_calculator", "arguments":{"base_amount": 123.45, "quote_currency": "EUR", "base_currency": "USD"}}]""" + + response_tool_calls = response_to_tool_call(response_content) + + expected_tool_calls = [ + {"name": "weather_forecast", "arguments": {"location": "New York"}}, + { + "name": "currency_calculator", + "arguments": {"base_amount": 123.45, "quote_currency": "EUR", "base_currency": "USD"}, + }, + ] + + assert ( + response_tool_calls == expected_tool_calls + ), "Manual Tool Calling Parsing of response did not yield correct tool_calls (full string match)" + + # Test the parsing with a substring containing the response content (should still pass) + response_content = """I will call two functions, weather_forecast and currency_calculator:\n[{"name": "weather_forecast", "arguments":{"location": "New York"}},{"name": "currency_calculator", "arguments":{"base_amount": 123.45, "quote_currency": "EUR", "base_currency": "USD"}}]""" + + response_tool_calls = response_to_tool_call(response_content) + + assert ( + response_tool_calls == expected_tool_calls + ), "Manual Tool Calling Parsing of response did not yield correct tool_calls (partial string match)" + + # Test the parsing with an invalid function call + response_content = """[{"function": "weather_forecast", "args":{"location": "New York"}},{"function": "currency_calculator", "args":{"base_amount": 123.45, "quote_currency": "EUR", "base_currency": "USD"}}]""" + + response_tool_calls = response_to_tool_call(response_content) + + assert ( + response_tool_calls is None + ), "Manual Tool Calling Parsing of response did not yield correct tool_calls (invalid function call)" + + # Test the parsing with plain text + response_content = """Call the weather_forecast function and pass in 'New York' as the 'location' argument.""" + + response_tool_calls = response_to_tool_call(response_content) + + assert ( + response_tool_calls is None + ), "Manual Tool Calling Parsing of response did not yield correct tool_calls (no function in text)" + + +# Test message conversion from OpenAI to Ollama format +@pytest.mark.skipif(skip, reason=skip_reason) +def test_oai_messages_to_ollama_messages(ollama_client): + # Test that the "name" key is removed + test_messages = [ + {"role": "system", "content": "You are a helpful AI bot."}, + {"role": "user", "name": "anne", "content": "Why is the sky blue?"}, + ] + messages = ollama_client.oai_messages_to_ollama_messages(test_messages, None) + + expected_messages = [ + {"role": "system", "content": "You are a helpful AI bot."}, + {"role": "user", "content": "Why is the sky blue?"}, + ] + + assert messages == expected_messages, "'name' was not removed from messages" + + # Test that there isn't a final system message and it's changed to user + test_messages.append({"role": "system", "content": "Summarise the conversation."}) + + messages = ollama_client.oai_messages_to_ollama_messages(test_messages, None) + + expected_messages = [ + {"role": "system", "content": "You are a helpful AI bot."}, + {"role": "user", "content": "Why is the sky blue?"}, + {"role": "user", "content": "Summarise the conversation."}, + ] + + assert messages == expected_messages, "Final 'system' message was not changed to 'user'" + + # Test that the last message is a user or system message and if not, add a continue message + test_messages[2] = {"role": "assistant", "content": "The sky is blue because that's a great colour."} + + messages = ollama_client.oai_messages_to_ollama_messages(test_messages, None) + + expected_messages = [ + {"role": "system", "content": "You are a helpful AI bot."}, + {"role": "user", "content": "Why is the sky blue?"}, + {"role": "assistant", "content": "The sky is blue because that's a great colour."}, + {"role": "user", "content": "Please continue."}, + ] + + assert messages == expected_messages, "'Please continue' message was not appended." diff --git a/website/docs/topics/non-openai-models/local-ollama.ipynb b/website/docs/topics/non-openai-models/local-ollama.ipynb new file mode 100644 index 000000000000..95803e50e593 --- /dev/null +++ b/website/docs/topics/non-openai-models/local-ollama.ipynb @@ -0,0 +1,631 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Ollama\n", + "\n", + "[Ollama](https://ollama.com/) is a local inference engine that enables you to run open-weight LLMs in your environment. It has native support for a large number of models such as Google's Gemma, Meta's Llama 2/3/3.1, Microsoft's Phi 3, Mistral.AI's Mistral/Mixtral, and Cohere's Command R models.\n", + "\n", + "Note: Previously, to use Ollama with AutoGen you required LiteLLM. Now it can be used directly and supports tool calling." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Features\n", + "\n", + "When using this Ollama client class, messages are tailored to accommodate the specific requirements of Ollama's API and this includes message role sequences, support for function/tool calling, and token usage.\n", + "\n", + "## Installing Ollama\n", + "\n", + "For Mac and Windows, [download Ollama](https://ollama.com/download).\n", + "\n", + "For Linux:\n", + "\n", + "```bash\n", + "curl -fsSL https://ollama.com/install.sh | sh\n", + "```\n", + "\n", + "## Downloading models for Ollama\n", + "\n", + "Ollama has a library of models to choose from, see them [here](https://ollama.com/library).\n", + "\n", + "Before you can use a model, you need to download it (using the name of the model from the library):\n", + "\n", + "```bash\n", + "ollama pull llama3.1\n", + "```\n", + "\n", + "To view the models you have downloaded and can use:\n", + "\n", + "```bash\n", + "ollama list\n", + "```\n", + "\n", + "## Getting started with AutoGen and Ollama\n", + "\n", + "When installing AutoGen, you need to install the `pyautogen` package with the Ollama library.\n", + "\n", + "``` bash\n", + "pip install pyautogen[ollama]\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "See the sample `OAI_CONFIG_LIST` below showing how the Ollama client class is used by specifying the `api_type` as `ollama`.\n", + "\n", + "```python\n", + "[\n", + " {\n", + " \"model\": \"llama3.1\",\n", + " \"api_type\": \"ollama\"\n", + " },\n", + " {\n", + " \"model\": \"llama3.1:8b-instruct-q6_K\",\n", + " \"api_type\": \"ollama\"\n", + " },\n", + " {\n", + " \"model\": \"mistral-nemo\",\n", + " \"api_type\": \"ollama\"\n", + " }\n", + "]\n", + "```\n", + "\n", + "If you need to specify the URL for your Ollama install, use the `client_host` key in your config as per the below example:\n", + "\n", + "```python\n", + "[\n", + " {\n", + " \"model\": \"llama3.1\",\n", + " \"api_type\": \"ollama\",\n", + " \"client_host\": \"http://192.168.0.1:11434\"\n", + " }\n", + "]\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## API parameters\n", + "\n", + "The following Ollama parameters can be added to your config. See [this link](https://github.com/ollama/ollama/blob/main/docs/api.md#parameters) for further information on them.\n", + "\n", + "- num_predict (integer): -1 is infinite, -2 is fill context, 128 is default\n", + "- repeat_penalty (float)\n", + "- seed (integer)\n", + "- stream (boolean)\n", + "- temperature (float)\n", + "- top_k (int)\n", + "- top_p (float)\n", + "\n", + "Example:\n", + "```python\n", + "[\n", + " {\n", + " \"model\": \"llama3.1:instruct\",\n", + " \"api_type\": \"ollama\",\n", + " \"num_predict\": -1,\n", + " \"repeat_penalty\": 1.1,\n", + " \"seed\": 42,\n", + " \"stream\": False,\n", + " \"temperature\": 1,\n", + " \"top_k\": 50,\n", + " \"top_p\": 0.8\n", + " }\n", + "]\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Two-Agent Coding Example\n", + "\n", + "In this example, we run a two-agent chat with an AssistantAgent (primarily a coding agent) to generate code to count the number of prime numbers between 1 and 10,000 and then it will be executed.\n", + "\n", + "We'll use Meta's Llama 3.1 model which is suitable for coding.\n", + "\n", + "In this example we will specify the URL for the Ollama installation using `client_host`." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "config_list = [\n", + " {\n", + " # Let's choose the Meta's Llama 3.1 model (model names must match Ollama exactly)\n", + " \"model\": \"llama3.1:8b\",\n", + " # We specify the API Type as 'ollama' so it uses the Ollama client class\n", + " \"api_type\": \"ollama\",\n", + " \"stream\": False,\n", + " \"client_host\": \"http://192.168.0.1:11434\",\n", + " }\n", + "]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Importantly, we have tweaked the system message so that the model doesn't return the termination keyword, which we've changed to FINISH, with the code block." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/usr/local/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", + " from .autonotebook import tqdm as notebook_tqdm\n" + ] + } + ], + "source": [ + "from pathlib import Path\n", + "\n", + "from autogen import AssistantAgent, UserProxyAgent\n", + "from autogen.coding import LocalCommandLineCodeExecutor\n", + "\n", + "# Setting up the code executor\n", + "workdir = Path(\"coding\")\n", + "workdir.mkdir(exist_ok=True)\n", + "code_executor = LocalCommandLineCodeExecutor(work_dir=workdir)\n", + "\n", + "# Setting up the agents\n", + "\n", + "# The UserProxyAgent will execute the code that the AssistantAgent provides\n", + "user_proxy_agent = UserProxyAgent(\n", + " name=\"User\",\n", + " code_execution_config={\"executor\": code_executor},\n", + " is_termination_msg=lambda msg: \"FINISH\" in msg.get(\"content\"),\n", + ")\n", + "\n", + "system_message = \"\"\"You are a helpful AI assistant who writes code and the user\n", + "executes it. Solve tasks using your python coding skills.\n", + "In the following cases, suggest python code (in a python coding block) for the\n", + "user to execute. When using code, you must indicate the script type in the code block.\n", + "You only need to create one working sample.\n", + "Do not suggest incomplete code which requires users to modify it.\n", + "Don't use a code block if it's not intended to be executed by the user. Don't\n", + "include multiple code blocks in one response. Do not ask users to copy and\n", + "paste the result. Instead, use 'print' function for the output when relevant.\n", + "Check the execution result returned by the user.\n", + "\n", + "If the result indicates there is an error, fix the error.\n", + "\n", + "IMPORTANT: If it has executed successfully, ONLY output 'FINISH'.\"\"\"\n", + "\n", + "# The AssistantAgent, using the Ollama config, will take the coding request and return code\n", + "assistant_agent = AssistantAgent(\n", + " name=\"Ollama Assistant\",\n", + " system_message=system_message,\n", + " llm_config={\"config_list\": config_list},\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can now start the chat." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[33mUser\u001b[0m (to Ollama Assistant):\n", + "\n", + "Provide code to count the number of prime numbers from 1 to 10000.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[33mOllama Assistant\u001b[0m (to User):\n", + "\n", + "```python\n", + "def is_prime(n):\n", + " if n <= 1:\n", + " return False\n", + " for i in range(2, int(n**0.5) + 1):\n", + " if n % i == 0:\n", + " return False\n", + " return True\n", + "\n", + "count = sum(is_prime(i) for i in range(1, 10001))\n", + "print(count)\n", + "```\n", + "\n", + "Please execute this code. I will wait for the result.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> NO HUMAN INPUT RECEIVED.\u001b[0m\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[31m\n", + ">>>>>>>> EXECUTING CODE BLOCK (inferred language is python)...\u001b[0m\n", + "\u001b[33mUser\u001b[0m (to Ollama Assistant):\n", + "\n", + "exitcode: 0 (execution succeeded)\n", + "Code output: 1229\n", + "\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[33mOllama Assistant\u001b[0m (to User):\n", + "\n", + "FINISH\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> NO HUMAN INPUT RECEIVED.\u001b[0m\n" + ] + } + ], + "source": [ + "# Start the chat, with the UserProxyAgent asking the AssistantAgent the message\n", + "chat_result = user_proxy_agent.initiate_chat(\n", + " assistant_agent,\n", + " message=\"Provide code to count the number of prime numbers from 1 to 10000.\",\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tool Calling - Native vs Manual\n", + "\n", + "Ollama supports native tool calling (Ollama v0.3.1 library onward). If you install AutoGen with `pip install pyautogen[ollama]` you will be able to use native tool calling.\n", + "\n", + "The parameter `native_tool_calls` in your configuration allows you to specify if you want to use Ollama's native tool calling (default) or manual tool calling.\n", + "\n", + "```python\n", + "[\n", + " {\n", + " \"model\": \"llama3.1\",\n", + " \"api_type\": \"ollama\",\n", + " \"client_host\": \"http://192.168.0.1:11434\",\n", + " \"native_tool_calls\": True # Use Ollama's native tool calling, False for manual\n", + " }\n", + "]\n", + "```\n", + "\n", + "Native tool calling only works with certain models and an exception will be thrown if you try to use it with an unsupported model.\n", + "\n", + "Manual tool calling allows you to use tool calling with any Ollama model. It incorporates guided tool calling messages into the prompt that guide the LLM through the process of selecting a tool and then evaluating the result of the tool. As to be expected, the ability to follow instructions and return formatted JSON is highly dependent on the model.\n", + "\n", + "You can tailor the manual tool calling messages by adding these parameters to your configuration:\n", + "\n", + "- `manual_tool_call_instruction`\n", + "- `manual_tool_call_step1`\n", + "- `manual_tool_call_step2`\n", + "\n", + "To use manual tool calling set `native_tool_calls` to `False`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Reducing repetitive tool calls\n", + "\n", + "By incorporating tools into a conversation, LLMs can often continually recommend them to be called, even after they've been called and a result returned. This can lead to a never ending cycle of tool calls.\n", + "\n", + "To remove the chance of an LLM recommending a tool call, an additional parameter called `hide_tools` can be used to specify when tools are hidden from the LLM. The string values for the parameter are:\n", + "\n", + "- 'never': tools are never hidden\n", + "- 'if_all_run': tools are hidden if all tools have been called\n", + "- 'if_any_run': tools are hidden if any tool has been called\n", + "\n", + "This can be used with native or manual tool calling, an example of a configuration is shown below.\n", + "\n", + "```python\n", + "[\n", + " {\n", + " \"model\": \"llama3.1\",\n", + " \"api_type\": \"ollama\",\n", + " \"client_host\": \"http://192.168.0.1:11434\",\n", + " \"native_tool_calls\": True,\n", + " \"hide_tools\": \"if_any_run\" # Hide tools once any tool has been called\n", + " }\n", + "]\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tool Call Example\n", + "\n", + "In this example, instead of writing code, we will have an agent assist with some trip planning using multiple tool calling.\n", + "\n", + "Again, we'll use Meta's versatile Llama 3.1.\n", + "\n", + "Native Ollama tool calling will be used and we'll utilise the `hide_tools` parameter to hide the tools once all have been called." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "import json\n", + "from typing import Literal\n", + "\n", + "from typing_extensions import Annotated\n", + "\n", + "import autogen\n", + "\n", + "config_list = [\n", + " {\n", + " # Let's choose the Meta's Llama 3.1 model (model names must match Ollama exactly)\n", + " \"model\": \"llama3.1:8b\",\n", + " \"api_type\": \"ollama\",\n", + " \"stream\": False,\n", + " \"client_host\": \"http://192.168.0.1:11434\",\n", + " \"hide_tools\": \"if_any_run\",\n", + " }\n", + "]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We'll create our agents. Importantly, we're using native Ollama tool calling and to help guide it we add the JSON to the system_message so that the number fields aren't wrapped in quotes (becoming strings)." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "# Create the agent for tool calling\n", + "chatbot = autogen.AssistantAgent(\n", + " name=\"chatbot\",\n", + " system_message=\"\"\"For currency exchange and weather forecasting tasks,\n", + " only use the functions you have been provided with.\n", + " Example of the return JSON is:\n", + " {\n", + " \"parameter_1_name\": 100.00,\n", + " \"parameter_2_name\": \"ABC\",\n", + " \"parameter_3_name\": \"DEF\",\n", + " }.\n", + " Another example of the return JSON is:\n", + " {\n", + " \"parameter_1_name\": \"GHI\",\n", + " \"parameter_2_name\": \"ABC\",\n", + " \"parameter_3_name\": \"DEF\",\n", + " \"parameter_4_name\": 123.00,\n", + " }.\n", + " Output 'HAVE FUN!' when an answer has been provided.\"\"\",\n", + " llm_config={\"config_list\": config_list},\n", + ")\n", + "\n", + "# Note that we have changed the termination string to be \"HAVE FUN!\"\n", + "user_proxy = autogen.UserProxyAgent(\n", + " name=\"user_proxy\",\n", + " is_termination_msg=lambda x: x.get(\"content\", \"\") and \"HAVE FUN!\" in x.get(\"content\", \"\"),\n", + " human_input_mode=\"NEVER\",\n", + " max_consecutive_auto_reply=1,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create and register our functions (tools). See the [tutorial chapter on tool use](/docs/tutorial/tool-use) \n", + "for more information." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "# Currency Exchange function\n", + "\n", + "CurrencySymbol = Literal[\"USD\", \"EUR\"]\n", + "\n", + "# Define our function that we expect to call\n", + "\n", + "\n", + "def exchange_rate(base_currency: CurrencySymbol, quote_currency: CurrencySymbol) -> float:\n", + " if base_currency == quote_currency:\n", + " return 1.0\n", + " elif base_currency == \"USD\" and quote_currency == \"EUR\":\n", + " return 1 / 1.1\n", + " elif base_currency == \"EUR\" and quote_currency == \"USD\":\n", + " return 1.1\n", + " else:\n", + " raise ValueError(f\"Unknown currencies {base_currency}, {quote_currency}\")\n", + "\n", + "\n", + "# Register the function with the agent\n", + "\n", + "\n", + "@user_proxy.register_for_execution()\n", + "@chatbot.register_for_llm(description=\"Currency exchange calculator.\")\n", + "def currency_calculator(\n", + " base_amount: Annotated[\n", + " float,\n", + " \"Amount of currency in base_currency. Type is float, not string, return value should be a number only, e.g. 987.65.\",\n", + " ],\n", + " base_currency: Annotated[CurrencySymbol, \"Base currency\"] = \"USD\",\n", + " quote_currency: Annotated[CurrencySymbol, \"Quote currency\"] = \"EUR\",\n", + ") -> str:\n", + " quote_amount = exchange_rate(base_currency, quote_currency) * base_amount\n", + " return f\"{format(quote_amount, '.2f')} {quote_currency}\"\n", + "\n", + "\n", + "# Weather function\n", + "\n", + "\n", + "# Example function to make available to model\n", + "def get_current_weather(location, unit=\"fahrenheit\"):\n", + " \"\"\"Get the weather for some location\"\"\"\n", + " if \"chicago\" in location.lower():\n", + " return json.dumps({\"location\": \"Chicago\", \"temperature\": \"13\", \"unit\": unit})\n", + " elif \"san francisco\" in location.lower():\n", + " return json.dumps({\"location\": \"San Francisco\", \"temperature\": \"55\", \"unit\": unit})\n", + " elif \"new york\" in location.lower():\n", + " return json.dumps({\"location\": \"New York\", \"temperature\": \"11\", \"unit\": unit})\n", + " else:\n", + " return json.dumps({\"location\": location, \"temperature\": \"unknown\"})\n", + "\n", + "\n", + "# Register the function with the agent\n", + "\n", + "\n", + "@user_proxy.register_for_execution()\n", + "@chatbot.register_for_llm(description=\"Weather forecast for US cities.\")\n", + "def weather_forecast(\n", + " location: Annotated[str, \"City name\"],\n", + ") -> str:\n", + " weather_details = get_current_weather(location=location)\n", + " weather = json.loads(weather_details)\n", + " return f\"{weather['location']} will be {weather['temperature']} degrees {weather['unit']}\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And run it!" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[33muser_proxy\u001b[0m (to chatbot):\n", + "\n", + "What's the weather in New York and can you tell me how much is 123.45 EUR in USD so I can spend it on my holiday? Throw a few holiday tips in as well.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[33mchatbot\u001b[0m (to user_proxy):\n", + "\n", + "\n", + "\u001b[32m***** Suggested tool call (ollama_func_4506): weather_forecast *****\u001b[0m\n", + "Arguments: \n", + "{\"location\": \"New York\"}\n", + "\u001b[32m********************************************************************\u001b[0m\n", + "\u001b[32m***** Suggested tool call (ollama_func_4507): currency_calculator *****\u001b[0m\n", + "Arguments: \n", + "{\"base_amount\": 123.45, \"base_currency\": \"EUR\", \"quote_currency\": \"USD\"}\n", + "\u001b[32m***********************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION weather_forecast...\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION currency_calculator...\u001b[0m\n", + "\u001b[33muser_proxy\u001b[0m (to chatbot):\n", + "\n", + "\u001b[33muser_proxy\u001b[0m (to chatbot):\n", + "\n", + "\u001b[32m***** Response from calling tool (ollama_func_4506) *****\u001b[0m\n", + "New York will be 11 degrees fahrenheit\n", + "\u001b[32m*********************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[33muser_proxy\u001b[0m (to chatbot):\n", + "\n", + "\u001b[32m***** Response from calling tool (ollama_func_4507) *****\u001b[0m\n", + "135.80 USD\n", + "\u001b[32m*********************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[33mchatbot\u001b[0m (to user_proxy):\n", + "\n", + "Based on the results, it seems that:\n", + "\n", + "* The weather forecast for New York is expected to be around 11 degrees Fahrenheit.\n", + "* The exchange rate for EUR to USD is currently 1 EUR = 1.3580 USD, so 123.45 EUR is equivalent to approximately 135.80 USD.\n", + "\n", + "As a bonus, here are some holiday tips in New York:\n", + "\n", + "* Be sure to try a classic New York-style hot dog from a street cart or a diner.\n", + "* Explore the iconic Central Park and take a stroll through the High Line for some great views of the city.\n", + "* Catch a Broadway show or a concert at one of the many world-class venues in the city.\n", + "\n", + "And... HAVE FUN!\n", + "\n", + "--------------------------------------------------------------------------------\n", + "LLM SUMMARY: The weather forecast for New York is expected to be around 11 degrees Fahrenheit.\n", + "123.45 EUR is equivalent to approximately 135.80 USD.\n", + "Try a classic New York-style hot dog, explore Central Park and the High Line, and catch a Broadway show or concert during your visit.\n" + ] + } + ], + "source": [ + "# start the conversation\n", + "res = user_proxy.initiate_chat(\n", + " chatbot,\n", + " message=\"What's the weather in New York and can you tell me how much is 123.45 EUR in USD so I can spend it on my holiday? Throw a few holiday tips in as well.\",\n", + " summary_method=\"reflection_with_llm\",\n", + ")\n", + "\n", + "print(f\"LLM SUMMARY: {res.summary['content']}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Great, we can see that Llama 3.1 has helped choose the right functions, their parameters, and then summarised them for us." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}