Update
This commit is contained in:
parent
d61cd1ab64
commit
a9b223109c
Binary file not shown.
|
|
@ -1,25 +1,154 @@
|
||||||
# actions.py
|
import requests
|
||||||
|
import json
|
||||||
from rasa_sdk import Action
|
from rasa_sdk import Action
|
||||||
from rasa_sdk.events import SlotSet
|
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):
|
def name(self):
|
||||||
return "action_send_email"
|
return "action_ask_llama"
|
||||||
|
|
||||||
def run(self, dispatcher, tracker, domain):
|
def run(self, dispatcher, tracker, domain):
|
||||||
# Simulation in write log in file
|
user_input = tracker.latest_message.get('text')
|
||||||
with open('../log.txt', 'w') as f:
|
|
||||||
f.write("Email sent\n")
|
|
||||||
|
|
||||||
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):
|
def name(self):
|
||||||
return "action_get_weather"
|
return "action_randomuser"
|
||||||
|
|
||||||
def run(self, dispatcher, tracker, domain):
|
def run(self, dispatcher, tracker, domain):
|
||||||
# Simulation in write log in file
|
url = "https://randomuser.me/api/"
|
||||||
with open('../log.txt', 'w') as f:
|
|
||||||
f.write("Wwaether API called\n")
|
|
||||||
|
|
||||||
|
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 []
|
||||||
|
|
@ -1,110 +1,40 @@
|
||||||
version: "3.1"
|
version: "3.1"
|
||||||
|
|
||||||
nlu:
|
nlu:
|
||||||
- intent: greet
|
- intent: intent_randomuser
|
||||||
examples: |
|
examples: |
|
||||||
- hey
|
- Génère moi un profil utilisateur
|
||||||
- hello
|
- Crée un profil d'utilisateur
|
||||||
- hi
|
- Peux-tu me donner un profil utilisateur aléatoire ?
|
||||||
- hello there
|
- J'ai besoin d'un profil utilisateur
|
||||||
- good morning
|
- Donne-moi un exemple de profil utilisateur
|
||||||
- good evening
|
- Fais-moi un profil utilisateur
|
||||||
- moin
|
- Génère un profil utilisateur au hasard
|
||||||
- hey there
|
- Crée un utilisateur aléatoire pour moi
|
||||||
- let's go
|
- Est-ce que tu peux créer un profil utilisateur ?
|
||||||
- hey dude
|
|
||||||
- goodmorning
|
|
||||||
- goodevening
|
|
||||||
- good afternoon
|
|
||||||
|
|
||||||
- intent: goodbye
|
- intent: intent_ask_llama
|
||||||
examples: |
|
examples: |
|
||||||
- cu
|
- Qu'est-ce que tu sais sur les modèles de langage ?
|
||||||
- good by
|
- Comment fonctionne un ordinateur ?
|
||||||
- cee you later
|
- Quelle est la capitale de la France ?
|
||||||
- good night
|
|
||||||
- bye
|
|
||||||
- goodbye
|
|
||||||
- have a nice day
|
|
||||||
- see you around
|
|
||||||
- bye bye
|
|
||||||
- see you later
|
|
||||||
|
|
||||||
- intent: affirm
|
- intent: intent_firstname_signification_by_llama
|
||||||
examples: |
|
examples: |
|
||||||
- yes
|
- Que signifie mon prénom [Guillaume](firstname) ?
|
||||||
- y
|
- Peux-tu me dire ce que signifie le prénom [Juliette](firstname) ?
|
||||||
- indeed
|
- Quelle est la signification de [Emma](firstname) ?
|
||||||
- of course
|
- Explique-moi la signification du prénom [Lucas](firstname).
|
||||||
- that sounds good
|
- Que veut dire le prénom [Sophie](firstname) ?
|
||||||
- correct
|
- 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: |
|
examples: |
|
||||||
- no
|
- Quel est le code postal en Suisse de ce numéro [1977](zipcode)
|
||||||
- n
|
- Donne-moi le code postal de [1977](zipcode)
|
||||||
- never
|
- Quel est le code postal pour [1977](zipcode) ?
|
||||||
- I don't think so
|
|
||||||
- don't like that
|
|
||||||
- no way
|
|
||||||
- not really
|
|
||||||
|
|
||||||
- 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 ?
|
|
||||||
|
|
@ -1,13 +1,21 @@
|
||||||
version: "3.1"
|
version: "3.1"
|
||||||
|
|
||||||
rules:
|
rules:
|
||||||
|
- rule: rule__randomuser
|
||||||
|
steps:
|
||||||
|
- intent: intent_randomuser
|
||||||
|
- action: action_randomuser
|
||||||
|
|
||||||
- rule: Say goodbye anytime the user says goodbye
|
- rule: rule_swiss_zipcode
|
||||||
steps:
|
steps:
|
||||||
- intent: goodbye
|
- intent: intent_swiss_zipcode
|
||||||
- action: utter_goodbye
|
- action: action_swiss_zipcode
|
||||||
|
|
||||||
- rule: Say 'I am a bot' anytime the user challenges
|
- rule: rule_firstname_signification_by_llama
|
||||||
steps:
|
steps:
|
||||||
- intent: bot_challenge
|
- intent: intent_firstname_signification_by_llama
|
||||||
- action: utter_iamabot
|
- action: action_firstname_signification_by_llama
|
||||||
|
|
||||||
|
- rule: General question redirect to LLM
|
||||||
|
steps:
|
||||||
|
- intent: intent_ask_llama
|
||||||
|
- action: action_ask_llama
|
||||||
|
|
@ -1,43 +1,21 @@
|
||||||
version: "3.1"
|
version: "3.1"
|
||||||
|
|
||||||
stories:
|
stories:
|
||||||
|
- story: request_randomuser
|
||||||
|
steps:
|
||||||
|
- intent: intent_randomuser
|
||||||
|
- action: action_randomuser
|
||||||
|
|
||||||
- story: happy path
|
- story: request_ask_llama
|
||||||
steps:
|
steps:
|
||||||
- intent: greet
|
- intent: intent_ask_llama
|
||||||
- action: utter_greet
|
- action: action_ask_llama
|
||||||
- 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_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
|
||||||
|
|
|
||||||
|
|
@ -1,41 +1,13 @@
|
||||||
version: "3.1"
|
version: "3.1"
|
||||||
|
|
||||||
intents:
|
intents:
|
||||||
- greet
|
- intent_randomuser
|
||||||
- goodbye
|
- intent_ask_llama
|
||||||
- affirm
|
- intent_swiss_zipcode
|
||||||
- deny
|
- intent_firstname_signification_by_llama
|
||||||
- mood_great
|
|
||||||
- mood_unhappy
|
|
||||||
- bot_challenge
|
|
||||||
- send_email
|
|
||||||
- get_weather
|
|
||||||
|
|
||||||
actions:
|
actions:
|
||||||
- action_send_email
|
- action_randomuser
|
||||||
- action_get_weather
|
- action_ask_llama
|
||||||
|
- action_swiss_zipcode
|
||||||
responses:
|
- action_firstname_signification_by_llama
|
||||||
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
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user