LeoWalker commited on
Commit
bca5d39
1 Parent(s): 9a1b773

app is now logging the question, results, vote and feedback in a mongodb collection.

Browse files
Files changed (3) hide show
  1. app.py +39 -6
  2. mongod_db.py +21 -6
  3. test_mongod_db.py +32 -0
app.py CHANGED
@@ -3,8 +3,10 @@ import random
3
  from helpers import query_you_com, query_tavily, query_perplexity, query_brave
4
  from provider_info import search_providers
5
  from mongod_db import MongoDBHandler
 
6
  # from swarms.utils.loguru_logger import logger
7
  import time
 
8
 
9
  # mongo = MongoDBHandler()
10
 
@@ -38,6 +40,7 @@ Join us in our mission to advance search technology through open collaboration a
38
 
39
  # Define the function to process the question
40
  def ProcessQuestion(question):
 
41
  # Randomly select two out of the four functions
42
  functions = [query_you_com, query_tavily, query_perplexity, query_brave]
43
  selected_functions = random.sample(functions, 2)
@@ -51,21 +54,42 @@ def ProcessQuestion(question):
51
 
52
  try:
53
  # logger.info(f"Logging question: {question}")
54
- mongo.add(
55
  {
56
  "question": question,
57
  "answer_a": answer_a,
58
  "answer_b": answer_b,
59
  "selected_functions": [f.__name__ for f in selected_functions],
60
  "query_time": time.time(),
 
61
  }
62
  )
63
  # logger.info("Successfully logged into mongodb")
64
  except Exception as e:
65
  # logger.error(f"Error logging into mongodb: {e}")
66
- print("Error logging into mongodb: {e}")
67
 
68
- return answer_a, answer_b, selected_functions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
 
71
  # Initialize session state if not already done
@@ -77,7 +101,10 @@ default_values = {
77
  "source_a": "",
78
  "source_b": "",
79
  "winner": "",
80
- "selected_button": ""
 
 
 
81
  }
82
 
83
  for key, value in default_values.items():
@@ -90,17 +117,19 @@ st.title("Search Engine Agent Comparison")
90
  def on_submit():
91
  question = st.session_state["question_input"]
92
  if question:
93
- answer_a, answer_b, selected_functions = ProcessQuestion(question)
94
  st.session_state["question"] = question
95
  st.session_state["answer_a"] = answer_a
96
  st.session_state["answer_b"] = answer_b
97
  st.session_state["source_a"] = selected_functions[0].__name__
98
  st.session_state["source_b"] = selected_functions[1].__name__
99
  st.session_state["state"] = "arena_review"
 
100
 
101
  def handle_vote(vote):
102
  st.session_state["winner"] = vote
103
  st.session_state["state"] = "arena_results"
 
104
 
105
  def get_provider_info(provider_function_name):
106
  provider_name_map = {
@@ -172,8 +201,12 @@ def render_results_state():
172
  st.markdown(f"<div style='padding: 10px; border: 1px solid #ddd;'>{st.session_state['answer_b']}</div>", unsafe_allow_html=True)
173
 
174
  st.write("### Feedback")
175
- st.text_area("Please provide feedback on why you chose the winner:", key="feedback")
 
 
 
176
  st.write("### About the search providers:")
 
177
  col1, col2 = st.columns(2)
178
  with col1:
179
  st.write(f"**Website:** [{provider_info_a['website']}]({provider_info_a['website']})")
 
3
  from helpers import query_you_com, query_tavily, query_perplexity, query_brave
4
  from provider_info import search_providers
5
  from mongod_db import MongoDBHandler
6
+ from bson.objectid import ObjectId
7
  # from swarms.utils.loguru_logger import logger
8
  import time
9
+ import uuid
10
 
11
  # mongo = MongoDBHandler()
12
 
 
40
 
41
  # Define the function to process the question
42
  def ProcessQuestion(question):
43
+ document_id = None
44
  # Randomly select two out of the four functions
45
  functions = [query_you_com, query_tavily, query_perplexity, query_brave]
46
  selected_functions = random.sample(functions, 2)
 
54
 
55
  try:
56
  # logger.info(f"Logging question: {question}")
57
+ document_id = mongo.add(
58
  {
59
  "question": question,
60
  "answer_a": answer_a,
61
  "answer_b": answer_b,
62
  "selected_functions": [f.__name__ for f in selected_functions],
63
  "query_time": time.time(),
64
+ "session_id": st.session_state.session_id,
65
  }
66
  )
67
  # logger.info("Successfully logged into mongodb")
68
  except Exception as e:
69
  # logger.error(f"Error logging into mongodb: {e}")
70
+ print(f"Error logging into mongodb: {e}")
71
 
72
+ return answer_a, answer_b, selected_functions, document_id
73
+
74
+ def UpdateVote(session_id, vote):
75
+ mongo = MongoDBHandler()
76
+ try:
77
+ mongo.update(
78
+ {"session_id": session_id},
79
+ {"$set": {"vote": vote}}
80
+ )
81
+ except Exception as e:
82
+ print(f"Error updating vote in mongodb: {e}")
83
+
84
+ def UpdateFeedback(session_id, feedback):
85
+ mongo = MongoDBHandler()
86
+ try:
87
+ mongo.update(
88
+ {"session_id": session_id},
89
+ {"$set": {"feedback": feedback}}
90
+ )
91
+ except Exception as e:
92
+ print(f"Error updating feedback in mongodb: {e}")
93
 
94
 
95
  # Initialize session state if not already done
 
101
  "source_a": "",
102
  "source_b": "",
103
  "winner": "",
104
+ "selected_button": "",
105
+ "document_id": "",
106
+ "feedback": "",
107
+ "session_id": str(uuid.uuid4())
108
  }
109
 
110
  for key, value in default_values.items():
 
117
  def on_submit():
118
  question = st.session_state["question_input"]
119
  if question:
120
+ answer_a, answer_b, selected_functions, document_id = ProcessQuestion(question)
121
  st.session_state["question"] = question
122
  st.session_state["answer_a"] = answer_a
123
  st.session_state["answer_b"] = answer_b
124
  st.session_state["source_a"] = selected_functions[0].__name__
125
  st.session_state["source_b"] = selected_functions[1].__name__
126
  st.session_state["state"] = "arena_review"
127
+ st.session_state["document_id"] = document_id
128
 
129
  def handle_vote(vote):
130
  st.session_state["winner"] = vote
131
  st.session_state["state"] = "arena_results"
132
+ UpdateVote(st.session_state["session_id"], vote)
133
 
134
  def get_provider_info(provider_function_name):
