|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# AutoGen Application Function Calling\n", |
| 8 | + "\n", |
| 9 | + "This notebook shows an example of building AutoGen application for currency exchange, using the LLM model from OCI Generative AI service.\n", |
| 10 | + "\n", |
| 11 | + "This notebook is for AutoGen v0.2\n", |
| 12 | + "```\n", |
| 13 | + "pip install autogen-agentchat~=0.2\n", |
| 14 | + "```\n", |
| 15 | + "\n", |
| 16 | + "This notebook is based on the example:\n", |
| 17 | + "https://microsoft.github.io/autogen/0.2/docs/notebooks/agentchat_function_call_currency_calculator\n", |
| 18 | + "\n", |
| 19 | + "This notebook requires **Python 3.9** or newer. Please also install the latest version of Oracle ADS:\n", |
| 20 | + "```\n", |
| 21 | + "pip install oracle-ads\n", |
| 22 | + "```\n", |
| 23 | + "\n", |
| 24 | + "The following dependencies are also required:\n", |
| 25 | + "```\n", |
| 26 | + "pip install \"langchain-community>0.3\" langchain-openai report_creator\n", |
| 27 | + "```" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "markdown", |
| 32 | + "metadata": {}, |
| 33 | + "source": [ |
| 34 | + "## Custom LLM Client for AutoGen\n", |
| 35 | + "\n", |
| 36 | + "ADS provides the custom LLM client for AutoGen to enable the use of LangChain chat models, including the [ChatOCIGenAI](https://python.langchain.com/docs/integrations/chat/oci_generative_ai/) and the [ChatOCIModelDeployment](https://python.langchain.com/docs/integrations/chat/oci_data_science/).\n", |
| 37 | + "\n", |
| 38 | + "When using custom client with AutoGen, you need to register the custom client with each agent. To simplify the process, ADS provides a `register_custom_client` function to register the custom client globally. With this function, you only need to register the client once. This will allow you to easily switch between OpenAI model and custom model in the LLM config without any code change or if/else logic." |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "code", |
| 43 | + "execution_count": null, |
| 44 | + "metadata": {}, |
| 45 | + "outputs": [], |
| 46 | + "source": [ |
| 47 | + "# Register the custom LLM globally so that we don't need to do it for each agent\n", |
| 48 | + "from ads.llm.autogen.v02 import LangChainModelClient, register_custom_client\n", |
| 49 | + " \n", |
| 50 | + "register_custom_client(LangChainModelClient)" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "markdown", |
| 55 | + "metadata": {}, |
| 56 | + "source": [ |
| 57 | + "## LLM Config\n", |
| 58 | + "\n", |
| 59 | + "Following is an example of the LLM config for the OCI Generative AI service.\n", |
| 60 | + "The `langchain_cls` is the full path for importing the LangChain chat model, while the `client_params` contains the arguments for initializing the LangChain chat model.\n", |
| 61 | + "```\n", |
| 62 | + "gen_ai_config = [\n", |
| 63 | + " {\n", |
| 64 | + " \"model_client_cls\": \"LangChainModelClient\",\n", |
| 65 | + " \"langchain_cls\": \"langchain_community.chat_models.oci_generative_ai.ChatOCIGenAI\",\n", |
| 66 | + " \"model\": \"cohere.command-r-plus\",\n", |
| 67 | + " # client_params will be used to initialize the LangChain ChatOCIGenAI class.\n", |
| 68 | + " \"client_params\": {\n", |
| 69 | + " \"model_id\": \"cohere.command-r-plus\",\n", |
| 70 | + " \"compartment_id\": COMPARTMENT_OCID,\n", |
| 71 | + " \"model_kwargs\": {\"temperature\": 0, \"max_tokens\": 2048},\n", |
| 72 | + " \"auth_type\": \"SECURITY_TOKEN\",\n", |
| 73 | + " \"auth_profile\": \"<PROFILE_NAME>\",\n", |
| 74 | + " },\n", |
| 75 | + " }\n", |
| 76 | + "]\n", |
| 77 | + "```" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "code", |
| 82 | + "execution_count": null, |
| 83 | + "metadata": {}, |
| 84 | + "outputs": [], |
| 85 | + "source": [ |
| 86 | + "\n", |
| 87 | + "gen_ai_config = [\n", |
| 88 | + " {\n", |
| 89 | + " \"model_client_cls\": \"LangChainModelClient\",\n", |
| 90 | + " \"langchain_cls\": \"langchain_community.chat_models.oci_generative_ai.ChatOCIGenAI\",\n", |
| 91 | + " \"model\": \"cohere.command-r-plus\",\n", |
| 92 | + " # client_params will be used to initialize the LangChain ChatOCIGenAI class.\n", |
| 93 | + " \"client_params\": {\n", |
| 94 | + " \"model_id\": \"cohere.command-r-plus-08-2024\",\n", |
| 95 | + " \"compartment_id\": \"<COMPARTMENT_OCID>\",\n", |
| 96 | + " \"service_endpoint\": \"https://inference.generativeai.us-chicago-1.oci.oraclecloud.com\",\n", |
| 97 | + " \"model_kwargs\": {\"temperature\": 0, \"max_tokens\": 4000},\n", |
| 98 | + " # Set auth_type to INSTANCE_PRINCIPAL or RESOURCE_PRINCIPAL\n", |
| 99 | + " \"auth_type\": \"SECURITY_TOKEN\",\n", |
| 100 | + " # auth_profile is needed only if you are using API_KEY or SECURITY_TOKEN\n", |
| 101 | + " \"auth_profile\": \"<PROFILE_NAME>\",\n", |
| 102 | + " },\n", |
| 103 | + " }\n", |
| 104 | + "]\n", |
| 105 | + "\n", |
| 106 | + "# AutoGen LLM config\n", |
| 107 | + "llm_config = {\n", |
| 108 | + " # To try other model, simply use a different config\n", |
| 109 | + " \"config_list\": gen_ai_config,\n", |
| 110 | + " # Disable cache\n", |
| 111 | + " # https://microsoft.github.io/autogen/0.2/docs/topics/llm-caching/\n", |
| 112 | + " \"cache_seed\": None,\n", |
| 113 | + "}" |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "markdown", |
| 118 | + "metadata": {}, |
| 119 | + "source": [ |
| 120 | + "## Create the Agents with LLM Config\n", |
| 121 | + "\n", |
| 122 | + "The following code creates two agents. Note that there is no need to register the custom LLM." |
| 123 | + ] |
| 124 | + }, |
| 125 | + { |
| 126 | + "cell_type": "code", |
| 127 | + "execution_count": 5, |
| 128 | + "metadata": {}, |
| 129 | + "outputs": [], |
| 130 | + "source": [ |
| 131 | + "import autogen\n", |
| 132 | + "\n", |
| 133 | + "chatbot = autogen.AssistantAgent(\n", |
| 134 | + " name=\"chatbot\",\n", |
| 135 | + " system_message=(\n", |
| 136 | + " \"For currency exchange rates, only use the functions you have been provided with. \"\n", |
| 137 | + " \"Calculate the currency exchange based on the rates returned by the function. \"\n", |
| 138 | + " \"Reply TERMINATE when the task is done.\"\n", |
| 139 | + " ),\n", |
| 140 | + " llm_config=llm_config,\n", |
| 141 | + ")\n", |
| 142 | + "\n", |
| 143 | + "# create a UserProxyAgent instance named \"user_proxy\"\n", |
| 144 | + "user_proxy = autogen.UserProxyAgent(\n", |
| 145 | + " name=\"user_proxy\",\n", |
| 146 | + " is_termination_msg=lambda x: x.get(\"content\", \"\") and x.get(\"content\", \"\").rstrip().endswith(\"TERMINATE\"),\n", |
| 147 | + " code_execution_config=False,\n", |
| 148 | + " human_input_mode=\"NEVER\",\n", |
| 149 | + " max_consecutive_auto_reply=10,\n", |
| 150 | + ")" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "markdown", |
| 155 | + "metadata": {}, |
| 156 | + "source": [ |
| 157 | + "## Define the Tools (Functions)\n", |
| 158 | + "\n", |
| 159 | + "The following code defines two tool. One for getting the currency exchange rate and the other for calculating the currency based on the exchange rate." |
| 160 | + ] |
| 161 | + }, |
| 162 | + { |
| 163 | + "cell_type": "code", |
| 164 | + "execution_count": 6, |
| 165 | + "metadata": {}, |
| 166 | + "outputs": [], |
| 167 | + "source": [ |
| 168 | + "import time\n", |
| 169 | + "from typing import Annotated\n", |
| 170 | + "import requests\n", |
| 171 | + "\n", |
| 172 | + "\n", |
| 173 | + "@user_proxy.register_for_execution()\n", |
| 174 | + "@chatbot.register_for_llm(description=\"Obtain exchange rate from one currency to other currencies.\")\n", |
| 175 | + "def get_exchange_rate(\n", |
| 176 | + " currency: Annotated[str, \"currency in ISO 4217 3-letter currency code\"]\n", |
| 177 | + " ) -> Annotated[dict, \"A dictionary containing the exchange rate to other currencies\"]:\n", |
| 178 | + " \"\"\"Obtain the current exchange rates of a currency in ISO 4217 3-letter currency code\"\"\"\n", |
| 179 | + " response = requests.get(f\"https://open.er-api.com/v6/latest/{currency}\")\n", |
| 180 | + " # Adding two seconds delay here to simulate a tool call that is running a bit longer.\n", |
| 181 | + " time.sleep(2)\n", |
| 182 | + " return response.json()\n", |
| 183 | + "\n", |
| 184 | + "@user_proxy.register_for_execution()\n", |
| 185 | + "@chatbot.register_for_llm(description=\"Currency exchange calculator.\")\n", |
| 186 | + "def currency_calculator(\n", |
| 187 | + " base_amount: Annotated[float, \"Amount of currency to be exchanged.\"],\n", |
| 188 | + " exchange_rate: Annotated[float, \"Exchange rate.\"],\n", |
| 189 | + " ) -> Annotated[float, \"Amount of currency exchanged into.\"]:\n", |
| 190 | + " \"\"\"Calculates the currency exchange\"\"\"\n", |
| 191 | + " return base_amount * exchange_rate" |
| 192 | + ] |
| 193 | + }, |
| 194 | + { |
| 195 | + "cell_type": "markdown", |
| 196 | + "metadata": {}, |
| 197 | + "source": [ |
| 198 | + "## Start the AutoGen Application\n", |
| 199 | + "\n", |
| 200 | + "Now everything is defined, we can start running the AutoGen application." |
| 201 | + ] |
| 202 | + }, |
| 203 | + { |
| 204 | + "cell_type": "code", |
| 205 | + "execution_count": 7, |
| 206 | + "metadata": {}, |
| 207 | + "outputs": [ |
| 208 | + { |
| 209 | + "name": "stdout", |
| 210 | + "output_type": "stream", |
| 211 | + "text": [ |
| 212 | + "\u001b[33muser_proxy\u001b[0m (to chatbot):\n", |
| 213 | + "\n", |
| 214 | + "How much is 123.45 USD in EUR?\n", |
| 215 | + "\n", |
| 216 | + "--------------------------------------------------------------------------------\n", |
| 217 | + "\u001b[33mchatbot\u001b[0m (to user_proxy):\n", |
| 218 | + "\n", |
| 219 | + "I will first find the exchange rate from USD to EUR. Then, I will use the currency calculator to find out how much 123.45 USD is in EUR.\n", |
| 220 | + "\u001b[32m***** Suggested tool call (b5657fd24d9d42f9899e3b441575f4e6): get_exchange_rate *****\u001b[0m\n", |
| 221 | + "Arguments: \n", |
| 222 | + "{\"currency\": \"USD\"}\n", |
| 223 | + "\u001b[32m*************************************************************************************\u001b[0m\n", |
| 224 | + "\n", |
| 225 | + "--------------------------------------------------------------------------------\n", |
| 226 | + "\u001b[35m\n", |
| 227 | + ">>>>>>>> EXECUTING FUNCTION get_exchange_rate...\u001b[0m\n", |
| 228 | + "\u001b[33muser_proxy\u001b[0m (to chatbot):\n", |
| 229 | + "\n", |
| 230 | + "\u001b[33muser_proxy\u001b[0m (to chatbot):\n", |
| 231 | + "\n", |
| 232 | + "\u001b[32m***** Response from calling tool (b5657fd24d9d42f9899e3b441575f4e6) *****\u001b[0m\n", |
| 233 | + "{\"result\": \"success\", \"provider\": \"https://www.exchangerate-api.com\", \"documentation\": \"https://www.exchangerate-api.com/docs/free\", \"terms_of_use\": \"https://www.exchangerate-api.com/terms\", \"time_last_update_unix\": 1744502552, \"time_last_update_utc\": \"Sun, 13 Apr 2025 00:02:32 +0000\", \"time_next_update_unix\": 1744590142, \"time_next_update_utc\": \"Mon, 14 Apr 2025 00:22:22 +0000\", \"time_eol_unix\": 0, \"base_code\": \"USD\", \"rates\": {\"USD\": 1, \"AED\": 3.6725, \"AFN\": 72.315442, \"ALL\": 89.82041, \"AMD\": 391.760802, \"ANG\": 1.79, \"AOA\": 919.902468, \"ARS\": 1078.38, \"AUD\": 1.598629, \"AWG\": 1.79, \"AZN\": 1.700444, \"BAM\": 1.724732, \"BBD\": 2, \"BDT\": 121.498618, \"BGN\": 1.724703, \"BHD\": 0.376, \"BIF\": 2981.819399, \"BMD\": 1, \"BND\": 1.31919, \"BOB\": 6.92917, \"BRL\": 5.884413, \"BSD\": 1, \"BTN\": 86.153674, \"BWP\": 14.01705, \"BYN\": 3.147656, \"BZD\": 2, \"CAD\": 1.38897, \"CDF\": 2892.630814, \"CHF\": 0.816402, \"CLP\": 986.757269, \"CNY\": 7.297059, \"COP\": 4359.055227, \"CRC\": 513.633717, \"CUP\": 24, \"CVE\": 97.236245, \"CZK\": 22.156761, \"DJF\": 177.721, \"DKK\": 6.579512, \"DOP\": 62.116155, \"DZD\": 133.461341, \"EGP\": 51.332767, \"ERN\": 15, \"ETB\": 130.618473, \"EUR\": 0.881858, \"FJD\": 2.291517, \"FKP\": 0.765378, \"FOK\": 6.579512, \"GBP\": 0.765384, \"GEL\": 2.755857, \"GGP\": 0.765378, \"GHS\": 15.595987, \"GIP\": 0.765378, \"GMD\": 72.681311, \"GNF\": 8689.910659, \"GTQ\": 7.714261, \"GYD\": 209.329928, \"HKD\": 7.756249, \"HNL\": 25.887868, \"HRK\": 6.644234, \"HTG\": 130.651593, \"HUF\": 361.266209, \"IDR\": 16801.184108, \"ILS\": 3.719332, \"IMP\": 0.765378, \"INR\": 86.153703, \"IQD\": 1309.978289, \"IRR\": 41981.906267, \"ISK\": 127.859821, \"JEP\": 0.765378, \"JMD\": 158.001693, \"JOD\": 0.709, \"JPY\": 143.339541, \"KES\": 129.466589, \"KGS\": 87.033165, \"KHR\": 3999.918965, \"KID\": 1.599492, \"KMF\": 433.837545, \"KRW\": 1428.952719, \"KWD\": 0.306711, \"KYD\": 0.833333, \"KZT\": 517.040547, \"LAK\": 21834.465928, \"LBP\": 89500, \"LKR\": 297.826046, \"LRD\": 200.130856, \"LSL\": 19.200078, \"LYD\": 5.552801, \"MAD\": 9.341925, \"MDL\": 17.675106, \"MGA\": 4626.235008, \"MKD\": 54.39328, \"MMK\": 2103.159926, \"MNT\": 3543.812227, \"MOP\": 7.988822, \"MRU\": 39.823934, \"MUR\": 44.390667, \"MVR\": 15.45553, \"MWK\": 1745.753339, \"MXN\": 20.353201, \"MYR\": 4.431012, \"MZN\": 63.925346, \"NAD\": 19.200078, \"NGN\": 1582.990538, \"NIO\": 36.804263, \"NOK\": 10.682889, \"NPR\": 137.845879, \"NZD\": 1.722785, \"OMR\": 0.384497, \"PAB\": 1, \"PEN\": 3.732809, \"PGK\": 4.092224, \"PHP\": 56.987615, \"PKR\": 280.351661, \"PLN\": 3.782799, \"PYG\": 8048.273633, \"QAR\": 3.64, \"RON\": 4.388126, \"RSD\": 103.786118, \"RUB\": 83.882837, \"RWF\": 1442.968815, \"SAR\": 3.75, \"SBD\": 8.208562, \"SCR\": 14.389741, \"SDG\": 509.063152, \"SEK\": 9.793539, \"SGD\": 1.319206, \"SHP\": 0.765378, \"SLE\": 22.790633, \"SLL\": 22790.632758, \"SOS\": 571.881808, \"SRD\": 36.612353, \"SSP\": 4532.387841, \"STN\": 21.605115, \"SYP\": 12910.641464, \"SZL\": 19.200078, \"THB\": 33.510629, \"TJS\": 10.913142, \"TMT\": 3.500243, \"TND\": 3.014602, \"TOP\": 2.404616, \"TRY\": 38.116588, \"TTD\": 6.763877, \"TVD\": 1.599492, \"TWD\": 32.269536, \"TZS\": 2654.720956, \"UAH\": 41.402619, \"UGX\": 3686.782764, \"UYU\": 42.93508, \"UZS\": 12971.767558, \"VES\": 78.3635, \"VND\": 25733.477084, \"VUV\": 125.036586, \"WST\": 2.845617, \"XAF\": 578.45006, \"XCD\": 2.7, \"XCG\": 1.79, \"XDR\": 0.731724, \"XOF\": 578.45006, \"XPF\": 105.231902, \"YER\": 245.323593, \"ZAR\": 19.185982, \"ZMW\": 28.198848, \"ZWL\": 26.8103}}\n", |
| 234 | + "\u001b[32m*************************************************************************\u001b[0m\n", |
| 235 | + "\n", |
| 236 | + "--------------------------------------------------------------------------------\n", |
| 237 | + "\u001b[33mchatbot\u001b[0m (to user_proxy):\n", |
| 238 | + "\n", |
| 239 | + "The exchange rate from USD to EUR is 0.881858. I will now use the currency calculator to find out how much 123.45 USD is in EUR.\n", |
| 240 | + "\u001b[32m***** Suggested tool call (dc13ab478c7a420996d4a8cf8dbdea7d): currency_calculator *****\u001b[0m\n", |
| 241 | + "Arguments: \n", |
| 242 | + "{\"base_amount\": 123.45, \"exchange_rate\": 0.881858}\n", |
| 243 | + "\u001b[32m***************************************************************************************\u001b[0m\n", |
| 244 | + "\n", |
| 245 | + "--------------------------------------------------------------------------------\n", |
| 246 | + "\u001b[35m\n", |
| 247 | + ">>>>>>>> EXECUTING FUNCTION currency_calculator...\u001b[0m\n", |
| 248 | + "\u001b[33muser_proxy\u001b[0m (to chatbot):\n", |
| 249 | + "\n", |
| 250 | + "\u001b[33muser_proxy\u001b[0m (to chatbot):\n", |
| 251 | + "\n", |
| 252 | + "\u001b[32m***** Response from calling tool (dc13ab478c7a420996d4a8cf8dbdea7d) *****\u001b[0m\n", |
| 253 | + "108.8653701\n", |
| 254 | + "\u001b[32m*************************************************************************\u001b[0m\n", |
| 255 | + "\n", |
| 256 | + "--------------------------------------------------------------------------------\n", |
| 257 | + "\u001b[33mchatbot\u001b[0m (to user_proxy):\n", |
| 258 | + "\n", |
| 259 | + "123.45 USD is **108.87 EUR**.\n", |
| 260 | + "\n", |
| 261 | + "--------------------------------------------------------------------------------\n", |
| 262 | + "\u001b[33muser_proxy\u001b[0m (to chatbot):\n", |
| 263 | + "\n", |
| 264 | + "\n", |
| 265 | + "\n", |
| 266 | + "--------------------------------------------------------------------------------\n", |
| 267 | + "\u001b[33mchatbot\u001b[0m (to user_proxy):\n", |
| 268 | + "\n", |
| 269 | + "TERMINATE\n", |
| 270 | + "\n", |
| 271 | + "--------------------------------------------------------------------------------\n" |
| 272 | + ] |
| 273 | + } |
| 274 | + ], |
| 275 | + "source": [ |
| 276 | + "res = user_proxy.initiate_chat(\n", |
| 277 | + " chatbot, message=\"How much is 123.45 USD in EUR?\", summary_method=\"reflection_with_llm\"\n", |
| 278 | + ")" |
| 279 | + ] |
| 280 | + } |
| 281 | + ], |
| 282 | + "metadata": { |
| 283 | + "kernelspec": { |
| 284 | + "display_name": "autogen", |
| 285 | + "language": "python", |
| 286 | + "name": "python3" |
| 287 | + }, |
| 288 | + "language_info": { |
| 289 | + "codemirror_mode": { |
| 290 | + "name": "ipython", |
| 291 | + "version": 3 |
| 292 | + }, |
| 293 | + "file_extension": ".py", |
| 294 | + "mimetype": "text/x-python", |
| 295 | + "name": "python", |
| 296 | + "nbconvert_exporter": "python", |
| 297 | + "pygments_lexer": "ipython3", |
| 298 | + "version": "3.11.10" |
| 299 | + } |
| 300 | + }, |
| 301 | + "nbformat": 4, |
| 302 | + "nbformat_minor": 2 |
| 303 | +} |
0 commit comments