mirror of
https://github.com/microsoft/autogen.git
synced 2026-04-20 03:02:16 -04:00
Implement Overwrite Tools Functionality in GPTAssistantAgent (#1434)
* added overwrite_tools logic to GPTAssistantAgent * added test test_gpt_assistant_tools_overwrite * fetch tools without get_assistant_tools method * fixed code formatting * fixed no .get found
This commit is contained in:
@@ -26,6 +26,7 @@ class GPTAssistantAgent(ConversableAgent):
|
||||
instructions: Optional[str] = None,
|
||||
llm_config: Optional[Union[Dict, bool]] = None,
|
||||
overwrite_instructions: bool = False,
|
||||
overwrite_tools: bool = False,
|
||||
**kwargs,
|
||||
):
|
||||
"""
|
||||
@@ -46,6 +47,7 @@ class GPTAssistantAgent(ConversableAgent):
|
||||
or build your own tools using Function calling. ref https://platform.openai.com/docs/assistants/tools
|
||||
- file_ids: files used by retrieval in run
|
||||
overwrite_instructions (bool): whether to overwrite the instructions of an existing assistant. This parameter is in effect only when assistant_id is specified in llm_config.
|
||||
overwrite_tools (bool): whether to overwrite the tools of an existing assistant. This parameter is in effect only when assistant_id is specified in llm_config.
|
||||
kwargs (dict): Additional configuration options for the agent.
|
||||
- verbose (bool): If set to True, enables more detailed output from the assistant thread.
|
||||
- Other kwargs: Except verbose, others are passed directly to ConversableAgent.
|
||||
@@ -108,6 +110,32 @@ class GPTAssistantAgent(ConversableAgent):
|
||||
"overwrite_instructions is False. Provided instructions will be used without permanently modifying the assistant in the API."
|
||||
)
|
||||
|
||||
# Check if tools are specified in llm_config
|
||||
specified_tools = llm_config.get("tools", None)
|
||||
|
||||
if specified_tools is None:
|
||||
# Check if the current assistant has tools defined
|
||||
if self._openai_assistant.tools:
|
||||
logger.warning(
|
||||
"No tools were provided for given assistant. Using existing tools from assistant API."
|
||||
)
|
||||
else:
|
||||
logger.info(
|
||||
"No tools were provided for the assistant, and the assistant currently has no tools set."
|
||||
)
|
||||
elif overwrite_tools is True:
|
||||
# Tools are specified and overwrite_tools is True; update the assistant's tools
|
||||
logger.warning(
|
||||
"overwrite_tools is True. Provided tools will be used and will modify the assistant in the API"
|
||||
)
|
||||
self._openai_assistant = self._openai_client.beta.assistants.update(
|
||||
assistant_id=openai_assistant_id,
|
||||
tools=llm_config.get("tools", []),
|
||||
)
|
||||
else:
|
||||
# Tools are specified but overwrite_tools is False; do not update the assistant's tools
|
||||
logger.warning("overwrite_tools is False. Using existing tools from assistant API.")
|
||||
|
||||
self._verbose = kwargs.pop("verbose", False)
|
||||
super().__init__(
|
||||
name=name, system_message=instructions, human_input_mode="NEVER", llm_config=llm_config, **kwargs
|
||||
|
||||
@@ -398,6 +398,120 @@ def test_assistant_mismatch_retrieval():
|
||||
assert len(candidates) == 0
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
sys.platform in ["darwin", "win32"] or skip,
|
||||
reason="do not run on MacOS or windows OR dependency is not installed OR requested to skip",
|
||||
)
|
||||
def test_gpt_assistant_tools_overwrite():
|
||||
"""
|
||||
Test that the tools of a GPTAssistantAgent can be overwritten or not depending on the value of the
|
||||
`overwrite_tools` parameter when creating a new assistant with the same ID.
|
||||
|
||||
Steps:
|
||||
1. Create a new GPTAssistantAgent with a set of tools.
|
||||
2. Get the ID of the assistant.
|
||||
3. Create a new GPTAssistantAgent with the same ID but different tools and `overwrite_tools=True`.
|
||||
4. Check that the tools of the assistant have been overwritten with the new ones.
|
||||
"""
|
||||
|
||||
name = "For test_gpt_assistant_tools_overwrite"
|
||||
original_tools = [
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "calculateTax",
|
||||
"description": "Calculate tax for a given amount",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"amount": {"type": "number", "description": "The amount to calculate tax on"},
|
||||
"tax_rate": {"type": "number", "description": "The tax rate to apply"},
|
||||
},
|
||||
"required": ["amount", "tax_rate"],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "convertCurrency",
|
||||
"description": "Convert currency from one type to another",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"amount": {"type": "number", "description": "The amount to convert"},
|
||||
"from_currency": {"type": "string", "description": "Currency type to convert from"},
|
||||
"to_currency": {"type": "string", "description": "Currency type to convert to"},
|
||||
},
|
||||
"required": ["amount", "from_currency", "to_currency"],
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
new_tools = [
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "findRestaurant",
|
||||
"description": "Find a restaurant based on cuisine type and location",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"cuisine": {"type": "string", "description": "Type of cuisine"},
|
||||
"location": {"type": "string", "description": "City or area for the restaurant search"},
|
||||
},
|
||||
"required": ["cuisine", "location"],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "calculateMortgage",
|
||||
"description": "Calculate monthly mortgage payments",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"principal": {"type": "number", "description": "The principal loan amount"},
|
||||
"interest_rate": {"type": "number", "description": "Annual interest rate"},
|
||||
"years": {"type": "integer", "description": "Number of years for the loan"},
|
||||
},
|
||||
"required": ["principal", "interest_rate", "years"],
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
# Create an assistant with original tools
|
||||
assistant = GPTAssistantAgent(
|
||||
name,
|
||||
llm_config={
|
||||
"config_list": config_list,
|
||||
"tools": original_tools,
|
||||
},
|
||||
)
|
||||
|
||||
assistant_id = assistant.assistant_id
|
||||
|
||||
# Create a new assistant with new tools and overwrite_tools set to True
|
||||
assistant = GPTAssistantAgent(
|
||||
name,
|
||||
llm_config={
|
||||
"config_list": config_list,
|
||||
"assistant_id": assistant_id,
|
||||
"tools": new_tools,
|
||||
},
|
||||
overwrite_tools=True,
|
||||
)
|
||||
|
||||
# Add logic to retrieve the tools from the assistant and assert
|
||||
retrieved_tools = assistant.llm_config.get("tools", [])
|
||||
assistant.delete_assistant()
|
||||
|
||||
assert retrieved_tools == new_tools
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_gpt_assistant_chat()
|
||||
test_get_assistant_instructions()
|
||||
@@ -405,3 +519,4 @@ if __name__ == "__main__":
|
||||
test_gpt_assistant_existing_no_instructions()
|
||||
test_get_assistant_files()
|
||||
test_assistant_mismatch_retrieval()
|
||||
test_gpt_assistant_tools_overwrite()
|
||||
|
||||
Reference in New Issue
Block a user