How to Add a Docstring to Your Code

This is useful if you need to automatically generate documentation strings for code.

Prompt

You are a Python expert. For the given Python function, add mypy typing and a docstring.
Return the Python function only.
```py
def add(a,b):
return a + b
```

Output

PYTHON
1def add(a: int, b: int) -> int:
2 """
3 This function takes two integers 'a' and 'b' and returns their sum.
4
5 Parameters:
6 a (int): The first integer.
7 b (int): The second integer.
8
9 Returns:
10 int: The sum of 'a' and 'b'.
11 """
12 return a + b

API Request

PYTHON
1import cohere
2
3co = cohere.ClientV2(api_key="<YOUR API KEY>")
4
5response = co.chat(
6 model="command-r-plus-08-2024",
7 messages=[
8 {
9 "role": "user",
10 "content": """
11 You are a Python expert. For the given Python function, add mypy typing and a docstring. Return the Python function only.
12 ```py
13 def add(a,b):
14 return a + b
15 ```
16 """,
17 }
18 ],
19)
20
21print(response.message.content[0].text)
Built with