generated from streamlit/chatbot-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphAgent.py
53 lines (43 loc) · 1.94 KB
/
GraphAgent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from neo4j import GraphDatabase
from langchain import GraphAgent
class GraphAgent:
def __init__(self, uri, user, password):
self.driver = GraphDatabase.driver(uri, auth=(user, password))
def close(self):
self.driver.close()
def run_query(self, query):
with self.driver.session() as session:
result = session.run(query)
return [record for record in result]
def ask_question(self, question):
# Process the question and convert it to a Cypher query
query = self.convert_question_to_query(question)
return self.run_query(query)
def convert_question_to_query(self, question):
# Basic question conversion (this can be extended with LLMs or NLP techniques)
if "tools in MERN stack" in question.lower():
return "MATCH (t:Tool)-[:PART_OF]->(s:Stack {name: 'MERN Stack'}) RETURN t.name"
# Add more question types here
return ""
# Example usage
if __name__ == "__main__":
agent = GraphAgent(uri="bolt://localhost:7687", user="neo4j", password="your_password")
result = agent.ask_question("What are the tools in MERN stack?")
print(result)
agent.close()
from langchain.llms import OpenAI
class ExtendedGraphAgent(GraphAgent):
def __init__(self, uri, user, password, openai_api_key):
super().__init__(uri, user, password)
self.llm = OpenAI(api_key=openai_api_key)
def convert_question_to_query(self, question):
# Use LLM to convert natural language question to Cypher query
prompt = f"Convert the following question to a Cypher query: {question}"
cypher_query = self.llm(prompt)
return cypher_query
# Example usage
if __name__ == "__main__":
agent = ExtendedGraphAgent(uri="bolt://localhost:7687", user="neo4j", password="your_password", openai_api_key="your_openai_api_key")
result = agent.ask_question("List all tools in the MERN stack.")
print(result)
agent.close()