-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
293 lines (235 loc) · 10.4 KB
/
service.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
import time
import uuid
from langchain.chains import GraphCypherQAChain, ConversationChain
from langchain.chat_models import AzureChatOpenAI, ChatVertexAI
from langchain.graphs import Neo4jGraph
from langchain.memory import ConversationSummaryBufferMemory
import streamlit as st
import os
import openai
from neo4j.exceptions import ConstraintError
import drivers
class NeoLangService:
def __init__(self, llm_type, temperature):
self.llm_type = llm_type
self.temperature = temperature
self.text_embedding_model = "textembedding-gecko@001"
self.db_user = os.environ.get('NEO4J_USERNAME')
self.db_password = os.environ.get('NEO4J_PASSWORD')
self.db_uri = os.environ.get('NEO4J_URI')
self.db_name = os.environ.get('NEO4J_DATABASE_NAME')
self.graph = Neo4jGraph(url=self.db_uri, username=self.db_user, password=self.db_password,
database=self.db_name)
self.driver = drivers.init_driver(self.db_uri, username=self.db_user,
password=self.db_password)
self.llm = self._init_llm()
def _init_llm(self):
if self.llm_type == "chat-bison 2k":
return ChatVertexAI(
model_name='chat-bison',
max_output_tokens=2048, # Adjusted for "2k" variant
temperature=self.temperature,
top_p=0.95,
top_k=40
)
elif self.llm_type == "GPT-4 8k":
return AzureChatOpenAI(
openai_api_version=openai.api_version,
openai_api_key=openai.api_key,
openai_api_base=os.environ.get('OPENAI_API_BASE'),
deployment_name=os.environ.get('GPT4_8K_NAME'),
model_name='gpt-4',
temperature=self.temperature
)
else:
raise ValueError(f"Unsupported LLM type: {self.llm_type}")
def get_graph_schema(self):
"""
Retrieves the graph schema from the Neo4j database.
"""
self.graph.refresh_schema()
return self.graph.schema
def generate_example_questions(self):
"""
Generates a set of example questions for Field Service Engineer context.
"""
return [
"What are the configurations associated with engine serial number XYZ123?",
"Which software components are contained in build ABC?",
"List all ECM codes related to configuration ID 456.",
]
def create_prompt(self, question: str):
"""
Creates a prompt using GraphCypherQAChain to query the Neo4j graph with the user's question.
"""
schema = self.get_graph_schema()
example_questions = self.generate_example_questions()
chain = GraphCypherQAChain.from_llm(
llm=self.llm,
graph=self.graph,
verbose=True
)
result = chain.run(question)
print(result)
prompt_template = f"""
Task: Generate Cypher statement to query a graph database.
Schema: {schema}
Example Questions: {', '.join(example_questions)}
The question is: {question}
Graph Query Results: {result}
Answer the question using the graph query results.
Provide explanations or sources if available.
"""
print(prompt_template + 'this is the prompt template')
return prompt_template
def create_conversation(self, llm_type: str):
"""
This function intializes a conversation with the llm.
The resulting conversation can be prompted successively and will
remember previous interactions.
"""
create_conversation_timer_start = time.perf_counter()
print("llm type: ", llm_type)
llm = self._init_llm()
st.session_state['llm_memory'] = ConversationSummaryBufferMemory(llm=llm, max_token_limit=1000)
res = ConversationChain(
llm=llm,
memory=st.session_state['llm_memory']
)
print("Create conversation time: " + str(
round(time.perf_counter() - create_conversation_timer_start, 4)) + " seconds.")
return res
def log_new_conversation(self, llm, user_input):
"""
This method creates a new conversation node and logs the
initial user message in the neo4j database.
Appropriate relationships are created.
"""
log_timer_start = time.perf_counter()
print('logging new conversation...')
messId = 'user-' + str(uuid.uuid4())
convId = 'conv-' + str(uuid.uuid4())
print('convId: ', convId)
def log(tx):
tx.run("""
create (c:Conversation)-[:FIRST]->(m:Message)
set c.id = $convId, c.llm = $llm,
c.temperature = $temperature,
c.public = toBoolean($public),
m.id = $messId, m.content = $content,
m.role = $role, m.postTime = datetime(),
m.embedding = $embedding,
m.public = toBoolean($public)
with c
merge (s:Session {id: $sessionId})
on create set s.createTime = datetime()
merge (s)-[:HAS_CONVERSATION]->(c)
""", convId=convId, llm=llm, messId=messId,
temperature=st.session_state['temperature'],
content=user_input, role='user', sessionId=st.session_state['session_id'],
embedding=st.session_state['recent_question_embedding'],
public=PUBLIC)
# update the latest message in the log chain
st.session_state['latest_message_id'] = messId
try:
with self.driver.session(database=self.database_name) as session:
session.execute_write(log)
except ConstraintError as err:
print(err)
session.close()
print(
'conversation init & user log time: ' + str(round(time.perf_counter() - log_timer_start, 4)) + " seconds.")
def log_user(self, user_input):
"""
This method logs a new user message to the neo4j database and
creates appropriate relationships.
"""
log_timer_start = time.perf_counter()
print('logging user message...')
prevMessId = st.session_state['latest_message_id']
messId = 'user-' + str(uuid.uuid4())
def log(tx):
tx.run("""
match (pm:Message {id: $prevMessId})
merge (m:Message {id: $messId})
set m.content = $content,
m.role = $role, m.postTime = datetime(),
m.embedding = $embedding, m.public = toBoolean($public)
merge (pm)-[:NEXT]->(m)
""", prevMessId=prevMessId, messId=messId, content=user_input, role='user',
embedding=st.session_state['recent_question_embedding'], public=PUBLIC)
# update the latest message in the log chain
st.session_state['latest_message_id'] = messId
try:
with self.driver.session(database=self.database_name) as session:
session.execute_write(log)
except Neo4jError as err:
print(err)
print('user log time: ' + str(round(time.perf_counter() - log_timer_start, 4)) + " seconds.")
def log_assistant(self, assistant_output, context_indices):
"""
This method logs a new assistant message to the neo4j database and
creates appropriate relationships.
"""
log_timer_start = time.perf_counter()
print('logging llm message...')
prevMessId = st.session_state['latest_message_id']
messId = 'llm-' + str(uuid.uuid4())
mem = st.session_state['llm_memory'].moving_summary_buffer
def log(tx):
tx.run("""
match (pm:Message {id: $prevMessId})
merge (m:Message {id: $messId})
set m.content = $content,
m.role = $role, m.postTime = datetime(),
m.numDocs = $numDocs,
m.vectorIndexSearch = true,
m.prompt = $prompt,
m.public = toBoolean($public),
m.resultingSummary = $resultingSummary
merge (pm)-[:NEXT]->(m)
with m
unwind $contextIndices as contextIdx
match (d:Document)
where d.index = contextIdx
with m, d
merge (m)-[:HAS_CONTEXT]->(d)
""", prevMessId=str(prevMessId), messId=str(messId), content=str(assistant_output),
role='assistant', contextIndices=context_indices,
numDocs=st.session_state['num_documents_for_context'],
prompt=st.session_state['general_prompt'],
resultingSummary=mem,
public=PUBLIC)
# update the latest message in the log chain
st.session_state['latest_message_id'] = messId
st.session_state['latest_llm_message_id'] = messId
try:
with self.driver.session(database=self.database_name) as session:
session.execute_write(log)
except ConstraintError as err:
print(err)
print('assistant log time: ' + str(round(time.perf_counter() - log_timer_start, 4)) + " seconds.")
def rate_message(self, rating_dict):
"""
This message rates an LLM message given a rating and uploads
the rating to the database.
"""
print('rating llm message...')
if 'latest_llm_message_id' in st.session_state:
rate_timer_start = time.perf_counter()
print('updating id: ', st.session_state['latest_llm_message_id'])
# parse rating info
message = rating_dict['text']
rating = 'Good' if rating_dict['score'] == '👍' else 'Bad'
def rate(tx):
tx.run("""
match (m:Message {id: $messId})
set m.rating = $rating,
m.ratingMessage = $message
""", rating=rating, message=message, messId=st.session_state['latest_llm_message_id'])
try:
with self.driver.session(database=self.database_name) as session:
session.execute_write(rate)
except ConstraintError as err:
print(err)
print('assistant rate time: ' + str(round(time.perf_counter() - rate_timer_start, 4)) + " seconds.")