Spaces:
Sleeping
Sleeping
Kye Gomez commited on
Commit ·
8534dd6
1
Parent(s): b846be6
[CLEANUP]
Browse files- .env.example +1 -0
- .gitignore +55 -2
- agents.py +4 -13
- app.py +43 -20
- helpers.py +31 -33
- mongod_db.py +67 -0
- requirements.txt +1 -0
- test_func.py +9 -3
.env.example
CHANGED
|
@@ -6,3 +6,4 @@ PPLX_API_KEY=""
|
|
| 6 |
EXA_API_KEY=""
|
| 7 |
TAVILY_API_KEY=""
|
| 8 |
YOU_API_KEY=""
|
|
|
|
|
|
| 6 |
EXA_API_KEY=""
|
| 7 |
TAVILY_API_KEY=""
|
| 8 |
YOU_API_KEY=""
|
| 9 |
+
MONGO_URI=""
|
.gitignore
CHANGED
|
@@ -1,11 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# Byte-compiled / optimized / DLL files
|
|
|
|
| 2 |
__pycache__/
|
| 3 |
*.py[cod]
|
| 4 |
*$py.class
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
# C extensions
|
| 7 |
*.so
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
# Distribution / packaging
|
| 10 |
.Python
|
| 11 |
build/
|
|
@@ -81,7 +133,7 @@ target/
|
|
| 81 |
# IPython
|
| 82 |
profile_default/
|
| 83 |
ipython_config.py
|
| 84 |
-
|
| 85 |
# pyenv
|
| 86 |
# For a library or package, you might want to ignore these files since the code is
|
| 87 |
# intended to run in multiple environments; otherwise, check them in:
|
|
@@ -158,3 +210,4 @@ cython_debug/
|
|
| 158 |
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 159 |
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 160 |
#.idea/
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
.venv/
|
| 3 |
+
|
| 4 |
+
.env
|
| 5 |
+
|
| 6 |
+
image/
|
| 7 |
+
audio/
|
| 8 |
+
video/
|
| 9 |
+
artifacts_three
|
| 10 |
+
dataframe/
|
| 11 |
+
|
| 12 |
+
static/generated
|
| 13 |
+
runs
|
| 14 |
+
artifacts_five
|
| 15 |
+
chroma
|
| 16 |
+
Unit Testing Agent_state.json
|
| 17 |
+
Devin_state.json
|
| 18 |
+
json_logs
|
| 19 |
+
Medical Image Diagnostic Agent_state.json
|
| 20 |
+
D_state.json
|
| 21 |
+
artifacts_six
|
| 22 |
+
artifacts_seven
|
| 23 |
+
swarms/__pycache__
|
| 24 |
+
artifacts
|
| 25 |
+
transcript_generator.json
|
| 26 |
+
venv
|
| 27 |
+
.DS_Store
|
| 28 |
+
Cargo.lock
|
| 29 |
+
.DS_STORE
|
| 30 |
+
artifacts_logs
|
| 31 |
+
Cargo.lock
|
| 32 |
+
Medical Treatment Recommendation Agent_state.json
|
| 33 |
+
swarms/agents/.DS_Store
|
| 34 |
+
artifacts_two
|
| 35 |
+
logs
|
| 36 |
+
T_state.json
|
| 37 |
+
_build
|
| 38 |
+
conversation.txt
|
| 39 |
+
t1_state.json
|
| 40 |
+
stderr_log.txt
|
| 41 |
+
t2_state.json
|
| 42 |
+
.vscode
|
| 43 |
+
.DS_STORE
|
| 44 |
# Byte-compiled / optimized / DLL files
|
| 45 |
+
Transcript Generator_state.json
|
| 46 |
__pycache__/
|
| 47 |
*.py[cod]
|
| 48 |
*$py.class
|
| 49 |
+
.grit
|
| 50 |
+
swarm-worker-01_state.json
|
| 51 |
+
error.txt
|
| 52 |
+
Devin Worker 2_state.json
|
| 53 |
# C extensions
|
| 54 |
*.so
|
| 55 |
+
.ruff_cache
|
| 56 |
+
|
| 57 |
|
| 58 |
+
errors.txt
|
| 59 |
+
|
| 60 |
+
Autonomous-Agent-XYZ1B_state.json
|
| 61 |
# Distribution / packaging
|
| 62 |
.Python
|
| 63 |
build/
|
|
|
|
| 133 |
# IPython
|
| 134 |
profile_default/
|
| 135 |
ipython_config.py
|
| 136 |
+
.DS_Store
|
| 137 |
# pyenv
|
| 138 |
# For a library or package, you might want to ignore these files since the code is
|
| 139 |
# intended to run in multiple environments; otherwise, check them in:
|
|
|
|
| 210 |
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 211 |
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 212 |
#.idea/
|
| 213 |
+
.vscode/settings.json
|
agents.py
CHANGED
|
@@ -32,7 +32,6 @@ class Perplexity(BaseLLM):
|
|
| 32 |
A class to interact with the Perplexity API using OpenAI's interface.
|
| 33 |
"""
|
| 34 |
|
| 35 |
-
|
| 36 |
def __init__(self, api_key: str = perplexity_api_key(), *args, **kwargs):
|
| 37 |
"""
|
| 38 |
Initialize the Perplexity class with an API key.
|
|
@@ -120,9 +119,7 @@ class ExaAgent(BaseLLM):
|
|
| 120 |
dict: The search results from the Exa API.
|
| 121 |
"""
|
| 122 |
try:
|
| 123 |
-
results = self.exa.search(
|
| 124 |
-
task, use_autoprompt=True, *args, **kwargs
|
| 125 |
-
)
|
| 126 |
return results
|
| 127 |
except Exception as e:
|
| 128 |
raise RuntimeError(f"Error running the search query: {e}")
|
|
@@ -153,9 +150,7 @@ class ResearchAgent:
|
|
| 153 |
"""
|
| 154 |
self.api_key = api_key
|
| 155 |
|
| 156 |
-
self.llm = OpenAIChat(
|
| 157 |
-
temperature=temperature, max_tokens=max_tokens
|
| 158 |
-
)
|
| 159 |
self.agent = self._initialize_agent()
|
| 160 |
|
| 161 |
def _initialize_agent(self):
|
|
@@ -197,9 +192,7 @@ class ResearchAgent:
|
|
| 197 |
Returns:
|
| 198 |
str: The agent's response after processing the task.
|
| 199 |
"""
|
| 200 |
-
articles = fetch_web_articles_bing_api(
|
| 201 |
-
task, subscription_key=self.api_key
|
| 202 |
-
)
|
| 203 |
sources_prompts = "".join([task, articles])
|
| 204 |
agent_response = self.agent.run(sources_prompts)
|
| 205 |
return agent_response
|
|
@@ -260,9 +253,7 @@ class TavilyWrapper:
|
|
| 260 |
raise TypeError("Task must be a string")
|
| 261 |
|
| 262 |
try:
|
| 263 |
-
response = self.client.search(
|
| 264 |
-
query=task, search_depth="advanced"
|
| 265 |
-
)
|
| 266 |
return response
|
| 267 |
except Exception as e:
|
| 268 |
raise RuntimeError(f"Error performing search: {e}")
|
|
|
|
| 32 |
A class to interact with the Perplexity API using OpenAI's interface.
|
| 33 |
"""
|
| 34 |
|
|
|
|
| 35 |
def __init__(self, api_key: str = perplexity_api_key(), *args, **kwargs):
|
| 36 |
"""
|
| 37 |
Initialize the Perplexity class with an API key.
|
|
|
|
| 119 |
dict: The search results from the Exa API.
|
| 120 |
"""
|
| 121 |
try:
|
| 122 |
+
results = self.exa.search(task, use_autoprompt=True, *args, **kwargs)
|
|
|
|
|
|
|
| 123 |
return results
|
| 124 |
except Exception as e:
|
| 125 |
raise RuntimeError(f"Error running the search query: {e}")
|
|
|
|
| 150 |
"""
|
| 151 |
self.api_key = api_key
|
| 152 |
|
| 153 |
+
self.llm = OpenAIChat(temperature=temperature, max_tokens=max_tokens)
|
|
|
|
|
|
|
| 154 |
self.agent = self._initialize_agent()
|
| 155 |
|
| 156 |
def _initialize_agent(self):
|
|
|
|
| 192 |
Returns:
|
| 193 |
str: The agent's response after processing the task.
|
| 194 |
"""
|
| 195 |
+
articles = fetch_web_articles_bing_api(task, subscription_key=self.api_key)
|
|
|
|
|
|
|
| 196 |
sources_prompts = "".join([task, articles])
|
| 197 |
agent_response = self.agent.run(sources_prompts)
|
| 198 |
return agent_response
|
|
|
|
| 253 |
raise TypeError("Task must be a string")
|
| 254 |
|
| 255 |
try:
|
| 256 |
+
response = self.client.search(query=task, search_depth="advanced")
|
|
|
|
|
|
|
| 257 |
return response
|
| 258 |
except Exception as e:
|
| 259 |
raise RuntimeError(f"Error performing search: {e}")
|
app.py
CHANGED
|
@@ -1,31 +1,50 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import random
|
| 3 |
from helpers import query_you_com, query_tavily, query_perplexity
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Set Streamlit to wide mode
|
| 6 |
st.set_page_config(layout="wide")
|
| 7 |
|
|
|
|
| 8 |
# Define the function to process the question
|
| 9 |
def ProcessQuestion(question):
|
| 10 |
# Randomly select two out of the three functions
|
| 11 |
functions = [query_you_com, query_tavily, query_perplexity]
|
| 12 |
selected_functions = random.sample(functions, 2)
|
| 13 |
-
|
| 14 |
# Get answers from the selected functions
|
| 15 |
answer_a = selected_functions[0](question)
|
| 16 |
answer_b = selected_functions[1](question)
|
| 17 |
-
|
| 18 |
return answer_a, answer_b
|
| 19 |
|
|
|
|
| 20 |
# Initialize session state if not already done
|
| 21 |
-
if
|
| 22 |
-
st.session_state[
|
| 23 |
-
if
|
| 24 |
-
st.session_state[
|
| 25 |
-
if
|
| 26 |
-
st.session_state[
|
| 27 |
-
if
|
| 28 |
-
st.session_state[
|
| 29 |
|
| 30 |
# Streamlit app layout
|
| 31 |
st.title("Chatbot Comparison")
|
|
@@ -35,7 +54,9 @@ input_col, control_col = st.columns([4, 1])
|
|
| 35 |
|
| 36 |
with input_col:
|
| 37 |
# Text box for user input with character limit
|
| 38 |
-
question = st.text_area(
|
|
|
|
|
|
|
| 39 |
|
| 40 |
with control_col:
|
| 41 |
# Submit button
|
|
@@ -48,27 +69,29 @@ if submit_button:
|
|
| 48 |
answer_a, answer_b = ProcessQuestion(question)
|
| 49 |
|
| 50 |
# Save answers and state to session state
|
| 51 |
-
st.session_state[
|
| 52 |
-
st.session_state[
|
| 53 |
-
st.session_state[
|
| 54 |
-
st.session_state[
|
| 55 |
else:
|
| 56 |
-
st.error(
|
|
|
|
|
|
|
| 57 |
else:
|
| 58 |
st.error("Please enter a question.")
|
| 59 |
|
| 60 |
# Display results if available in session state
|
| 61 |
-
if st.session_state[
|
| 62 |
col1, col2 = st.columns(2)
|
| 63 |
|
| 64 |
with col1:
|
| 65 |
st.write("### Output A")
|
| 66 |
-
st.write(st.session_state[
|
| 67 |
|
| 68 |
with col2:
|
| 69 |
st.write("### Output B")
|
| 70 |
-
st.write(st.session_state[
|
| 71 |
-
|
| 72 |
feedback_col = st.columns([1, 1, 1, 1])
|
| 73 |
|
| 74 |
with feedback_col[0]:
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import random
|
| 3 |
from helpers import query_you_com, query_tavily, query_perplexity
|
| 4 |
+
from mongod_db import MongoDBHandler
|
| 5 |
+
|
| 6 |
+
from pydantic import BaseModel
|
| 7 |
+
from typing import List
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
db_handler = MongoDBHandler()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class UsageLog(BaseModel):
|
| 14 |
+
question: str
|
| 15 |
+
selected_functions: List[str]
|
| 16 |
+
answer_a: str
|
| 17 |
+
answer_b: str
|
| 18 |
+
feedback: str
|
| 19 |
+
timestamp: str
|
| 20 |
+
|
| 21 |
|
| 22 |
# Set Streamlit to wide mode
|
| 23 |
st.set_page_config(layout="wide")
|
| 24 |
|
| 25 |
+
|
| 26 |
# Define the function to process the question
|
| 27 |
def ProcessQuestion(question):
|
| 28 |
# Randomly select two out of the three functions
|
| 29 |
functions = [query_you_com, query_tavily, query_perplexity]
|
| 30 |
selected_functions = random.sample(functions, 2)
|
| 31 |
+
|
| 32 |
# Get answers from the selected functions
|
| 33 |
answer_a = selected_functions[0](question)
|
| 34 |
answer_b = selected_functions[1](question)
|
| 35 |
+
|
| 36 |
return answer_a, answer_b
|
| 37 |
|
| 38 |
+
|
| 39 |
# Initialize session state if not already done
|
| 40 |
+
if "results_displayed" not in st.session_state:
|
| 41 |
+
st.session_state["results_displayed"] = False
|
| 42 |
+
if "answer_a" not in st.session_state:
|
| 43 |
+
st.session_state["answer_a"] = ""
|
| 44 |
+
if "answer_b" not in st.session_state:
|
| 45 |
+
st.session_state["answer_b"] = ""
|
| 46 |
+
if "question" not in st.session_state:
|
| 47 |
+
st.session_state["question"] = ""
|
| 48 |
|
| 49 |
# Streamlit app layout
|
| 50 |
st.title("Chatbot Comparison")
|
|
|
|
| 54 |
|
| 55 |
with input_col:
|
| 56 |
# Text box for user input with character limit
|
| 57 |
+
question = st.text_area(
|
| 58 |
+
"Enter your question here (max 1000 characters):", max_chars=1000
|
| 59 |
+
)
|
| 60 |
|
| 61 |
with control_col:
|
| 62 |
# Submit button
|
|
|
|
| 69 |
answer_a, answer_b = ProcessQuestion(question)
|
| 70 |
|
| 71 |
# Save answers and state to session state
|
| 72 |
+
st.session_state["answer_a"] = answer_a
|
| 73 |
+
st.session_state["answer_b"] = answer_b
|
| 74 |
+
st.session_state["question"] = question
|
| 75 |
+
st.session_state["results_displayed"] = True
|
| 76 |
else:
|
| 77 |
+
st.error(
|
| 78 |
+
"Your question exceeds the 1,000 character limit. Please shorten your question."
|
| 79 |
+
)
|
| 80 |
else:
|
| 81 |
st.error("Please enter a question.")
|
| 82 |
|
| 83 |
# Display results if available in session state
|
| 84 |
+
if st.session_state["results_displayed"]:
|
| 85 |
col1, col2 = st.columns(2)
|
| 86 |
|
| 87 |
with col1:
|
| 88 |
st.write("### Output A")
|
| 89 |
+
st.write(st.session_state["answer_a"])
|
| 90 |
|
| 91 |
with col2:
|
| 92 |
st.write("### Output B")
|
| 93 |
+
st.write(st.session_state["answer_b"])
|
| 94 |
+
|
| 95 |
feedback_col = st.columns([1, 1, 1, 1])
|
| 96 |
|
| 97 |
with feedback_col[0]:
|
helpers.py
CHANGED
|
@@ -6,10 +6,11 @@ import os
|
|
| 6 |
load_dotenv()
|
| 7 |
|
| 8 |
# Get API keys from environment variables
|
| 9 |
-
YOU_COM_API_KEY = os.getenv(
|
| 10 |
-
TAVILY_API_KEY = os.getenv(
|
| 11 |
-
PERPLEXITY_API_KEY = os.getenv(
|
| 12 |
-
BRAVE_API_KEY = os.getenv(
|
|
|
|
| 13 |
|
| 14 |
def query_you_com(query):
|
| 15 |
headers = {"X-API-Key": YOU_COM_API_KEY}
|
|
@@ -22,7 +23,7 @@ def query_you_com(query):
|
|
| 22 |
)
|
| 23 |
response.raise_for_status() # Raises an HTTPError if the response code was unsuccessful
|
| 24 |
resp = response.json()
|
| 25 |
-
return resp[
|
| 26 |
except requests.exceptions.HTTPError as http_err:
|
| 27 |
return f"HTTP error occurred: {http_err}"
|
| 28 |
except Exception as err:
|
|
@@ -39,21 +40,22 @@ def query_tavily(query):
|
|
| 39 |
"include_raw_content": False,
|
| 40 |
"max_results": 1,
|
| 41 |
"include_domains": [],
|
| 42 |
-
"exclude_domains": []
|
| 43 |
}
|
| 44 |
response = requests.post("https://api.tavily.com/search", json=payload)
|
| 45 |
if response.status_code == 200:
|
| 46 |
resp = response.json()
|
| 47 |
-
return resp[
|
| 48 |
else:
|
| 49 |
return f"Request failed with status code: {response.status_code}"
|
| 50 |
|
|
|
|
| 51 |
def query_perplexity(query):
|
| 52 |
-
url =
|
| 53 |
headers = {
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
}
|
| 58 |
data = {
|
| 59 |
"model": "llama-3-sonar-large-32k-online",
|
|
@@ -62,23 +64,18 @@ def query_perplexity(query):
|
|
| 62 |
"frequency_penalty": 1,
|
| 63 |
"temperature": 0.0,
|
| 64 |
"messages": [
|
| 65 |
-
{
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
},
|
| 69 |
-
{
|
| 70 |
-
"role": "user",
|
| 71 |
-
"content": query
|
| 72 |
-
}
|
| 73 |
-
]
|
| 74 |
}
|
| 75 |
response = requests.post(url, headers=headers, json=data)
|
| 76 |
if response.status_code == 200:
|
| 77 |
result = response.json()
|
| 78 |
-
return result[
|
| 79 |
else:
|
| 80 |
return f"Request failed with status code: {response.status_code}"
|
| 81 |
|
|
|
|
| 82 |
# def query_brave(query):
|
| 83 |
# headers = {"X-API-Key": BRAVE_API_KEY}
|
| 84 |
# params = {
|
|
@@ -91,58 +88,59 @@ def query_perplexity(query):
|
|
| 91 |
# return response.json().get("summary", "No summary available.")
|
| 92 |
# else:
|
| 93 |
# return f"Request failed with status code: {response}"
|
| 94 |
-
|
| 95 |
|
| 96 |
# def brave_search_summarization(query):
|
| 97 |
# # Endpoint for web search with summary
|
| 98 |
# web_search_url = "https://api.search.brave.com/res/v1/web/search"
|
| 99 |
# summarizer_url = "https://api.search.brave.com/res/v1/summarizer/search"
|
| 100 |
-
|
| 101 |
# # Headers for the requests
|
| 102 |
# headers = {
|
| 103 |
# "Accept": "application/json",
|
| 104 |
# "Accept-Encoding": "gzip",
|
| 105 |
# "X-Subscription-Token": BRAVE_API_KEY
|
| 106 |
# }
|
| 107 |
-
|
| 108 |
# # Parameters for the initial web search request
|
| 109 |
# web_search_params = {
|
| 110 |
# "q": query,
|
| 111 |
# "summary": 1
|
| 112 |
# }
|
| 113 |
-
|
| 114 |
# # Make the initial request to the web search endpoint
|
| 115 |
# web_search_response = requests.get(web_search_url, headers=headers, params=web_search_params)
|
| 116 |
-
|
| 117 |
# # Check if the request was successful
|
| 118 |
# if web_search_response.status_code != 200:
|
| 119 |
# raise Exception(f"Web search request failed with status code {web_search_response.status_code}")
|
| 120 |
-
|
| 121 |
# web_search_data = web_search_response.json()
|
| 122 |
-
|
| 123 |
# # Extract the summarizer key from the response
|
| 124 |
# summarizer_key = web_search_data.get('summarizer', {}).get('key')
|
| 125 |
# if not summarizer_key:
|
| 126 |
# raise Exception("No summarizer key found in the web search response")
|
| 127 |
-
|
| 128 |
# # Parameters for the summarizer request
|
| 129 |
# summarizer_params = {
|
| 130 |
# "key": summarizer_key,
|
| 131 |
# "entity_info": 1
|
| 132 |
# }
|
| 133 |
-
|
| 134 |
# # Make the request to the summarizer endpoint
|
| 135 |
# summarizer_response = requests.get(summarizer_url, headers=headers, params=summarizer_params)
|
| 136 |
-
|
| 137 |
# # Check if the request was successful
|
| 138 |
# if summarizer_response.status_code != 200:
|
| 139 |
# raise Exception(f"Summarizer request failed with status code {summarizer_response.status_code}")
|
| 140 |
-
|
| 141 |
# summarizer_data = summarizer_response.json()
|
| 142 |
-
|
| 143 |
# # Return the summarized content
|
| 144 |
# return summarizer_data
|
| 145 |
|
|
|
|
| 146 |
def ProcessQuestion(question, model):
|
| 147 |
if model == "You.com":
|
| 148 |
return query_you_com(question)
|
|
|
|
| 6 |
load_dotenv()
|
| 7 |
|
| 8 |
# Get API keys from environment variables
|
| 9 |
+
YOU_COM_API_KEY = os.getenv("YOU_API_KEY")
|
| 10 |
+
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
|
| 11 |
+
PERPLEXITY_API_KEY = os.getenv("PPLX_API_KEY")
|
| 12 |
+
BRAVE_API_KEY = os.getenv("BRAVESEARCH_API_KEY")
|
| 13 |
+
|
| 14 |
|
| 15 |
def query_you_com(query):
|
| 16 |
headers = {"X-API-Key": YOU_COM_API_KEY}
|
|
|
|
| 23 |
)
|
| 24 |
response.raise_for_status() # Raises an HTTPError if the response code was unsuccessful
|
| 25 |
resp = response.json()
|
| 26 |
+
return resp["answer"]
|
| 27 |
except requests.exceptions.HTTPError as http_err:
|
| 28 |
return f"HTTP error occurred: {http_err}"
|
| 29 |
except Exception as err:
|
|
|
|
| 40 |
"include_raw_content": False,
|
| 41 |
"max_results": 1,
|
| 42 |
"include_domains": [],
|
| 43 |
+
"exclude_domains": [],
|
| 44 |
}
|
| 45 |
response = requests.post("https://api.tavily.com/search", json=payload)
|
| 46 |
if response.status_code == 200:
|
| 47 |
resp = response.json()
|
| 48 |
+
return resp["answer"]
|
| 49 |
else:
|
| 50 |
return f"Request failed with status code: {response.status_code}"
|
| 51 |
|
| 52 |
+
|
| 53 |
def query_perplexity(query):
|
| 54 |
+
url = "https://api.perplexity.ai/chat/completions"
|
| 55 |
headers = {
|
| 56 |
+
"Accept": "application/json",
|
| 57 |
+
"Content-Type": "application/json",
|
| 58 |
+
"Authorization": f"Bearer {PERPLEXITY_API_KEY}",
|
| 59 |
}
|
| 60 |
data = {
|
| 61 |
"model": "llama-3-sonar-large-32k-online",
|
|
|
|
| 64 |
"frequency_penalty": 1,
|
| 65 |
"temperature": 0.0,
|
| 66 |
"messages": [
|
| 67 |
+
{"role": "system", "content": "Be precise and concise in your responses."},
|
| 68 |
+
{"role": "user", "content": query},
|
| 69 |
+
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
}
|
| 71 |
response = requests.post(url, headers=headers, json=data)
|
| 72 |
if response.status_code == 200:
|
| 73 |
result = response.json()
|
| 74 |
+
return result["choices"][0]["message"]["content"]
|
| 75 |
else:
|
| 76 |
return f"Request failed with status code: {response.status_code}"
|
| 77 |
|
| 78 |
+
|
| 79 |
# def query_brave(query):
|
| 80 |
# headers = {"X-API-Key": BRAVE_API_KEY}
|
| 81 |
# params = {
|
|
|
|
| 88 |
# return response.json().get("summary", "No summary available.")
|
| 89 |
# else:
|
| 90 |
# return f"Request failed with status code: {response}"
|
| 91 |
+
|
| 92 |
|
| 93 |
# def brave_search_summarization(query):
|
| 94 |
# # Endpoint for web search with summary
|
| 95 |
# web_search_url = "https://api.search.brave.com/res/v1/web/search"
|
| 96 |
# summarizer_url = "https://api.search.brave.com/res/v1/summarizer/search"
|
| 97 |
+
|
| 98 |
# # Headers for the requests
|
| 99 |
# headers = {
|
| 100 |
# "Accept": "application/json",
|
| 101 |
# "Accept-Encoding": "gzip",
|
| 102 |
# "X-Subscription-Token": BRAVE_API_KEY
|
| 103 |
# }
|
| 104 |
+
|
| 105 |
# # Parameters for the initial web search request
|
| 106 |
# web_search_params = {
|
| 107 |
# "q": query,
|
| 108 |
# "summary": 1
|
| 109 |
# }
|
| 110 |
+
|
| 111 |
# # Make the initial request to the web search endpoint
|
| 112 |
# web_search_response = requests.get(web_search_url, headers=headers, params=web_search_params)
|
| 113 |
+
|
| 114 |
# # Check if the request was successful
|
| 115 |
# if web_search_response.status_code != 200:
|
| 116 |
# raise Exception(f"Web search request failed with status code {web_search_response.status_code}")
|
| 117 |
+
|
| 118 |
# web_search_data = web_search_response.json()
|
| 119 |
+
|
| 120 |
# # Extract the summarizer key from the response
|
| 121 |
# summarizer_key = web_search_data.get('summarizer', {}).get('key')
|
| 122 |
# if not summarizer_key:
|
| 123 |
# raise Exception("No summarizer key found in the web search response")
|
| 124 |
+
|
| 125 |
# # Parameters for the summarizer request
|
| 126 |
# summarizer_params = {
|
| 127 |
# "key": summarizer_key,
|
| 128 |
# "entity_info": 1
|
| 129 |
# }
|
| 130 |
+
|
| 131 |
# # Make the request to the summarizer endpoint
|
| 132 |
# summarizer_response = requests.get(summarizer_url, headers=headers, params=summarizer_params)
|
| 133 |
+
|
| 134 |
# # Check if the request was successful
|
| 135 |
# if summarizer_response.status_code != 200:
|
| 136 |
# raise Exception(f"Summarizer request failed with status code {summarizer_response.status_code}")
|
| 137 |
+
|
| 138 |
# summarizer_data = summarizer_response.json()
|
| 139 |
+
|
| 140 |
# # Return the summarized content
|
| 141 |
# return summarizer_data
|
| 142 |
|
| 143 |
+
|
| 144 |
def ProcessQuestion(question, model):
|
| 145 |
if model == "You.com":
|
| 146 |
return query_you_com(question)
|
mongod_db.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Dict, List
|
| 2 |
+
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 |
+
|
| 14 |
+
|
| 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 |
+
) -> None:
|
| 22 |
+
"""
|
| 23 |
+
Initializes the MongoDBHandler with the specified database and collection.
|
| 24 |
+
|
| 25 |
+
Args:
|
| 26 |
+
uri (str): The MongoDB URI connection string.
|
| 27 |
+
db_name (str): The name of the database.
|
| 28 |
+
collection_name (str): The name of the collection.
|
| 29 |
+
"""
|
| 30 |
+
self.client = MongoClient(uri)
|
| 31 |
+
self.db = self.client[db_name]
|
| 32 |
+
self.collection = self.db[collection_name]
|
| 33 |
+
|
| 34 |
+
def add(self, document: Dict[str, Any]) -> None:
|
| 35 |
+
"""
|
| 36 |
+
Adds a document to the MongoDB collection.
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
document (Dict[str, Any]): The document to be added to the collection.
|
| 40 |
+
|
| 41 |
+
Raises:
|
| 42 |
+
PyMongoError: If an error occurs while inserting the document.
|
| 43 |
+
"""
|
| 44 |
+
try:
|
| 45 |
+
self.collection.insert_one(document)
|
| 46 |
+
except PyMongoError as e:
|
| 47 |
+
print(f"Error: {e}")
|
| 48 |
+
raise
|
| 49 |
+
|
| 50 |
+
def query(self, query: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 51 |
+
"""
|
| 52 |
+
Queries documents from the MongoDB collection based on the provided query.
|
| 53 |
+
|
| 54 |
+
Args:
|
| 55 |
+
query (Dict[str, Any]): The query to filter documents.
|
| 56 |
+
|
| 57 |
+
Returns:
|
| 58 |
+
List[Dict[str, Any]]: A list of documents that match the query.
|
| 59 |
+
|
| 60 |
+
Raises:
|
| 61 |
+
PyMongoError: If an error occurs while querying the documents.
|
| 62 |
+
"""
|
| 63 |
+
try:
|
| 64 |
+
return list(self.collection.find(query))
|
| 65 |
+
except PyMongoError as e:
|
| 66 |
+
print(f"Error: {e}")
|
| 67 |
+
raise
|
requirements.txt
CHANGED
|
@@ -3,3 +3,4 @@ exa_py
|
|
| 3 |
tavily-python
|
| 4 |
openai
|
| 5 |
exa_py
|
|
|
|
|
|
| 3 |
tavily-python
|
| 4 |
openai
|
| 5 |
exa_py
|
| 6 |
+
pymongo
|
test_func.py
CHANGED
|
@@ -1,4 +1,9 @@
|
|
| 1 |
-
from helpers import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def test_queries():
|
| 4 |
test_query = "How is the weather in Palo Alto, CA?"
|
|
@@ -6,11 +11,11 @@ def test_queries():
|
|
| 6 |
print("Testing You.com API:")
|
| 7 |
# you_com_result = query_you_com(test_query)
|
| 8 |
you_com_result = query_you_com(test_query)
|
| 9 |
-
print(you_com_result[
|
| 10 |
|
| 11 |
print("\nTesting Tavily.com API:")
|
| 12 |
tavily_result = query_tavily(test_query)
|
| 13 |
-
print(tavily_result[
|
| 14 |
|
| 15 |
print("\nTesting Perplexity.ai API:")
|
| 16 |
perplexity_result = query_perplexity(test_query)
|
|
@@ -20,5 +25,6 @@ def test_queries():
|
|
| 20 |
# brave_result = brave_search_summarization(test_query)
|
| 21 |
# print(brave_result)
|
| 22 |
|
|
|
|
| 23 |
if __name__ == "__main__":
|
| 24 |
test_queries()
|
|
|
|
| 1 |
+
from helpers import (
|
| 2 |
+
query_you_com,
|
| 3 |
+
query_tavily,
|
| 4 |
+
query_perplexity,
|
| 5 |
+
) # , brave_search_summarization,
|
| 6 |
+
|
| 7 |
|
| 8 |
def test_queries():
|
| 9 |
test_query = "How is the weather in Palo Alto, CA?"
|
|
|
|
| 11 |
print("Testing You.com API:")
|
| 12 |
# you_com_result = query_you_com(test_query)
|
| 13 |
you_com_result = query_you_com(test_query)
|
| 14 |
+
print(you_com_result["answer"])
|
| 15 |
|
| 16 |
print("\nTesting Tavily.com API:")
|
| 17 |
tavily_result = query_tavily(test_query)
|
| 18 |
+
print(tavily_result["answer"])
|
| 19 |
|
| 20 |
print("\nTesting Perplexity.ai API:")
|
| 21 |
perplexity_result = query_perplexity(test_query)
|
|
|
|
| 25 |
# brave_result = brave_search_summarization(test_query)
|
| 26 |
# print(brave_result)
|
| 27 |
|
| 28 |
+
|
| 29 |
if __name__ == "__main__":
|
| 30 |
test_queries()
|