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.

README.md 9.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # AutoGen Python Development Guide
  2. [![Docs (dev)](https://img.shields.io/badge/Docs-dev-blue)](https://microsoft.github.io/autogen/dev/)
  3. [![Docs (latest release)](https://img.shields.io/badge/Docs-latest%20release-blue)](https://microsoft.github.io/autogen/dev/)
  4. [![PyPi autogen-core](https://img.shields.io/badge/PyPi-autogen--core-blue?logo=pypi)](https://pypi.org/project/autogen-core/) [![PyPi autogen-agentchat](https://img.shields.io/badge/PyPi-autogen--agentchat-blue?logo=pypi)](https://pypi.org/project/autogen-agentchat/) [![PyPi autogen-ext](https://img.shields.io/badge/PyPi-autogen--ext-blue?logo=pypi)](https://pypi.org/project/autogen-ext/)
  5. This directory works as a single `uv` workspace containing all project packages, including:
  6. - `packages/autogen-core`: interface definitions and reference implementations of agent runtime, model, tool, workbench, memory, tracing.
  7. - `packages/autogen-agentchat`: single and multi-agent workflows built on top of `autogen-core`.
  8. - `packages/autogen-ext`: implementations for ecosystem integrations. For example, `autogen-ext[openai]` provides the OpenAI model client.
  9. - `packages/autogen-studio`: a web-based IDE for building and running AutoGen agents.
  10. ## Migrating from 0.2.x?
  11. Please refer to the [migration guide](./migration_guide.md) for how to migrate your code from 0.2.x to 0.4.x.
  12. ## Quick Start
  13. **TL;DR**, run all checks with:
  14. ```sh
  15. uv sync --all-extras
  16. source .venv/bin/activate
  17. poe check
  18. ```
  19. ## Setup
  20. `uv` is a package manager that assists in creating the necessary environment and installing packages to run AutoGen.
  21. - [Install `uv`](https://docs.astral.sh/uv/getting-started/installation/).
  22. To upgrade `uv` to the latest version, run:
  23. ```sh
  24. uv self update
  25. ```
  26. ## Virtual Environment
  27. During development, you may need to test changes made to any of the packages.\
  28. To do so, create a virtual environment where the AutoGen packages are installed based on the current state of the directory.\
  29. Run the following commands at the root level of the Python directory:
  30. ```sh
  31. uv sync --all-extras
  32. source .venv/bin/activate
  33. ```
  34. - `uv sync --all-extras` will create a `.venv` directory at the current level and install packages from the current directory along with any other dependencies. The `all-extras` flag adds optional dependencies.
  35. - `source .venv/bin/activate` activates the virtual environment.
  36. ## Common Tasks
  37. To create a pull request (PR), ensure the following checks are met. You can run each check individually:
  38. - Format: `poe format`
  39. - Lint: `poe lint`
  40. - Test: `poe test`
  41. - Mypy: `poe mypy`
  42. - Pyright: `poe pyright`
  43. - Build docs: `poe docs-build`
  44. - Check docs: `poe docs-check`
  45. - Clean docs: `poe docs-clean`
  46. - Check code blocks in API references: `poe docs-check-examples`
  47. - Auto rebuild+serve docs: `poe docs-serve`
  48. - Check samples in `python/samples`: `poe samples-code-check`
  49. Alternatively, you can run all the checks with:
  50. - `poe check`
  51. > [!NOTE]
  52. > These need to be run in the virtual environment.
  53. ## Syncing Dependencies
  54. When you pull new changes, you may need to update the dependencies.
  55. To do so, first make sure you are in the virtual environment, and then in the `python` directory, run:
  56. ```sh
  57. uv sync --all-extras
  58. ```
  59. This will update the dependencies in the virtual environment.
  60. ## Building Documentation
  61. The documentation source directory is located at `docs/src/`.
  62. To build the documentation, run this from the root of the Python directory:
  63. ```sh
  64. poe docs-build
  65. ```
  66. To serve the documentation locally, run:
  67. ```sh
  68. poe docs-serve
  69. ```
  70. When you make changes to the doc strings or add new modules, you may need to
  71. refresh the API references in the documentation by first cleaning the docs and
  72. then building them again:
  73. ```sh
  74. poe docs-clean # This will remove the build directory and the reference directory
  75. poe docs-build # This will rebuild the documentation from scratch
  76. ```
  77. ## Writing Documentation
  78. When you add a new public class or function, you should always add a docstring
  79. to it. The docstring should follow the
  80. [Google style](https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings) layout
  81. and the Sphinx RST format for Python docstrings.
  82. The docstring for a public class or function should include:
  83. - A short description of the class or function at the beginning immediately after the `"""`.
  84. - A longer description if necessary, explaining the purpose and usage.
  85. - A list of arguments with their types and descriptions, using the `Args` section.
  86. Each argument should be listed with its name, type, and a brief description.
  87. - A description of the return value and its type, using the `Returns` section.
  88. If the function does not return anything, you can omit this section.
  89. - A list of exceptions that the function may raise, with descriptions,
  90. using the `Raises` section. This is optional but recommended if the function can raise exceptions that users should be aware of.
  91. - Examples of how to use the class or function, using the `Examples` section,
  92. and formatted using `.. code-block:: python` directive. Optionally, also include the output of the example using
  93. `.. code-block:: text` directive.
  94. Here is an example of a docstring for `McpWorkbench` class:
  95. ```python
  96. class McpWorkbench(Workbench, Component[McpWorkbenchConfig]):
  97. """A workbench that wraps an MCP server and provides an interface
  98. to list and call tools provided by the server.
  99. This workbench should be used as a context manager to ensure proper
  100. initialization and cleanup of the underlying MCP session.
  101. Args:
  102. server_params (McpServerParams): The parameters to connect to the MCP server.
  103. This can be either a :class:`StdioServerParams` or :class:`SseServerParams`.
  104. tool_overrides (Optional[Dict[str, ToolOverride]]): Optional mapping of original tool
  105. names to override configurations for name and/or description. This allows
  106. customizing how server tools appear to consumers while maintaining the underlying
  107. tool functionality.
  108. Raises:
  109. ValueError: If there are conflicts in tool override names.
  110. Examples:
  111. Here is a simple example of how to use the workbench with a `mcp-server-fetch` server:
  112. .. code-block:: python
  113. import asyncio
  114. from autogen_ext.tools.mcp import McpWorkbench, StdioServerParams
  115. async def main() -> None:
  116. params = StdioServerParams(
  117. command="uvx",
  118. args=["mcp-server-fetch"],
  119. read_timeout_seconds=60,
  120. )
  121. # You can also use `start()` and `stop()` to manage the session.
  122. async with McpWorkbench(server_params=params) as workbench:
  123. tools = await workbench.list_tools()
  124. print(tools)
  125. result = await workbench.call_tool(tools[0]["name"], {"url": "https://github.com/"})
  126. print(result)
  127. asyncio.run(main())
  128. ```
  129. The code blocks with `.. code-block:: python` is checked by the `docs-check-examples` task using Pyright,
  130. so make sure the code is valid. Running the code as a script and checking it using `pyright`
  131. is a good way to ensure the code examples are correct.
  132. When you reference a class, method, or function in the docstring, you should always
  133. use the `:class:`, `:meth:`, or `:func:` directive to create a link to the class or function.
  134. Always use the fully qualified name of the class or function, including the package name, but
  135. prefix it with a `~` for shorter rendering in the documentation.
  136. For example, if you are referencing the `AssistantAgent` class in the `autogen-agentchat` package,
  137. you should write it as `:class:~autogen_agentchat.AssistantAgent`.
  138. For a public data class, including those that are Pydantic models, you should also include docstrings
  139. for each field in the class.
  140. ## Writing Tests
  141. When you add a new public class or function, you should also always add tests for it.
  142. We track test coverage and aim for not reducing the coverage percentage with new changes.
  143. We use `pytest` for testing, and you should always use fixtures to set up the test dependencies.
  144. Use mock objects to simulate dependencies and avoid making real API calls or database queries in tests.
  145. See existing tests for examples of how to use fixtures and mocks.
  146. For model clients, use `autogen_ext.models.replay.ReplayChatCompletionClient` as a
  147. drop-in replacement for the model client to simulate responses without making real API calls.
  148. When certain tests requires interaction with actual model APIs or other external services,
  149. you should configure the tests to be skipped if the required services are not available.
  150. For example, if you are testing a model client that requires an OpenAI API key,
  151. you can use the `pytest.mark.skipif` decorator to skip the test if the environment variable for the API key is not set.
  152. ## Creating a New Package
  153. To create a new package, similar to `autogen-core` or `autogen-chat`, use the following:
  154. ```sh
  155. uv sync --python 3.12
  156. source .venv/bin/activate
  157. cookiecutter ./templates/new-package/
  158. ```