135
  provider_name_map = {
 
201
  st.markdown(f"<div style='padding: 10px; border: 1px solid #ddd;'>{st.session_state['answer_b']}</div>", unsafe_allow_html=True)
202
 
203
  st.write("### Feedback")
204
+ feedback = st.text_area("Please provide feedback on why you chose the winner:")
205
+ if feedback:
206
+ UpdateFeedback(st.session_state["session_id"], feedback)
207
+
208
  st.write("### About the search providers:")
209
+
210
  col1, col2 = st.columns(2)
211
  with col1:
212
  st.write(f"**Website:** [{provider_info_a['website']}]({provider_info_a['website']})")
mongod_db.py CHANGED
@@ -3,11 +3,10 @@ from pymongo import MongoClient
3
  from pymongo.errors import PyMongoError
4
  import os
5
  from dotenv import load_dotenv
6
-
7
 
8
  load_dotenv()
9
 
10
-
11
  def mongo_db_uri():
12
  return os.getenv("MONGODB_URI")
13
 
@@ -15,9 +14,9 @@ def mongo_db_uri():
15
  class MongoDBHandler:
16
  def __init__(
17
  self,
18
- uri: str = mongo_db_uri(),
19
- db_name: str = "search-arena",
20
- collection_name: str = "search-arena-usage",
21
  *args,
22
  **kwargs
23
  ) -> None:
@@ -48,7 +47,23 @@ class MongoDBHandler:
48
  except PyMongoError as e:
49
  print(f"Error: {e}")
50
  raise
 
 
 
 
51
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  def query(self, query: Dict[str, Any]) -> List[Dict[str, Any]]:
53
  """
54
  Queries documents from the MongoDB collection based on the provided query.
@@ -66,4 +81,4 @@ class MongoDBHandler:
66
  return list(self.collection.find(query))
67
  except PyMongoError as e:
68
  print(f"Error: {e}")
69
- raise
 
3
  from pymongo.errors import PyMongoError
4
  import os
5
  from dotenv import load_dotenv
6
+ import unittest
7
 
8
  load_dotenv()
9
 
 
10
  def mongo_db_uri():
11
  return os.getenv("MONGODB_URI")
12
 
 
14
  class MongoDBHandler:
15
  def __init__(
16
  self,
17
+ uri: str = os.getenv("MONGODB_URI") or mongo_db_uri(),
18
+ db_name: str = os.getenv("MONGODB_DB") or "search-arena",
19
+ collection_name: str = os.getenv("MONGODB_COLLECTION") or "search-arena-usage",
20
  *args,
21
  **kwargs
22
  ) -> None:
 
47
  except PyMongoError as e:
48
  print(f"Error: {e}")
49
  raise
50
+
51
+ def update(self, query: Dict[str, Any], update: Dict[str, Any]) -> None:
52
+ """
53
+ Updates documents in the MongoDB collection based on the provided query and update.
54
 
55
+ Args:
56
+ query (Dict[str, Any]): The query to filter documents.
57
+ update (Dict[str, Any]): The update to apply to the documents.
58
+
59
+ Raises:
60
+ PyMongoError: If an error occurs while updating the documents.
61
+ """
62
+ try:
63
+ self.collection.update_many(query, update)
64
+ except PyMongoError as e:
65
+ print(f"Error: {e}")
66
+ raise
67
  def query(self, query: Dict[str, Any]) -> List[Dict[str, Any]]:
68
  """
69
  Queries documents from the MongoDB collection based on the provided query.
 
81
  return list(self.collection.find(query))
82
  except PyMongoError as e:
83
  print(f"Error: {e}")
84
+ raise
test_mongod_db.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+ from unittest.mock import MagicMock
3
+ from mongod_db import MongoDBHandler
4
+
5
+ class MongoDBHandlerTests(unittest.TestCase):
6
+ def setUp(self):
7
+ self.handler = MongoDBHandler(collection_name='search-arena-test')
8
+
9
+ def tearDown(self):
10
+ self.handler.collection.delete_many({})
11
+
12
+ def test_add_document(self):
13
+ document = {"name": "John Doe", "age": 30}
14
+ self.handler.add(document)
15
+ result = self.handler.collection.find_one(document)
16
+ self.assertIsNotNone(result)
17
+ self.assertEqual(result["name"], "John Doe")
18
+ self.assertEqual(result["age"], 30)
19
+
20
+ def test_query_documents(self):
21
+ document1 = {"name": "John Doe", "age": 30}
22
+ document2 = {"name": "Jane Smith", "age": 25}
23
+ self.handler.add(document1)
24
+ self.handler.add(document2)
25
+ query = {"age": {"$gt": 28}}
26
+ results = self.handler.query(query)
27
+ self.assertEqual(len(results), 1)
28
+ self.assertEqual(results[0]["name"], "John Doe")
29
+ self.assertEqual(results[0]["age"], 30)
30
+
31
+ if __name__ == "__main__":
32
+ unittest.main()