# Credit: Some test cases were auto-generated using Cursor AI.# Additional modifications and refinements were made for completeness and correctness.fromopenaiimportOpenAIfromdotenvimportload_dotenvimportos
[docs]deffill_docstring_with_ai(docstring_template:str,function_source:str)->str:""" Generates a detailed docstring for a given function source by filling in a provided template using OpenAI's language model. Args: docstring_template (str): The docstring template with placeholders for descriptions. function_source (str): The source code of the function to extract context. Returns: str: The completed docstring with detailed descriptions. Raises: ValueError: If either `docstring_template` or `function_source` is empty. RuntimeError: If the API request fails or returns an unexpected response. """ifnotdocstring_template:raiseValueError("The docstring template cannot be empty.")ifnotfunction_source:raiseValueError("The function source cannot be empty.")# Load environment variablesload_dotenv()# Set OpenAI API keyclient=OpenAI(api_key=os.getenv("OPENAI_API_KEY"))prompt=("You are an expert Python programmer. Given the following function source code and a ""docstring template, replace the placeholders with appropriate descriptions for parameters, ""return values, and examples. Follow the NumPy/Google format for writing docstrings.\n\n"f"Docstring Template:\n{docstring_template}\n\nFunction Source:\n{function_source}")try:response=client.chat.completions.create(model="gpt-4",messages=[{"role":"system","content":"You are an expert Python developer writing docstrings."},{"role":"user","content":prompt},])returnresponse.choices[0].message.contentexceptExceptionase:raiseRuntimeError(f"An unexpected error occurred")