Spaces:
Sleeping
Sleeping
Update rasa-assistant-2/actions/actions.py
Browse files
rasa-assistant-2/actions/actions.py
CHANGED
@@ -1,3 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# This files contains your custom actions which can be used to run
|
2 |
# custom Python code.
|
3 |
#
|
|
|
1 |
+
import requests
|
2 |
+
from typing import Any, Text, Dict, List
|
3 |
+
from rasa_sdk import Action, Tracker
|
4 |
+
from rasa_sdk.executor import CollectingDispatcher
|
5 |
+
|
6 |
+
GROQ_API_KEY = "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" # Replace with your Groq API Key
|
7 |
+
GROQ_MODEL = "llama3-8b-8192" # Change the model if needed
|
8 |
+
|
9 |
+
class ActionGroqChat(Action):
|
10 |
+
def name(self) -> Text:
|
11 |
+
return "action_groq_chat"
|
12 |
+
|
13 |
+
def run(self, dispatcher: CollectingDispatcher,
|
14 |
+
tracker: Tracker,
|
15 |
+
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
|
16 |
+
|
17 |
+
user_message = tracker.latest_message.get('text')
|
18 |
+
|
19 |
+
url = "https://api.groq.com/v1/chat/completions"
|
20 |
+
headers = {
|
21 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
22 |
+
"Content-Type": "application/json"
|
23 |
+
}
|
24 |
+
payload = {
|
25 |
+
"model": GROQ_MODEL,
|
26 |
+
"messages": [{"role": "user", "content": user_message}]
|
27 |
+
}
|
28 |
+
|
29 |
+
try:
|
30 |
+
response = requests.post(url, json=payload, headers=headers)
|
31 |
+
response_json = response.json()
|
32 |
+
bot_reply = response_json["choices"][0]["message"]["content"]
|
33 |
+
except Exception as e:
|
34 |
+
bot_reply = "Sorry, I couldn't process your request."
|
35 |
+
|
36 |
+
dispatcher.utter_message(text=bot_reply)
|
37 |
+
return []
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
# This files contains your custom actions which can be used to run
|
46 |
# custom Python code.
|
47 |
#
|