Added documentation

This commit is contained in:
Roger Gonzalez 2023-05-13 20:42:45 -03:00
parent 1dcb3cd5d4
commit bd61e44368
Signed by: rogs
GPG Key ID: C7ECE9C6C36EC2E6

43
app.py
View File

@ -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",