Artem0729 commited on
Commit
a5fba64
·
verified ·
1 Parent(s): f6b36be

Add function to search for 3 or more mediators

Browse files
Files changed (1) hide show
  1. app.py +26 -40
app.py CHANGED
@@ -36,6 +36,10 @@ class MediatorRetriever(BaseRetriever):
36
  "parameters": {
37
  "type": "object",
38
  "properties": {
 
 
 
 
39
  "mediator country": {
40
  "type": "string",
41
  "description": "Extract mediator's country that user wants to search."
@@ -44,10 +48,6 @@ class MediatorRetriever(BaseRetriever):
44
  "type": "string",
45
  "description": "Extract mediator's city that user wants to search."
46
  },
47
- "mediator state": {
48
- "type": "string",
49
- "description": "Extract mediator's state that user wants to search. If both mediator city and mediator state are possible, please extract as mediator state."
50
- },
51
  "mediator areas of practice": {
52
  "type": "array",
53
  "description": description,
@@ -61,7 +61,7 @@ class MediatorRetriever(BaseRetriever):
61
  response = openai_client.chat.completions.create(
62
  model="gpt-4-1106-preview",
63
  messages=[
64
- {"role": "system", "content": "You are a professional mediation field searcher. Your role is to extract information about mediators from user's message. but you have to provide several mediators not one. If there are no mediators in the specific city, then it should return mediators from the state."},
65
  {"role": "user", "content": message}
66
  ],
67
  functions=tools,
@@ -74,7 +74,6 @@ class MediatorRetriever(BaseRetriever):
74
  yield {"search_status" : False, "data" : delta.content}
75
  elif delta and delta.function_call:
76
  new_message += delta.function_call.arguments
77
-
78
  if new_message:
79
  yield {"search_status" : True, "data" : new_message}
80
 
@@ -83,6 +82,7 @@ class MediatorRetriever(BaseRetriever):
83
  matching_documents = []
84
  message = ""
85
  metaData = self.getMetadata(query)
 
86
  for data in metaData:
87
  if data['data'] is None:
88
  break
@@ -112,7 +112,7 @@ class MediatorRetriever(BaseRetriever):
112
  response = openai_client.chat.completions.create(
113
  model="gpt-4-1106-preview",
114
  messages=[
115
- {"role": "system", "content": "Generate a message asking the user about the conflict to better match them with a mediator."},
116
  {"role": "user", "content": query}
117
  ],
118
  stream=True
@@ -150,7 +150,7 @@ class MediatorRetriever(BaseRetriever):
150
  "properties": {
151
  "mediator": {
152
  "type": "number",
153
- "description": "The number of mediators that the user wants to search. If the user asks for a list of mediators, it means the user wants to search at least 3 mediators. If the user's message doesn't have information about the number of mediators, you have to respond with at least 3.",
154
  "default": 3
155
  }
156
  },
@@ -169,7 +169,7 @@ class MediatorRetriever(BaseRetriever):
169
  stream=True
170
  )
171
  try:
172
- number_str = response.choices[0].message.tool_calls[0].function.arguments
173
  mediator_num = max(int(json.loads(number_str)['mediator']), 3)
174
  except:
175
  mediator_num = 3
@@ -193,7 +193,7 @@ class MediatorRetriever(BaseRetriever):
193
  embeddings = OpenAIEmbeddings(api_key=openai_api_key)
194
 
195
  index = pc.Index(pinecone_index)
196
-
197
  results = index.query(
198
  vector=embeddings.embed_query(query),
199
  top_k=5,
@@ -201,24 +201,17 @@ class MediatorRetriever(BaseRetriever):
201
  include_metadata=True
202
  )
203
 
204
- if (not results['matches']) and 'mediator state' in metadata:
205
- del metadata['mediator city']
206
- results = index.query(
207
- vector=embeddings.embed_query(query),
208
- top_k=5,
209
- filter=metadata,
210
- include_metadata=True
211
- )
212
-
213
  new_data = []
214
  for result in results['matches']:
215
  data = {}
216
  for metadata_key in metadata_list:
217
  data[metadata_key] = result['metadata'].get(metadata_key, "N/A")
 
 
 
218
 
219
- if practice_data in result['metadata'].get('mediator areas of practice', []):
220
  new_data.append(data)
221
-
222
  random.shuffle(new_data)
223
 
224
  if new_data:
@@ -298,7 +291,7 @@ chatbot = gr.Chatbot(avatar_images=["user.png", "bot.jpg"], height="100%",contai
298
 
299
  js_code = """
300
  <style>
301
- html body {
302
  background: transparent !important;
303
  }
304
 
@@ -352,8 +345,8 @@ js_code = """
352
  border-radius: 9px;
353
  height: 100%;
354
  opacity: 0;
355
- transform: scaleY(0); /* Start with scaleY 0 */
356
- transform-origin: bottom; /* Transform origin at bottom */
357
  transition: transform 0.5s ease-out, opacity 0.5s ease-out;
358
  overflow: hidden;
359
  position:fixed;
@@ -376,15 +369,16 @@ js_code = """
376
  border-bottom-left-radius: 0px;
377
  border-bottom-right-radius: 0px;
378
  }
379
- .gradio-container {
380
  padding:0 !important;
 
381
  }
382
-
383
  gradio-app {
384
  background: none !important;
385
  }
386
 
387
- </style>
388
  <script>
389
  function toggleChatbox() {
390
  window.parent.postMessage(
@@ -406,33 +400,25 @@ js_code = """
406
 
407
 
408
  function autoScroll() {
409
- const chatbox = document.querySelector('.bubble-wrap');
410
  if (chatbox) {
411
  chatbox.scrollTop = chatbox.scrollHeight;
412
  }
413
  }
414
 
415
  setTimeout(() => {
416
- document.getElementById('message-btn').addEventListener('click', toggleChatbox);
417
  const userInputBox = document.querySelector('textarea.scroll-hide');
418
  userInputBox?.addEventListener('keypress', autoScroll, false);
419
- }, 2000);
420
  </script>
421
  """
422
 
423
- def toggle_chatbox():
424
- global chatbox_visible
425
- chatbox_visible = not chatbox_visible
426
- return gr.update(visible=chatbox_visible)
427
-
428
- chatbox_visible = True
429
-
430
  with gr.Blocks(head=js_code) as demo:
431
- with gr.Column(visible=chatbox_visible) as chatbox_container:
432
  chatbox = gr.ChatInterface(fn=search, title="Mediate.com Chatbot Application", multimodal=False, retry_btn=None, clear_btn=None, undo_btn=None, chatbot=chatbot)
433
 
434
- # Add an event listener to the button
435
- gr.HTML(f'<button src="/file=message.png" id="message-btn"/><img style="display: inline;" src="/file=message.png" id="custom-btn"/><button>')
436
 
437
  if __name__ == "__main__":
438
  demo.launch(debug=True, allowed_paths=["./"])
 
36
  "parameters": {
37
  "type": "object",
38
  "properties": {
39
+ "mediator state": {
40
+ "type": "string",
41
+ "description": "Extract mediator's state that user wants to search. If both mediator city and mediator state are possible, please extract as mediator state."
42
+ },
43
  "mediator country": {
44
  "type": "string",
45
  "description": "Extract mediator's country that user wants to search."
 
48
  "type": "string",
49
  "description": "Extract mediator's city that user wants to search."
50
  },
 
 
 
 
51
  "mediator areas of practice": {
52
  "type": "array",
53
  "description": description,
 
61
  response = openai_client.chat.completions.create(
62
  model="gpt-4-1106-preview",
63
  messages=[
64
+ {"role": "system", "content": "You are a professional arbitration field searcher. Your role is to extract information about the moderator from the user's message. If you have less than 3 mediators in a particular city, you should search for mediators in your state. If you have less than 3 mediators in a particular state that you are searching for, you should search for mediators in the states closest to the state that you are searching for. For example, if you have less than 3 mediators in California, you should search for mediators in Oregon, Arizona, or Nevada closest to California. Important: You must provide at least three moderators."},
65
  {"role": "user", "content": message}
66
  ],
67
  functions=tools,
 
74
  yield {"search_status" : False, "data" : delta.content}
75
  elif delta and delta.function_call:
76
  new_message += delta.function_call.arguments
 
77
  if new_message:
78
  yield {"search_status" : True, "data" : new_message}
79
 
 
82
  matching_documents = []
83
  message = ""
84
  metaData = self.getMetadata(query)
85
+
86
  for data in metaData:
87
  if data['data'] is None:
88
  break
 
112
  response = openai_client.chat.completions.create(
113
  model="gpt-4-1106-preview",
114
  messages=[
115
+ {"role": "system", "content": "Generate a message asking the user about the conflict to better match them with mediators."},
116
  {"role": "user", "content": query}
117
  ],
118
  stream=True
 
150
  "properties": {
151
  "mediator": {
152
  "type": "number",
153
+ "description": "The number of mediators the user wants to search for. If a user requests a list of mediators, this means that the user wants to search for at least three mediators. If your message does not include information about the number of mediators, you must provide at least three mediators. If you only have one arbitrator, you must search three mediators in the state and out of state. In example, If there is no three mediators in California search the mediators from Oregon, Arizona, or Nevada. So you must provide at least three mediators. IMPORTANT: you must provide at least three mediators.",
154
  "default": 3
155
  }
156
  },
 
169
  stream=True
170
  )
171
  try:
172
+ number_str = response.choices[0].message.function_call.arguments
173
  mediator_num = max(int(json.loads(number_str)['mediator']), 3)
174
  except:
175
  mediator_num = 3
 
193
  embeddings = OpenAIEmbeddings(api_key=openai_api_key)
194
 
195
  index = pc.Index(pinecone_index)
196
+
197
  results = index.query(
198
  vector=embeddings.embed_query(query),
199
  top_k=5,
 
201
  include_metadata=True
202
  )
203
 
 
 
 
 
 
 
 
 
 
204
  new_data = []
205
  for result in results['matches']:
206
  data = {}
207
  for metadata_key in metadata_list:
208
  data[metadata_key] = result['metadata'].get(metadata_key, "N/A")
209
+ # practice_data = result['metadata'].get('mediator areas of practice', "N/A")
210
+ if not practice_data:
211
+ continue
212
 
213
+ if practice_data:
214
  new_data.append(data)
 
215
  random.shuffle(new_data)
216
 
217
  if new_data:
 
291
 
292
  js_code = """
293
  <style>
294
+ html body {
295
  background: transparent !important;
296
  }
297
 
 
345
  border-radius: 9px;
346
  height: 100%;
347
  opacity: 0;
348
+ transform: scaleY(0);
349
+ transform-origin: bottom;
350
  transition: transform 0.5s ease-out, opacity 0.5s ease-out;
351
  overflow: hidden;
352
  position:fixed;
 
369
  border-bottom-left-radius: 0px;
370
  border-bottom-right-radius: 0px;
371
  }
372
+ .gradio-container{
373
  padding:0 !important;
374
+ background-color:transparent;
375
  }
376
+
377
  gradio-app {
378
  background: none !important;
379
  }
380
 
381
+ </style>
382
  <script>
383
  function toggleChatbox() {
384
  window.parent.postMessage(
 
400
 
401
 
402
  function autoScroll() {
403
+ const chatbox = document.querySelector('#component-4');
404
  if (chatbox) {
405
  chatbox.scrollTop = chatbox.scrollHeight;
406
  }
407
  }
408
 
409
  setTimeout(() => {
410
+ document.getElementById('message-btn').addEventListener('click', toggleChatbox);
411
  const userInputBox = document.querySelector('textarea.scroll-hide');
412
  userInputBox?.addEventListener('keypress', autoScroll, false);
413
+ }, 1000);
414
  </script>
415
  """
416
 
 
 
 
 
 
 
 
417
  with gr.Blocks(head=js_code) as demo:
418
+ with gr.Column() as chatbox_container:
419
  chatbox = gr.ChatInterface(fn=search, title="Mediate.com Chatbot Application", multimodal=False, retry_btn=None, clear_btn=None, undo_btn=None, chatbot=chatbot)
420
 
421
+ gr.HTML(f'<button id="message-btn"/><img style="display: inline;" src="/file=message.png" id="custom-btn"/><button>')
 
422
 
423
  if __name__ == "__main__":
424
  demo.launch(debug=True, allowed_paths=["./"])