Custom action and endpoint for neo4j

How does one use Neo4j in a rasa chatbot, specifically in accessing the credentials? I assume I would want to create an endpoint in endpoints.yml listing my URL, username, and password (or perhaps credentials.yml), and then somehow read those three fields from inside my custom actions inside actions.py. How do I do that?

2 Likes

hi, I just wonder that whether you solved the problem :slight_smile: I just struggled with the same one.

I want to save the customer mention in chatbot on neo4j.

I made a little progress last year. For example, I could query a Neo4j graph inside Rasa and it would answer certain questions using that information. Then I changed jobs and took a 9-month break from Rasa work. But I am starting to refresh my Rasa+Neo code this month. I’m happy to chat more as I work through it again. Let me know if you have specific questions. I will try to remember to come back here and update this thread and maybe even write a blog post with details of connecting Rasa and Neo4j if I get far enough along.

1 Like

Thank you so much for your answering!

It’s really excite that i can talk with this subject @tomp. this is so hopeful to me.

Well the question that i struggled with is connecting rasa with neo4j.

I tried inserting 'http: // localhost: 7474' at 'action_point' in the rasa 'endpoints.yml', but it didn’t work.

I just wonder that is it possible connecting neo4j in the similary way as connecting track_store(something like mongodb, sql) in rasa.

And I have a question on your mention that Is ‘insert neo4j graph query’ means insert neo4j query in rasa actions.py?

You don’t have to respond urgently I will be studying about the py2neo driver and the chatbot graph structure while waiting for your answer

And If you have a chance, I’d like to ask you about the sentence linguistic graph that will go into rasa next time.

Thanks again and Hope you have a great time rest of this weeks! :slight_smile:

I will quickly say one thing, and then try to work on this in a week or two to get a better answer. What I tried a year ago was to hard-code the connection settings inside actions.py, e.g.:

from neo4j import GraphDatabase  # Might be using an old version of neo4j

#  TODO get credentials from config file instead of hard coding
neo_database_url = "bolt://localhost:7687"
neo_username = "xxxxx"
neo_password = "xxxxx"

Then I created a class inside the same file actions.py. In the run() method, I have code like this:

        #  Connect to Neo
        try:
            #  TODO get credentials from config file
            driver = GraphDatabase.driver(neo_database_url, auth=(neo_username, neo_password))
        except Exception as error:
            dispatcher.utter_message(error)
            print(error)

This might also rely on an old version of Rasa. :slight_smile: I will try the new versions and then try to put credentials in endpoints later.

Good luck.

1 Like

Thanks, I’ll try in this way :slight_smile:

**Below content is my attachment after tried something!

def __init__(self, uri, user, password):
          self.driver = GraphDatabase.driver(uri, auth=(user, password), connection_timeout=15, encrypted=False)

Above code works on me. So the next challenge for me is to connect the user message in chatbot, with neo4j database

I successfully revived my code to work with the newest versions of Rasa and Neo4j. I also hooked it up to Facebook Messenger. So, in total, it can demonstrate how a user can ask a question in Facebook Messenger, that question is sent to my laptop using ngrok, Rasa calls custom Python code in actions.py which queries a Neo4j database also running on my laptop, receives and parses the Neo4j response, writes the information into an English response string to return via ngrok to the Facebook Messenger chat as an answer to the user.

One difference from what I did before is this:

graph = Graph(password=neo_password)
...
neo_result = graph.run(query)

Let me know if you have any questions. I will probably clean up my code and write a blog post explaining all the pieces in a week or two.

It’s really appreciated of your comment above

I’ll try that today on my office hour, and post question if i have.

Have a greate time rest of the days :slight_smile:

class rasa_test_work(Action): def name(self): return ‘action_rasa_test_work’

def run(self, dispatcher, tracker, domain):
    from neo4j import GraphDatabase
    ent = tracker.latest_message['entities']
    print(ent)
    print('result is \n\n\n\n\n\n\n',tracker.latest_message)
    result_restaurant = []
    uri = "bolt://localhost:7687"
    driver = GraphDatabase.driver(uri, auth=("neo4j", "mypwd"), encrypted=False)

    def test_rasa_restaurant(tx, cuisine):
        query = '''MATCH (philip:Person {name:"Philip"}),
                (philip)-[:IS_FRIEND_OF]-(friend),
                (restaurant:Restaurant)-[:LOCATED_IN]->(:City {name:"New York"}),
                (restaurant)-[:SERVES]->(:Cuisine {name:$cuisine}),
                (friend)-[:LIKES]->(restaurant)
                RETURN restaurant.name, collect(friend.name) AS likers, count(*) AS occurence
                ORDER BY occurence DESC'''

        for record in tx.run(query, cuisine=cuisine):
            result_restaurant.append(record['restaurant.name'])
            # print(record['restaurant.name'])

    with driver.session() as session:
        session.read_transaction(test_rasa_restaurant, "Sushi")

    **driver.close()**
    dispatcher.utter_message('Recommended Restaurant is ')
    for i in result_restaurant:
        dispatcher.utter_message(i)

    return []

