- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的聊天机器人是在 Dialogflow 中创建的,我现在正尝试从 Python 访问它,以获取用户输入并在 GUI 中显示输出(想想一个基本的聊天机器人 gui)。
我已将我的 Python 环境连接到 Dialogflow 和 firestore,
这是检测意图的代码;
#Detection of Dialogflow intents, project and input.
def detect_intent_texts(project_id, session_id, texts, language_code):
#Returns the result of detect intent with texts as inputs, later can implement same `session_id` between requests allows continuation of the conversaion.
import dialogflow_v2 as dialogflow
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)
#To ensure session path is correct - print('Session path: {}\n'.format(session))
for text in texts:
text_input = dialogflow.types.TextInput(text=text, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = session_client.detect_intent(session=session, query_input=query_input)
print ('Chatbot:{}\n'.format(response.query_result.fulfillment_text))
detect_intent_texts("chat-8","abc",["Hey"],"en-us")
我需要以某种方式说如果这个意图被触发,从数据库中获取一些东西并显示给用户。
更新
我当前的完整代码,在我看来一切正常,但它抛出了一个我不明白的错误,感谢 Sid8491 到目前为止的帮助。
简而言之,我的问题是,我以前的代码允许我输入一些内容,聊天机器人会做出响应,这一切都在控制台中,但它有效......新代码应该允许我说“当这个意图是触发,执行此操作"
import os, json
import sys
import dialogflow
from dialogflow_v2beta1 import *
import firebase_admin
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
from firebase_admin import firestore
from firebase_admin import credentials
import requests.packages.urllib3
from Tkinter import *
from dialogflow_v2beta1 import agents_client
import Tkinter as tk
result = None
window = Tk()
def Response():
#no need to use global here
result = myText.get()
displayText.configure(state='normal')
displayText.insert(END, "User:"+ result + '\n')
displayText.configure(state='disabled')
#Creating the GUI
myText = tk.StringVar()
window.resizable(False, False)
window.title("Chatbot")
window.geometry('400x400')
User_Input = tk.Entry(window, textvariable=myText, width=50).place(x=20, y=350)
subButton = tk.Button(window, text="Send", command=Response).place(x =350, y=350)
displayText = Text(window, height=20, width=40)
displayText.pack()
scroll = Scrollbar(window, command=displayText).pack(side=RIGHT)
window.mainloop()
#Initialize the firebase admin SDK
cred = credentials.Certificate('./file.json')
default_app = firebase_admin.initialize_app(cred)
db = firestore.client()
def getCourse():
doc_ref = db.collection(u"Course_Information").document(u"CourseTypes")
try:
doc = doc_ref.get()
return 'Document data: {}'.format(doc.to_dict())
except google.cloud.exceptions.NotFound:
return 'Not found'
def detect_intent_text(project_id, session_id, text, language_code):
GOOGLE_APPLICATION_CREDENTIALS=".chat-8.json"
session_client = dialogflow.SessionsClient(GOOGLE_APPLICATION_CREDENTIALS)
session = session_client.session_path(project_id, session_id)
text_input = dialogflow.types.TextInput(
text=text, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = session_client.detect_intent(
session=session, query_input=query_input)
queryText = [myText.get()]
res = detect_intent_text('chat-8', 'session-test', queryText, 'en')
intentName = res['query_result']['intent']['display_name']
if intentName == 'CourseEnquiry':
reponse = getCourse()
print json.dumps({
'fulfillmentText': reponse,
})
elif intentName == 'Greetings':
print "Yo"
detect_intent_texts("chat-8","abc", queryText,"en-us")
但是我得到这个错误:
C:\Users\chat\PycharmProjects\Chatbot\venv\Scripts\python.exe C:/Users/chat/PycharmProjects/Chatbot/venv/Chatbot.py
Traceback (most recent call last):
File "C:/Users/chat/PycharmProjects/Chatbot/venv/Chatbot.py", line 65, in <module>
res = detect_intent_text('chat-8', 'session-test', queryText, 'en')
File "C:/Users/chat/PycharmProjects/Chatbot/venv/Chatbot.py", line 51, in detect_intent_text
session_client = dialogflow.SessionsClient(GOOGLE_APPLICATION_CREDENTIALS)
File "C:\Users\chat\PycharmProjects\Chatbot\venv\lib\site-packages\dialogflow_v2\gapic\sessions_client.py", line 109, in __init__
self.sessions_stub = (session_pb2.SessionsStub(channel))
File "C:\Users\chat\PycharmProjects\Chatbot\venv\lib\site-packages\dialogflow_v2\proto\session_pb2.py", line 1248, in __init__
self.DetectIntent = channel.unary_unary(
AttributeError: 'str' object has no attribute 'unary_unary'
Process finished with exit code 1
最佳答案
是的,我认为您走在正确的轨道上。
您需要从dialogFlow
得到的响应中提取intentName
或actionaName
并调用相应的函数,然后将响应发回给用户。
res = detect_intent_texts("chat-8","abc",["Hey"],"en-us")
action = res['queryResult']['action']
if action == 'getSomethingFromDb':
reponse = someFunction(req)
return json.dumps({
'fulfillmentText': reponse,
})
elif action == 'somethingElse':
....
如果你想让它使用 intentName
而不是 actionName
那么你可以提取 intentName
如下所示
intentName = res['query_result']['intent']['display_name']
编辑 1:
示例 -
import dialogflow
import os, json
def getCourse():
doc_ref = db.collection(u"Course_Information").document(u"CourseTypes")
try:
doc = doc_ref.get()
return 'Document data: {}'.format(doc.to_dict())
except google.cloud.exceptions.NotFound:
return 'Not found'
def detect_intent_text(project_id, session_id, text, language_code):
GOOGLE_APPLICATION_CREDENTIALS="C:\\pyth_to_...\\cred.json"
session_client = dialogflow.SessionsClient(GOOGLE_APPLICATION_CREDENTIALS)
session = session_client.session_path(project_id, session_id)
text_input = dialogflow.types.TextInput(
text=text, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = session_client.detect_intent(
session=session, query_input=query_input)
queryText = 'get courses of Python' # you will call some function to get text from your app
res = detect_intent_text('project_1234', 'session-test', queryText, 'en')
intentName = res['query_result']['intent']['display_name']
if intentName == 'getCourse':
reponse = getCourse()
return json.dumps({
'fulfillmentText': reponse,
})
尝试上面的示例并根据您对应用程序的需求进行更改。我的建议是首先让 DialogFlow 在没有应用程序的情况下工作,然后将其与应用程序集成。否则您将无法了解问题是出现在 DialogFlow 中还是您的应用中。
希望对您有所帮助。
关于python - 采用 Dialogflow 意图并查询 Firestore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51621553/
作为后续问题:Laravel Redirect::intended() conditional fallbacks 我遇到了一个问题,在设置了预期的 URL session 后,即使用户决定不登录而是
我面临一个问题,即与任何意图不匹配的单词,它会假设它属于标记最多的话语的意图。 示例:如果 意图 A 由动物等话语组成 意图 B 包含“水果”等话语 意图 C 由诸如昆虫之类的话语组成 意图 D 由诸
拥有什么实际区别 subroutine fillName(person) type(PersonType), intent(inout) :: person person%name = "
我想知道 Dialogflow 中是否有任何商定的意图、事件和上下文的命名约定。 如果没有,那么如果您分享您自己的命名约定,我将不胜感激! 最佳答案 我发现“只要别人容易理解就行”这句话有点矛盾。如果
我正在尝试了解使用队列的用例。 我的理解:队列意味着一对一。唯一的用例(如果不是罕见的话,很少)是:消息仅供一次使用。 但即使在这些情况下,我也可能想使用主题(只是为了将来安全)。唯一需要额外注意的是
我的 Xcode 是 v10,我正在为 SiriKit 开发一个针对 iOS 12 的自定义 intent。 在 Xcode 10 中,自定义意图是在 .intentdefinition 文件中设计的
我有一个设置了 .intentdefinition 文件的 WidgetKit ,我可以在运行我的 WidgetKit 时从我的枚举中进行选择,但我不确定如何在代码中使用这些信息。 我希望能够根据用户
我需要为意图过滤器注册(在运行时)自定义 BroadcastReceiver 可以在 list 中描述为 并在用户通过按应用程序中的某个按钮退出应用程序时取消注册接收
根据 Fortran 标准: The INTENT (OUT) attribute for a nonpointer dummy argument specifies that the dummy a
我正在使用 Twitter Web Intents 来检查是否有人关注我。现在的问题是;我只在事件对象中获取我自己的屏幕名作为回调。 twttr.events.bind('follow', funct
编辑 很抱歉大家,这只是由于意图名称后面缺少逗号。非常抱歉x: 我最近使用 Microsoft Bot Framework (botbuilder v3.14.0)、Node.js (v8.9.4)
我正在开发的产品: RequeSTLy - Chrome 和 Firefox 扩展设置重定向、修改 header 、切换主机、插入用户脚本 ( https://www.requestly.in/ )
有什么方法可以有目的地合并对话框,这样我就不需要多余的代码片段? bot.dialog('whats-your-name', require('./dialogs/whats-your-name')
我是 Dialogflow 的新手,虽然它很容易理解,但我无法使用自定义事件触发 Intent。 我必须实现的任务是,当在后端 Node.js Webhook 中检测到警报时(例如:老板想要做某事),
在 Microsoft Bot Framework 中,我已经开始对话并运行一些意图,假设“登录”,但是当我向用户询问用户名或密码时,他可能会说“取消该”或“取消登录”,我如何获得此意图:“取消”以及
我使用 LUIS 框架构建了一个运行良好的机器人。在研究过程中遇到了以下几点 与 LUIS 意图连接后;机器人无法检查正则表达式意图喜欢 对于我正在尝试设置的对话框.matches('^helpdes
我想知道 URL 是发布到 facebook 的链接。在 Twitter 上,我可以使用“http://twitter.com/intent/tweet?text=”来发推文。虽然我试图寻找一个,但我
通读,http://www.w3schools.com/angular/angular_directives.asp我遇到了一个在评论中调用指令的例子,具体来说: 您可以在 http://www.w
我在 Stack Overflow 上的许多帖子中读到,可分配数组在传递到虚拟参数为 intent(out) 的子例程中时会被释放。 如果我考虑以下代码: program main real, di
API.ai 的预构建包可让您轻松获得长长的意图列表。目前,我正在尝试利用他们的 smalltalk 包,该包有大约 100 个意图,并对每个意图做出响应。 我正在使用api-ai-recognize
我是一名优秀的程序员,十分优秀!