Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: GPTAssistantAgent does not invoke assistant API when called generate_reply #2697

Closed
ekzhu opened this issue May 15, 2024 · 7 comments
Closed
Assignees
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed openai-assistant Issues related to assistants from OpenAI

Comments

@ekzhu
Copy link
Collaborator

ekzhu commented May 15, 2024

Describe the bug

Calling generate_reply without a sender on a GPTAssistantAgent won't invoke Assistant API -- it uses the ChatCompletion API.

Steps to reproduce

import os

from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent
from dotenv import load_dotenv

load_dotenv()

assistant_id = os.environ.get("ASSISTANT_ID", None)
llm_config = {
    "config_list": [
    {
        "model": "gpt-4-turbo",
        "api_key": os.environ.get("OPENAI_API_KEY", None),
    }
],
}

assistant_config = {
    "tools": [
        {"type": "code_interpreter"},
    ]
}

code_oai_agent = GPTAssistantAgent(
    name="code_oai_agent",
    instructions="You are a code assistant tool that can run code in the code interpreter",
    llm_config=llm_config,
    assistant_config=assistant_config,
)

print(
    code_oai_agent.generate_reply(
        messages=[
            {
                "content": "\
                    Use code interpreter to run a Monty Hall problem simulation 10000 times, and \
                    tell me how often you win by switching doors.", 
                "role": "user"
            }
        ]
    )
)

Output:

OpenAI client config of GPTAssistantAgent(code_oai_agent) - model: gpt-4-turbo
No matching assistant found, creating a new assistant
Sure, I will simulate the Monty Hall problem 10,000 times and check how often switching doors leads to a win.

In the Monty Hall problem, you choose one of three doors. Behind one door is a car (which you want to win) and behind the other two doors are goats. After selecting a door, the host, who knows what's behind each door, opens another door revealing a goat. You then have the choice to either stick with your initial selection or switch to the other unopened door. The strategy we'll analyze here is always switching.

Let's run the simulation:

```python
import random

def monty_hall_sim(num_trials, switch):
    win_count = 0
    for _ in range(num_trials):
        # There are three doors, one door has the car (win), two have goats (lose)
        doors = ['goat', 'goat', 'car']
        random.shuffle(doors)
        
        # The player picks a random door
        chosen_door = random.randint(0, 2)
        
        # Monty eliminates one of the doors containing a goat
        # Monty cannot open the door chosen by the player if it contains the car
        remaining_doors = [i for i in range(3) if i != chosen_door and doors[i] == 'goat']
        monty_opens = random.choice(remaining_doors)
        
        # Decide whether to switch based on the argument passed
        if switch:
            # The player switches to the remaining unopened door
            possible_doors = [i for i in range(3) if i != chosen_door and i != monty_opens]
            chosen_door = possible_doors[0]
        
        # Check if player has won
        if doors[chosen_door] == 'car':
            win_count += 1
    
    return win_count

# Simulate 10,000 times and always switch
num_trials = 10000
switch = True
wins_by_switching = monty_hall_sim(num_trials, switch)

# Calculate percentage win rate
win_percentage = (wins_by_switching / num_trials) * 100
win_percentage
```

This code will simulate the Monty Hall problem 10,000 times, with the assumption that the player always switches the door after Monty opens a door revealing a goat. It then calculates the percentage of times the player wins the car when using this strategy. Let's execute this simulation.

Expected Behavior

The assistant should use the code interpreter and generate a response using the result of the simulation.

@ekzhu ekzhu added bug Something isn't working openai-assistant Issues related to assistants from OpenAI labels May 15, 2024
@ekzhu
Copy link
Collaborator Author

ekzhu commented May 15, 2024

I believe the fix should be just to change this line:

self.register_reply(Agent, GPTAssistantAgent._invoke_assistant, position=2)

change Agent to [Agent, None].

@ekzhu ekzhu added help wanted Extra attention is needed good first issue Good for newcomers labels May 15, 2024
@krishnashed krishnashed self-assigned this May 16, 2024
@Hk669
Copy link
Collaborator

Hk669 commented May 16, 2024

@krishnashed add these changes to the PR #2677. Thanks

@ekzhu
Copy link
Collaborator Author

ekzhu commented May 21, 2024

@Hk669 @krishnashed I think a separate PR is more suitable. #2677 has some issues and I think will take some time to merge.

@krishnashed
Copy link
Collaborator

Ya makes sense. Let me raise another one

This was referenced May 22, 2024
@krishnashed
Copy link
Collaborator

@ekzhu we are facing same issues in the new PR as well, #2751

The checks still fail

@prithvi2226
Copy link

Hi! Has this issue been solved?

@krishnashed
Copy link
Collaborator

Yes it is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed openai-assistant Issues related to assistants from OpenAI
Projects
None yet
Development

No branches or pull requests

4 participants