Created
December 7, 2024 17:23
-
-
Save pwais/583164e1e4ea2c489bee5d93b883a566 to your computer and use it in GitHub Desktop.
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
| if __name__ == "__main__": | |
| import autogen | |
| log.info(f"Using autogen {autogen.__version__}") | |
| # https://microsoft.github.io/autogen/0.2/docs/tutorial/tool-use | |
| from typing import Annotated, Literal | |
| Operator = Literal["+", "-", "*", "/"] | |
| def calculator(a: int, b: int, operator: Annotated[Operator, "operator"]) -> int: | |
| if operator == "+": | |
| return a + b | |
| elif operator == "-": | |
| return a - b | |
| elif operator == "*": | |
| return a * b | |
| elif operator == "/": | |
| return int(a / b) | |
| else: | |
| raise ValueError("Invalid operator") | |
| llm_config = {"config_list": [{ | |
| # "model": "gpt-4o-mini", | |
| "model": "llama3.1-70b-instruct-berkeley", | |
| "base_url": "https://api.lambdalabs.com/v1/", # ensure trailing slash!!! | |
| # "api_rate_limit": 60.0, # Set to allow up to 60 API requests per second. | |
| "api_key": os.environ.get("OPENAI_API_KEY"), | |
| "temperature": 0.9, | |
| }]} | |
| from autogen import ConversableAgent | |
| # Let's first define the assistant agent that suggests tool calls. | |
| assistant = ConversableAgent( | |
| name="Assistant", | |
| system_message="You are a helpful AI assistant. " | |
| "You can help with simple calculations. " | |
| "Return 'TERMINATE' when the task is done.", | |
| llm_config=llm_config, | |
| ) | |
| # The user proxy agent is used for interacting with the assistant agent | |
| # and executes tool calls. | |
| user_proxy = ConversableAgent( | |
| name="User", | |
| llm_config=False, | |
| is_termination_msg=lambda msg: msg.get("content") is not None and "TERMINATE" in msg["content"], | |
| human_input_mode="NEVER", | |
| ) | |
| # Register the tool signature with the assistant agent. | |
| assistant.register_for_llm(name="calculator", description="A simple calculator")(calculator) | |
| # Register the tool function with the user proxy agent. | |
| user_proxy.register_for_execution(name="calculator")(calculator) | |
| chat_result = user_proxy.initiate_chat(assistant, message="What is (44232 + 13312 / (232 - 32)) * 5?") | |
| print(chat_result) | |
| assert False |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I get like: