|
6 | 6 | from langchain.chains import RetrievalQA |
7 | 7 | from langchain_openai import ChatOpenAI |
8 | 8 | from langchain_openai import OpenAIEmbeddings |
| 9 | +import logging |
9 | 10 | load_dotenv() |
10 | 11 |
|
11 | 12 | openai_api_key = os.environ.get('OPENAI_API_KEY') |
12 | 13 |
|
13 | 14 | def vector_embed_results(qa,question): |
14 | | - # question ="What do you know about machine learning" |
15 | | - result = qa({"query": question}) |
16 | 15 | vector_res={} |
17 | | - vector_res['result']=result["result"] |
18 | | - list_source_docs=[] |
19 | | - for i in result["source_documents"]: |
20 | | - list_source_docs.append(i.metadata['source']) |
21 | | - vector_res['source']=list_source_docs |
| 16 | + try: |
| 17 | + # question ="What do you know about machine learning" |
| 18 | + result = qa({"query": question}) |
| 19 | + vector_res['result']=result["result"] |
| 20 | + list_source_docs=[] |
| 21 | + for i in result["source_documents"]: |
| 22 | + list_source_docs.append(i.metadata['source']) |
| 23 | + vector_res['source']=list_source_docs |
| 24 | + except Exception as e: |
| 25 | + error_message = str(e) |
| 26 | + logging.exception(f'Exception in vector embedding in QA component:{error_message}') |
| 27 | + raise Exception(error_message) |
| 28 | + |
22 | 29 | return vector_res |
23 | 30 |
|
24 | 31 | def cypher_results(graph,question,model_version): |
25 | | - graph.refresh_schema() |
26 | | - cypher_chain = GraphCypherQAChain.from_llm( |
27 | | - graph=graph, |
28 | | - # cypher_llm=ChatOpenAI(temperature=0, model="gpt-4"), |
29 | | - cypher_llm=ChatOpenAI(temperature=0, model=model_version), |
30 | | - qa_llm=ChatOpenAI(temperature=0, model=model_version), |
31 | | - validate_cypher=True, # Validate relationship directions |
32 | | - verbose=True, |
33 | | - top_k=2 |
34 | | - ) |
| 32 | + cypher_res={} |
| 33 | + try: |
| 34 | + graph.refresh_schema() |
| 35 | + cypher_chain = GraphCypherQAChain.from_llm( |
| 36 | + graph=graph, |
| 37 | + # cypher_llm=ChatOpenAI(temperature=0, model="gpt-4"), |
| 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 | + |
| 45 | + cypher_res=cypher_chain.invoke({"query": question}) |
| 46 | + |
| 47 | + except Exception as e: |
| 48 | + error_message = str(e) |
| 49 | + logging.exception(f'Exception in CypherQAChain in QA component:{error_message}') |
| 50 | + raise Exception(error_message) |
35 | 51 |
|
36 | | - cypher_res=cypher_chain.invoke({"query": question}) |
37 | 52 | return cypher_res |
| 53 | + |
38 | 54 |
|
39 | 55 |
|
40 | 56 | def QA_RAG(uri,userName,password,model_version,question): |
41 | | - if model_version=='OpenAI GPT 3.5': |
42 | | - model_version='gpt-3.5-turbo' |
43 | | - elif model_version=='OpenAI GPT 4': |
44 | | - model_version='gpt-4-0125-preview' |
45 | | - retrieval_query=""" |
46 | | - MATCH (node)-[:PART_OF]->(d:Document) |
47 | | - WITH d, apoc.text.join(collect(node.text),"\n----\n") as text, avg(score) as score |
48 | | - RETURN text, score, {source: COALESCE(CASE WHEN d.url CONTAINS "None" THEN d.fileName ELSE d.url END, d.fileName)} as metadata |
49 | | - """ |
| 57 | + try: |
| 58 | + if model_version=='OpenAI GPT 3.5': |
| 59 | + model_version='gpt-3.5-turbo' |
| 60 | + elif model_version=='OpenAI GPT 4': |
| 61 | + model_version='gpt-4-0125-preview' |
| 62 | + retrieval_query=""" |
| 63 | + MATCH (node)-[:PART_OF]->(d:Document) |
| 64 | + WITH d, apoc.text.join(collect(node.text),"\n----\n") as text, avg(score) as score |
| 65 | + RETURN text, score, {source: COALESCE(CASE WHEN d.url CONTAINS "None" THEN d.fileName ELSE d.url END, d.fileName)} as metadata |
| 66 | + """ |
50 | 67 |
|
51 | | - neo_db=Neo4jVector.from_existing_index( |
52 | | - embedding=OpenAIEmbeddings(), |
| 68 | + neo_db=Neo4jVector.from_existing_index( |
| 69 | + embedding=OpenAIEmbeddings(), |
| 70 | + url=uri, |
| 71 | + username=userName, |
| 72 | + password=password, |
| 73 | + database="neo4j", |
| 74 | + index_name="vector", |
| 75 | + retrieval_query=retrieval_query, |
| 76 | + ) |
| 77 | + llm = ChatOpenAI(model= model_version, temperature=0) |
| 78 | + |
| 79 | + qa = RetrievalQA.from_chain_type( |
| 80 | + llm=llm, chain_type="stuff", retriever=neo_db.as_retriever(search_kwargs={"score_threshold": 0.5}), return_source_documents=True |
| 81 | + ) |
| 82 | + |
| 83 | + graph = Neo4jGraph( |
53 | 84 | url=uri, |
54 | 85 | username=userName, |
55 | | - password=password, |
56 | | - database="neo4j", |
57 | | - index_name="vector", |
58 | | - retrieval_query=retrieval_query, |
| 86 | + password=password |
59 | 87 | ) |
60 | | - llm = ChatOpenAI(model= model_version, temperature=0) |
61 | | - |
62 | | - qa = RetrievalQA.from_chain_type( |
63 | | - llm=llm, chain_type="stuff", retriever=neo_db.as_retriever(search_kwargs={"score_threshold": 0.5}), return_source_documents=True |
64 | | - ) |
65 | | - |
66 | | - graph = Neo4jGraph( |
67 | | - url=uri, |
68 | | - username=userName, |
69 | | - password=password |
70 | | - ) |
71 | | - vector_res=vector_embed_results(qa,question) |
72 | | - print(vector_res) |
73 | | - cypher_res= cypher_results(graph,question,model_version) |
74 | | - print(cypher_res) |
75 | | - final_prompt = f"""You are a helpful question-answering agent. Your task is to analyze |
76 | | - and synthesize information from two sources: the top result from a similarity search |
77 | | - (unstructured information) and relevant data from a graph database (structured information). |
78 | | - Given the user's query: {question}, provide a meaningful and efficient answer based |
79 | | - on the insights derived from the following data: |
80 | | - Structured information: {cypher_res['result']}. |
81 | | - Unstructured information: {vector_res['result']}. |
| 88 | + vector_res=vector_embed_results(qa,question) |
| 89 | + print(vector_res) |
| 90 | + cypher_res= cypher_results(graph,question,model_version) |
| 91 | + print(cypher_res) |
| 92 | + final_prompt = f"""You are a helpful question-answering agent. Your task is to analyze |
| 93 | + and synthesize information from two sources: the top result from a similarity search |
| 94 | + (unstructured information) and relevant data from a graph database (structured information). |
| 95 | + Given the user's query: {question}, provide a meaningful and efficient answer based |
| 96 | + on the insights derived from the following data: |
| 97 | + Structured information: {cypher_res.get('result','')}. |
| 98 | + Unstructured information: {vector_res.get('result','')}. |
82 | 99 |
|
83 | | - If structured information fails to find an answer then use the answer from unstructured information and vice versa. I only want a straightforward answer without mentioning from which source you got the answer. |
84 | | - """ |
85 | | - response = llm.predict(final_prompt) |
86 | | - res={"message":response,"user":"chatbot"} |
87 | | - return res |
| 100 | + If structured information fails to find an answer then use the answer from unstructured information and vice versa. I only want a straightforward answer without mentioning from which source you got the answer. |
| 101 | + """ |
| 102 | + print(final_prompt) |
| 103 | + response = llm.predict(final_prompt) |
| 104 | + res={"message":response,"user":"chatbot"} |
| 105 | + return res |
| 106 | + except Exception as e: |
| 107 | + error_message = str(e) |
| 108 | + logging.exception(f'Exception in in QA component:{error_message}') |
| 109 | + raise Exception(error_message) |
0 commit comments