Download coresugg.py from Koottu/Chat: direct link, hf CLI and curl.
- Browser
- Download file 1.83 kB
-
https://huggingface.co/Koottu/Chat/resolve/main/coresugg.py
- Command line
-
hf download hf://Koottu/Chat/coresugg.py
-
curl -L -o coresugg.py https://huggingface.co/Koottu/Chat/resolve/main/coresugg.py
1.83 kB
| # 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)}") | |