Spaces:
Sleeping
Sleeping
you.com, tavily, and perplexity is working in the helper functions.
Browse files- app.py +0 -1
- helpers.py +154 -0
- test_func.py +24 -0
app.py
CHANGED
@@ -46,7 +46,6 @@ if submit_button:
|
|
46 |
# Save answers and state to session state
|
47 |
st.session_state['answer_a'] = answer_a
|
48 |
st.session_state['answer_b'] = answer_b
|
49 |
-
# st.session_state['selected_model'] = selected_model
|
50 |
st.session_state['question'] = question
|
51 |
st.session_state['results_displayed'] = True
|
52 |
else:
|
|
|
46 |
# Save answers and state to session state
|
47 |
st.session_state['answer_a'] = answer_a
|
48 |
st.session_state['answer_b'] = answer_b
|
|
|
49 |
st.session_state['question'] = question
|
50 |
st.session_state['results_displayed'] = True
|
51 |
else:
|
helpers.py
ADDED
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Load environment variables from .env file
|
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 |
+
def query_you_com(query):
|
15 |
+
headers = {"X-API-Key": YOU_COM_API_KEY}
|
16 |
+
params = {"query": query}
|
17 |
+
try:
|
18 |
+
response = requests.get(
|
19 |
+
"https://api.ydc-index.io/rag", # Verify the correctness of the API endpoint
|
20 |
+
params=params,
|
21 |
+
headers=headers,
|
22 |
+
)
|
23 |
+
response.raise_for_status() # Raises an HTTPError if the response code was unsuccessful
|
24 |
+
return response.json()
|
25 |
+
except requests.exceptions.HTTPError as http_err:
|
26 |
+
return f"HTTP error occurred: {http_err}"
|
27 |
+
except Exception as err:
|
28 |
+
return f"An error occurred: {err}"
|
29 |
+
|
30 |
+
|
31 |
+
def query_tavily(query):
|
32 |
+
payload = {
|
33 |
+
"api_key": TAVILY_API_KEY,
|
34 |
+
"query": query,
|
35 |
+
"search_depth": "basic",
|
36 |
+
"include_answer": True,
|
37 |
+
"include_images": False,
|
38 |
+
"include_raw_content": False,
|
39 |
+
"max_results": 1,
|
40 |
+
"include_domains": [],
|
41 |
+
"exclude_domains": []
|
42 |
+
}
|
43 |
+
response = requests.post("https://api.tavily.com/search", json=payload)
|
44 |
+
if response.status_code == 200:
|
45 |
+
return response.json()
|
46 |
+
else:
|
47 |
+
return f"Request failed with status code: {response.status_code}"
|
48 |
+
|
49 |
+
def query_perplexity(query):
|
50 |
+
url = 'https://api.perplexity.ai/chat/completions'
|
51 |
+
headers = {
|
52 |
+
'Accept': 'application/json',
|
53 |
+
'Content-Type': 'application/json',
|
54 |
+
'Authorization': f'Bearer {PERPLEXITY_API_KEY}'
|
55 |
+
}
|
56 |
+
data = {
|
57 |
+
"model": "llama-3-sonar-large-32k-online",
|
58 |
+
"stream": False,
|
59 |
+
"max_tokens": 1024,
|
60 |
+
"frequency_penalty": 1,
|
61 |
+
"temperature": 0.0,
|
62 |
+
"messages": [
|
63 |
+
{
|
64 |
+
"role": "system",
|
65 |
+
"content": "Be precise and concise in your responses."
|
66 |
+
},
|
67 |
+
{
|
68 |
+
"role": "user",
|
69 |
+
"content": query
|
70 |
+
}
|
71 |
+
]
|
72 |
+
}
|
73 |
+
response = requests.post(url, headers=headers, json=data)
|
74 |
+
if response.status_code == 200:
|
75 |
+
result = response.json()
|
76 |
+
return result['choices'][0]['message']['content']
|
77 |
+
else:
|
78 |
+
return f"Request failed with status code: {response.status_code}"
|
79 |
+
|
80 |
+
# def query_brave(query):
|
81 |
+
# headers = {"X-API-Key": BRAVE_API_KEY}
|
82 |
+
# params = {
|
83 |
+
# "q": query,
|
84 |
+
# "count": 1,
|
85 |
+
# "summary": True
|
86 |
+
# }
|
87 |
+
# response = requests.get("https://api.search.brave.com/res/v1/web/search", params=params, headers=headers)
|
88 |
+
# if response.status_code == 200:
|
89 |
+
# return response.json().get("summary", "No summary available.")
|
90 |
+
# else:
|
91 |
+
# return f"Request failed with status code: {response}"
|
92 |
+
|
93 |
+
|
94 |
+
# def brave_search_summarization(query):
|
95 |
+
# # Endpoint for web search with summary
|
96 |
+
# web_search_url = "https://api.search.brave.com/res/v1/web/search"
|
97 |
+
# summarizer_url = "https://api.search.brave.com/res/v1/summarizer/search"
|
98 |
+
|
99 |
+
# # Headers for the requests
|
100 |
+
# headers = {
|
101 |
+
# "Accept": "application/json",
|
102 |
+
# "Accept-Encoding": "gzip",
|
103 |
+
# "X-Subscription-Token": BRAVE_API_KEY
|
104 |
+
# }
|
105 |
+
|
106 |
+
# # Parameters for the initial web search request
|
107 |
+
# web_search_params = {
|
108 |
+
# "q": query,
|
109 |
+
# "summary": 1
|
110 |
+
# }
|
111 |
+
|
112 |
+
# # Make the initial request to the web search endpoint
|
113 |
+
# web_search_response = requests.get(web_search_url, headers=headers, params=web_search_params)
|
114 |
+
|
115 |
+
# # Check if the request was successful
|
116 |
+
# if web_search_response.status_code != 200:
|
117 |
+
# raise Exception(f"Web search request failed with status code {web_search_response.status_code}")
|
118 |
+
|
119 |
+
# web_search_data = web_search_response.json()
|
120 |
+
|
121 |
+
# # Extract the summarizer key from the response
|
122 |
+
# summarizer_key = web_search_data.get('summarizer', {}).get('key')
|
123 |
+
# if not summarizer_key:
|
124 |
+
# raise Exception("No summarizer key found in the web search response")
|
125 |
+
|
126 |
+
# # Parameters for the summarizer request
|
127 |
+
# summarizer_params = {
|
128 |
+
# "key": summarizer_key,
|
129 |
+
# "entity_info": 1
|
130 |
+
# }
|
131 |
+
|
132 |
+
# # Make the request to the summarizer endpoint
|
133 |
+
# summarizer_response = requests.get(summarizer_url, headers=headers, params=summarizer_params)
|
134 |
+
|
135 |
+
# # Check if the request was successful
|
136 |
+
# if summarizer_response.status_code != 200:
|
137 |
+
# raise Exception(f"Summarizer request failed with status code {summarizer_response.status_code}")
|
138 |
+
|
139 |
+
# summarizer_data = summarizer_response.json()
|
140 |
+
|
141 |
+
# # Return the summarized content
|
142 |
+
# return summarizer_data
|
143 |
+
|
144 |
+
def ProcessQuestion(question, model):
|
145 |
+
if model == "You.com":
|
146 |
+
return query_you_com(question)
|
147 |
+
elif model == "Tavily.com":
|
148 |
+
return query_tavily(question)
|
149 |
+
elif model == "Perplexity.ai":
|
150 |
+
return query_perplexity(question)
|
151 |
+
elif model == "Brave.com":
|
152 |
+
return query_brave(question)
|
153 |
+
else:
|
154 |
+
return "Model not supported"
|
test_func.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from helpers import query_you_com, query_tavily, query_perplexity #, brave_search_summarization,
|
2 |
+
|
3 |
+
def test_queries():
|
4 |
+
test_query = "How is the weather in Palo Alto, CA?"
|
5 |
+
|
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['answer'])
|
10 |
+
|
11 |
+
print("\nTesting Tavily.com API:")
|
12 |
+
tavily_result = query_tavily(test_query)
|
13 |
+
print(tavily_result['answer'])
|
14 |
+
|
15 |
+
print("\nTesting Perplexity.ai API:")
|
16 |
+
perplexity_result = query_perplexity(test_query)
|
17 |
+
print(perplexity_result)
|
18 |
+
|
19 |
+
# print("\nTesting Brave.com API:")
|
20 |
+
# brave_result = brave_search_summarization(test_query)
|
21 |
+
# print(brave_result)
|
22 |
+
|
23 |
+
if __name__ == "__main__":
|
24 |
+
test_queries()
|