You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

agentchat_oai_assistant_twoagents_basic.ipynb 7.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "## OpenAI Assistants in AutoGen\n",
  8. "\n",
  9. "This notebook shows a very basic example of the [`GPTAssistantAgent`](https://github.com/microsoft/autogen/blob/main/autogen/agentchat/contrib/gpt_assistant_agent.py#L16C43-L16C43), which is an experimental AutoGen agent class that leverages the [OpenAI Assistant API](https://platform.openai.com/docs/assistants/overview) for conversational capabilities, working with\n",
  10. "`UserProxyAgent` in AutoGen."
  11. ]
  12. },
  13. {
  14. "cell_type": "code",
  15. "execution_count": 1,
  16. "metadata": {},
  17. "outputs": [
  18. {
  19. "name": "stderr",
  20. "output_type": "stream",
  21. "text": [
  22. "OpenAI client config of GPTAssistantAgent(assistant) - model: gpt-4-turbo-preview\n",
  23. "GPT Assistant only supports one OpenAI client. Using the first client in the list.\n",
  24. "No matching assistant found, creating a new assistant\n"
  25. ]
  26. },
  27. {
  28. "name": "stdout",
  29. "output_type": "stream",
  30. "text": [
  31. "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
  32. "\n",
  33. "Print hello world\n",
  34. "\n",
  35. "--------------------------------------------------------------------------------\n",
  36. "\u001b[33massistant\u001b[0m (to user_proxy):\n",
  37. "\n",
  38. "```python\n",
  39. "print(\"Hello, world!\")\n",
  40. "```\n",
  41. "\n",
  42. "\n",
  43. "--------------------------------------------------------------------------------\n",
  44. "\u001b[31m\n",
  45. ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
  46. "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
  47. "\n",
  48. "exitcode: 0 (execution succeeded)\n",
  49. "Code output: \n",
  50. "Hello, world!\n",
  51. "\n",
  52. "\n",
  53. "--------------------------------------------------------------------------------\n",
  54. "\u001b[33massistant\u001b[0m (to user_proxy):\n",
  55. "\n",
  56. "TERMINATE\n",
  57. "\n",
  58. "\n",
  59. "--------------------------------------------------------------------------------\n"
  60. ]
  61. },
  62. {
  63. "data": {
  64. "text/plain": [
  65. "ChatResult(chat_id=None, chat_history=[{'content': 'Print hello world', 'role': 'assistant'}, {'content': '```python\\nprint(\"Hello, world!\")\\n```\\n', 'role': 'user'}, {'content': 'exitcode: 0 (execution succeeded)\\nCode output: \\nHello, world!\\n', 'role': 'assistant'}, {'content': 'TERMINATE\\n', 'role': 'user'}], summary='\\n', cost=({'total_cost': 0}, {'total_cost': 0}), human_input=[])"
  66. ]
  67. },
  68. "execution_count": 1,
  69. "metadata": {},
  70. "output_type": "execute_result"
  71. }
  72. ],
  73. "source": [
  74. "import logging\n",
  75. "import os\n",
  76. "\n",
  77. "from autogen import AssistantAgent, UserProxyAgent, config_list_from_json\n",
  78. "from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent\n",
  79. "\n",
  80. "logger = logging.getLogger(__name__)\n",
  81. "logger.setLevel(logging.WARNING)\n",
  82. "\n",
  83. "assistant_id = os.environ.get(\"ASSISTANT_ID\", None)\n",
  84. "\n",
  85. "config_list = config_list_from_json(\"OAI_CONFIG_LIST\")\n",
  86. "llm_config = {\"config_list\": config_list}\n",
  87. "\n",
  88. "assistant_config = {\"assistant_id\": assistant_id}\n",
  89. "\n",
  90. "gpt_assistant = GPTAssistantAgent(\n",
  91. " name=\"assistant\",\n",
  92. " instructions=AssistantAgent.DEFAULT_SYSTEM_MESSAGE,\n",
  93. " llm_config=llm_config,\n",
  94. " assistant_config=assistant_config,\n",
  95. ")\n",
  96. "\n",
  97. "user_proxy = UserProxyAgent(\n",
  98. " name=\"user_proxy\",\n",
  99. " code_execution_config={\n",
  100. " \"work_dir\": \"coding\",\n",
  101. " \"use_docker\": False,\n",
  102. " }, # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.\n",
  103. " is_termination_msg=lambda msg: \"TERMINATE\" in msg[\"content\"],\n",
  104. " human_input_mode=\"NEVER\",\n",
  105. " max_consecutive_auto_reply=1,\n",
  106. ")\n",
  107. "user_proxy.initiate_chat(gpt_assistant, message=\"Print hello world\")"
  108. ]
  109. },
  110. {
  111. "cell_type": "code",
  112. "execution_count": 2,
  113. "metadata": {},
  114. "outputs": [
  115. {
  116. "name": "stdout",
  117. "output_type": "stream",
  118. "text": [
  119. "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
  120. "\n",
  121. "Write py code to eval 2 + 2\n",
  122. "\n",
  123. "--------------------------------------------------------------------------------\n",
  124. "\u001b[33massistant\u001b[0m (to user_proxy):\n",
  125. "\n",
  126. "```python\n",
  127. "# Calculate 2+2 and print the result\n",
  128. "result = 2 + 2\n",
  129. "print(result)\n",
  130. "```\n",
  131. "\n",
  132. "\n",
  133. "--------------------------------------------------------------------------------\n",
  134. "\u001b[31m\n",
  135. ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n",
  136. "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
  137. "\n",
  138. "exitcode: 0 (execution succeeded)\n",
  139. "Code output: \n",
  140. "4\n",
  141. "\n",
  142. "\n",
  143. "--------------------------------------------------------------------------------\n",
  144. "\u001b[33massistant\u001b[0m (to user_proxy):\n",
  145. "\n",
  146. "The Python code successfully calculated \\(2 + 2\\) and printed the result, which is \\(4\\).\n",
  147. "\n",
  148. "TERMINATE\n",
  149. "\n",
  150. "\n",
  151. "--------------------------------------------------------------------------------\n"
  152. ]
  153. },
  154. {
  155. "data": {
  156. "text/plain": [
  157. "ChatResult(chat_id=None, chat_history=[{'content': 'Write py code to eval 2 + 2', 'role': 'assistant'}, {'content': '```python\\n# Calculate 2+2 and print the result\\nresult = 2 + 2\\nprint(result)\\n```\\n', 'role': 'user'}, {'content': 'exitcode: 0 (execution succeeded)\\nCode output: \\n4\\n', 'role': 'assistant'}, {'content': 'The Python code successfully calculated \\\\(2 + 2\\\\) and printed the result, which is \\\\(4\\\\).\\n\\nTERMINATE\\n', 'role': 'user'}], summary='The Python code successfully calculated \\\\(2 + 2\\\\) and printed the result, which is \\\\(4\\\\).\\n\\n\\n', cost=({'total_cost': 0}, {'total_cost': 0}), human_input=[])"
  158. ]
  159. },
  160. "execution_count": 2,
  161. "metadata": {},
  162. "output_type": "execute_result"
  163. }
  164. ],
  165. "source": [
  166. "user_proxy.initiate_chat(gpt_assistant, message=\"Write py code to eval 2 + 2\", clear_history=True)"
  167. ]
  168. },
  169. {
  170. "cell_type": "code",
  171. "execution_count": 3,
  172. "metadata": {},
  173. "outputs": [
  174. {
  175. "name": "stderr",
  176. "output_type": "stream",
  177. "text": [
  178. "Permanently deleting assistant...\n"
  179. ]
  180. }
  181. ],
  182. "source": [
  183. "gpt_assistant.delete_assistant()"
  184. ]
  185. }
  186. ],
  187. "metadata": {
  188. "kernelspec": {
  189. "display_name": "Python 3",
  190. "language": "python",
  191. "name": "python3"
  192. },
  193. "language_info": {
  194. "codemirror_mode": {
  195. "name": "ipython",
  196. "version": 3
  197. },
  198. "file_extension": ".py",
  199. "mimetype": "text/x-python",
  200. "name": "python",
  201. "nbconvert_exporter": "python",
  202. "pygments_lexer": "ipython3",
  203. "version": "3.10.13"
  204. }
  205. },
  206. "nbformat": 4,
  207. "nbformat_minor": 2
  208. }