File size: 1,827 Bytes
fa947da |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# core.py
from pydantic import BaseModel
import openai
from environs import Env
# Load environment variables
env = Env()
env.read_env("openai.env") # Adjust the file name as needed
# Set OpenAI API key
openai.api_key = env.str("OPENAI_API_KEY")
# Define constants from environment variables
SYSTEM_PROMPT = env.str("SYSTEM_PROMPT", "generate 3 different friendly short conversation starter for a user to another unknown user.")
MODEL = env.str("MODEL", "gpt-3.5-turbo")
NUMBER_OF_MESSAGES_FOR_CONTEXT = min(env.int("NUMBER_OF_MESSAGES_FOR_CONTEXT", 4), 10)
AI_RESPONSE_TIMEOUT = env.int("AI_RESPONSE_TIMEOUT", 20)
class ConversationPayload(BaseModel):
fromusername: str
tousername: str
FromUserKavasQuestions: list
ToUserKavasQuestions: list
Chatmood: str
def create_conversation_starter_prompt(user_questions, chatmood):
formatted_info = " ".join([f"{qa['Question']} - {qa['Answer']}" for qa in user_questions if qa['Answer']])
prompt = (f"Based on user profile info and a {chatmood} mood, "
f"generate 3 subtle and very short conversation starters. "
f"Explore various topics like travel, hobbies, movies, and not just culinary tastes. "
f"\nProfile Info: {formatted_info}")
return prompt
def generate_conversation_starters(prompt):
try:
response = openai.ChatCompletion.create(
model=MODEL,
messages=[{"role": "system", "content": prompt}],
temperature=0.7,
max_tokens=100,
n=1,
request_timeout=AI_RESPONSE_TIMEOUT
)
return response.choices[0].message["content"]
except openai.error.OpenAIError as e:
raise Exception(f"OpenAI API error: {str(e)}")
except Exception as e:
raise Exception(f"Unexpected error: {str(e)}")
|