microsoft / autogen

A programming framework for agentic AI. Discord: https://aka.ms/autogen-dc. Roadmap: https://aka.ms/autogen-roadmap
https://microsoft.github.io/autogen/
Creative Commons Attribution 4.0 International
28.17k stars 4.11k forks source link

[Issue]: autogen agents for gmail with langchain gmail toolkit giving errors with transform_history #2210

Open abhinandanshahdev opened 3 months ago

abhinandanshahdev commented 3 months ago

Describe the issue

since the new token and message limiting feature is not released yet, I am trying to use transform_history in my code. Here is the code:


import autogen
from autogen.agentchat.contrib.capabilities import context_handling
import os
import json
from dotenv import load_dotenv
from typing import Dict, List, Optional
from langchain_community.agent_toolkits import GmailToolkit
from langchain_community.tools.gmail.utils import (
    build_resource_service,
    get_gmail_credentials,
)
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

# Your dictionary
config_list = [
    {
        "model": "gpt-3.5-turbo-0125",
        "api_key": os.getenv("OPENAI_API_KEY")
    }
    # You can add more configurations here as needed
]

# Serialize the dictionary into a JSON string
config_string = json.dumps(config_list)

# Set the environment variable
os.environ["OAI_CONFIG_LIST"] = config_string

import os   

bing_api_key = os.getenv("BING_API_KEY")
tools = []
function_map = {}

def generate_llm_config(tool):
    # Define the function schema based on the tool's args_schema
    function_schema = {
        "name": tool.name.lower().replace(" ", "_"),
        "description": tool.description,
        "parameters": {
            "type": "object",
            "properties": {},
            "required": [],
        },
    }

    if tool.args is not None:
        function_schema["parameters"]["properties"] = tool.args

    return function_schema

# Gmail setup
credentials = get_gmail_credentials(
    token_file="token.json",
    scopes=["https://www.googleapis.com/auth/gmail.modify"],
    client_secrets_file="credentials.json",
)
api_resource = build_resource_service(credentials=credentials)

toolkit = GmailToolkit(api_resource=api_resource)

for tool in toolkit.get_tools():  # debug_toolkit if you want to use tools directly
    tool_schema = generate_llm_config(tool)
    print(tool_schema)
    tools.append(tool_schema)
    function_map[tool.name] = tool._run

# Construct the llm_config
llm_config = {
    "functions": tools,
    "config_list": config_list,  # Assuming you have this defined elsewhere
    "timeout": 120,
}

user_proxy = autogen.UserProxyAgent(
    name="user_proxy",
    is_termination_msg=lambda x: x.get("content", "") and x.get("content", "").rstrip().endswith("TERMINATE"),
    human_input_mode="NEVER",
    max_consecutive_auto_reply=10,
    code_execution_config={
        "work_dir": "coding",
        "use_docker": False,
    },  # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.
)

user_proxy.register_function(function_map=function_map)

chatbot = autogen.AssistantAgent(
    name="chatbot",
    system_message="only use the functions you have been provided with. Reply TERMINATE when the task is done.",
    llm_config=llm_config,
)

# Instantiate the capability to manage chat history
manage_chat_history = context_handling.TransformChatHistory(max_tokens_per_message=50, max_messages=20, max_tokens=1000)
# Add the capability to the assistant
manage_chat_history.add_to_agent(chatbot)

user_proxy.initiate_chat(
    chatbot,
    message="what does email from Susan Cain Say?",
    llm_config=llm_config,
    clear_history=False
)

At least it seems like a bug, but I may be wrong, please tell me if its the case. I am using Python 3.11 and running in a venv in VS Code.

Steps to reproduce

Run the code as described

Screenshots and logs


erties': {'message_id': {'title': 'Message Id', 'description': 'The unique ID of the email message, retrieved from a search.', 'type': 'string'}}, 'required': []}}
{'name': 'get_gmail_thread', 'description': 'Use this tool to search for email messages. The input must be a valid Gmail query. The output is a JSON list of messages.', 'parameters': {'type': 'object', 'properties': {'thread_id': {'title': 'Thread Id', 'description': 'The thread ID.', 'type': 'string'}}, 'required': []}}
Traceback (most recent call last):
  File "/Users/incubatorone/Documents/AutoGen/main.py", line 94, in <module>
    chatbot = autogen.AssistantAgent(
              ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/incubatorone/Documents/AutoGen/venv/lib/python3.11/site-packages/autogen/agentchat/assistant_agent.py", line 61, in __init__
    super().__init__(
TypeError: ConversableAgent.__init__() got an unexpected keyword argument 'clear_history'

Additional Information

pip list
Package                  Version
------------------------ ----------
aiohttp                  3.9.3
aiosignal                1.3.1
annotated-types          0.6.0
anyio                    4.3.0
attrs                    23.2.0
beautifulsoup4           4.12.3
cachetools               5.3.3
certifi                  2024.2.2
charset-normalizer       3.3.2
dataclasses-json         0.6.4
diskcache                5.6.3
distro                   1.9.0
docker                   7.0.0
FLAML                    2.1.2
frozenlist               1.4.1
google-api-core          2.18.0
google-api-python-client 2.123.0
google-auth              2.29.0
google-auth-httplib2     0.2.0
google-auth-oauthlib     1.2.0
googleapis-common-protos 1.63.0
h11                      0.14.0
httpcore                 1.0.5
httplib2                 0.22.0
httpx                    0.27.0
idna                     3.6
jsonpatch                1.33
jsonpointer              2.4
langchain                0.1.13
langchain-community      0.0.29
langchain-core           0.1.36
langchain-openai         0.1.1
langchain-text-splitters 0.0.1
langsmith                0.1.36
marshmallow              3.21.1
multidict                6.0.5
mypy-extensions          1.0.0
numpy                    1.26.4
oauthlib                 3.2.2
openai                   1.14.3
orjson                   3.10.0
packaging                23.2
pip                      24.0
proto-plus               1.23.0
protobuf                 4.25.3
pyasn1                   0.6.0
pyasn1_modules           0.4.0
pyautogen                0.2.21
pydantic                 2.6.4
pydantic_core            2.16.3
pyparsing                3.1.2
python-dotenv            1.0.1
PyYAML                   6.0.1
regex                    2023.12.25
requests                 2.31.0
requests-oauthlib        2.0.0
rsa                      4.9
setuptools               69.0.2
sniffio                  1.3.1
soupsieve                2.5
SQLAlchemy               2.0.29
tenacity                 8.2.3
termcolor                2.4.0
tiktoken                 0.6.0
tqdm                     4.66.2
typing_extensions        4.10.0
typing-inspect           0.9.0
uritemplate              4.1.1
urllib3                  2.2.1
yarl                     1.9.4
WaelKarkoub commented 3 months ago

@abhinandanshahdev the error says that you passed in a clear_history argument to the chatbot, but the code provided doesn't do that. Is the error you provided up to date?

qingyun-wu commented 2 weeks ago

Hi @abhinandanshahdev, is this issue still open?