| import openai, json, re, random |
| import pandas as pd |
| from utilities import date_format, prompt_constants |
|
|
| def Completion(slack_message): |
| response = openai.ChatCompletion.create( |
| model="gpt-3.5-turbo", |
| messages=[ |
| {"role": "system", "content": prompt_constants.SLACK_SENTIMENT_SYSTEM_PROMPT}, |
| {"role": "user", "content": slack_message} ]) |
| print("response") |
| print(response["choices"][0]["message"]["content"]) |
| return response["choices"][0]["message"]["content"] |
|
|
| def sanitize_blob(blob_str): |
| return re.sub(r"(?<=: )'", '"', re.sub(r"'(?=:)", '"', blob_str)) |
|
|
| def FindScore(response): |
| match = re.search(r"\b(0(\.\d+)?|1(\.0+)?)\b", response) |
| random_offset = round(random.uniform(0.01, .099), 3) |
| if match: |
| value = round(float(match.group(1)), 2) |
| return value + random_offset |
| else: |
| return 0 |
| |
| def CheckType(response): |
| if isinstance(response, float): |
| return round(response, 2) |
| elif isinstance(response, str): |
| return FindScore(response) |
| |
| def ProcessMessage(message, summary_messages, slack_messages, id, parent_user=None ): |
| user = message["user"] |
| message_text = message["text"] |
| timestamp = message["timestamp"] |
| response = Completion(message_text) |
| summary_messages.append({"role": "user", "content": message_text}) |
| summary_messages.append({"role": "assistant", "content": response}) |
|
|
| sentiment_score = CheckType(response) |
|
|
| sentiment = "Neutral" |
| if sentiment_score == 0: |
| sentiment = "Undefined" |
| elif 0 < sentiment_score < 0.3: |
| sentiment = "Negative" |
| elif sentiment_score > 0.6: |
| sentiment = "Positive" |
| |
| dateX, timeX, twentyfour_time = date_format.TimeStampToDateAndTime(timestamp) |
|
|
| message_obj = { |
| "id": id, |
| "user": user, |
| "message": f"{message_text}", |
| "date": dateX +": " +twentyfour_time, |
| "time": timestamp, |
| "twentyfour_time": twentyfour_time, |
| "sentiment_score": sentiment_score, |
| "sentiment": sentiment, |
| "size": 8, |
| "parent_user": parent_user |
| } |
| id=id+1 |
| slack_messages.append(message_obj) |
| |
| if "replies" in message: |
| for reply in message["replies"]: |
| ProcessMessage(reply, summary_messages, slack_messages, id, parent_user=user) |
| id=id+1 |
|
|
| def ParseBlobs(blob, summary_messages): |
| global id |
| sanitized_blob = sanitize_blob(blob) |
| try: |
| response_data = json.loads(sanitized_blob) |
| except json.JSONDecodeError: |
| print("Invalid JSON format.") |
| return None |
| |
| slack_messages = [] |
| summary_messages.append({"role": "system", "content": prompt_constants.SLACK_SENTIMENT_SYSTEM_PROMPT}) |
| for message in response_data["messages"]: |
| ProcessMessage(message,summary_messages,slack_messages, id) |
| id=id+1 |
| jsonobj = json.dumps(slack_messages, ensure_ascii=False) |
| return jsonobj,summary_messages |
|
|
| def AnalyzeSentiment(blob): |
| global id |
| summary_messages = [] |
| id=3 |
| slack_blobs,summary_messages=ParseBlobs(blob,summary_messages) |
| df = pd.DataFrame(summary_messages) |
| sentimentDF=pd.read_json(slack_blobs) |
| return df, sentimentDF, id+3 |