From bd61e4436898e3ac99bdba60dcbd12b85181f49b Mon Sep 17 00:00:00 2001 From: Roger Gonzalez Date: Sat, 13 May 2023 20:42:45 -0300 Subject: [PATCH] Added documentation --- app.py | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 1943e45..ebba4a9 100644 --- a/app.py +++ b/app.py @@ -14,12 +14,25 @@ app = Flask(__name__) openai.api_key = os.getenv("OPENAI_KEY") -def format_message(message): +def format_message(message: str) -> list: + """ + Formats the message to send to chatgpt + + Args: + message: The message to send to chatgpt + + Returns: + The formatted message in a list + + Raises: + None + """ + return [ { "role": "system", "content": ( - "You are a very knowledgeable person," " with random knowledge for everything the humanity has ever done" + "You are a very knowledgeable person with random knowledge for everything the humanity has ever done" ), }, {"role": "user", "content": message}, @@ -27,6 +40,19 @@ def format_message(message): def extract_html_from_chatgpt_response(returned_string: str) -> Union[str, None]: + """ + Extracts the HTML from the chatgpt response + + Args: + returned_string: The string returned by chatgpt + + Returns: + The HTML if it is valid, None otherwise + + Raises: + None + """ + validator = HTMLParser(strict=True) try: validator.parse(returned_string) @@ -46,6 +72,19 @@ def extract_html_from_chatgpt_response(returned_string: str) -> Union[str, None] def get_chatgpt_response(message: str) -> str: + """ + Gets the response from chatgpt + + Args: + message: The message to send to chatgpt + + Returns: + The HTML response from chatgpt + + Raises: + None + """ + try: completion = openai.ChatCompletion.create( model="gpt-3.5-turbo",