# Vector Search with Traverse Functionality using Python Libraries

**URL:** <https://community.neo4j.com/t/vector-search-with-traverse-functionality-using-python-libraries/72331>\
**Category:** General\
**Tags:** rag\
**Created:** [January 15, 2025, 4:20pm UTC](https://community.neo4j.com/t/vector-search-with-traverse-functionality-using-python-libraries/72331 "2025-01-15T16:20:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hossein](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/hossein/32/33817_2.png) [@hossein](https://community.neo4j.com/u/hossein)\
**Post date:** [January 15, 2025, 4:20pm UTC](https://community.neo4j.com/t/vector-search-with-traverse-functionality-using-python-libraries/72331/1 "2025-01-15T16:20:49Z")

</div>

🖐Hello Neo4j Community,

I am learning the basics and looking forward to your expertise.

Working on a project, I am leveraging **RAG** with LangChain to query nodes in Neo4j.

_Please see the python codes at the end of this message_.

Here’s the scenario I am dealing with:

I have a Node B (Income Statement) with a text property and an embedding property populated. Using LangChain in python script, I can easily query this node and retrieve relevant information.

Now, I have added Node A, which is connected to Node B. Node A has properties, such as a year property with a value (e.g., 2000). This node doesn't have embedding property.

My goal is to ask a question that queries both Node B and Node A, retrieving relevant information, including the properties of both nodes (e.g., the value of year from Node A and text-related content from Node B).

What kind of libraries or LangChain functions should I use to achieve this functionality effectively? Specifically:

How can I ensure that queries traverse the connection between Node A and Node B using Python scripts?

How can I retrieve both embeddings-based content from Node B and property-based data from Node A in the same query?

Any guidance or best practices would be greatly appreciated. Thank you!

Hossein J.

My python script at this time looks like this:

> import dotenv  
> import os  
> from langchain.chains import RetrievalQA  
> from langchain\_community.vectorstores import Neo4jVector  
> from langchain\_openai import OpenAIEmbeddings  
> from langchain\_openai import ChatOpenAI
> 
> # Load environment variables
> 
> dotenv.load\_dotenv()  
> os.environ['OPENAI\_API\_KEY'] = os.getenv("OPENAI\_API\_KEY")
> 
> # Function to initialize Neo4j vector index
> 
> def get\_vector\_index():  
> return Neo4jVector.from\_existing\_graph(  
> OpenAIEmbeddings(),  
> url=os.getenv('NEO4J\_URI'),  
> username=os.getenv('NEO4J\_USERNAME'),  
> password=os.getenv('NEO4J\_PASSWORD'),  
> index\_name='Vi',  
> node\_label="Income Statement",  
> text\_node\_properties=['title', 'drafting', 'engineering\_consulting', 'total\_revenue'],  
> embedding\_node\_property='embedding'  
> )
> 
> # Create vector index retriever
> 
> vector\_index = get\_vector\_index()
> 
> # Initialize the RAG QA chain
> 
> vector\_qa = RetrievalQA.from\_chain\_type(  
> llm=ChatOpenAI(),  
> chain\_type="stuff",  
> retriever=vector\_index.as\_retriever()  
> )
> 
> # Manually input your query
> 
> query = input("Enter your question here: ")
> 
> # Run the query and get the result
> 
> try:  
> response = vector\_qa.invoke({"query": query})  
> print("Query Result:")  
> print(response['result'])  
> except Exception as e:  
> print(f"An error occurred: {e}")

---

<div class="post-metadata">

**Author:** ![hossein](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/hossein/32/33817_2.png) [@hossein](https://community.neo4j.com/u/hossein)\
**Post date:** [January 17, 2025, 6:32pm UTC](https://community.neo4j.com/t/vector-search-with-traverse-functionality-using-python-libraries/72331/2 "2025-01-17T18:32:41Z")

</div>

I spent a bit more and realized that I can use CypherQAChain.

It does what I was initially looking for, except the response is chopped, rather than a human-like response.

I can imagine the chopped replies can be fed into llm as context and receive a human-like reply.

I am hoping that maybe there is another easier way to retrieve information from nodes that do not have embedding property.

Any hints would be appreciated.

Thank you.  
Hossein

---

<div class="post-metadata">

**Author:** ![hossein](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/hossein/32/33817_2.png) [@hossein](https://community.neo4j.com/u/hossein)\
**Post date:** [February 7, 2025, 11:00pm UTC](https://community.neo4j.com/t/vector-search-with-traverse-functionality-using-python-libraries/72331/3 "2025-02-07T23:00:34Z")

</div>

As I am learning more. I can see that there are various ways to do the search.  
They are Vector, Graph and FullText.  
The next step is to figure out, how to put the results of 3 searches together and generate a human like response.

---

<div class="post-metadata">

**Author:** ![hossein](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/hossein/32/33817_2.png) [@hossein](https://community.neo4j.com/u/hossein)\
**Post date:** [February 12, 2025, 6:52pm UTC](https://community.neo4j.com/t/vector-search-with-traverse-functionality-using-python-libraries/72331/4 "2025-02-12T18:52:09Z")

</div>

I found more related information to my original question at:  
[GraphRAG Github](https://github.com/neo4j/neo4j-graphrag-python)

Let me explain more:  
I did let Knowledge Graph Builder to do the chunking and embedding.  
Then I wanted to put together a python script to search through the nodes.  
Above link provides directions.  
When you visit the Readme file of that site, scroll almost to the end. Find the heading of " **Performing a Similarity Search**"  
That provided ultimate answer I was looking for.  
Now I still need more improvement like inserting some Prompt Engineering so the query doesn't return information from external sources and Chat Memory features.

Happy coding,  
Hossein

---

<div class="post-metadata">

**Author:** ![hossein](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/hossein/32/33817_2.png) [@hossein](https://community.neo4j.com/u/hossein)\
**Post date:** [February 19, 2025, 4:59am UTC](https://community.neo4j.com/t/vector-search-with-traverse-functionality-using-python-libraries/72331/5 "2025-02-19T04:59:05Z")

</div>

Ok, more lessons learned:  
It seems that with GraphRag, I have to use OpenAILLM.

That is all right - but how do I utilize prompts? After some tryouts, I realized that the answer is: RagTemplate()

The next question is how to keep the chat history? With ChatOpenAI, it is relatively easy. But with GraphRAG, I have to use OpenAILLM. I have to read more and find out how to implement chat history with this wrapper(OpenAILLM)?