@tomp Hi, I did in this way!

Run query in python actions.py and it works.

By the way I have some question that should i close the driver?? ( I make bold that sentence )

Thank you :relaxed:

Plus, I really want to know how can i implement the real-time recommendation using Neo4j. It’s really confuse to know and use the concept of real-time recommendation in neo4j.

Any comments would be appriciated

Regards,

Good work.

When I used driver, e.g.

driver = GraphDatabase.driver(neo_database_url, auth=(neo_username, neo_password))

I would also close it

driver.close()

but in my new approach:

graph = Graph(password=neo_password)

I couldn’t find a close method, so I haven’t been closing it lately.

But, in both cases, I was creating a new graph each time the user uttered an utterance, which may not be best. I will probably refactor my code so it creates the graph one time at startup, uses the same graph for all utterances, and then closes it when the bot is shut down, if there is a way to close the graph. Maybe when the bot ends, everything will be closed anyway, so there may not be a need to worry about it. It might only be a concern if we opened a connection every time the user uttered a response and ran out of memory or available connections.

Let me know if you find out anything.

@tomp hi, I tried in other way.

First tried the syntax you mentioned above, graph = Graph(password=neo_password)

This, but it occurs error for me ( the log tell me there is no Graph module).

so that I try below syntax, which is just add cypher query that make a label etc. def make_graph(tx):

        query = '''WITH split("''' + last_user_message +'''"," ") AS text
                UNWIND range(0, size(text)-2) AS i 
                MERGE (w1:test_word_1{name: text[i]}) 
                MERGE (w2:test_word_1{name: text[i+1]})
                MERGE (w1)-[:NEXT]->(w2)'''
    
        tx.run(query)

The latest message is declared like below on my code.

       ` last_user_message = tracker.latest_message['text']`

Eventually, I can get below consequence.

And above picture is the chatbot result

image

But there are many challenge lefts on me Because I have to integrate the Recommend system + NLP system with neo4j.

Hope this comments are helpful for you, and Any comments are apprciated

That looks good.

I believe we are using different versions of py2neo.

My current version (after updating a week ago):

py2neo                    4.3.0                    pypi_0    pypi

Say more about your new challenges. Are you planning to integrate a recommendation system to help recommend restaurants? And what kind of NLP system?

Well,

I’m now engaged in chatbot project, I’m going to use neo4j and rasa to implement this!

And i think this is the combination of NLP and recommendation system.

Because Chatbot do recommend talking with people.

And now, is the beginning level of start, I do research to implement this.

Regards,

Sujeong

That sounds like a very good project. Yes, it sounds like a good combination of recommender systems and NLP. Hopefully Rasa’s built-in NLP will be a good start. We can always add more NLP later if we find that Rasa’s NLP is not sufficient, because it is open source.

yeah, I agree. I’ll update you if I have any questions or updates. Because I’m a novice developer, so there are a lot of things that I don’t know and get confused. But I’ll keep working hard.

Thanks
Oh, and i also I agree that we are in difference version of py2neo.

Hi Tomp I am wondering if you are working on Graph DB yet, I have couples of questions related to integration RASA and another type of Graph DB and I think your help would be useful, did you post your blogs? my questions is related to user utterances (questions) before chatbot actions come up, how can I get rid of md files of Rasa and transfer them to Graph and later make a connection between all files in flow of conversation inside the Graph, I am now wanna to know how conversation patterns works in RASA and how can I transfer it to Graph completely, Thanks

Sorry, I have not written my blog yet. It may be a while …

I’m not sure I understand your question. In my project, I was just using Neo4j like a database on the back end, not as a replacement for the Rasa file, and not as a model of conversation patterns. For example, I imagine that you want to create a graph of utterances where each node is an utterance or response and edges represent one utterance following another? And you are thinking about storing Rasa training data in a graph DB? I am sure things like these could be done with a graph DB but I’m not sure I can help figure out how to do it any time soon. Good luck. Let me know if I misunderstand your question.