Spaces:
Sleeping
Sleeping
worked on getting the summary from Brave Search, got the web search results and the summary output working but still need to work on converting the resulting webpages to documents.
Browse files- brave_ai.py +88 -0
- test_func.py +36 -0
brave_ai.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import requests
|
3 |
+
from typing import List, Optional
|
4 |
+
from langchain_community.document_transformers import MarkdownifyTransformer
|
5 |
+
from langchain_core.documents import Document
|
6 |
+
from langchain_core.pydantic_v1 import BaseModel, Field
|
7 |
+
|
8 |
+
class BraveAIWrapper(BaseModel):
|
9 |
+
api_key: str = Field(..., description="API key for Brave Search")
|
10 |
+
base_search_url: str = Field("https://api.search.brave.com/res/v1/web/search", const=True)
|
11 |
+
base_summarize_url: str = Field("https://api.search.brave.com/res/v1/summarizer/search", const=True)
|
12 |
+
headers: dict = Field(default_factory=dict, description="HTTP headers for API requests")
|
13 |
+
|
14 |
+
class Config:
|
15 |
+
arbitrary_types_allowed = True
|
16 |
+
|
17 |
+
def __init__(self, **data):
|
18 |
+
super().__init__(**data)
|
19 |
+
self.headers = {
|
20 |
+
"X-Subscription-Token": self.api_key,
|
21 |
+
"Accept": "application/json",
|
22 |
+
"Accept-Encoding": "gzip",
|
23 |
+
}
|
24 |
+
|
25 |
+
def get_brave_results(self, query: str, count: int = 3, safe_search: str = 'off') -> Optional[dict]:
|
26 |
+
"""
|
27 |
+
Get search results from Brave Search.
|
28 |
+
|
29 |
+
Args:
|
30 |
+
query (str): The search query.
|
31 |
+
count (int): Number of results to return.
|
32 |
+
safe_search (str): Safe search filter (off, moderate, strict).
|
33 |
+
|
34 |
+
Returns:
|
35 |
+
Optional[dict]: JSON response from Brave Search API or None if an error occurs.
|
36 |
+
"""
|
37 |
+
params = {
|
38 |
+
"q": query,
|
39 |
+
"count": count,
|
40 |
+
"summary": True,
|
41 |
+
"safe_search": safe_search,
|
42 |
+
}
|
43 |
+
try:
|
44 |
+
response = requests.get(self.base_search_url, headers=self.headers, params=params)
|
45 |
+
response.raise_for_status()
|
46 |
+
return response.json()
|
47 |
+
except requests.exceptions.RequestException as e:
|
48 |
+
print(f"Error: {e}")
|
49 |
+
return None
|
50 |
+
|
51 |
+
def summarize_results(self, summarizer_key: str) -> Optional[dict]:
|
52 |
+
"""
|
53 |
+
Summarize search results using Brave Summarizer.
|
54 |
+
|
55 |
+
Args:
|
56 |
+
summarizer_key (str): The key for the summarizer.
|
57 |
+
|
58 |
+
Returns:
|
59 |
+
Optional[dict]: JSON response from Brave Summarizer API or None if an error occurs.
|
60 |
+
"""
|
61 |
+
params = {"key": summarizer_key}
|
62 |
+
try:
|
63 |
+
response = requests.get(self.base_summarize_url, headers=self.headers, params=params)
|
64 |
+
response.raise_for_status()
|
65 |
+
return response.json()
|
66 |
+
except requests.exceptions.RequestException as e:
|
67 |
+
print(f"Error: {e}")
|
68 |
+
return None
|
69 |
+
|
70 |
+
def get_and_summarize(self, query: str, count: int = 3, safe_search: str = 'off') -> Optional[str]:
|
71 |
+
"""
|
72 |
+
Get and summarize search results from Brave Search.
|
73 |
+
|
74 |
+
Args:
|
75 |
+
query (str): The search query.
|
76 |
+
count (int): Number of results to return.
|
77 |
+
safe_search (str): Safe search filter (off, moderate, strict).
|
78 |
+
|
79 |
+
Returns:
|
80 |
+
Optional[str]: Summarized result or None if an error occurs.
|
81 |
+
"""
|
82 |
+
results = self.get_brave_results(query, count, safe_search)
|
83 |
+
if results and 'summarizer' in results:
|
84 |
+
summarizer_key = results['summarizer']['key']
|
85 |
+
summary = self.summarize_results(summarizer_key)
|
86 |
+
if summary and 'summary' in summary and len(summary['summary']) > 0:
|
87 |
+
return summary['summary'][0]['data']
|
88 |
+
return None
|
test_func.py
CHANGED
@@ -24,7 +24,43 @@ def test_queries():
|
|
24 |
# print("\nTesting Brave.com API:")
|
25 |
# brave_result = brave_search_summarization(test_query)
|
26 |
# print(brave_result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
|
29 |
if __name__ == "__main__":
|
30 |
test_queries()
|
|
|
|
24 |
# print("\nTesting Brave.com API:")
|
25 |
# brave_result = brave_search_summarization(test_query)
|
26 |
# print(brave_result)
|
27 |
+
|
28 |
+
def test_brave_ai_wrapper():
|
29 |
+
# Initialize the BraveAIWrapper with your API key
|
30 |
+
api_key = "your_api_key_here"
|
31 |
+
brave_ai = BraveAIWrapper(api_key=api_key)
|
32 |
+
|
33 |
+
# Define the test query
|
34 |
+
query = "What is some of the best mountain biking near Crested Butte, CO?"
|
35 |
+
|
36 |
+
# Test get_brave_results
|
37 |
+
print("Testing get_brave_results...")
|
38 |
+
results = brave_ai.get_brave_results(query)
|
39 |
+
if results:
|
40 |
+
print("get_brave_results output:", json.dumps(results, indent=2))
|
41 |
+
else:
|
42 |
+
print("get_brave_results failed.")
|
43 |
+
|
44 |
+
# Test get_and_summarize
|
45 |
+
print("\nTesting get_and_summarize...")
|
46 |
+
summary = brave_ai.get_and_summarize(query)
|
47 |
+
if summary:
|
48 |
+
print("get_and_summarize output:", summary)
|
49 |
+
else:
|
50 |
+
print("get_and_summarize failed.")
|
51 |
+
|
52 |
+
# Test download_documents
|
53 |
+
print("\nTesting download_documents...")
|
54 |
+
documents = brave_ai.download_documents(query)
|
55 |
+
if documents:
|
56 |
+
for doc in documents:
|
57 |
+
print("Document metadata:", doc.metadata)
|
58 |
+
print("Document content:", doc.page_content[:200]) # Print first 200 characters
|
59 |
+
print("-" * 40)
|
60 |
+
else:
|
61 |
+
print("download_documents failed.")
|
62 |
|
63 |
|
64 |
if __name__ == "__main__":
|
65 |
test_queries()
|
66 |
+
test_brave_ai_wrapper()
|