我正在使用 Flask 与 google cloud discoveryengine_v1 合作,我正在尝试请求新的对话。
我从文档here获取了示例代码,但在测试时它返回一个类型错误,表明该函数不返回任何内容。
代码如下:
from flask import Flask, jsonify, request
from google.cloud import discoveryengine_v1
@app.route("/new", methods=['POST'])
def new_question():
api_data = request.get_json()
answer_response = sample_converse_conversation(api_data['question']) # api_data['question'] is a string
print(answer_response)
return answer_response
def sample_converse_conversation(question):
try:
# Create a client
client = discoveryengine_v1.ConversationalSearchServiceClient()
# Initialize request argument(s)
request = discoveryengine_v1.ConverseConversationRequest(
name="<name value>", # removed the value but this has the project details
)
# Make the request
response = client.converse_conversation(request=request)
# Handle the response
return response
except Exception as e:
print(e)
以下错误:
Traceback (most recent call last):
File "C:\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 1478, in __call__
return self.wsgi_app(environ, start_response)
File "C:\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 1458, in wsgi_app
response = self.handle_exception(e)
File "C:\AppData\Local\Programs\Python\Python310\lib\site-packages\flask_cors\extension.py", line 176, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "C:\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 1455, in wsgi_app
response = self.full_dispatch_request()
File "C:\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 869, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\AppData\Local\Programs\Python\Python310\lib\site-packages\flask_cors\extension.py", line 176, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "C:\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 867, in full_dispatch_request
rv = self.dispatch_request()
File "C:\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 852, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "C:\AppData\Local\Programs\Python\Python310\lib\site-packages\flask_cors\decorator.py", line 130, in wrapped_function
resp = make_response(f(*args, **kwargs))
File "C:\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\helpers.py", line 175, in make_response
return current_app.make_response(args) # type: ignore
File "C:\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 1161, in make_response
raise TypeError( TypeError: The view function for 'new_question' did not return a valid response. The function either returned None or ended without a return statement.
我的后台目录:
为了测试,我使用邮递员来运行 API 请求。
彻底检查文档后,分配给属性名称的值末尾缺少 (-)。
projects/{project_number}/locations/{location_id}/collections/{collection}/dataStores/{data_store_id}/conversations/{conversation_id}. Use projects/{project_number}/locations/{location_id}/collections/{collection}/dataStores/{data_store_id}/conversations/-
如果有人遇到同样的情况就在这里补充一下哈哈!