Update app.py
Browse files
app.py
CHANGED
@@ -5,24 +5,23 @@ import streamlit as st
|
|
5 |
def generate_email(api_key, email_type, details):
|
6 |
openai.api_key = api_key # Set OpenAI API key provided by the user
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
"""
|
14 |
|
15 |
try:
|
16 |
-
# Request to OpenAI API
|
17 |
-
response = openai.
|
18 |
-
|
19 |
-
|
20 |
max_tokens=1200,
|
21 |
temperature=0.7
|
22 |
)
|
23 |
|
24 |
# Return the generated email content
|
25 |
-
return response.choices[0].
|
26 |
except Exception as e:
|
27 |
return f"Error: {str(e)}"
|
28 |
|
|
|
5 |
def generate_email(api_key, email_type, details):
|
6 |
openai.api_key = api_key # Set OpenAI API key provided by the user
|
7 |
|
8 |
+
# Define the conversation in the chat-based format
|
9 |
+
messages = [
|
10 |
+
{"role": "system", "content": "You are a helpful assistant that writes professional emails."},
|
11 |
+
{"role": "user", "content": f"I need to write a professional email for the following situation:\nEmail Type: {email_type}\nDetails: {details}\nPlease generate the body of the email, including suggestions for the subject line and sign-off. Ensure the email tone is professional and polite."}
|
12 |
+
]
|
|
|
13 |
|
14 |
try:
|
15 |
+
# Request to OpenAI API (using chat/completions endpoint)
|
16 |
+
response = openai.ChatCompletion.create(
|
17 |
+
model="gpt-4o", # You can replace this with any other chat-based model like `gpt-4`
|
18 |
+
messages=messages,
|
19 |
max_tokens=1200,
|
20 |
temperature=0.7
|
21 |
)
|
22 |
|
23 |
# Return the generated email content
|
24 |
+
return response.choices[0].message['content'].strip()
|
25 |
except Exception as e:
|
26 |
return f"Error: {str(e)}"
|
27 |
|