-
Notifications
You must be signed in to change notification settings - Fork 22
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
Fix role mapping in GPTAssistantAgent for OpenAI API compatibility #46
Conversation
@evandavid1 is it good to have a test that covers the new case supported in the contrib-openai CI? |
Hi @sonichi yes, good idea. Will add tests and include going forward |
@evandavid1, thanks for the great contribution! Are you working on adding tests? Also, I see that there is a conflict in the file |
Hi - sorry for delay, reviewing conflict and will submit tests
…On Sun, Oct 20, 2024 at 11:43 AM Qingyun Wu ***@***.***> wrote:
@evandavid1 <https://github.com/evandavid1>, thanks for the great
contribution! Are you working on adding tests? Also, I see that there is a
conflict in the file autogen/agentchat/contrib/gpt_assistant_agent.py.
Could you fix the conflict? Thank you!
—
Reply to this email directly, view it on GitHub
<#46 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ANXHUETSWT24E4PCJ27YJLDZ4P2T5AVCNFSM6AAAAABPCCFYXCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDIMRVGE3TIMJXGE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
This test ensures proper role mapping for message role types tool and function in GPTAssistantAgent. It verifies that these roles are correctly converted to 'assistant' before API calls to maintain compatibility with OpenAI's Assistant API.
Merge branch 'gptassistant-fix' of https://github.com/evandavid1/autogen into gptassistant-fix
@qingyun-wu the GPTAssistant test in contrib-openai.yml is disabled. Shall we enable the test or run the test manually? |
For now, we will need to run the tests manually. I will figure out how to add the tests back! |
Thanks for the contribution @evandavid1! Tests pass when I run them locally LGTM |
Why are these changes needed?
This change addresses a bug where an openai.BadRequestError occurs when using a GPTAssistantAgent and a ConversableAgent that utilizes a tool call within the same GroupChat. Specifically, when the ConversableAgent performs a tool call, messages with internal role 'tool' are added to the chat history. When the GPTAssistantAgent then tries to generate a response, it sends this conversation history to the OpenAI Assistant API, which returns an error because it only accepts messages with roles 'user', 'assistant', per the error message.
Note: Was previously merged to Microsoft autogen repo:
microsoft#3555
Problem Details:
The primary problem occurs when messages with roles like "tool" or "function" are submitted to the OpenAI Assistant API, which expects roles to be either "user" or "assistant". As I was reviewing the best approach to fix this issue, it was clear that modifying the role directly in _append_oai_message can disrupt the internal message history and cause unit tests to fail. Therefore, the recommended solution is to adjust roles within the GPTAssistantAgent when sending to OpenAI API.
Rationale:
By isolating changes to gpt_assistant_agent.py, we avoid impacting other agents and parts of the codebase.
Also we preserve internal logic, so internal AutoGen message roles remain unchanged, ensuring that any internal processing that depends on roles like "function" or "tool" continues to work correctly.
Agents Involved:
A GPTAssistantAgent.
A ConversableAgent that performs tool calls.
A ConversableAgent as user proxy
Error Message:
openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid value: 'tool'. Supported values are: 'user' and 'assistant'.", 'type': 'invalid_request_error', 'param': 'role', 'code': 'invalid_value'}}
Solution:
Role Mapping: The fix involves mapping internal message roles to roles accepted by the OpenAI Assistant API before sending messages.
Implementation:
Added a _map_role_for_api method in gpt_assistant_agent.py to map internal roles to valid API roles.
Internal roles such as 'tool' and 'function' are mapped to 'assistant'.
Modified the _invoke_assistant method to apply this mapping when constructing messages for the API call.
Benefits:
Ensures compatibility with the OpenAI API by sending only accepted roles, preventing the BadRequestError from occurring. Allows the GPTAssistantAgent to function correctly within a groupchat that includes ConversableAgents utilizing tool calls.
Software versions:
pyautogen=0.2.35
python=3.12.2
tested with both openai=1.30.1 and openai=1.47.0
tested with gpt-4o and gpt-4o-mini
Logging:
if you add logging to the mre program, you can see the role issue in the log:
logging:
import logging
logging.basicConfig(level=logging.DEBUG)
output before fix: (role='tool')
DEBUG:openai._base_client:Request options: {'method': 'post', 'url': '/threads/thread_hSK0dCkoMCgRTwXDjxQOeV8v/messages', 'headers': {'OpenAI-Beta': 'assistants=v2'}, 'files': None, 'json_data': {'content': "This is a message from the 'print_message' function.", 'role': 'tool'}}
after fix: (role='system')
'role': 'user', 'name': 'Analyst'}, {'content': "Read the above conversation. Then select the next role from ['user_proxy', 'FunctionAgent', 'Analyst'] to play. Only return the role.", 'name': 'checking_agent', 'role': 'system'}], 'model': 'gpt-4o', 'stream': False, 'temperature': 0.0}}
A Minimal Reproducible Example (MRE) was created to demonstrate the issue and verify that the fix works consistently.
MRE Overview:
Creates an openai assistant analyst agent, then plugs in the asssistant id into the gpt assistant in the groupchat.
Set up GroupChat with GPTAssistantAgent and a ConversableAgent that performs a tool call.
The ConversableAgent invokes a tool, adding a message with role 'tool' to the chat history.
When the GPTAssistantAgent attempts to process the chat history, it sends the messages to the OpenAI API.
Without the fix, the API returns a BadRequestError due to unrecognized roles.
With the fix, the internal roles are correctly mapped, and no error occurs.
MRE:
Testing:
All unit tests have been run locally to ensure existing functionality remains unaffected.
Related issue number
Closes microsoft#3284
microsoft#3284
Checks