Spaces:
Sleeping
Sleeping
OnlyBiggg
commited on
Commit
·
648bcdc
1
Parent(s):
d73e455
fix error
Browse files- app/services/service.py +43 -40
app/services/service.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
from dotenv import load_dotenv
|
|
|
3 |
from google.cloud import dialogflowcx_v3beta1 as dialogflow
|
4 |
from google.api_core.client_options import ClientOptions
|
5 |
from app.core.config import settings
|
@@ -15,43 +16,45 @@ def chatbot_response(
|
|
15 |
texts: list
|
16 |
):
|
17 |
"""Gửi văn bản đến Dialogflow CX và nhận phản hồi"""
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
1 |
import os
|
2 |
from dotenv import load_dotenv
|
3 |
+
from fastapi import HTTPException, status
|
4 |
from google.cloud import dialogflowcx_v3beta1 as dialogflow
|
5 |
from google.api_core.client_options import ClientOptions
|
6 |
from app.core.config import settings
|
|
|
16 |
texts: list
|
17 |
):
|
18 |
"""Gửi văn bản đến Dialogflow CX và nhận phản hồi"""
|
19 |
+
try:
|
20 |
+
endpoint = f"{LOCATION}-dialogflow.googleapis.com"
|
21 |
+
client_options = ClientOptions(api_endpoint=endpoint)
|
22 |
+
client = dialogflow.SessionsClient(client_options=client_options)
|
23 |
+
|
24 |
+
session_path = f"projects/{PROJECT_ID}/locations/{LOCATION}/agents/{AGENT_ID}/sessions/{session_id}"
|
25 |
+
|
26 |
+
extracted_texts = []
|
27 |
+
all_chip_options = []
|
28 |
+
for text in texts:
|
29 |
+
text_input = dialogflow.TextInput(text=text)
|
30 |
+
query_input = dialogflow.QueryInput(text=text_input, language_code=LANGUAGE_CODE)
|
31 |
+
|
32 |
+
request = dialogflow.DetectIntentRequest(
|
33 |
+
session=session_path,
|
34 |
+
query_input=query_input
|
35 |
+
)
|
36 |
+
|
37 |
+
response = client.detect_intent(request=request)
|
38 |
+
query_result = response.query_result
|
39 |
+
|
40 |
+
for message in query_result.response_messages:
|
41 |
+
if hasattr(message, "text") and message.text.text:
|
42 |
+
extracted_texts.extend(message.text.text)
|
43 |
+
|
44 |
+
chip_opttion = []
|
45 |
+
if hasattr(message, "payload") and message.payload:
|
46 |
+
payload_dict = dict(message.payload)
|
47 |
+
if "richContent" in payload_dict:
|
48 |
+
rich_content = payload_dict["richContent"]
|
49 |
+
for item_group in rich_content:
|
50 |
+
for item in item_group:
|
51 |
+
if item.get("type") == "chips":
|
52 |
+
options = item.get("options", [])
|
53 |
+
chip_opttion.extend([opt["text"] for opt in options])
|
54 |
+
all_chip_options.append(chip_opttion)
|
55 |
+
|
56 |
+
response = {"text": extracted_texts, "payload": all_chip_options}
|
57 |
+
|
58 |
+
return response
|
59 |
+
except Exception as e:
|
60 |
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|