Update handler.py
Browse files- handler.py +45 -29
handler.py
CHANGED
|
@@ -1,8 +1,5 @@
|
|
| 1 |
-
from typing import Dict, Any, List
|
| 2 |
-
from environs import Env
|
| 3 |
import json
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
|
| 7 |
def download_env_file(url: str, local_path: str):
|
| 8 |
response = requests.get(url)
|
|
@@ -15,32 +12,51 @@ env_file_url = "https://www.dropbox.com/scl/fi/21ldek2cdsak2v3mhyy5x/openai.env?
|
|
| 15 |
local_env_path = "openai.env"
|
| 16 |
download_env_file(env_file_url, local_env_path)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
from
|
| 20 |
-
|
| 21 |
-
env = Env()
|
| 22 |
|
| 23 |
class EndpointHandler:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
starter_suggestion = generate_conversation_starters(prompt)
|
| 40 |
-
return
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
else:
|
| 43 |
-
raise
|
| 44 |
-
|
| 45 |
-
if __name__ == "__main__":
|
| 46 |
-
handler = EndpointHandler()
|
|
|
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
+
from typing import Dict, List, Any
|
|
|
|
| 3 |
|
| 4 |
def download_env_file(url: str, local_path: str):
|
| 5 |
response = requests.get(url)
|
|
|
|
| 12 |
local_env_path = "openai.env"
|
| 13 |
download_env_file(env_file_url, local_env_path)
|
| 14 |
|
| 15 |
+
# Importing modules from both scripts
|
| 16 |
+
from coresugg import ConversationPayload as ConversationPayloadSugg, create_conversation_starter_prompt, generate_conversation_starters, NUMBER_OF_MESSAGES_FOR_CONTEXT as NUMBER_OF_MESSAGES_FOR_CONTEXT_SUGG
|
| 17 |
+
from corechat import ConversationPayload as ConversationPayloadChat, get_conversation_suggestions, NUMBER_OF_MESSAGES_FOR_CONTEXT as NUMBER_OF_MESSAGES_FOR_CONTEXT_CHAT
|
|
|
|
| 18 |
|
| 19 |
class EndpointHandler:
|
| 20 |
+
def __init__(self):
|
| 21 |
+
pass
|
| 22 |
+
|
| 23 |
+
def integration(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
| 24 |
+
payload = ConversationPayloadSugg(**data)
|
| 25 |
+
from_user_questions = payload.FromUserKavasQuestions[-NUMBER_OF_MESSAGES_FOR_CONTEXT_SUGG:]
|
| 26 |
+
to_user_questions = payload.ToUserKavasQuestions[-NUMBER_OF_MESSAGES_FOR_CONTEXT_SUGG:]
|
| 27 |
+
ai_prompt = create_conversation_starter_prompt(from_user_questions + to_user_questions, payload.Chatmood)
|
| 28 |
+
conversation_starters = generate_conversation_starters(ai_prompt)
|
| 29 |
+
return {"conversation_starters": conversation_starters}
|
| 30 |
+
|
| 31 |
+
def chat_integration(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
| 32 |
+
payload = ConversationPayloadChat(**data)
|
| 33 |
+
last_chat_messages = payload.LastChatMessages[-NUMBER_OF_MESSAGES_FOR_CONTEXT_CHAT:]
|
| 34 |
+
suggestions = get_conversation_suggestions(last_chat_messages)
|
| 35 |
+
return {"version": "1.0.0-alpha", "suggested_responses": suggestions}
|
| 36 |
+
|
| 37 |
+
def upload(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
| 38 |
+
if "file" not in data:
|
| 39 |
+
raise Exception("No file provided")
|
| 40 |
+
|
| 41 |
+
file_data = data["file"]
|
| 42 |
+
try:
|
| 43 |
+
json_data = json.loads(file_data)
|
| 44 |
+
except json.JSONDecodeError:
|
| 45 |
+
raise Exception("Invalid JSON format.")
|
| 46 |
+
|
| 47 |
+
if "FromUserKavasQuestions" in json_data and "Chatmood" in json_data:
|
| 48 |
+
prompt = create_conversation_starter_prompt(
|
| 49 |
+
json_data["FromUserKavasQuestions"],
|
| 50 |
+
json_data["Chatmood"]
|
| 51 |
+
)
|
| 52 |
starter_suggestion = generate_conversation_starters(prompt)
|
| 53 |
+
return {"conversation_starter": starter_suggestion}
|
| 54 |
+
elif "LastChatMessages" in json_data:
|
| 55 |
+
last_chat_messages = json_data["LastChatMessages"][-NUMBER_OF_MESSAGES_FOR_CONTEXT_CHAT:]
|
| 56 |
+
response = {
|
| 57 |
+
"version": "1.0.0-alpha",
|
| 58 |
+
"suggested_responses": get_conversation_suggestions(last_chat_messages)
|
| 59 |
+
}
|
| 60 |
+
return response
|
| 61 |
else:
|
| 62 |
+
raise Exception("Invalid JSON structure.")
|
|
|
|
|
|
|
|
|