Skip to content

Commit 23a7060

Browse files
📝 Add docstrings to rag
Docstrings generation was requested by @JarbasAl. * #37 (comment) The following files were modified: * `ovos_solver_openai_persona/engines.py` * `ovos_solver_openai_persona/rag.py` * `setup.py`
1 parent c035edc commit 23a7060

File tree

3 files changed

+127
-135
lines changed

3 files changed

+127
-135
lines changed

ovos_solver_openai_persona/engines.py

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ def __init__(self, config=None,
101101
enable_cache: bool = False,
102102
internal_lang: Optional[str] = None):
103103
"""
104-
Initializes the OpenAIChatCompletionsSolver with API configuration, memory settings, and system prompt.
105-
106-
Raises:
107-
ValueError: If the API key is not provided in the configuration.
108-
"""
104+
Initialize an OpenAIChatCompletionsSolver instance with API configuration, conversation memory settings, and system prompt.
105+
106+
Raises:
107+
ValueError: If the API key is missing from the configuration.
108+
"""
109109
super().__init__(config=config, translator=translator,
110110
detector=detector, priority=priority,
111111
enable_tx=enable_tx, enable_cache=enable_cache,
@@ -131,16 +131,16 @@ def __init__(self, config=None,
131131
# OpenAI API integration
132132
def _do_api_request(self, messages):
133133
"""
134-
Sends a chat completion request to the OpenAI API and returns the assistant's reply.
134+
Send a chat completion request to the OpenAI API using the provided conversation history and return the assistant's reply.
135135
136-
Args:
137-
messages: A list of message dictionaries representing the conversation history.
136+
Parameters:
137+
messages (list): Conversation history as a list of message dictionaries.
138138
139139
Returns:
140-
The content of the assistant's reply as a string.
140+
str: The assistant's reply content.
141141
142142
Raises:
143-
RequestException: If the OpenAI API returns an error in the response.
143+
RequestException: If the OpenAI API response contains an error.
144144
"""
145145
s = requests.Session()
146146
headers = {
@@ -243,14 +243,14 @@ def get_chat_history(self, system_prompt=None):
243243

244244
def get_messages(self, utt, system_prompt=None) -> MessageList:
245245
"""
246-
Builds a list of chat messages including the system prompt, recent conversation history, and the current user utterance.
246+
Constructs a list of chat messages for the API, including the system prompt, recent conversation history, and the current user utterance.
247247
248-
Args:
249-
utt: The current user input to be appended as the latest message.
248+
Parameters:
249+
utt: The current user input to be added as the latest message.
250250
system_prompt: Optional system prompt to use as the initial message.
251251
252252
Returns:
253-
A list of message dictionaries representing the chat context for the API.
253+
A list of message dictionaries representing the chat context.
254254
"""
255255
messages = self.get_chat_history(system_prompt)
256256
messages.append({"role": "user", "content": utt})
@@ -261,18 +261,18 @@ def continue_chat(self, messages: MessageList,
261261
lang: Optional[str],
262262
units: Optional[str] = None) -> Optional[str]:
263263
"""
264-
Generates a chat response using the provided message history and updates memory if enabled.
265-
266-
If the first message is not a system prompt, prepends the system prompt. Processes the API response and returns a cleaned answer, or None if the answer is empty or only punctuation/underscores. Updates internal memory with the latest question and answer if memory is enabled.
267-
268-
Args:
269-
messages: List of chat messages with 'role' and 'content' keys.
270-
lang: Optional language code for the response.
271-
units: Optional unit system for numerical values.
272-
273-
Returns:
274-
The generated response as a string, or None if no valid response is produced.
275-
"""
264+
Generate a chat response based on the provided message history and update conversation memory if enabled.
265+
266+
If the first message is not a system prompt, prepends the system prompt. Returns a cleaned response string, or None if the response is empty or contains only punctuation or underscores. Updates internal memory with the latest user message and answer when memory is enabled.
267+
268+
Parameters:
269+
messages (MessageList): List of chat messages, each with 'role' and 'content' keys.
270+
lang (Optional[str]): Language code for the response.
271+
units (Optional[str]): Unit system for numerical values.
272+
273+
Returns:
274+
Optional[str]: The generated response string, or None if no valid response is produced.
275+
"""
276276
if messages[0]["role"] != "system":
277277
messages = [{"role": "system", "content": self.system_prompt }] + messages
278278
response = self._do_api_request(messages)
@@ -288,16 +288,10 @@ def stream_chat_utterances(self, messages: MessageList,
288288
lang: Optional[str] = None,
289289
units: Optional[str] = None) -> Iterable[str]:
290290
"""
291-
Stream utterances for the given chat history as they become available.
292-
293-
Args:
294-
messages: The chat messages.
295-
lang (Optional[str]): Optional language code. Defaults to None.
296-
units (Optional[str]): Optional units for the query. Defaults to None.
297-
298-
Returns:
299-
Iterable[str]: An iterable of utterances.
300-
"""
291+
Streams partial assistant responses for a chat conversation as they are generated.
292+
293+
Yields post-processed segments of the assistant's reply, emitting each chunk when a sentence or phrase boundary is detected. If conversation memory is enabled, updates the internal memory with the accumulating answer.
294+
"""
301295
if messages[0]["role"] != "system":
302296
messages = [{"role": "system", "content": self.system_prompt }] + messages
303297
answer = ""
@@ -322,16 +316,16 @@ def stream_utterances(self, query: str,
322316
lang: Optional[str] = None,
323317
units: Optional[str] = None) -> Iterable[str]:
324318
"""
325-
Stream utterances for the given query as they become available.
326-
327-
Args:
328-
query (str): The query text.
329-
lang (Optional[str]): Optional language code. Defaults to None.
330-
units (Optional[str]): Optional units for the query. Defaults to None.
331-
332-
Returns:
333-
Iterable[str]: An iterable of utterances.
334-
"""
319+
Yields partial responses for a query as they are generated by the chat completions API.
320+
321+
Parameters:
322+
query (str): The user query to send to the chat model.
323+
lang (Optional[str]): Language code for the response, if applicable.
324+
units (Optional[str]): Units relevant to the query, if applicable.
325+
326+
Returns:
327+
Iterable[str]: An iterator yielding segments of the model's response as they become available.
328+
"""
335329
messages = self.get_messages(query)
336330
yield from self.stream_chat_utterances(messages, lang, units)
337331

0 commit comments

Comments
 (0)