Spaces:
Runtime error
Runtime error
Add function to search for 3 or more mediators
Browse files
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
|
| 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
|
| 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
|
| 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.
|
| 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
|
| 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 |
-
|
| 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);
|
| 356 |
-
transform-origin: 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 |
-
|
| 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('
|
| 410 |
if (chatbox) {
|
| 411 |
chatbox.scrollTop = chatbox.scrollHeight;
|
| 412 |
}
|
| 413 |
}
|
| 414 |
|
| 415 |
setTimeout(() => {
|
| 416 |
-
|
| 417 |
const userInputBox = document.querySelector('textarea.scroll-hide');
|
| 418 |
userInputBox?.addEventListener('keypress', autoScroll, false);
|
| 419 |
-
},
|
| 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(
|
| 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 |
-
|
| 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=["./"])
|