0

对于下面的代码,我收到一个错误,请告诉我如何解决这个问题

class GenerateQuery:

    @staticmethod
    def get_nlg(graph_query):
#        graph = Graph("http://localhost:7474",auth=("neo4j", "pass"))
#        graph_response = graph.evaluate(graph_query)
#        return graph_response
        driver = GraphDatabase.driver("neo4j://localhost:7687", auth=("neo4j","pass"))
        with driver.session() as session:
            graph_response = session.run(graph_query)
            return graph_response

    @staticmethod
    def product_review(summary_comp,prod_comp):
        """
        :param summary_comp: product summary
        :param prod_comp: product node name
        :return: Summary/Review of the corresponding product
        """
        query = u'MATCH(s:Store)<-[r:REVIEWED]-(c:Customer) RETURN s.name as ProductName, r.summary as ProductReview'
        graph_response = GenerateQuery.get_nlg(query)
        return graph_response

当上面的结果传递给下面的代码时,它给出了一个错误:

class ProductReview(Action):
    def name(self):
        return "action_review"

    def run(self, dispatcher, tracker, domain):
        intent = tracker.latest_message['intent']
        summary_comp = tracker.get_slot('summary')
        prod_comp = tracker.get_slot('node')
        graph_response = GenerateQuery.product_review(summary_comp,prod_comp)
        dispatcher.utter_message(json.dumps(graph_response))

错误是:

Traceback (most recent call last):
  File "/home/sangeetha/Desktop/RiQue/venv/lib/python3.6/site-packages/sanic/app.py", line 939, in handle_request
    response = await response
  File "/home/sangeetha/Desktop/RiQue/venv/lib/python3.6/site-packages/rasa_sdk/endpoint.py", line 112, in webhook
    return response.json(result, status=200)
  File "/home/sangeetha/Desktop/RiQue/venv/lib/python3.6/site-packages/sanic/response.py", line 210, in json
    dumps(body, **kwargs),
TypeError: <neo4j.work.result.Result object at 0x7f3f4bd01470> is not JSON serializable
4

1 回答 1

0

Result并不意味着要序列化,它包含在事务终止时释放的事务绑定数据。

您必须先提取数据,然后再对其进行序列化。您可以get_nlg使用以下内容进行更改:

return [record.data() for record in graph_response]

作为旁注,session.run最好替换为session.read_transaction(又名交易功能)。

于 2020-11-19T14:54:10.873 回答