diff --git a/rasa/actions/__pycache__/actions.cpython-38.pyc b/rasa/actions/__pycache__/actions.cpython-38.pyc index 6b5a89e..fb4c7d8 100644 Binary files a/rasa/actions/__pycache__/actions.cpython-38.pyc and b/rasa/actions/__pycache__/actions.cpython-38.pyc differ diff --git a/rasa/actions/actions.py b/rasa/actions/actions.py index c06ef7b..1bc4bfb 100644 --- a/rasa/actions/actions.py +++ b/rasa/actions/actions.py @@ -1,25 +1,154 @@ -# actions.py - +import requests +import json from rasa_sdk import Action from rasa_sdk.events import SlotSet -import smtplib -import requests -class ActionSendEmail(Action): +# Action to ask to the Llama model +class ActionAskLlama(Action): def name(self): - return "action_send_email" + return "action_ask_llama" def run(self, dispatcher, tracker, domain): - # Simulation in write log in file - with open('../log.txt', 'w') as f: - f.write("Email sent\n") + user_input = tracker.latest_message.get('text') -class ActionGetWeather(Action): + # Call API (Streaming) + url = "http://localhost:11434/api/generate" + payload = { + "model": "llama3.1", + "prompt": user_input + } + + try: + response = requests.post(url, json=payload, stream=True) + + if response.status_code == 200: + llm_response = "" + # Read the response line by line + for line in response.iter_lines(): + if line: + json_line = line.decode('utf-8') + json_data = json.loads(json_line) + + # Done flag for defining the end of the response + if json_data.get("done"): + break + llm_response += json_data.get("response", "") + dispatcher.utter_message(text=llm_response.strip()) + else: + dispatcher.utter_message(text=f"Erreur lors de l'appel à Ollama: {response.status_code} - {response.text}") + except Exception as e: + dispatcher.utter_message(text=f"Une exception est survenue : {str(e)}") + + return [] + +# Action to ask to tje randomuser API +class ActionRandomuser(Action): def name(self): - return "action_get_weather" + return "action_randomuser" def run(self, dispatcher, tracker, domain): - # Simulation in write log in file - with open('../log.txt', 'w') as f: - f.write("Wwaether API called\n") + url = "https://randomuser.me/api/" + try: + response = requests.get(url) + + if response.status_code == 200: + user_data = response.json() + result = user_data["results"][0] + + # Extract data + first_name = result["name"]["first"] + last_name = result["name"]["last"] + age = result["dob"]["age"] + street = result["location"]["street"]["name"] + city = result["location"]["city"] + state = result["location"]["state"] + country = result["location"]["country"] + + # Send message + message = ( + f"Nom: {last_name}, Prénom: {first_name}, Age: {age}, " + f"Lieu d'habitation: {street}, {city}, {state}, {country}" + ) + dispatcher.utter_message(text=message) + else: + dispatcher.utter_message(text=f"Erreur lors de l'appel à l'API: {response.status_code} - {response.text}") + except Exception as e: + dispatcher.utter_message(text=f"Une exception est survenue : {str(e)}") + +# Action to get city and state from a zipcode (Swiss) +class ActionGetZipcode(Action): + + def name(self) -> str: + return "action_swiss_zipcode" + + def run(self, dispatcher, tracker, domain): + zipcode = next(tracker.get_latest_entity_values("zipcode"), None) + with open('../logs.txt', 'a') as f: + f.write(zipcode + '\n') + + + if zipcode: + url = f"https://api.zippopotam.us/ch/{zipcode}" + response = requests.get(url) + + if response.status_code == 200: + data = response.json() + places = data.get("places", []) + if places: + place_info = places[0] + city = place_info.get("place name") + state = place_info.get("state") + message = f"Le code postal {zipcode} correspond à {city}, {state}." + else: + message = f"Aucune information trouvée pour le code postal {zipcode}." + else: + message = f"Erreur lors de l'appel à l'API : {response.status_code}." + else: + message = "Je n'ai pas pu extraire le code postal." + + dispatcher.utter_message(text=message) + return [] + +# Action to get the signification of a firstname and 3 famous people known from llama model +class ActionFirstnameSignificationLlama(Action): + def name(self): + return "action_firstname_signification_by_llama" + + def run(self, dispatcher, tracker, domain): + firstname = next(tracker.get_latest_entity_values("firstname"), None) + + with open('../logs.txt', 'a') as f: + f.write(firstname + '\n') + + user_input = "Quelle est la signification du prénom " + firstname + " ? Donne une courte signification. Puis liste 3 personnalités connues portant ce prénom." + + # Call API (Streaming) + url = "http://localhost:11434/api/generate" + payload = { + "model": "llama3.1", + "prompt": user_input + } + + try: + response = requests.post(url, json=payload, stream=True) + + if response.status_code == 200: + llm_response = "" + # Read the response line by line + for line in response.iter_lines(): + if line: + json_line = line.decode('utf-8') + json_data = json.loads(json_line) + + # Done flag for defining the end of the response + if json_data.get("done"): + break + llm_response += json_data.get("response", "") + dispatcher.utter_message(text=llm_response.strip()) + else: + dispatcher.utter_message(text=f"Erreur lors de l'appel à Ollama: {response.status_code} - {response.text}") + except Exception as e: + dispatcher.utter_message(text=f"Une exception est survenue : {str(e)}") + + return [] \ No newline at end of file diff --git a/rasa/data/nlu.yml b/rasa/data/nlu.yml index 53a5262..fb1e8ae 100644 --- a/rasa/data/nlu.yml +++ b/rasa/data/nlu.yml @@ -1,110 +1,40 @@ version: "3.1" nlu: -- intent: greet +- intent: intent_randomuser examples: | - - hey - - hello - - hi - - hello there - - good morning - - good evening - - moin - - hey there - - let's go - - hey dude - - goodmorning - - goodevening - - good afternoon + - Génère moi un profil utilisateur + - Crée un profil d'utilisateur + - Peux-tu me donner un profil utilisateur aléatoire ? + - J'ai besoin d'un profil utilisateur + - Donne-moi un exemple de profil utilisateur + - Fais-moi un profil utilisateur + - Génère un profil utilisateur au hasard + - Crée un utilisateur aléatoire pour moi + - Est-ce que tu peux créer un profil utilisateur ? -- intent: goodbye +- intent: intent_ask_llama examples: | - - cu - - good by - - cee you later - - good night - - bye - - goodbye - - have a nice day - - see you around - - bye bye - - see you later + - Qu'est-ce que tu sais sur les modèles de langage ? + - Comment fonctionne un ordinateur ? + - Quelle est la capitale de la France ? -- intent: affirm +- intent: intent_firstname_signification_by_llama examples: | - - yes - - y - - indeed - - of course - - that sounds good - - correct + - Que signifie mon prénom [Guillaume](firstname) ? + - Peux-tu me dire ce que signifie le prénom [Juliette](firstname) ? + - Quelle est la signification de [Emma](firstname) ? + - Explique-moi la signification du prénom [Lucas](firstname). + - Que veut dire le prénom [Sophie](firstname) ? + - Quelle est l'origine du prénom [Maxime](firstname) ? + - Dis-moi la signification de [Chloé](firstname). + - Que représente le prénom [Thomas](firstname) ? + - Je voudrais savoir ce que signifie le prénom [Camille](firstname). + - Que signifie le prénom [Alice](firstname) ? -- intent: deny +- intent: intent_swiss_zipcode examples: | - - no - - n - - never - - I don't think so - - don't like that - - no way - - not really + - Quel est le code postal en Suisse de ce numéro [1977](zipcode) + - Donne-moi le code postal de [1977](zipcode) + - Quel est le code postal pour [1977](zipcode) ? -- intent: mood_great - examples: | - - perfect - - great - - amazing - - feeling like a king - - wonderful - - I am feeling very good - - I am great - - I am amazing - - I am going to save the world - - super stoked - - extremely good - - so so perfect - - so good - - so perfect - -- intent: mood_unhappy - examples: | - - my day was horrible - - I am sad - - I don't feel very well - - I am disappointed - - super sad - - I'm so sad - - sad - - very sad - - unhappy - - not good - - not very good - - extremly sad - - so saad - - so sad - -- intent: bot_challenge - examples: | - - are you a bot? - - are you a human? - - am I talking to a bot? - - am I talking to a human? - - -- intent: send_email - examples: | - - Envoie-moi un email - - Je veux envoyer un mail - - Peux-tu envoyer un email ? - - Envoie un mail à mon ami - - Pourrais-tu m'envoyer un email ? - - Fais un mail - -- intent: get_weather - examples: | - - Quelle est la météo ? - - Récupère la météo - - Donne-moi la météo - - Quel temps fait-il ? - - Quelle est la météo pour aujourd'hui ? - - Quel temps fait-il à Paris ? \ No newline at end of file diff --git a/rasa/data/rules.yml b/rasa/data/rules.yml index a9987ee..7fa98c7 100644 --- a/rasa/data/rules.yml +++ b/rasa/data/rules.yml @@ -1,13 +1,21 @@ version: "3.1" - rules: + - rule: rule__randomuser + steps: + - intent: intent_randomuser + - action: action_randomuser -- rule: Say goodbye anytime the user says goodbye - steps: - - intent: goodbye - - action: utter_goodbye + - rule: rule_swiss_zipcode + steps: + - intent: intent_swiss_zipcode + - action: action_swiss_zipcode -- rule: Say 'I am a bot' anytime the user challenges - steps: - - intent: bot_challenge - - action: utter_iamabot + - rule: rule_firstname_signification_by_llama + steps: + - intent: intent_firstname_signification_by_llama + - action: action_firstname_signification_by_llama + + - rule: General question redirect to LLM + steps: + - intent: intent_ask_llama + - action: action_ask_llama \ No newline at end of file diff --git a/rasa/data/stories.yml b/rasa/data/stories.yml index 45b72db..b3a7a32 100644 --- a/rasa/data/stories.yml +++ b/rasa/data/stories.yml @@ -1,43 +1,21 @@ version: "3.1" - stories: + - story: request_randomuser + steps: + - intent: intent_randomuser + - action: action_randomuser -- story: happy path - steps: - - intent: greet - - action: utter_greet - - intent: mood_great - - action: utter_happy - -- story: sad path 1 - steps: - - intent: greet - - action: utter_greet - - intent: mood_unhappy - - action: utter_cheer_up - - action: utter_did_that_help - - intent: affirm - - action: utter_happy - -- story: sad path 2 - steps: - - intent: greet - - action: utter_greet - - intent: mood_unhappy - - action: utter_cheer_up - - action: utter_did_that_help - - intent: deny - - action: utter_goodbye - - -- story: Envoie un email - steps: - - intent: send_email - - action: action_send_email - -- story: Récupère la météo - steps: - - intent: get_weather - - action: action_get_weather + - story: request_ask_llama + steps: + - intent: intent_ask_llama + - action: action_ask_llama + - story: request_firstname_signification_by_llama + steps: + - intent: intent_firstname_signification_by_llama + - action: action_firstname_signification_by_llama + - story: request_swiss_zipcode + steps: + - intent: intent_swiss_zipcode + - action: action_swiss_zipcode diff --git a/rasa/domain.yml b/rasa/domain.yml index 636af27..cc84cb2 100644 --- a/rasa/domain.yml +++ b/rasa/domain.yml @@ -1,41 +1,13 @@ version: "3.1" intents: - - greet - - goodbye - - affirm - - deny - - mood_great - - mood_unhappy - - bot_challenge - - send_email - - get_weather + - intent_randomuser + - intent_ask_llama + - intent_swiss_zipcode + - intent_firstname_signification_by_llama actions: - - action_send_email - - action_get_weather - -responses: - utter_greet: - - text: "Salut ! Comment puis-je t'aider aujourd'hui ?" - - utter_cheer_up: - - text: "Here is something to cheer you up:" - image: "https://i.imgur.com/nGF1K8f.jpg" - - utter_did_that_help: - - text: "Did that help you?" - - utter_happy: - - text: "Great, carry on!" - - utter_goodbye: - - text: "Bye" - - utter_iamabot: - - text: "I am a bot, powered by Rasa." - -session_config: - session_expiration_time: 60 - carry_over_slots_to_new_session: true - + - action_randomuser + - action_ask_llama + - action_swiss_zipcode + - action_firstname_signification_by_llama \ No newline at end of file