]> git.angelumana.com Git - diane/.git/commitdiff
Add OpenAI API, remove if-then format
authoribidyouadu <angel.d.umana@gmail.com>
Sun, 23 Jul 2023 23:20:17 +0000 (18:20 -0500)
committeribidyouadu <angel.d.umana@gmail.com>
Sun, 23 Jul 2023 23:20:17 +0000 (18:20 -0500)
.gitignore
babys_first_whatsapp_bot.py [deleted file]
main.py [new file with mode: 0644]
settings.py [new file with mode: 0644]

index 2d97991d7edb176fa2b5829b9f3ceb6405bdb108..0bbae6f580bde068ce8fe6adc30ff84d7bdb95ee 100644 (file)
@@ -1,3 +1,3 @@
 .DS_Store
 __pycache__/
-
+.env
diff --git a/babys_first_whatsapp_bot.py b/babys_first_whatsapp_bot.py
deleted file mode 100644 (file)
index b9b9dc4..0000000
+++ /dev/null
@@ -1,134 +0,0 @@
-from flask import Flask, request
-import requests
-from twilio.twiml.messaging_response import MessagingResponse
-import random
-import os
-
-state = "none"
-OPENWEATHER_KEY = os.environ["OPENWEATHER_KEY"]
-
-app = Flask(__name__)
-
-@app.route("/", methods=["POST"])
-
-def bot():
-    global state
-    incoming_message = request.values.get("Body", "").lower()
-
-    response = MessagingResponse()
-    msg = response.message()
-
-    if state == "none":
-        if incoming_message in ['hello', 'helo', 'hola', 'hi']:
-            body = "Hello to you too !"
-            msg.body(body)
-        elif incoming_message == "play":
-            body = \
-                "Let's play ! \n" \
-                "Rock paper scissors shoot !\n" \
-                "1. Rock\n" \
-                "2. Paper\n" \
-                "3. Scissors"
-            state = "play"
-            msg.body(body)
-        elif incoming_message == "weather":
-            weather_report = weather()
-            msg.body(weather_report)
-        else:
-            body = "I don't understand ! I will respond to these words:\n" \
-                "play\n" \
-                "weather"
-            msg.body(body)
-
-    elif state == "play":
-        num = random.random()
-
-        if incoming_message not in ['1', '2', '3', 'rock', 'paper', 'scissors']:
-            body = "I don't understand ! Please say one of the options !"
-            msg.body(body)
-
-            return str(response)
-
-        if num < 0.33:
-            body = "rock!"
-        elif num < 0.66:
-            body = "paper!"
-        else:
-            body = "scissors!"
-
-        result = RPS(incoming_message, body)
-
-        msg.body(body + '\n' + result)
-
-        state = "none"
-
-
-    return str(response)
-
-def wind_description(wind_speed):
-    if wind_speed < 5.0:
-        return "no breeze"
-    elif wind_speed < 9.0:
-        return "light breeze"
-    elif wind_speed < 14.0: # TBD
-        return "gentle breeze"
-    else:
-        return "lots of wind !"
-
-def weather(): # TODO respond to location
-    global OPENWEATHER_KEY
-
-    location_param = "Washington+DC,US"
-    geocode_api_url = f"https://api.openweathermap.org/geo/1.0/direct?" \
-        f"q={location_param}&limit=1&appid={OPENWEATHER_KEY}"
-    geocode_resp = requests.get(geocode_api_url)
-
-    lat = geocode_resp.json()[0]['lat']
-    lon = geocode_resp.json()[0]['lon']
-
-    weather_api_url = f"https://api.openweathermap.org/data/3.0/onecall?" \
-        f"lat={lat}&lon={lon}&appid={OPENWEATHER_KEY}&units=imperial"
-    weather_resp = requests.get(weather_api_url)
-
-    today_weather = weather_resp.json()['daily'][0]
-    weather = today_weather['weather'][0]['description']
-    wind = wind_description(today_weather['wind_speed'])
-    temps = today_weather['temp']
-    avg_temp = round((temps['morn'] + temps['day'] + temps['eve']) / 3)
-    humidity = today_weather['humidity']
-
-    report = \
-        f"{weather} with {wind}.\n" \
-        f"Around {avg_temp} degrees. Humidity {humidity}%."
-
-    return report
-
-def RPS(user_pick, bot_pick):
-    win = "Aww you win !"
-    loss = "Haha I win !"
-    tie = "It's a tie !"
-
-    if user_pick in ['1', 'rock']:
-        if bot_pick == 'rock!':
-            return tie
-        elif bot_pick == 'paper!':
-            return loss
-        else:
-            return win
-    elif user_pick in ['2', 'paper']:
-        if bot_pick == 'rock!':
-            return win
-        elif bot_pick == 'paper!':
-            return tie
-        else:
-            return loss
-    elif user_pick in ['3', 'scissors']:
-        if bot_pick == 'rock!':
-            return loss
-        elif bot_pick == 'paper!':
-            return win
-        else:
-            return tie
-
-if __name__ == "__main__":
-    app.run()
diff --git a/main.py b/main.py
new file mode 100644 (file)
index 0000000..0f04567
--- /dev/null
+++ b/main.py
@@ -0,0 +1,41 @@
+from flask import Flask, request
+import requests
+from twilio.twiml.messaging_response import MessagingResponse
+import random
+import os
+import openai
+from settings import OPENAI_API_KEY, OPENWEATHER_API_KEY
+
+openai.api_key = OPENAI_API_KEY
+
+app = Flask(__name__)
+
+@app.route("/", methods=["POST"])
+
+def main():
+    input_msg = request.values.get("Body", "")
+    response = MessagingResponse()
+    msg = response.message()
+
+    openai_res = openai.ChatCompletion.create(
+        model="gpt-3.5-turbo",
+        messages=[
+            {
+                "role": "system",
+                "content": "You are a helpful assistant named Diane."
+            },
+            {
+                "role": "user",
+                "content": input_msg
+            }
+        ],
+        max_tokens=256,
+        temperature=0.6
+    )
+
+    msg.body(openai_res.choices[0].message.content)
+
+    return str(response)
+
+if __name__ == "__main__":
+    app.run()
\ No newline at end of file
diff --git a/settings.py b/settings.py
new file mode 100644 (file)
index 0000000..9947170
--- /dev/null
@@ -0,0 +1,7 @@
+import os
+from dotenv import load_dotenv
+
+load_dotenv()
+
+OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
+OPENWEATHER_API_KEY = os.environ["OPENWEATHER_API_KEY"]
\ No newline at end of file