Created
October 15, 2023 01:35
-
-
Save bonadio/3a806c9e6a2829a59ba45932b8b079e7 to your computer and use it in GitHub Desktop.
Autogen Agent interacting with user_proxy answering function_call
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# %% | |
#https://github.com/microsoft/autogen | |
import os | |
import openai | |
import autogen | |
import types | |
from dotenv import load_dotenv, find_dotenv | |
_ = load_dotenv(find_dotenv()) # read local .env file | |
openai.api_key = os.environ['OPENAI_API_KEY'] | |
# openai.log='debug' | |
# %% | |
config_list = [ | |
{ | |
"model": "gpt-3.5-turbo" | |
} | |
] | |
llm_config_order_search = { | |
"use_cache": False, | |
"model":"gpt-3.5-turbo", | |
"temperature": 0, | |
"config_list": config_list, | |
"functions": [ | |
{ | |
"name": "order_search", | |
"description": "search for order status", | |
"parameters": { | |
"type": "object", | |
"properties": { | |
"order_number": { | |
"type": "integer", | |
"description": "Order number", | |
}, | |
"customer_number": { | |
"type": "string", | |
"description": "Customer number", | |
} | |
}, | |
"required": ["order_number","customer_number"], | |
}, | |
}, | |
], | |
} | |
# %% | |
def order_search(order_number, customer_number): | |
return "Order status: delivered" | |
# %% | |
order_assistant = autogen.AssistantAgent( | |
name="order_assistant", | |
llm_config=llm_config_order_search, | |
human_input_mode="NEVER", | |
code_execution_config=False, | |
system_message="""Order assistant, you help the user find the status of his order. | |
Only use the tools provided to do the search. Only execute the search after you have all the information needed. | |
Ask the user for the information you need to perform the search, always add the word "BRKT"" at the end of your question. | |
When you responde with the status add the word TERMINATE""", | |
function_map={ | |
"order_search": order_search | |
} | |
) | |
def check_for_termination(message): | |
# print("check_for_termination", message) | |
content = message.get("content") | |
if content != None and ("BRKT" in content or "TERMINATE" in content): | |
print("should terminate") | |
return True | |
return False | |
user_proxy = autogen.UserProxyAgent( | |
name="user_proxy", | |
human_input_mode="TERMINATE", | |
max_consecutive_auto_reply=5, | |
is_termination_msg=check_for_termination, | |
code_execution_config=False, #{"work_dir": "web", "use_docker":True}, | |
function_map={ | |
"order_search": order_search | |
} | |
) | |
# def get_human_input(self, prompt: str) -> str: | |
# # print("get_human_input",self.last_message()) | |
# last_message = self.last_message() | |
# content = last_message.get("content") | |
# if content != None and "TERMINATE" in content: | |
# return "exit" | |
# reply = input(prompt) | |
# return reply | |
# user_proxy.get_human_input = types.MethodType(get_human_input, user_proxy) | |
# %% | |
user_proxy.initiate_chat( | |
order_assistant, | |
clear_history=True, | |
message=""" | |
Was my order delivered? | |
""", | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment