1
1
# retrieval_engine.py
2
2
3
- from langchain_neo4j import GraphCypherQAChain
4
- from langchain_openai import ChatOpenAI
5
- from langchain .chains .retrieval import create_retrieval_chain
6
3
from langchain .chains .combine_documents import create_stuff_documents_chain
7
- from configs . openai_key import get_openai_api_key # New import
4
+ from langchain . chains . retrieval import create_retrieval_chain
8
5
from langchain .prompts import PromptTemplate
9
6
7
+
10
8
class RetrievalEngine :
11
9
def __init__ (self , resume_processor , neo4j_model ):
12
10
"""
@@ -21,15 +19,18 @@ def __init__(self, resume_processor, neo4j_model):
21
19
22
20
# Initialize Language Model (already initialized in Neo4jModel)
23
21
self .llm = self .neo4j_model .llm
24
-
22
+
25
23
# Initialize GraphCypherQAChain (already initialized in Neo4jModel)
26
24
self .graph_chain = self .neo4j_model .get_graph_chain ()
27
25
28
26
# Define the PromptTemplate with 'context' as input variable
29
- template = """
30
- You are an assistant that matches resumes to relevant job descriptions.
27
+ prompt = PromptTemplate (
28
+ template = """
29
+ You are an expert Cypher query writer for a Neo4j graph database.
31
30
32
- Given the user's resume, find the most relevant job descriptions.
31
+ Given the user's question, generate an efficient Cypher query that:
32
+ - extract entities and relationships from the following resume.
33
+ - Focus solely on the resume content.
33
34
34
35
**Entities to Extract:**
35
36
- **Education (Edu):** Details about degrees, fields of study, institutions, start and end years, GPA.
@@ -39,19 +40,29 @@ def __init__(self, resume_processor, neo4j_model):
39
40
- **Certifications (Cert):** Certification names, issuing organizations, expiration dates.
40
41
- **Soft Skills (SSkill):** Non-technical skills like leadership, communication.
41
42
43
+ **Relationships to Identify:**
44
+ - **UTILIZES_SKILL:** A Work Experience (WE) node utilizes a Skill (Skill) node.
45
+ - **USES_TECH:** A Project (Proj) node uses a Skill (Skill) node as a technology.
46
+ - **REL_TO (Proj to Skill):** A Project (Proj) node is related to a Skill (Skill) node.
47
+ - **REL_TO (Skill to Skill):** A Skill (Skill) node is similar to another Skill (Skill) node.
48
+
42
49
**Resume:**
43
50
\" \" \"
44
51
{context}
45
52
\" \" \"
46
- """
47
-
48
- self .prompt_template = PromptTemplate (
49
- template = template ,
53
+ """ ,
50
54
input_variables = ["input" ]
51
55
)
52
56
53
57
# Create a documents chain
54
- self .combine_docs_chain = create_stuff_documents_chain (self .llm , self .prompt_template )
58
+ self .combine_docs_chain = create_stuff_documents_chain (self .llm , prompt = prompt )
59
+
60
+ # Initialize Retrieval Chain
61
+ # Default node_label is 'JD'; can be adjusted as needed
62
+ self .retrieval_chain = create_retrieval_chain (
63
+ self .neo4j_model .get_retriever (node_label = "JD" ),
64
+ self .combine_docs_chain
65
+ )
55
66
56
67
def perform_mixed_retrieval (self , resume_text , node_label = "JD" ):
57
68
"""
@@ -66,38 +77,30 @@ def perform_mixed_retrieval(self, resume_text, node_label="JD"):
66
77
"""
67
78
# Process resume into a Document
68
79
doc = self .resume_processor .process_resume (resume_text )
69
-
80
+
70
81
if not doc :
71
82
return [], {}
72
-
83
+
73
84
# Store the Document in the appropriate vector store
74
85
self .neo4j_model .store_documents ([doc ], node_label = node_label )
75
-
86
+
76
87
# Access the schema property correctly
77
88
schema = self .neo4j_model .graph .get_schema
78
89
79
- # Get the retriever for the given node label
80
- retriever = self .neo4j_model .get_retriever (node_label = node_label )
81
-
82
- # Create the retrieval chain with the retriever and the combine_docs_chain
83
- retrieval_chain = create_retrieval_chain (
84
- retriever ,
85
- self .combine_docs_chain
86
- )
87
-
88
90
# Perform vector similarity search
89
- similar_docs_result = retrieval_chain .invoke ({"input" : resume_text }) # Corrected to 'context'
91
+ similar_docs_result = self . retrieval_chain .invoke ({"input" : resume_text }) # Corrected to 'context'
90
92
similar_docs = similar_docs_result .get ("output" , [])
91
93
print ("similar_docs_result:" , similar_docs_result )
92
94
print ("Keys in similar_docs_result:" , similar_docs_result .keys ())
93
-
95
+
94
96
for doc in similar_docs :
95
97
print ("Document Metadata:" , doc .metadata )
96
98
97
- query = f"Based on the following resume, recommend relevant job positions: { resume_text } "
99
+ query = (f"Based on the following resume, recommend relevant job positions based on skills and experience, "
100
+ f"while ignoring the location: { resume_text } " )
98
101
graph_response = self .graph_chain .invoke ({"query" : query , "schema" : schema })
99
102
# After graph query
100
103
print ("Graph Response:" )
101
104
print (graph_response )
102
-
103
- return similar_docs , graph_response
105
+
106
+ return similar_docs , graph_response
0 commit comments