Open ttzeng opened 1 year ago
A typical example, a row interaction log from the Dialogflow ES console:
{
"id": "8d9aebfb-bc0a-4090-ac21-ecb32e30607b-fb923cf8",
"fulfillmentText": "對不起,我聽不懂你的問題。",
"language_code": "zh-tw",
"queryText": "如何投資海外債券",
"webhookPayload": {},
"intentDetectionConfidence": 1,
"action": "input.unknown",
"webhookSource": "",
"parameters": {},
"fulfillmentMessages": [
{
"text": {
"text": [
"對不起,我聽不懂你的問題。"
]
}
}
],
"diagnosticInfo": {
"webhook_latency_ms": "4893.0"
},
"webhookStatus": {
"webhookStatus": {
"code": 4,
"message": "Webhook call failed. Error: DEADLINE_EXCEEDED, State: URL_TIMEOUT, Reason: TIMEOUT_WEB."
},
"webhookUsed": true
},
"outputContexts": [
{
"lifespanCount": 1,
"name": "__system_counters__",
"parameters": {
"no-input": "0.0",
"no-match": "8.0"
}
}
],
"intent": {
"isFallback": true,
"displayName": "Default Fallback Intent",
"id": "53efae5c-f5b1-402c-be69-34f52c5d58c7"
}
}
The OpenAI may take time to respond longer context, TIMEOUT still occurs if the overall elapsed time exceeds the fulfillment requirement:
Webhook input from Dialogflow:
{'responseId': '9ee05192-88bb-498f-89a9-4e244cb4fc4f-fb923cf8', 'queryResult': {'queryText': 'No, I meant a template of LINE message API using openai', 'action': 'input.unknown', 'parameters': {}, 'allRequiredParamsPresent': True, 'fulfillmentText': '對不起,我聽不懂你的問題。', 'fulfillmentMessages': [{'text': {'text': ['對不起,我聽不懂你的問題。']}}], 'outputContexts': [{'name': 'projects/ttzeng-gcp/agent/sessions/801e75c5-6920-3c28-8c0b-d933e45206cf/contexts/__system_counters__', 'lifespanCount': 1, 'parameters': {'no-input': 0.0, 'no-match': 3.0}}], 'intent': {'name': 'projects/ttzeng-gcp/agent/intents/53efae5c-f5b1-402c-be69-34f52c5d58c7', 'displayName': 'Default Fallback Intent', 'isFallback': True}, 'intentDetectionConfidence': 1.0, 'languageCode': 'zh-tw'}, 'originalDetectIntentRequest': {'source': 'line', 'payload': {'data': {'replyToken': '84ec21db19f6457989f68c514080b87c', 'type': 'message', 'source': {'type': 'user', 'userId': 'U83c6e8f0e536cbca2a61f0eb8a1f6951'}, 'timestamp': '1717990700058', 'message': {'type': 'text', 'id': '511972510029054098', 'text': 'No, I meant a template of LINE message API using openai'}}}}, 'session': 'projects/ttzeng-gcp/agent/sessions/801e75c5-6920-3c28-8c0b-d933e45206cf'}
Response from OpenAI in the webhook:
Here is a template for using the LINE messaging API with OpenAI:
```python
import requests
# Set up LINE messaging API credentials
LINE_API_URL = "https://api.line.me/v2/bot/message/push"
LINE_ACCESS_TOKEN = "YOUR_LINE_ACCESS_TOKEN"
# Set up OpenAI API credentials
OPENAI_API_URL = "https://api.openai.com/v1/engines/davinci/completions"
OPENAI_API_KEY = "YOUR_OPENAI_API_KEY"
# Define function to send message using LINE API
def send_line_message(user_id, message):
headers = {
"Authorization": f"Bearer {LINE_ACCESS_TOKEN}",
"Content-Type": "application/json"
}
data = {
"to": user_id,
"messages": [
{
"type": "text",
"text": message
}
]
}
response = requests.post(LINE_API_URL, headers=headers, json=data)
return response.json()
# Define function to generate response using OpenAI API
def generate_openai_response(prompt):
headers = {
"Authorization": f"Bearer {OPENAI_API_KEY}",
"Content-Type": "application/json"
}
data = {
"prompt": prompt,
"max_tokens": 100
}
response = requests.post(OPENAI_API_URL, headers=headers, json=data)
return response.json()["choices"][0]["text"]
# Example usage
user_id = "USER_ID"
prompt = "Hello, how can I help you today?"
response = generate_openai_response(prompt)
send_line_message(user_id, response)
In this template, you can replace YOUR_LINE_ACCESS_TOKEN
and YOUR_OPENAI_API_KEY
with your actual credentials. You can then use the send_line_message
function to send a message to a specific user on LINE, and the generate_openai_response
function to generate a response using OpenAI's language model.
Row interaction log in the Dialogflow ES console:
{
"id": "9ee05192-88bb-498f-89a9-4e244cb4fc4f-fb923cf8",
"fulfillmentText": "對不起,我聽不懂你的問題。",
"language_code": "zh-tw",
"queryText": "No, I meant a template of LINE message API using openai",
"webhookPayload": {},
"intentDetectionConfidence": 1,
"action": "input.unknown",
"webhookSource": "",
"parameters": {},
"fulfillmentMessages": [
{
"text": {
"text": [
"對不起,我聽不懂你的問題。"
]
}
}
],
"diagnosticInfo": {
"webhook_latency_ms": "4893.0"
},
"webhookStatus": {
"webhookStatus": {
"code": 4,
"message": "Webhook call failed. Error: DEADLINE_EXCEEDED, State: URL_TIMEOUT, Reason: TIMEOUT_WEB."
},
"webhookUsed": true
},
"outputContexts": [
{
"lifespanCount": 1,
"name": "__system_counters__",
"parameters": {
"no-input": "0.0",
"no-match": "3.0"
}
}
],
"intent": {
"isFallback": true,
"displayName": "Default Fallback Intent",
"id": "53efae5c-f5b1-402c-be69-34f52c5d58c7"
}
}
The webhook response in Diaglogflow must within 5 seconds, or the request will time out. Right now the
input.unknown
action relays the requests to OpenAI API, that may fail and the static responses are return.