ag2 0.10.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (423) hide show
  1. ag2-0.10.2.dist-info/METADATA +819 -0
  2. ag2-0.10.2.dist-info/RECORD +423 -0
  3. ag2-0.10.2.dist-info/WHEEL +4 -0
  4. ag2-0.10.2.dist-info/licenses/LICENSE +201 -0
  5. ag2-0.10.2.dist-info/licenses/NOTICE.md +19 -0
  6. autogen/__init__.py +88 -0
  7. autogen/_website/__init__.py +3 -0
  8. autogen/_website/generate_api_references.py +426 -0
  9. autogen/_website/generate_mkdocs.py +1216 -0
  10. autogen/_website/notebook_processor.py +475 -0
  11. autogen/_website/process_notebooks.py +656 -0
  12. autogen/_website/utils.py +413 -0
  13. autogen/a2a/__init__.py +36 -0
  14. autogen/a2a/agent_executor.py +86 -0
  15. autogen/a2a/client.py +357 -0
  16. autogen/a2a/errors.py +18 -0
  17. autogen/a2a/httpx_client_factory.py +79 -0
  18. autogen/a2a/server.py +221 -0
  19. autogen/a2a/utils.py +207 -0
  20. autogen/agentchat/__init__.py +47 -0
  21. autogen/agentchat/agent.py +180 -0
  22. autogen/agentchat/assistant_agent.py +86 -0
  23. autogen/agentchat/chat.py +325 -0
  24. autogen/agentchat/contrib/__init__.py +5 -0
  25. autogen/agentchat/contrib/agent_eval/README.md +7 -0
  26. autogen/agentchat/contrib/agent_eval/agent_eval.py +108 -0
  27. autogen/agentchat/contrib/agent_eval/criterion.py +43 -0
  28. autogen/agentchat/contrib/agent_eval/critic_agent.py +44 -0
  29. autogen/agentchat/contrib/agent_eval/quantifier_agent.py +39 -0
  30. autogen/agentchat/contrib/agent_eval/subcritic_agent.py +45 -0
  31. autogen/agentchat/contrib/agent_eval/task.py +42 -0
  32. autogen/agentchat/contrib/agent_optimizer.py +432 -0
  33. autogen/agentchat/contrib/capabilities/__init__.py +5 -0
  34. autogen/agentchat/contrib/capabilities/agent_capability.py +20 -0
  35. autogen/agentchat/contrib/capabilities/generate_images.py +301 -0
  36. autogen/agentchat/contrib/capabilities/teachability.py +393 -0
  37. autogen/agentchat/contrib/capabilities/text_compressors.py +66 -0
  38. autogen/agentchat/contrib/capabilities/tools_capability.py +22 -0
  39. autogen/agentchat/contrib/capabilities/transform_messages.py +93 -0
  40. autogen/agentchat/contrib/capabilities/transforms.py +578 -0
  41. autogen/agentchat/contrib/capabilities/transforms_util.py +122 -0
  42. autogen/agentchat/contrib/capabilities/vision_capability.py +215 -0
  43. autogen/agentchat/contrib/captainagent/__init__.py +9 -0
  44. autogen/agentchat/contrib/captainagent/agent_builder.py +790 -0
  45. autogen/agentchat/contrib/captainagent/captainagent.py +514 -0
  46. autogen/agentchat/contrib/captainagent/tool_retriever.py +334 -0
  47. autogen/agentchat/contrib/captainagent/tools/README.md +44 -0
  48. autogen/agentchat/contrib/captainagent/tools/__init__.py +5 -0
  49. autogen/agentchat/contrib/captainagent/tools/data_analysis/calculate_correlation.py +40 -0
  50. autogen/agentchat/contrib/captainagent/tools/data_analysis/calculate_skewness_and_kurtosis.py +28 -0
  51. autogen/agentchat/contrib/captainagent/tools/data_analysis/detect_outlier_iqr.py +28 -0
  52. autogen/agentchat/contrib/captainagent/tools/data_analysis/detect_outlier_zscore.py +28 -0
  53. autogen/agentchat/contrib/captainagent/tools/data_analysis/explore_csv.py +21 -0
  54. autogen/agentchat/contrib/captainagent/tools/data_analysis/shapiro_wilk_test.py +30 -0
  55. autogen/agentchat/contrib/captainagent/tools/information_retrieval/arxiv_download.py +27 -0
  56. autogen/agentchat/contrib/captainagent/tools/information_retrieval/arxiv_search.py +53 -0
  57. autogen/agentchat/contrib/captainagent/tools/information_retrieval/extract_pdf_image.py +53 -0
  58. autogen/agentchat/contrib/captainagent/tools/information_retrieval/extract_pdf_text.py +38 -0
  59. autogen/agentchat/contrib/captainagent/tools/information_retrieval/get_wikipedia_text.py +21 -0
  60. autogen/agentchat/contrib/captainagent/tools/information_retrieval/get_youtube_caption.py +34 -0
  61. autogen/agentchat/contrib/captainagent/tools/information_retrieval/image_qa.py +60 -0
  62. autogen/agentchat/contrib/captainagent/tools/information_retrieval/optical_character_recognition.py +61 -0
  63. autogen/agentchat/contrib/captainagent/tools/information_retrieval/perform_web_search.py +47 -0
  64. autogen/agentchat/contrib/captainagent/tools/information_retrieval/scrape_wikipedia_tables.py +33 -0
  65. autogen/agentchat/contrib/captainagent/tools/information_retrieval/transcribe_audio_file.py +21 -0
  66. autogen/agentchat/contrib/captainagent/tools/information_retrieval/youtube_download.py +35 -0
  67. autogen/agentchat/contrib/captainagent/tools/math/calculate_circle_area_from_diameter.py +21 -0
  68. autogen/agentchat/contrib/captainagent/tools/math/calculate_day_of_the_week.py +18 -0
  69. autogen/agentchat/contrib/captainagent/tools/math/calculate_fraction_sum.py +28 -0
  70. autogen/agentchat/contrib/captainagent/tools/math/calculate_matrix_power.py +31 -0
  71. autogen/agentchat/contrib/captainagent/tools/math/calculate_reflected_point.py +16 -0
  72. autogen/agentchat/contrib/captainagent/tools/math/complex_numbers_product.py +25 -0
  73. autogen/agentchat/contrib/captainagent/tools/math/compute_currency_conversion.py +23 -0
  74. autogen/agentchat/contrib/captainagent/tools/math/count_distinct_permutations.py +27 -0
  75. autogen/agentchat/contrib/captainagent/tools/math/evaluate_expression.py +28 -0
  76. autogen/agentchat/contrib/captainagent/tools/math/find_continuity_point.py +34 -0
  77. autogen/agentchat/contrib/captainagent/tools/math/fraction_to_mixed_numbers.py +39 -0
  78. autogen/agentchat/contrib/captainagent/tools/math/modular_inverse_sum.py +23 -0
  79. autogen/agentchat/contrib/captainagent/tools/math/simplify_mixed_numbers.py +36 -0
  80. autogen/agentchat/contrib/captainagent/tools/math/sum_of_digit_factorials.py +15 -0
  81. autogen/agentchat/contrib/captainagent/tools/math/sum_of_primes_below.py +15 -0
  82. autogen/agentchat/contrib/captainagent/tools/requirements.txt +10 -0
  83. autogen/agentchat/contrib/captainagent/tools/tool_description.tsv +34 -0
  84. autogen/agentchat/contrib/gpt_assistant_agent.py +526 -0
  85. autogen/agentchat/contrib/graph_rag/__init__.py +9 -0
  86. autogen/agentchat/contrib/graph_rag/document.py +29 -0
  87. autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py +167 -0
  88. autogen/agentchat/contrib/graph_rag/falkor_graph_rag_capability.py +103 -0
  89. autogen/agentchat/contrib/graph_rag/graph_query_engine.py +53 -0
  90. autogen/agentchat/contrib/graph_rag/graph_rag_capability.py +63 -0
  91. autogen/agentchat/contrib/graph_rag/neo4j_graph_query_engine.py +263 -0
  92. autogen/agentchat/contrib/graph_rag/neo4j_graph_rag_capability.py +83 -0
  93. autogen/agentchat/contrib/graph_rag/neo4j_native_graph_query_engine.py +210 -0
  94. autogen/agentchat/contrib/graph_rag/neo4j_native_graph_rag_capability.py +93 -0
  95. autogen/agentchat/contrib/img_utils.py +397 -0
  96. autogen/agentchat/contrib/llamaindex_conversable_agent.py +117 -0
  97. autogen/agentchat/contrib/llava_agent.py +189 -0
  98. autogen/agentchat/contrib/math_user_proxy_agent.py +464 -0
  99. autogen/agentchat/contrib/multimodal_conversable_agent.py +125 -0
  100. autogen/agentchat/contrib/qdrant_retrieve_user_proxy_agent.py +325 -0
  101. autogen/agentchat/contrib/rag/__init__.py +10 -0
  102. autogen/agentchat/contrib/rag/chromadb_query_engine.py +268 -0
  103. autogen/agentchat/contrib/rag/llamaindex_query_engine.py +195 -0
  104. autogen/agentchat/contrib/rag/mongodb_query_engine.py +319 -0
  105. autogen/agentchat/contrib/rag/query_engine.py +76 -0
  106. autogen/agentchat/contrib/retrieve_assistant_agent.py +59 -0
  107. autogen/agentchat/contrib/retrieve_user_proxy_agent.py +704 -0
  108. autogen/agentchat/contrib/society_of_mind_agent.py +200 -0
  109. autogen/agentchat/contrib/swarm_agent.py +1404 -0
  110. autogen/agentchat/contrib/text_analyzer_agent.py +79 -0
  111. autogen/agentchat/contrib/vectordb/__init__.py +5 -0
  112. autogen/agentchat/contrib/vectordb/base.py +224 -0
  113. autogen/agentchat/contrib/vectordb/chromadb.py +316 -0
  114. autogen/agentchat/contrib/vectordb/couchbase.py +405 -0
  115. autogen/agentchat/contrib/vectordb/mongodb.py +551 -0
  116. autogen/agentchat/contrib/vectordb/pgvectordb.py +927 -0
  117. autogen/agentchat/contrib/vectordb/qdrant.py +320 -0
  118. autogen/agentchat/contrib/vectordb/utils.py +126 -0
  119. autogen/agentchat/contrib/web_surfer.py +304 -0
  120. autogen/agentchat/conversable_agent.py +4307 -0
  121. autogen/agentchat/group/__init__.py +67 -0
  122. autogen/agentchat/group/available_condition.py +91 -0
  123. autogen/agentchat/group/context_condition.py +77 -0
  124. autogen/agentchat/group/context_expression.py +238 -0
  125. autogen/agentchat/group/context_str.py +39 -0
  126. autogen/agentchat/group/context_variables.py +182 -0
  127. autogen/agentchat/group/events/transition_events.py +111 -0
  128. autogen/agentchat/group/group_tool_executor.py +324 -0
  129. autogen/agentchat/group/group_utils.py +659 -0
  130. autogen/agentchat/group/guardrails.py +179 -0
  131. autogen/agentchat/group/handoffs.py +303 -0
  132. autogen/agentchat/group/llm_condition.py +93 -0
  133. autogen/agentchat/group/multi_agent_chat.py +291 -0
  134. autogen/agentchat/group/on_condition.py +55 -0
  135. autogen/agentchat/group/on_context_condition.py +51 -0
  136. autogen/agentchat/group/patterns/__init__.py +18 -0
  137. autogen/agentchat/group/patterns/auto.py +160 -0
  138. autogen/agentchat/group/patterns/manual.py +177 -0
  139. autogen/agentchat/group/patterns/pattern.py +295 -0
  140. autogen/agentchat/group/patterns/random.py +106 -0
  141. autogen/agentchat/group/patterns/round_robin.py +117 -0
  142. autogen/agentchat/group/reply_result.py +24 -0
  143. autogen/agentchat/group/safeguards/__init__.py +21 -0
  144. autogen/agentchat/group/safeguards/api.py +241 -0
  145. autogen/agentchat/group/safeguards/enforcer.py +1158 -0
  146. autogen/agentchat/group/safeguards/events.py +140 -0
  147. autogen/agentchat/group/safeguards/validator.py +435 -0
  148. autogen/agentchat/group/speaker_selection_result.py +41 -0
  149. autogen/agentchat/group/targets/__init__.py +4 -0
  150. autogen/agentchat/group/targets/function_target.py +245 -0
  151. autogen/agentchat/group/targets/group_chat_target.py +133 -0
  152. autogen/agentchat/group/targets/group_manager_target.py +151 -0
  153. autogen/agentchat/group/targets/transition_target.py +424 -0
  154. autogen/agentchat/group/targets/transition_utils.py +6 -0
  155. autogen/agentchat/groupchat.py +1832 -0
  156. autogen/agentchat/realtime/__init__.py +3 -0
  157. autogen/agentchat/realtime/experimental/__init__.py +20 -0
  158. autogen/agentchat/realtime/experimental/audio_adapters/__init__.py +8 -0
  159. autogen/agentchat/realtime/experimental/audio_adapters/twilio_audio_adapter.py +148 -0
  160. autogen/agentchat/realtime/experimental/audio_adapters/websocket_audio_adapter.py +139 -0
  161. autogen/agentchat/realtime/experimental/audio_observer.py +42 -0
  162. autogen/agentchat/realtime/experimental/clients/__init__.py +15 -0
  163. autogen/agentchat/realtime/experimental/clients/gemini/__init__.py +7 -0
  164. autogen/agentchat/realtime/experimental/clients/gemini/client.py +274 -0
  165. autogen/agentchat/realtime/experimental/clients/oai/__init__.py +8 -0
  166. autogen/agentchat/realtime/experimental/clients/oai/base_client.py +220 -0
  167. autogen/agentchat/realtime/experimental/clients/oai/rtc_client.py +243 -0
  168. autogen/agentchat/realtime/experimental/clients/oai/utils.py +48 -0
  169. autogen/agentchat/realtime/experimental/clients/realtime_client.py +191 -0
  170. autogen/agentchat/realtime/experimental/function_observer.py +84 -0
  171. autogen/agentchat/realtime/experimental/realtime_agent.py +158 -0
  172. autogen/agentchat/realtime/experimental/realtime_events.py +42 -0
  173. autogen/agentchat/realtime/experimental/realtime_observer.py +100 -0
  174. autogen/agentchat/realtime/experimental/realtime_swarm.py +533 -0
  175. autogen/agentchat/realtime/experimental/websockets.py +21 -0
  176. autogen/agentchat/realtime_agent/__init__.py +21 -0
  177. autogen/agentchat/user_proxy_agent.py +114 -0
  178. autogen/agentchat/utils.py +206 -0
  179. autogen/agents/__init__.py +3 -0
  180. autogen/agents/contrib/__init__.py +10 -0
  181. autogen/agents/contrib/time/__init__.py +8 -0
  182. autogen/agents/contrib/time/time_reply_agent.py +74 -0
  183. autogen/agents/contrib/time/time_tool_agent.py +52 -0
  184. autogen/agents/experimental/__init__.py +27 -0
  185. autogen/agents/experimental/deep_research/__init__.py +7 -0
  186. autogen/agents/experimental/deep_research/deep_research.py +52 -0
  187. autogen/agents/experimental/discord/__init__.py +7 -0
  188. autogen/agents/experimental/discord/discord.py +66 -0
  189. autogen/agents/experimental/document_agent/__init__.py +19 -0
  190. autogen/agents/experimental/document_agent/chroma_query_engine.py +301 -0
  191. autogen/agents/experimental/document_agent/docling_doc_ingest_agent.py +113 -0
  192. autogen/agents/experimental/document_agent/document_agent.py +643 -0
  193. autogen/agents/experimental/document_agent/document_conditions.py +50 -0
  194. autogen/agents/experimental/document_agent/document_utils.py +376 -0
  195. autogen/agents/experimental/document_agent/inmemory_query_engine.py +214 -0
  196. autogen/agents/experimental/document_agent/parser_utils.py +134 -0
  197. autogen/agents/experimental/document_agent/url_utils.py +417 -0
  198. autogen/agents/experimental/reasoning/__init__.py +7 -0
  199. autogen/agents/experimental/reasoning/reasoning_agent.py +1178 -0
  200. autogen/agents/experimental/slack/__init__.py +7 -0
  201. autogen/agents/experimental/slack/slack.py +73 -0
  202. autogen/agents/experimental/telegram/__init__.py +7 -0
  203. autogen/agents/experimental/telegram/telegram.py +76 -0
  204. autogen/agents/experimental/websurfer/__init__.py +7 -0
  205. autogen/agents/experimental/websurfer/websurfer.py +70 -0
  206. autogen/agents/experimental/wikipedia/__init__.py +7 -0
  207. autogen/agents/experimental/wikipedia/wikipedia.py +88 -0
  208. autogen/browser_utils.py +309 -0
  209. autogen/cache/__init__.py +10 -0
  210. autogen/cache/abstract_cache_base.py +71 -0
  211. autogen/cache/cache.py +203 -0
  212. autogen/cache/cache_factory.py +88 -0
  213. autogen/cache/cosmos_db_cache.py +144 -0
  214. autogen/cache/disk_cache.py +97 -0
  215. autogen/cache/in_memory_cache.py +54 -0
  216. autogen/cache/redis_cache.py +119 -0
  217. autogen/code_utils.py +598 -0
  218. autogen/coding/__init__.py +30 -0
  219. autogen/coding/base.py +120 -0
  220. autogen/coding/docker_commandline_code_executor.py +283 -0
  221. autogen/coding/factory.py +56 -0
  222. autogen/coding/func_with_reqs.py +203 -0
  223. autogen/coding/jupyter/__init__.py +23 -0
  224. autogen/coding/jupyter/base.py +36 -0
  225. autogen/coding/jupyter/docker_jupyter_server.py +160 -0
  226. autogen/coding/jupyter/embedded_ipython_code_executor.py +182 -0
  227. autogen/coding/jupyter/import_utils.py +82 -0
  228. autogen/coding/jupyter/jupyter_client.py +224 -0
  229. autogen/coding/jupyter/jupyter_code_executor.py +154 -0
  230. autogen/coding/jupyter/local_jupyter_server.py +164 -0
  231. autogen/coding/local_commandline_code_executor.py +341 -0
  232. autogen/coding/markdown_code_extractor.py +44 -0
  233. autogen/coding/utils.py +55 -0
  234. autogen/coding/yepcode_code_executor.py +197 -0
  235. autogen/doc_utils.py +35 -0
  236. autogen/environments/__init__.py +10 -0
  237. autogen/environments/docker_python_environment.py +365 -0
  238. autogen/environments/python_environment.py +125 -0
  239. autogen/environments/system_python_environment.py +85 -0
  240. autogen/environments/venv_python_environment.py +220 -0
  241. autogen/environments/working_directory.py +74 -0
  242. autogen/events/__init__.py +7 -0
  243. autogen/events/agent_events.py +1016 -0
  244. autogen/events/base_event.py +100 -0
  245. autogen/events/client_events.py +168 -0
  246. autogen/events/helpers.py +44 -0
  247. autogen/events/print_event.py +45 -0
  248. autogen/exception_utils.py +73 -0
  249. autogen/extensions/__init__.py +5 -0
  250. autogen/fast_depends/__init__.py +16 -0
  251. autogen/fast_depends/_compat.py +75 -0
  252. autogen/fast_depends/core/__init__.py +14 -0
  253. autogen/fast_depends/core/build.py +206 -0
  254. autogen/fast_depends/core/model.py +527 -0
  255. autogen/fast_depends/dependencies/__init__.py +15 -0
  256. autogen/fast_depends/dependencies/model.py +30 -0
  257. autogen/fast_depends/dependencies/provider.py +40 -0
  258. autogen/fast_depends/library/__init__.py +10 -0
  259. autogen/fast_depends/library/model.py +46 -0
  260. autogen/fast_depends/py.typed +6 -0
  261. autogen/fast_depends/schema.py +66 -0
  262. autogen/fast_depends/use.py +272 -0
  263. autogen/fast_depends/utils.py +177 -0
  264. autogen/formatting_utils.py +83 -0
  265. autogen/function_utils.py +13 -0
  266. autogen/graph_utils.py +173 -0
  267. autogen/import_utils.py +539 -0
  268. autogen/interop/__init__.py +22 -0
  269. autogen/interop/crewai/__init__.py +7 -0
  270. autogen/interop/crewai/crewai.py +88 -0
  271. autogen/interop/interoperability.py +71 -0
  272. autogen/interop/interoperable.py +46 -0
  273. autogen/interop/langchain/__init__.py +8 -0
  274. autogen/interop/langchain/langchain_chat_model_factory.py +156 -0
  275. autogen/interop/langchain/langchain_tool.py +78 -0
  276. autogen/interop/litellm/__init__.py +7 -0
  277. autogen/interop/litellm/litellm_config_factory.py +178 -0
  278. autogen/interop/pydantic_ai/__init__.py +7 -0
  279. autogen/interop/pydantic_ai/pydantic_ai.py +172 -0
  280. autogen/interop/registry.py +70 -0
  281. autogen/io/__init__.py +15 -0
  282. autogen/io/base.py +151 -0
  283. autogen/io/console.py +56 -0
  284. autogen/io/processors/__init__.py +12 -0
  285. autogen/io/processors/base.py +21 -0
  286. autogen/io/processors/console_event_processor.py +61 -0
  287. autogen/io/run_response.py +294 -0
  288. autogen/io/thread_io_stream.py +63 -0
  289. autogen/io/websockets.py +214 -0
  290. autogen/json_utils.py +42 -0
  291. autogen/llm_clients/MIGRATION_TO_V2.md +782 -0
  292. autogen/llm_clients/__init__.py +77 -0
  293. autogen/llm_clients/client_v2.py +122 -0
  294. autogen/llm_clients/models/__init__.py +55 -0
  295. autogen/llm_clients/models/content_blocks.py +389 -0
  296. autogen/llm_clients/models/unified_message.py +145 -0
  297. autogen/llm_clients/models/unified_response.py +83 -0
  298. autogen/llm_clients/openai_completions_client.py +444 -0
  299. autogen/llm_config/__init__.py +11 -0
  300. autogen/llm_config/client.py +59 -0
  301. autogen/llm_config/config.py +461 -0
  302. autogen/llm_config/entry.py +169 -0
  303. autogen/llm_config/types.py +37 -0
  304. autogen/llm_config/utils.py +223 -0
  305. autogen/logger/__init__.py +11 -0
  306. autogen/logger/base_logger.py +129 -0
  307. autogen/logger/file_logger.py +262 -0
  308. autogen/logger/logger_factory.py +42 -0
  309. autogen/logger/logger_utils.py +57 -0
  310. autogen/logger/sqlite_logger.py +524 -0
  311. autogen/math_utils.py +338 -0
  312. autogen/mcp/__init__.py +7 -0
  313. autogen/mcp/__main__.py +78 -0
  314. autogen/mcp/helpers.py +45 -0
  315. autogen/mcp/mcp_client.py +349 -0
  316. autogen/mcp/mcp_proxy/__init__.py +19 -0
  317. autogen/mcp/mcp_proxy/fastapi_code_generator_helpers.py +62 -0
  318. autogen/mcp/mcp_proxy/mcp_proxy.py +577 -0
  319. autogen/mcp/mcp_proxy/operation_grouping.py +166 -0
  320. autogen/mcp/mcp_proxy/operation_renaming.py +110 -0
  321. autogen/mcp/mcp_proxy/patch_fastapi_code_generator.py +98 -0
  322. autogen/mcp/mcp_proxy/security.py +399 -0
  323. autogen/mcp/mcp_proxy/security_schema_visitor.py +37 -0
  324. autogen/messages/__init__.py +7 -0
  325. autogen/messages/agent_messages.py +946 -0
  326. autogen/messages/base_message.py +108 -0
  327. autogen/messages/client_messages.py +172 -0
  328. autogen/messages/print_message.py +48 -0
  329. autogen/oai/__init__.py +61 -0
  330. autogen/oai/anthropic.py +1516 -0
  331. autogen/oai/bedrock.py +800 -0
  332. autogen/oai/cerebras.py +302 -0
  333. autogen/oai/client.py +1658 -0
  334. autogen/oai/client_utils.py +196 -0
  335. autogen/oai/cohere.py +494 -0
  336. autogen/oai/gemini.py +1045 -0
  337. autogen/oai/gemini_types.py +156 -0
  338. autogen/oai/groq.py +319 -0
  339. autogen/oai/mistral.py +311 -0
  340. autogen/oai/oai_models/__init__.py +23 -0
  341. autogen/oai/oai_models/_models.py +16 -0
  342. autogen/oai/oai_models/chat_completion.py +86 -0
  343. autogen/oai/oai_models/chat_completion_audio.py +32 -0
  344. autogen/oai/oai_models/chat_completion_message.py +97 -0
  345. autogen/oai/oai_models/chat_completion_message_tool_call.py +60 -0
  346. autogen/oai/oai_models/chat_completion_token_logprob.py +62 -0
  347. autogen/oai/oai_models/completion_usage.py +59 -0
  348. autogen/oai/ollama.py +657 -0
  349. autogen/oai/openai_responses.py +451 -0
  350. autogen/oai/openai_utils.py +897 -0
  351. autogen/oai/together.py +387 -0
  352. autogen/remote/__init__.py +18 -0
  353. autogen/remote/agent.py +199 -0
  354. autogen/remote/agent_service.py +197 -0
  355. autogen/remote/errors.py +17 -0
  356. autogen/remote/httpx_client_factory.py +131 -0
  357. autogen/remote/protocol.py +37 -0
  358. autogen/remote/retry.py +102 -0
  359. autogen/remote/runtime.py +96 -0
  360. autogen/retrieve_utils.py +490 -0
  361. autogen/runtime_logging.py +161 -0
  362. autogen/testing/__init__.py +12 -0
  363. autogen/testing/messages.py +45 -0
  364. autogen/testing/test_agent.py +111 -0
  365. autogen/token_count_utils.py +280 -0
  366. autogen/tools/__init__.py +20 -0
  367. autogen/tools/contrib/__init__.py +9 -0
  368. autogen/tools/contrib/time/__init__.py +7 -0
  369. autogen/tools/contrib/time/time.py +40 -0
  370. autogen/tools/dependency_injection.py +249 -0
  371. autogen/tools/experimental/__init__.py +54 -0
  372. autogen/tools/experimental/browser_use/__init__.py +7 -0
  373. autogen/tools/experimental/browser_use/browser_use.py +154 -0
  374. autogen/tools/experimental/code_execution/__init__.py +7 -0
  375. autogen/tools/experimental/code_execution/python_code_execution.py +86 -0
  376. autogen/tools/experimental/crawl4ai/__init__.py +7 -0
  377. autogen/tools/experimental/crawl4ai/crawl4ai.py +150 -0
  378. autogen/tools/experimental/deep_research/__init__.py +7 -0
  379. autogen/tools/experimental/deep_research/deep_research.py +329 -0
  380. autogen/tools/experimental/duckduckgo/__init__.py +7 -0
  381. autogen/tools/experimental/duckduckgo/duckduckgo_search.py +103 -0
  382. autogen/tools/experimental/firecrawl/__init__.py +7 -0
  383. autogen/tools/experimental/firecrawl/firecrawl_tool.py +836 -0
  384. autogen/tools/experimental/google/__init__.py +14 -0
  385. autogen/tools/experimental/google/authentication/__init__.py +11 -0
  386. autogen/tools/experimental/google/authentication/credentials_hosted_provider.py +43 -0
  387. autogen/tools/experimental/google/authentication/credentials_local_provider.py +91 -0
  388. autogen/tools/experimental/google/authentication/credentials_provider.py +35 -0
  389. autogen/tools/experimental/google/drive/__init__.py +9 -0
  390. autogen/tools/experimental/google/drive/drive_functions.py +124 -0
  391. autogen/tools/experimental/google/drive/toolkit.py +88 -0
  392. autogen/tools/experimental/google/model.py +17 -0
  393. autogen/tools/experimental/google/toolkit_protocol.py +19 -0
  394. autogen/tools/experimental/google_search/__init__.py +8 -0
  395. autogen/tools/experimental/google_search/google_search.py +93 -0
  396. autogen/tools/experimental/google_search/youtube_search.py +181 -0
  397. autogen/tools/experimental/messageplatform/__init__.py +17 -0
  398. autogen/tools/experimental/messageplatform/discord/__init__.py +7 -0
  399. autogen/tools/experimental/messageplatform/discord/discord.py +284 -0
  400. autogen/tools/experimental/messageplatform/slack/__init__.py +7 -0
  401. autogen/tools/experimental/messageplatform/slack/slack.py +385 -0
  402. autogen/tools/experimental/messageplatform/telegram/__init__.py +7 -0
  403. autogen/tools/experimental/messageplatform/telegram/telegram.py +271 -0
  404. autogen/tools/experimental/perplexity/__init__.py +7 -0
  405. autogen/tools/experimental/perplexity/perplexity_search.py +249 -0
  406. autogen/tools/experimental/reliable/__init__.py +10 -0
  407. autogen/tools/experimental/reliable/reliable.py +1311 -0
  408. autogen/tools/experimental/searxng/__init__.py +7 -0
  409. autogen/tools/experimental/searxng/searxng_search.py +142 -0
  410. autogen/tools/experimental/tavily/__init__.py +7 -0
  411. autogen/tools/experimental/tavily/tavily_search.py +176 -0
  412. autogen/tools/experimental/web_search_preview/__init__.py +7 -0
  413. autogen/tools/experimental/web_search_preview/web_search_preview.py +120 -0
  414. autogen/tools/experimental/wikipedia/__init__.py +7 -0
  415. autogen/tools/experimental/wikipedia/wikipedia.py +284 -0
  416. autogen/tools/function_utils.py +412 -0
  417. autogen/tools/tool.py +188 -0
  418. autogen/tools/toolkit.py +86 -0
  419. autogen/types.py +29 -0
  420. autogen/version.py +7 -0
  421. templates/client_template/main.jinja2 +72 -0
  422. templates/config_template/config.jinja2 +7 -0
  423. templates/main.jinja2 +61 -0
