r"""Generates a docstring for a given function code using LLM. This function leverages a language model to generate a PEP 8/PEP 257-compliant docstring for a provided Python function. If no model is supplied, a default gpt-4o-mini is used. Args: code (str): The source code
(
code: str,
model: Optional[BaseModelBackend] = None,
)
| 220 | |
| 221 | |
| 222 | def generate_docstring( |
| 223 | code: str, |
| 224 | model: Optional[BaseModelBackend] = None, |
| 225 | ) -> str: |
| 226 | r"""Generates a docstring for a given function code using LLM. |
| 227 | |
| 228 | This function leverages a language model to generate a |
| 229 | PEP 8/PEP 257-compliant docstring for a provided Python function. |
| 230 | If no model is supplied, a default gpt-4o-mini is used. |
| 231 | |
| 232 | Args: |
| 233 | code (str): The source code of the function. |
| 234 | model (Optional[BaseModelBackend]): An optional language model backend |
| 235 | instance. If not provided, a default gpt-4o-mini is used. |
| 236 | |
| 237 | Returns: |
| 238 | str: The generated docstring. |
| 239 | """ |
| 240 | |
| 241 | from camel.agents import ChatAgent |
| 242 | |
| 243 | # Create the docstring prompt |
| 244 | docstring_prompt = textwrap.dedent( |
| 245 | """\ |
| 246 | **Role**: Generate professional Python docstrings conforming to PEP 8/PEP 257. |
| 247 | |
| 248 | **Requirements**: |
| 249 | - Use appropriate format: reST, Google, or NumPy, as needed. |
| 250 | - Include parameters, return values, and exceptions. |
| 251 | - Reference any existing docstring in the function and retain useful information. |
| 252 | |
| 253 | **Input**: Python function. |
| 254 | |
| 255 | **Output**: Docstring content (plain text, no code markers). |
| 256 | |
| 257 | **Example:** |
| 258 | |
| 259 | Input: |
| 260 | ```python |
| 261 | def add(a: int, b: int) -> int: |
| 262 | return a + b |
| 263 | ``` |
| 264 | |
| 265 | Output: |
| 266 | Adds two numbers. |
| 267 | Args: |
| 268 | a (int): The first number. |
| 269 | b (int): The second number. |
| 270 | |
| 271 | Returns: |
| 272 | int: The sum of the two numbers. |
| 273 | |
| 274 | **Task**: Generate a docstring for the function below. |
| 275 | """ # noqa: E501 |
| 276 | ) |
| 277 | # Initialize assistant with system message and model |
| 278 | assistant_sys_msg = "You are a helpful assistant." |
| 279 | docstring_assistant = ChatAgent(assistant_sys_msg, model=model) |
no test coverage detected