Skip to content

Commit 5cdab44

Browse files
authored
Add docstrings (sinaptik-ai#20)
Add docstrings
2 parents ce303ee + c7449db commit 5cdab44

File tree

3 files changed

+103
-17
lines changed

3 files changed

+103
-17
lines changed

pandasai/exceptions.py

+30-5
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,45 @@
22

33

44
class APIKeyNotFoundError(Exception):
5-
"""Raised when the API key is not defined/declared."""
5+
"""
6+
Raised when the API key is not defined/declared.
7+
8+
Args:
9+
Exception (Exception): APIKeyNotFoundError
10+
"""
611

712

813
class LLMNotFoundError(Exception):
9-
"""Raised when the LLM is not provided"""
14+
"""
15+
Raised when the LLM is not provided.
16+
17+
Args:
18+
Exception (Exception): LLMNotFoundError
19+
"""
1020

1121

1222
class NoCodeFoundError(Exception):
13-
"""Raised when no code is found in the response"""
23+
"""
24+
Raised when no code is found in the response.
25+
26+
Args:
27+
Exception (Exception): NoCodeFoundError
28+
"""
1429

1530

1631
class MethodNotImplementedError(Exception):
17-
"""Raised when a method is not implemented"""
32+
"""
33+
Raised when a method is not implemented.
34+
35+
Args:
36+
Exception (Exception): MethodNotImplementedError
37+
"""
1838

1939

2040
class UnsupportedOpenAIModelError(Exception):
21-
"""Raised when an unsupported OpenAI model is used"""
41+
"""
42+
Raised when an unsupported OpenAI model is used.
43+
44+
Args:
45+
Exception (Exception): UnsupportedOpenAIModelError
46+
"""

pandasai/llm/base.py

+44-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ class LLM:
1717

1818
@property
1919
def type(self) -> str:
20-
"""Return type of llm."""
20+
"""
21+
Return type of LLM.
22+
23+
Raises:
24+
APIKeyNotFoundError: Type has not been implemented
25+
26+
Returns:
27+
str: Type of LLM a string
28+
"""
2129
raise APIKeyNotFoundError("Type has not been implemented")
2230

2331
def _remove_imports(self, code: str) -> str:
@@ -32,12 +40,16 @@ def _remove_imports(self, code: str) -> str:
3240
return astor.to_source(new_tree)
3341

3442
def _polish_code(self, code: str) -> str:
35-
"""Polish the code:
36-
- remove the leading "python" or "py"
37-
- remove the imports
38-
- remove trailing spaces and new lines
3943
"""
44+
Polish the code by removing the leading "python" or "py", \
45+
removing the imports and removing trailing spaces and new lines.
4046
47+
Args:
48+
code (str): Code
49+
50+
Returns:
51+
str: Polished code
52+
"""
4153
if re.match(r"^(python|py)", code):
4254
code = re.sub(r"^(python|py)", "", code)
4355
if re.match(r"^`.*`$", code):
@@ -54,8 +66,19 @@ def _is_python_code(self, string):
5466
return False
5567

5668
def _extract_code(self, response: str, separator: str = "```") -> str:
57-
"""Extract the code from the response"""
69+
"""
70+
Extract the code from the response.
71+
72+
Args:
73+
response (str): Response
74+
separator (str, optional): Separator. Defaults to "```".
5875
76+
Raises:
77+
NoCodeFoundError: No code found in the response
78+
79+
Returns:
80+
str: Extracted code from the response
81+
"""
5982
code = response
6083
if len(response.split(separator)) > 1:
6184
code = response.split(separator)[1]
@@ -67,12 +90,24 @@ def _extract_code(self, response: str, separator: str = "```") -> str:
6790

6891
return code
6992

70-
def call(self, instruction: str, value: str) -> str:
71-
"""Execute the llm with the given prompt"""
93+
def call(self, instruction: str, value: str) -> None:
94+
"""
95+
Execute the LLM with given prompt.
96+
97+
Args:
98+
instruction (str): Prompt
99+
value (str): Value
72100
101+
Raises:
102+
MethodNotImplementedError: Call method has not been implemented
103+
"""
73104
raise MethodNotImplementedError("Call method has not been implemented")
74105

75106
def generate_code(self, instruction: str, prompt: str) -> str:
76-
"""Generate the code based on the instruction and the prompt"""
107+
"""
108+
Generate the code based on the instruction and the given prompt.
77109
110+
Returns:
111+
str: Code
112+
"""
78113
return self._extract_code(self.call(instruction, prompt))

pandasai/llm/openai.py

+29-3
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,15 @@ def _set_params(self, **kwargs):
5858
setattr(self, key, value)
5959

6060
def completion(self, prompt: str) -> str:
61-
"""Query the completion API"""
61+
"""
62+
Query the completation API
6263
64+
Args:
65+
prompt (str): Prompt
66+
67+
Returns:
68+
str: LLM response
69+
"""
6370
params = {
6471
"model": self.model,
6572
"prompt": prompt,
@@ -78,8 +85,15 @@ def completion(self, prompt: str) -> str:
7885
return response["choices"][0]["text"]
7986

8087
def chat_completion(self, value: str) -> str:
81-
"""Query the chat completion API"""
88+
"""
89+
Query the chat completion API
90+
91+
Args:
92+
value (str): Prompt
8293
94+
Returns:
95+
str: LLM response
96+
"""
8397
params = {
8498
"model": self.model,
8599
"temperature": self.temperature,
@@ -103,7 +117,19 @@ def chat_completion(self, value: str) -> str:
103117
return response["choices"][0]["message"]["content"]
104118

105119
def call(self, instruction: str, value: str) -> str:
106-
"""Call the openAI LLM"""
120+
"""
121+
Call the OpenAI LLM.
122+
123+
Args:
124+
instruction (str): Instruction to pass
125+
value (str): Value to pass
126+
127+
Raises:
128+
UnsupportedOpenAIModelError: Unsupported model
129+
130+
Returns:
131+
str: Response
132+
"""
107133
self.last_prompt = str(instruction) + str(value)
108134

109135
if self.model in self._supported_completion_models:

0 commit comments

Comments
 (0)