-
Notifications
You must be signed in to change notification settings - Fork 4
/
ContractService.py
323 lines (263 loc) · 13 KB
/
ContractService.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
from neo4j import GraphDatabase
from typing import List
from AgreementSchema import Agreement, ClauseType,Party, ContractClause
from neo4j_graphrag.retrievers import VectorCypherRetriever,Text2CypherRetriever
from neo4j_graphrag.embeddings import OpenAIEmbeddings
from formatters import my_vector_search_excerpt_record_formatter
from neo4j_graphrag.llm import OpenAILLM
class ContractSearchService:
def __init__(self, uri, user ,pwd ):
driver = GraphDatabase.driver(uri, auth=(user, pwd))
self._driver = driver
self._openai_embedder = OpenAIEmbeddings(model = "text-embedding-3-small")
# Create LLM object. Used to generate the CYPHER queries
self._llm = OpenAILLM(model_name="gpt-4o", model_params={"temperature": 0})
async def get_contract(self, contract_id: int) -> Agreement:
GET_CONTRACT_BY_ID_QUERY = """
MATCH (a:Agreement {contract_id: $contract_id})-[:HAS_CLAUSE]->(clause:ContractClause)
WITH a, collect(clause) as clauses
MATCH (country:Country)-[i:INCORPORATED_IN]-(p:Organization)-[r:IS_PARTY_TO]-(a)
WITH a, clauses, collect(p) as parties, collect(country) as countries, collect(r) as roles, collect(i) as states
RETURN a as agreement, clauses, parties, countries, roles, states
"""
agreement_node = {}
records, _, _ = self._driver.execute_query(GET_CONTRACT_BY_ID_QUERY,{'contract_id':contract_id})
if (len(records)==1):
agreement_node = records[0].get('agreement')
party_list = records[0].get('parties')
role_list = records[0].get('roles')
country_list = records[0].get('countries')
state_list = records[0].get('states')
clause_list = records[0].get('clauses')
return await self._get_agreement(
agreement_node, format="long",
party_list=party_list, role_list=role_list,
country_list=country_list,state_list=state_list,
clause_list=clause_list
)
async def get_contracts(self, organization_name: str) -> List[Agreement]:
GET_CONTRACTS_BY_PARTY_NAME = """
CALL db.index.fulltext.queryNodes('organizationNameTextIndex', $organization_name)
YIELD node AS o, score
WITH o, score
ORDER BY score DESC
LIMIT 1
WITH o
MATCH (o)-[:IS_PARTY_TO]->(a:Agreement)
WITH a
MATCH (country:Country)-[i:INCORPORATED_IN]-(p:Organization)-[r:IS_PARTY_TO]-(a:Agreement)
RETURN a as agreement, collect(p) as parties, collect(r) as roles, collect(country) as countries, collect(i) as states
"""
#run the Cypher query
records, _ , _ = self._driver.execute_query(GET_CONTRACTS_BY_PARTY_NAME,{'organization_name':organization_name})
#Build the result
all_aggrements = []
for row in records:
agreement_node = row['agreement']
party_list = row['parties']
role_list = row['roles']
country_list = row['countries']
state_list = row['states']
agreement : Agreement = await self._get_agreement(
format="short",
agreement_node=agreement_node,
party_list=party_list,
role_list=role_list,
country_list=country_list,
state_list=state_list
)
all_aggrements.append(agreement)
return all_aggrements
async def get_contracts_with_clause_type(self, clause_type: ClauseType) -> List[Agreement]:
GET_CONTRACT_WITH_CLAUSE_TYPE_QUERY = """
MATCH (a:Agreement)-[:HAS_CLAUSE]->(cc:ContractClause {type: $clause_type})
WITH a
MATCH (country:Country)-[i:INCORPORATED_IN]-(p:Organization)-[r:IS_PARTY_TO]-(a:Agreement)
RETURN a as agreement, collect(p) as parties, collect(r) as roles, collect(country) as countries, collect(i) as states
"""
#run the Cypher query
records, _ , _ = self._driver.execute_query(GET_CONTRACT_WITH_CLAUSE_TYPE_QUERY,{'clause_type': str(clause_type.value)})
# Process the results
all_agreements = []
for row in records:
agreement_node = row['agreement']
party_list = row['parties']
role_list = row['roles']
country_list = row['countries']
state_list = row['states']
agreement : Agreement = await self._get_agreement(
format="short",
agreement_node=agreement_node,
party_list=party_list,
role_list=role_list,
country_list=country_list,
state_list=state_list
)
all_agreements.append(agreement)
return all_agreements
async def get_contracts_without_clause(self, clause_type: ClauseType) -> List[Agreement]:
GET_CONTRACT_WITHOUT_CLAUSE_TYPE_QUERY = """
MATCH (a:Agreement)
OPTIONAL MATCH (a)-[:HAS_CLAUSE]->(cc:ContractClause {type: $clause_type})
WITH a,cc
WHERE cc is NULL
WITH a
MATCH (country:Country)-[i:INCORPORATED_IN]-(p:Organization)-[r:IS_PARTY_TO]-(a)
RETURN a as agreement, collect(p) as parties, collect(r) as roles, collect(country) as countries, collect(i) as states
"""
#run the Cypher query
records, _ , _ = self._driver.execute_query(GET_CONTRACT_WITHOUT_CLAUSE_TYPE_QUERY,{'clause_type':clause_type.value})
all_agreements = []
for row in records:
agreement_node = row['agreement']
party_list = row['parties']
role_list = row['roles']
country_list = row['countries']
state_list = row['states']
agreement : Agreement = await self._get_agreement(
format="short",
agreement_node=agreement_node,
party_list=party_list,
role_list=role_list,
country_list=country_list,
state_list=state_list
)
all_agreements.append(agreement)
return all_agreements
async def get_contracts_similar_text(self, clause_text: str) -> List[Agreement]:
#Cypher to traverse from the semantically similar excerpts back to the agreement
EXCERPT_TO_AGREEMENT_TRAVERSAL_QUERY="""
MATCH (a:Agreement)-[:HAS_CLAUSE]->(cc:ContractClause)-[:HAS_EXCERPT]-(node)
RETURN a.name as agreement_name, a.contract_id as contract_id, cc.type as clause_type, node.text as excerpt
"""
#Set up vector Cypher retriever
retriever = VectorCypherRetriever(
driver= self._driver,
index_name="excerpt_embedding",
embedder=self._openai_embedder,
retrieval_query=EXCERPT_TO_AGREEMENT_TRAVERSAL_QUERY,
result_formatter=my_vector_search_excerpt_record_formatter
)
# run vector search query on excerpts and get results containing the relevant agreement and clause
retriever_result = retriever.search(query_text=clause_text, top_k=3)
#set up List of Agreements (with partial data) to be returned
agreements = []
for item in retriever_result.items:
content = item.content
a : Agreement = {
'agreement_name': content['agreement_name'],
'contract_id': content['contract_id']
}
c : ContractClause = {
"clause_type": content['clause_type'],
"excerpts" : [content['excerpt']]
}
a['clauses'] = [c]
agreements.append(a)
return agreements
async def answer_aggregation_question(self, user_question) -> str:
answer = ""
NEO4J_SCHEMA = """
Node properties:
Agreement {agreement_type: STRING, contract_id: INTEGER,effective_date: STRING,renewal_term: STRING, name: STRING}
ContractClause {type: STRING}
ClauseType {name: STRING}
Country {name: STRING}
Excerpt {text: STRING}
Organization {name: STRING}
Relationship properties:
IS_PARTY_TO {role: STRING}
GOVERNED_BY_LAW {state: STRING}
HAS_CLAUSE {type: STRING}
INCORPORATED_IN {state: STRING}
The relationships:
(:Agreement)-[:HAS_CLAUSE]->(:ContractClause)
(:ContractClause)-[:HAS_EXCERPT]->(:Excerpt)
(:ContractClause)-[:HAS_TYPE]->(:ClauseType)
(:Agreement)-[:GOVERNED_BY_LAW]->(:Country)
(:Organization)-[:IS_PARTY_TO]->(:Agreement)
(:Organization)-[:INCORPORATED_IN]->(:Country)
"""
# Initialize the retriever
retriever = Text2CypherRetriever(
driver=self._driver,
llm=self._llm,
neo4j_schema=NEO4J_SCHEMA
)
# Generate a Cypher query using the LLM, send it to the Neo4j database, and return the results
retriever_result = retriever.search(query_text=user_question)
for item in retriever_result.items:
content = str(item.content)
if content:
answer += content + '\n\n'
return answer
async def _get_agreement (self,agreement_node, format="short", party_list=None, role_list=None,country_list=None,
state_list=None,clause_list=None,clause_dict=None):
agreement : Agreement = {}
if format == "short" and agreement_node:
agreement: Agreement = {
"contract_id" : agreement_node.get('contract_id'),
"name" : agreement_node.get('name'),
"agreement_type": agreement_node.get('agreement_type')
}
agreement['parties']= await self._get_parties (
party_list=party_list,
role_list=role_list,
country_list=country_list,
state_list=state_list)
elif format=="long" and agreement_node:
agreement: Agreement = {
"contract_id" : agreement_node.get('contract_id'),
"name" : agreement_node.get('name'),
"agreement_type": agreement_node.get('agreement_type'),
"agreement_date": agreement_node.get('agreement_date'),
"expiration_date": agreement_node.get('expiration_date'),
"renewal_term": agreement_node.get('renewal_term')
}
agreement['parties'] = await self._get_parties (
party_list=party_list,
role_list=role_list,
country_list=country_list,
state_list=state_list)
clauses = []
if clause_list:
for clause in clause_list:
clause : ContractClause = {"clause_type": clause.get('type')}
clauses.append(clause)
elif clause_dict:
for clause_type_key in clause_dict:
clause : ContractClause = {"clause_type": clause_type_key,"excerpts": clause_dict[clause_type_key]}
clauses.append(clause)
agreement['clauses'] = clauses
return agreement
async def _get_parties (self, party_list=None, role_list=None,country_list=None,state_list=None):
parties = []
if party_list:
for i in range(len(party_list)):
p: Party = {
"name": party_list[i].get('name'),
"role": role_list[i].get('role'),
"incorporation_country": country_list[i].get('name'),
"incorporation_state": state_list[i].get('state')
}
parties.append(p)
return parties
async def get_contract_excerpts (self, contract_id:int):
GET_CONTRACT_CLAUSES_QUERY = """
MATCH (a:Agreement {contract_id: $contract_id})-[:HAS_CLAUSE]->(cc:ContractClause)-[:HAS_EXCERPT]->(e:Excerpt)
RETURN a as agreement, cc.type as contract_clause_type, collect(e.text) as excerpts
"""
#run CYPHER query
clause_records, _, _ = self._driver.execute_query(GET_CONTRACT_CLAUSES_QUERY,{'contract_id':contract_id})
#get a dict d[clause_type]=list(Excerpt)
clause_dict = {}
for row in clause_records:
agreement_node = row['agreement']
clause_type = row['contract_clause_type']
relevant_excerpts = row['excerpts']
clause_dict[clause_type] = relevant_excerpts
#Agreement to return
agreement = await self._get_agreement(
format="long",agreement_node=agreement_node,
clause_dict=clause_dict)
return agreement