Extracts metadata from a function docstring, in preparation for sending it to an LLM as a tool. Args: func: The function to extract documentation from. style: The style of the docstring to use for parsing. If not provided, we will attempt to auto-detect the styl
(
func: Callable[..., Any], style: DocstringStyle | None = None
)
| 146 | |
| 147 | |
| 148 | def generate_func_documentation( |
| 149 | func: Callable[..., Any], style: DocstringStyle | None = None |
| 150 | ) -> FuncDocumentation: |
| 151 | """ |
| 152 | Extracts metadata from a function docstring, in preparation for sending it to an LLM as a tool. |
| 153 | |
| 154 | Args: |
| 155 | func: The function to extract documentation from. |
| 156 | style: The style of the docstring to use for parsing. If not provided, we will attempt to |
| 157 | auto-detect the style. |
| 158 | |
| 159 | Returns: |
| 160 | A FuncDocumentation object containing the function's name, description, and parameter |
| 161 | descriptions. |
| 162 | """ |
| 163 | name = func.__name__ |
| 164 | doc = inspect.getdoc(func) |
| 165 | if not doc: |
| 166 | return FuncDocumentation(name=name, description=None, param_descriptions=None) |
| 167 | |
| 168 | with _suppress_griffe_logging(): |
| 169 | docstring = Docstring(doc, lineno=1, parser=style or _detect_docstring_style(doc)) |
| 170 | parsed = docstring.parse() |
| 171 | |
| 172 | description: str | None = next( |
| 173 | (section.value for section in parsed if section.kind == DocstringSectionKind.text), None |
| 174 | ) |
| 175 | |
| 176 | param_descriptions: dict[str, str] = { |
| 177 | param.name: param.description |
| 178 | for section in parsed |
| 179 | if section.kind == DocstringSectionKind.parameters |
| 180 | for param in section.value |
| 181 | } |
| 182 | |
| 183 | return FuncDocumentation( |
| 184 | name=func.__name__, |
| 185 | description=description, |
| 186 | param_descriptions=param_descriptions or None, |
| 187 | ) |
| 188 | |
| 189 | |
| 190 | def _strip_annotated(annotation: Any) -> tuple[Any, tuple[Any, ...]]: |