OnlyBiggg commited on
Commit
648bcdc
·
1 Parent(s): d73e455
Files changed (1) hide show
  1. 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
- endpoint = f"{LOCATION}-dialogflow.googleapis.com"
20
- client_options = ClientOptions(api_endpoint=endpoint)
21
- client = dialogflow.SessionsClient(client_options=client_options)
22
-
23
- session_path = f"projects/{PROJECT_ID}/locations/{LOCATION}/agents/{AGENT_ID}/sessions/{session_id}"
24
-
25
- extracted_texts = []
26
- all_chip_options = []
27
- for text in texts:
28
- text_input = dialogflow.TextInput(text=text)
29
- query_input = dialogflow.QueryInput(text=text_input, language_code=LANGUAGE_CODE)
30
-
31
- request = dialogflow.DetectIntentRequest(
32
- session=session_path,
33
- query_input=query_input
34
- )
35
-
36
- response = client.detect_intent(request=request)
37
- query_result = response.query_result
38
-
39
- for message in query_result.response_messages:
40
- if hasattr(message, "text") and message.text.text:
41
- extracted_texts.extend(message.text.text)
42
-
43
- chip_opttion = []
44
- if hasattr(message, "payload") and message.payload:
45
- payload_dict = dict(message.payload)
46
- if "richContent" in payload_dict:
47
- rich_content = payload_dict["richContent"]
48
- for item_group in rich_content:
49
- for item in item_group:
50
- if item.get("type") == "chips":
51
- options = item.get("options", [])
52
- chip_opttion.extend([opt["text"] for opt in options])
53
- all_chip_options.append(chip_opttion)
54
-
55
- response = {"text": extracted_texts, "payload": all_chip_options}
56
-
57
- return response
 
 
 
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))