88from langchain_openai import OpenAIEmbeddings
99import logging
1010from langchain_community .chat_message_histories import Neo4jChatMessageHistory
11- import asyncio
1211load_dotenv ()
1312
1413openai_api_key = os .environ .get ('OPENAI_API_KEY' )
@@ -30,29 +29,29 @@ def vector_embed_results(qa,question):
3029
3130 return vector_res
3231
33- def cypher_results (graph ,question ):
34- cypher_res = {}
35- try :
36- graph .refresh_schema ()
37- cypher_chain = GraphCypherQAChain .from_llm (
38- graph = graph ,
39- cypher_llm = ChatOpenAI (temperature = 0 , model = model_version ),
40- qa_llm = ChatOpenAI (temperature = 0 , model = model_version ),
41- validate_cypher = True , # Validate relationship directions
42- verbose = True ,
43- top_k = 2
44- )
45- try :
46- cypher_res = cypher_chain .invoke ({"query" : question })
47- except :
48- cypher_res = {}
32+ # def cypher_results(graph,question):
33+ # cypher_res={}
34+ # try:
35+ # graph.refresh_schema()
36+ # cypher_chain = GraphCypherQAChain.from_llm(
37+ # graph=graph,
38+ # cypher_llm=ChatOpenAI(temperature=0, model=model_version),
39+ # qa_llm=ChatOpenAI(temperature=0, model=model_version),
40+ # validate_cypher=True, # Validate relationship directions
41+ # verbose=True,
42+ # top_k=2
43+ # )
44+ # try:
45+ # cypher_res=cypher_chain.invoke({"query": question})
46+ # except:
47+ # cypher_res={}
4948
50- except Exception as e :
51- error_message = str (e )
52- logging .exception (f'Exception in CypherQAChain in QA component:{ error_message } ' )
53- # raise Exception(error_message)
49+ # except Exception as e:
50+ # error_message = str(e)
51+ # logging.exception(f'Exception in CypherQAChain in QA component:{error_message}')
52+ # # raise Exception(error_message)
5453
55- return cypher_res
54+ # return cypher_res
5655
5756def save_chat_history (uri ,userName ,password ,session_id ,user_message ,ai_message ):
5857 try :
@@ -117,17 +116,19 @@ def QA_RAG(uri,userName,password,question,session_id):
117116 llm = llm , chain_type = "stuff" , retriever = neo_db .as_retriever (search_kwargs = {'k' : 3 ,"score_threshold" : 0.5 }), return_source_documents = True
118117 )
119118
120- graph = Neo4jGraph (
121- url = uri ,
122- username = userName ,
123- password = password
124- )
125119 vector_res = vector_embed_results (qa ,question )
126120 print ('Response from Vector embeddings' )
127121 print (vector_res )
128- cypher_res = cypher_results (graph ,question )
129- print ('Response from CypherQAChain' )
130- print (cypher_res )
122+
123+ # Disable Cypher Chain QA
124+ # graph = Neo4jGraph(
125+ # url=uri,
126+ # username=userName,
127+ # password=password
128+ # )
129+ # cypher_res= cypher_results(graph,question)
130+ # print('Response from CypherQAChain')
131+ # print(cypher_res)
131132
132133 chat_summary = get_chat_history (llm ,uri ,userName ,password ,session_id )
133134
@@ -140,10 +141,11 @@ def QA_RAG(uri,userName,password,question,session_id):
140141 Given the user's query: { question } , provide a meaningful and efficient answer based
141142 on the insights derived from the following data:
142143 chat_summary:{ chat_summary }
143- Structured information: { cypher_res . get ( 'result' , '' ) } .
144+ Structured information: .
144145 Unstructured information: { vector_res .get ('result' ,'' )} .
145146
146- """
147+ """
148+
147149 print (final_prompt )
148150 response = llm .predict (final_prompt )
149151 ai_message = response
0 commit comments