autogen/oai/ollama.py ADDED
@@ -0,0 +1,657 @@
1
+ # Copyright (c) 2023 - 2025, AG2ai, Inc., AG2ai open-source projects maintainers and core contributors
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+ #
5
+ # Portions derived from https://github.com/microsoft/autogen are under the MIT License.
6
+ # SPDX-License-Identifier: MIT
7
+ """Create an OpenAI-compatible client using Ollama's API.
8
+
9
+ Example:
10
+ ```python
11
+ llm_config = {"config_list": [{"api_type": "ollama", "model": "mistral:7b-instruct-v0.3-q6_K"}]}
12
+
13
+ agent = autogen.AssistantAgent("my_agent", llm_config=llm_config)
14
+ ```
15
+
16
+ Install Ollama's python library using: pip install --upgrade ollama
17
+ Install fix-busted-json library: pip install --upgrade fix-busted-json
18
+
19
+ Resources:
20
+ - https://github.com/ollama/ollama-python
21
+ """
22
+
23
+ from __future__ import annotations
24
+
25
+ import ast
26
+ import copy
27
+ import json
28
+ import random
29
+ import re
30
+ import time
31
+ import warnings
32
+ from typing import Any, Literal
33
+
34
+ from pydantic import BaseModel, Field, HttpUrl
35
+
36
+ from ..import_utils import optional_import_block, require_optional_import
37
+ from ..llm_config.entry import LLMConfigEntry, LLMConfigEntryDict
38
+ from .client_utils import FormatterProtocol, should_hide_tools, validate_parameter
39
+ from .oai_models import ChatCompletion, ChatCompletionMessage, ChatCompletionMessageToolCall, Choice, CompletionUsage
40
+
41
+ with optional_import_block():
42
+ import ollama
43
+ from fix_busted_json import repair_json
44
+ from ollama import Client
45
+
46
+
47
+ class OllamaEntryDict(LLMConfigEntryDict, total=False):
48
+ api_type: Literal["ollama"]
49
+ client_host: HttpUrl | None
50
+ stream: bool
51
+ num_predict: int
52
+ num_ctx: int
53
+ repeat_penalty: float
54
+ seed: int
55
+ top_k: int
56
+ hide_tools: Literal["if_all_run", "if_any_run", "never"]
57
+ native_tool_calls: bool
58
+
59
+
60
+ class OllamaLLMConfigEntry(LLMConfigEntry):
61
+ api_type: Literal["ollama"] = "ollama"
62
+ # TODO: max_tokens
63
+ client_host: HttpUrl | None = None
64
+ stream: bool = False
65
+ num_predict: int = Field(
66
+ default=-1,
67
+ description="Maximum number of tokens to predict, note: -1 is infinite (default), -2 is fill context.",
68
+ )
69
+ num_ctx: int = Field(default=2048)
70
+ repeat_penalty: float = Field(default=1.1)
71
+ seed: int = Field(default=0)
72
+ top_k: int = Field(default=40)
73
+ hide_tools: Literal["if_all_run", "if_any_run", "never"] = "never"
74
+ native_tool_calls: bool = False
75
+
76
+ def create_client(self):
77
+ raise NotImplementedError("OllamaLLMConfigEntry.create_client is not implemented.")
78
+
79
+
80
+ class OllamaClient:
81
+ """Client for Ollama's API."""
82
+
83
+ RESPONSE_USAGE_KEYS: list[str] = ["prompt_tokens", "completion_tokens", "total_tokens", "cost", "model"]
84
+
85
+ # Defaults for manual tool calling
86
+ # Instruction is added to the first system message and provides directions to follow a two step
87
+ # process
88
+ # 1. (before tools have been called) Return JSON with the functions to call
89
+ # 2. (directly after tools have been called) Return Text describing the results of the function calls in text format
90
+
91
+ # Override using "manual_tool_call_instruction" config parameter
92
+ TOOL_CALL_MANUAL_INSTRUCTION = (
93
+ "You are to follow a strict two step process that will occur over "
94
+ "a number of interactions, so pay attention to what step you are in based on the full "
95
+ "conversation. We will be taking turns so only do one step at a time so don't perform step "
96
+ "2 until step 1 is complete and I've told you the result. The first step is to choose one "
97
+ "or more functions based on the request given and return only JSON with the functions and "
98
+ "arguments to use. The second step is to analyse the given output of the function and summarise "
99
+ "it returning only TEXT and not Python or JSON. "
100
+ "For argument values, be sure numbers aren't strings, they should not have double quotes around them. "
101
+ "In terms of your response format, for step 1 return only JSON and NO OTHER text, "
102
+ "for step 2 return only text and NO JSON/Python/Markdown. "
103
+ 'The format for running a function is [{"name": "function_name1", "arguments":{"argument_name": "argument_value"}},{"name": "function_name2", "arguments":{"argument_name": "argument_value"}}] '
104
+ 'Make sure the keys "name" and "arguments" are as described. '
105
+ "If you don't get the format correct, try again. "
106
+ "The following functions are available to you:[FUNCTIONS_LIST]"
107
+ )
108
+
109
+ # Appended to the last user message if no tools have been called
110
+ # Override using "manual_tool_call_step1" config parameter
111
+ TOOL_CALL_MANUAL_STEP1 = " (proceed with step 1)"
112
+
113
+ # Appended to the user message after tools have been executed. Will create a 'user' message if one doesn't exist.
114
+ # Override using "manual_tool_call_step2" config parameter
115
+ TOOL_CALL_MANUAL_STEP2 = " (proceed with step 2)"
116
+
117
+ def __init__(self, response_format: BaseModel | dict[str, Any] | None = None, **kwargs):
118
+ """Note that no api_key or environment variable is required for Ollama."""
119
+ # Store the response format, if provided (for structured outputs)
120
+ self._response_format: BaseModel | dict[str, Any] | None = response_format
121
+
122
+ def message_retrieval(self, response) -> list:
123
+ """Retrieve and return a list of strings or a list of Choice.Message from the response.
124
+
125
+ NOTE: if a list of Choice.Message is returned, it currently needs to contain the fields of OpenAI's ChatCompletion Message object,
126
+ since that is expected for function or tool calling in the rest of the codebase at the moment, unless a custom agent is being used.
127
+ """
128
+ return [choice.message for choice in response.choices]
129
+
130
+ def cost(self, response) -> float:
131
+ return response.cost
132
+
133
+ @staticmethod
134
+ def get_usage(response) -> dict:
135
+ """Return usage summary of the response using RESPONSE_USAGE_KEYS."""
136
+ # ... # pragma: no cover
137
+ return {
138
+ "prompt_tokens": response.usage.prompt_tokens,
139
+ "completion_tokens": response.usage.completion_tokens,
140
+ "total_tokens": response.usage.total_tokens,
141
+ "cost": response.cost,
142
+ "model": response.model,
143
+ }
144
+
145
+ def parse_params(self, params: dict[str, Any]) -> dict[str, Any]:
146
+ """Loads the parameters for Ollama API from the passed in parameters and returns a validated set. Checks types, ranges, and sets defaults"""
147
+ ollama_params = {}
148
+
149
+ # Check that we have what we need to use Ollama's API
150
+ # https://github.com/ollama/ollama/blob/main/docs/api.md#generate-a-completion
151
+
152
+ # The main parameters are model, prompt, stream, and options
153
+ # Options is a dictionary of parameters for the model
154
+ # There are other, advanced, parameters such as format, system (to override system message), template, raw, etc. - not used
155
+
156
+ # We won't enforce the available models
157
+ ollama_params["model"] = params.get("model")
158
+ assert ollama_params["model"], (
159
+ "Please specify the 'model' in your config list entry to nominate the Ollama model to use."
160
+ )
161
+
162
+ ollama_params["stream"] = validate_parameter(params, "stream", bool, True, False, None, None)
163
+
164
+ # Build up the options dictionary
165
+ # https://github.com/ollama/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values
166
+ options_dict = {}
167
+
168
+ if "num_predict" in params:
169
+ # Maximum number of tokens to predict, note: -1 is infinite, -2 is fill context, 128 is default
170
+ options_dict["num_predict"] = validate_parameter(params, "num_predict", int, False, 128, None, None)
171
+
172
+ if "num_ctx" in params:
173
+ # Set size of context window used to generate next token, 2048 is default
174
+ options_dict["num_ctx"] = validate_parameter(params, "num_ctx", int, False, 2048, None, None)
175
+
176
+ if "repeat_penalty" in params:
177
+ options_dict["repeat_penalty"] = validate_parameter(
178
+ params, "repeat_penalty", (int, float), False, 1.1, None, None
179
+ )
180
+
181
+ if "seed" in params:
182
+ options_dict["seed"] = validate_parameter(params, "seed", int, False, 42, None, None)
183
+
184
+ if "temperature" in params:
185
+ options_dict["temperature"] = validate_parameter(
186
+ params, "temperature", (int, float), False, 0.8, None, None
187
+ )
188
+
189
+ if "top_k" in params:
190
+ options_dict["top_k"] = validate_parameter(params, "top_k", int, False, 40, None, None)
191
+
192
+ if "top_p" in params:
193
+ options_dict["top_p"] = validate_parameter(params, "top_p", (int, float), False, 0.9, None, None)
194
+
195
+ if self._native_tool_calls and self._tools_in_conversation and not self._should_hide_tools:
196
+ ollama_params["tools"] = params["tools"]
197
+
198
+ # Ollama doesn't support streaming with tools natively
199
+ if ollama_params["stream"] and self._native_tool_calls:
200
+ warnings.warn(
201
+ "Streaming is not supported when using tools and 'Native' tool calling, streaming will be disabled.",
202
+ UserWarning,
203
+ )
204
+
205
+ ollama_params["stream"] = False
206
+
207
+ if not self._native_tool_calls and self._tools_in_conversation:
208
+ # For manual tool calling we have injected the available tools into the prompt
209
+ # and we don't want to force JSON mode
210
+ ollama_params["format"] = "" # Don't force JSON for manual tool calling mode
211
+
212
+ if len(options_dict) != 0:
213
+ ollama_params["options"] = options_dict
214
+
215
+ # Structured outputs (see https://ollama.com/blog/structured-outputs)
216
+ if not self._response_format and params.get("response_format"):
217
+ self._response_format = params["response_format"]
218
+
219
+ if self._response_format:
220
+ if isinstance(self._response_format, dict):
221
+ ollama_params["format"] = self._response_format
222
+ else:
223
+ # Keep self._response_format as a Pydantic model for when process the response
224
+ ollama_params["format"] = self._response_format.model_json_schema()
225
+
226
+ return ollama_params
227
+
228
+ @require_optional_import(["ollama", "fix_busted_json"], "ollama")
229
+ def create(self, params: dict) -> ChatCompletion:
230
+ messages = params.get("messages", [])
231
+
232
+ # Are tools involved in this conversation?
233
+ self._tools_in_conversation = "tools" in params
234
+
235
+ # We provide second-level filtering out of tools to avoid LLMs re-calling tools continuously
236
+ if self._tools_in_conversation:
237
+ hide_tools = validate_parameter(
238
+ params, "hide_tools", str, False, "never", None, ["if_all_run", "if_any_run", "never"]
239
+ )
240
+ self._should_hide_tools = should_hide_tools(messages, params["tools"], hide_tools)
241
+ else:
242
+ self._should_hide_tools = False
243
+
244
+ # Are we using native Ollama tool calling, otherwise we're doing manual tool calling
245
+ # We allow the user to decide if they want to use Ollama's tool calling
246
+ # or for tool calling to be handled manually through text messages
247
+ # Default is True = Ollama's tool calling
248
+ self._native_tool_calls = validate_parameter(params, "native_tool_calls", bool, False, True, None, None)
249
+
250
+ if not self._native_tool_calls:
251
+ # Load defaults
252
+ self._manual_tool_call_instruction = validate_parameter(
253
+ params, "manual_tool_call_instruction", str, False, self.TOOL_CALL_MANUAL_INSTRUCTION, None, None
254
+ )
255
+ self._manual_tool_call_step1 = validate_parameter(
256
+ params, "manual_tool_call_step1", str, False, self.TOOL_CALL_MANUAL_STEP1, None, None
257
+ )
258
+ self._manual_tool_call_step2 = validate_parameter(
259
+ params, "manual_tool_call_step2", str, False, self.TOOL_CALL_MANUAL_STEP2, None, None
260
+ )
261
+
262
+ # Convert AG2 messages to Ollama messages
263
+ ollama_messages = self.oai_messages_to_ollama_messages(
264
+ messages,
265
+ (
266
+ params["tools"]
267
+ if (not self._native_tool_calls and self._tools_in_conversation) and not self._should_hide_tools
268
+ else None
269
+ ),
270
+ )
271
+
272
+ # Parse parameters to the Ollama API's parameters
273
+ ollama_params = self.parse_params(params)
274
+
275
+ ollama_params["messages"] = ollama_messages
276
+
277
+ # Token counts will be returned
278
+ prompt_tokens = 0
279
+ completion_tokens = 0
280
+ total_tokens = 0
281
+
282
+ ans = None
283
+ if "client_host" in params:
284
+ # Convert client_host to string from HttpUrl
285
+ client = Client(host=str(params["client_host"]))
286
+ response = client.chat(**ollama_params)
287
+ else:
288
+ response = ollama.chat(**ollama_params)
289
+
290
+ if ollama_params["stream"]:
291
+ # Read in the chunks as they stream, taking in tool_calls which may be across
292
+ # multiple chunks if more than one suggested
293
+ ans = ""
294
+ for chunk in response:
295
+ ans = ans + (chunk["message"]["content"] or "")
296
+
297
+ if "done_reason" in chunk:
298
+ prompt_tokens = chunk.get("prompt_eval_count", 0)
299
+ completion_tokens = chunk.get("eval_count", 0)
300
+ total_tokens = prompt_tokens + completion_tokens
301
+ else:
302
+ # Non-streaming finished
303
+ ans: str = response["message"]["content"]
304
+
305
+ prompt_tokens = response.get("prompt_eval_count", 0)
306
+ completion_tokens = response.get("eval_count", 0)
307
+ total_tokens = prompt_tokens + completion_tokens
308
+
309
+ if response is not None:
310
+ # Defaults
311
+ ollama_finish = "stop"
312
+ tool_calls = None
313
+
314
+ # Id and streaming text into response
315
+ if ollama_params["stream"]:
316
+ response_content = ans
317
+ response_id = chunk["created_at"]
318
+ else:
319
+ response_content = response["message"]["content"]
320
+ response_id = response["created_at"]
321
+
322
+ # Process tools in the response
323
+ if self._tools_in_conversation:
324
+ if self._native_tool_calls:
325
+ if not ollama_params["stream"]:
326
+ response_content = response["message"]["content"]
327
+
328
+ # Native tool calling
329
+ if "tool_calls" in response["message"]:
330
+ ollama_finish = "tool_calls"
331
+ tool_calls = []
332
+ random_id = random.randint(0, 10000)
333
+ for tool_call in response["message"]["tool_calls"]:
334
+ tool_calls.append(
335
+ ChatCompletionMessageToolCall(
336
+ id=f"ollama_func_{random_id}",
337
+ function={
338
+ "name": tool_call["function"]["name"],
339
+ "arguments": json.dumps(tool_call["function"]["arguments"]),
340
+ },
341
+ type="function",
342
+ )
343
+ )
344
+
345
+ random_id += 1
346
+
347
+ elif not self._native_tool_calls:
348
+ # Try to convert the response to a tool call object
349
+ response_toolcalls = response_to_tool_call(ans)
350
+
351
+ # If we can, then we've got tool call(s)
352
+ if response_toolcalls is not None:
353
+ ollama_finish = "tool_calls"
354
+ tool_calls = []
355
+ random_id = random.randint(0, 10000)
356
+
357
+ for json_function in response_toolcalls:
358
+ tool_calls.append(
359
+ ChatCompletionMessageToolCall(
360
+ id=f"ollama_manual_func_{random_id}",
361
+ function={
362
+ "name": json_function["name"],
363
+ "arguments": (
364
+ json.dumps(json_function["arguments"])
365
+ if "arguments" in json_function
366
+ else "{}"
367
+ ),
368
+ },
369
+ type="function",
370
+ )
371
+ )
372
+
373
+ random_id += 1
374
+
375
+ # Blank the message content
376
+ response_content = ""
377
+
378
+ if ollama_finish == "stop": # noqa: SIM102
379
+ # Not a tool call, so let's check if we need to process structured output
380
+ if self._response_format and response_content:
381
+ try:
382
+ parsed_response = self._convert_json_response(response_content)
383
+ response_content = _format_json_response(parsed_response, response_content)
384
+ except ValueError as e:
385
+ response_content = str(e)
386
+ else:
387
+ raise RuntimeError("Failed to get response from Ollama.")
388
+
389
+ # Convert response to AG2 response
390
+ message = ChatCompletionMessage(
391
+ role="assistant",
392
+ content=response_content,
393
+ function_call=None,
394
+ tool_calls=tool_calls,
395
+ )
396
+ choices = [Choice(finish_reason=ollama_finish, index=0, message=message)]
397
+
398
+ response_oai = ChatCompletion(
399
+ id=response_id,
400
+ model=ollama_params["model"],
401
+ created=int(time.time()),
402
+ object="chat.completion",
403
+ choices=choices,
404
+ usage=CompletionUsage(
405
+ prompt_tokens=prompt_tokens,
406
+ completion_tokens=completion_tokens,
407
+ total_tokens=total_tokens,
408
+ ),
409
+ cost=0, # Local models, FREE!
410
+ )
411
+
412
+ return response_oai
413
+
414
+ def oai_messages_to_ollama_messages(self, messages: list[dict[str, Any]], tools: list) -> list[dict[str, Any]]:
415
+ """Convert messages from OAI format to Ollama's format.
416
+ We correct for any specific role orders and types, and convert tools to messages (as Ollama can't use tool messages)
417
+ """
418
+ ollama_messages = copy.deepcopy(messages)
419
+
420
+ # Remove the name field
421
+ for message in ollama_messages:
422
+ if "name" in message:
423
+ message.pop("name", None)
424
+
425
+ # Having a 'system' message on the end does not work well with Ollama, so we change it to 'user'
426
+ # 'system' messages on the end are typical of the summarisation message: summary_method="reflection_with_llm"
427
+ if len(ollama_messages) > 1 and ollama_messages[-1]["role"] == "system":
428
+ ollama_messages[-1]["role"] = "user"
429
+
430
+ # Process messages for tool calling manually
431
+ if tools is not None and not self._native_tool_calls:
432
+ # 1. We need to append instructions to the starting system message on function calling
433
+ # 2. If we have not yet called tools we append "step 1 instruction" to the latest user message
434
+ # 3. If we have already called tools we append "step 2 instruction" to the latest user message
435
+
436
+ have_tool_calls = False
437
+ have_tool_results = False
438
+ last_tool_result_index = -1
439
+
440
+ for i, message in enumerate(ollama_messages):
441
+ if "tool_calls" in message:
442
+ have_tool_calls = True
443
+ if "tool_call_id" in message:
444
+ have_tool_results = True
445
+ last_tool_result_index = i
446
+
447
+ tool_result_is_last_msg = have_tool_results and last_tool_result_index == len(ollama_messages) - 1
448
+
449
+ if ollama_messages[0]["role"] == "system":
450
+ manual_instruction = self._manual_tool_call_instruction
451
+
452
+ # Build a string of the functions available
453
+ functions_string = ""
454
+ for function in tools:
455
+ functions_string += f"""\n{function}\n"""
456
+
457
+ # Replace single quotes with double questions - Not sure why this helps the LLM perform
458
+ # better, but it seems to. Monitor and remove if not necessary.
459
+ functions_string = functions_string.replace("'", '"')
460
+
461
+ manual_instruction = manual_instruction.replace("[FUNCTIONS_LIST]", functions_string)
462
+
463
+ # Update the system message with the instructions and functions
464
+ ollama_messages[0]["content"] = ollama_messages[0]["content"] + manual_instruction.rstrip()
465
+
466
+ # If we are still in the function calling or evaluating process, append the steps instruction
467
+ if (not have_tool_calls or tool_result_is_last_msg) and ollama_messages[0]["role"] == "system":
468
+ # NOTE: we require a system message to exist for the manual steps texts
469
+ # Append the manual step instructions
470
+ content_to_append = (
471
+ self._manual_tool_call_step1 if not have_tool_results else self._manual_tool_call_step2
472
+ )
473
+
474
+ if content_to_append != "":
475
+ # Append the relevant tool call instruction to the latest user message
476
+ if ollama_messages[-1]["role"] == "user":
477
+ ollama_messages[-1]["content"] = ollama_messages[-1]["content"] + content_to_append
478
+ else:
479
+ ollama_messages.append({"role": "user", "content": content_to_append})
480
+
481
+ # Convert tool call and tool result messages to normal text messages for Ollama
482
+ for i, message in enumerate(ollama_messages):
483
+ if "tool_calls" in message:
484
+ # Recommended tool calls
485
+ content = "Run the following function(s):"
486
+ for tool_call in message["tool_calls"]:
487
+ content = content + "\n" + str(tool_call)
488
+ ollama_messages[i] = {"role": "assistant", "content": content}
489
+ if "tool_call_id" in message:
490
+ # Executed tool results
491
+ message["result"] = message["content"]
492
+ del message["content"]
493
+ del message["role"]
494
+ content = "The following function was run: " + str(message)
495
+ ollama_messages[i] = {"role": "user", "content": content}
496
+
497
+ # As we are changing messages, let's merge if they have two user messages on the end and the last one is tool call step instructions
498
+ if (
499
+ len(ollama_messages) >= 2
500
+ and not self._native_tool_calls
501
+ and ollama_messages[-2]["role"] == "user"
502
+ and ollama_messages[-1]["role"] == "user"
503
+ and (
504
+ ollama_messages[-1]["content"] == self._manual_tool_call_step1
505
+ or ollama_messages[-1]["content"] == self._manual_tool_call_step2
506
+ )
507
+ ):
508
+ ollama_messages[-2]["content"] = ollama_messages[-2]["content"] + ollama_messages[-1]["content"]
509
+ del ollama_messages[-1]
510
+
511
+ # Ensure the last message is a user / system message, if not, add a user message
512
+ if ollama_messages[-1]["role"] != "user" and ollama_messages[-1]["role"] != "system":
513
+ ollama_messages.append({"role": "user", "content": "Please continue."})
514
+
515
+ return ollama_messages
516
+
517
+ def _convert_json_response(self, response: str) -> Any:
518
+ """Extract and validate JSON response from the output for structured outputs.
519
+
520
+ Args:
521
+ response (str): The response from the API.
522
+
523
+ Returns:
524
+ Any: The parsed JSON response.
525
+ """
526
+ if not self._response_format:
527
+ return response
528
+
529
+ try:
530
+ # Parse JSON and validate against the Pydantic model if Pydantic model was provided
531
+ if isinstance(self._response_format, dict):
532
+ return response
533
+ else:
534
+ return self._response_format.model_validate_json(response)
535
+ except Exception as e:
536
+ raise ValueError(f"Failed to parse response as valid JSON matching the schema for Structured Output: {e!s}")
537
+
538
+
539
+ def _format_json_response(response: Any, original_answer: str) -> str:
540
+ """Formats the JSON response for structured outputs using the format method if it exists."""
541
+ return response.format() if isinstance(response, FormatterProtocol) else original_answer
542
+
543
+
544
+ @require_optional_import("fix_busted_json", "ollama")
545
+ def response_to_tool_call(response_string: str) -> Any:
546
+ """Attempts to convert the response to an object, aimed to align with function format `[{},{}]`"""
547
+ # We try and detect the list[dict[str, Any]] format:
548
+ # Pattern 1 is [{},{}]
549
+ # Pattern 2 is {} (without the [], so could be a single function call)
550
+ patterns = [r"\[[\s\S]*?\]", r"\{[\s\S]*\}"]
551
+
552
+ for i, pattern in enumerate(patterns):
553
+ # Search for the pattern in the input string
554
+ matches = re.findall(pattern, response_string.strip())
555
+
556
+ for match in matches:
557
+ # It has matched, extract it and load it
558
+ json_str = match.strip()
559
+ data_object = None
560
+
561
+ try:
562
+ # Attempt to convert it as is
563
+ data_object = json.loads(json_str)
564
+ except Exception:
565
+ try:
566
+ # If that fails, attempt to repair it
567
+
568
+ if i == 0:
569
+ # Enclose to a JSON object for repairing, which is restored upon fix
570
+ fixed_json = repair_json("{'temp':" + json_str + "}")
571
+ data_object = json.loads(fixed_json)
572
+ data_object = data_object["temp"]
573
+ else:
574
+ fixed_json = repair_json(json_str)
575
+ data_object = json.loads(fixed_json)
576
+ except json.JSONDecodeError as e:
577
+ if e.msg == "Invalid \\escape":
578
+ # Handle Mistral/Mixtral trying to escape underlines with \\
579
+ try:
580
+ json_str = json_str.replace("\\_", "_")
581
+ if i == 0:
582
+ fixed_json = repair_json("{'temp':" + json_str + "}")
583
+ data_object = json.loads(fixed_json)
584
+ data_object = data_object["temp"]
585
+ else:
586
+ fixed_json = repair_json("{'temp':" + json_str + "}")
587
+ data_object = json.loads(fixed_json)
588
+ except Exception:
589
+ pass
590
+ except Exception:
591
+ pass
592
+
593
+ if data_object is not None:
594
+ data_object = _object_to_tool_call(data_object)
595
+
596
+ if data_object is not None:
597
+ return data_object
598
+
599
+ # There's no tool call in the response
600
+ return None
601
+
602
+
603
+ def _object_to_tool_call(data_object: Any) -> list[dict[str, Any]]:
604
+ """Attempts to convert an object to a valid tool call object List[Dict] and returns it, if it can, otherwise None"""
605
+ # If it's a dictionary and not a list then wrap in a list
606
+ if isinstance(data_object, dict):
607
+ data_object = [data_object]
608
+
609
+ # Validate that the data is a list of dictionaries
610
+ if isinstance(data_object, list) and all(isinstance(item, dict) for item in data_object):
611
+ # Perfect format, a list of dictionaries
612
+
613
+ # Check that each dictionary has at least 'name', optionally 'arguments' and no other keys
614
+ is_invalid = False
615
+ for item in data_object:
616
+ if not is_valid_tool_call_item(item):
617
+ is_invalid = True
618
+ break
619
+
620
+ # All passed, name and (optionally) arguments exist for all entries.
621
+ if not is_invalid:
622
+ return data_object
623
+ elif isinstance(data_object, list):
624
+ # If it's a list but the items are not dictionaries, check if they are strings that can be converted to dictionaries
625
+ data_copy = data_object.copy()
626
+ is_invalid = False
627
+ for i, item in enumerate(data_copy):
628
+ try:
629
+ new_item = ast.literal_eval(item)
630
+ if isinstance(new_item, dict):
631
+ if is_valid_tool_call_item(new_item):
632
+ data_object[i] = new_item
633
+ else:
634
+ is_invalid = True
635
+ break
636
+ else:
637
+ is_invalid = True
638
+ break
639
+ except Exception:
640
+ is_invalid = True
641
+ break
642
+
643
+ if not is_invalid:
644
+ return data_object
645
+
646
+ return None
647
+
648
+
649
+ def is_valid_tool_call_item(call_item: dict) -> bool:
650
+ """Check that a dictionary item has at least 'name', optionally 'arguments' and no other keys to match a tool call JSON"""
651
+ if "name" not in call_item or not isinstance(call_item["name"], str):
652
+ return False
653
+
654
+ if set(call_item.keys()) - {"name", "arguments"}: # noqa: SIM103
655
+ return False
656
+
657
+ return True