Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Google search engine #67

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions search_with_lepton.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,12 @@ def search_with_google(query: str, subscription_key: str, cx: str):
raise HTTPException(response.status_code, "Search engine error.")
json_content = response.json()
try:
contexts = json_content["items"][:REFERENCE_COUNT]
# convert to the same format as bing
items = json_content["items"][:REFERENCE_COUNT]
contexts = [
{"id": c["cacheId"], "name": c["title"], "url": c["link"], "snippet": c["snippet"]}
for c in items
]
except KeyError:
logger.error(f"Error encountered: {json_content}")
return []
Expand Down Expand Up @@ -169,7 +174,7 @@ def search_with_serper(query: str, subscription_key: str):
raise HTTPException(response.status_code, "Search engine error.")
json_content = response.json()
try:
# convert to the same format as bing/google
# convert to the same format as bing
contexts = []
if json_content.get("knowledgeGraph"):
url = json_content["knowledgeGraph"].get("descriptionUrl") or json_content["knowledgeGraph"].get("website")
Expand Down Expand Up @@ -226,7 +231,7 @@ def search_with_searchapi(query: str, subscription_key: str):
raise HTTPException(response.status_code, "Search engine error.")
json_content = response.json()
try:
# convert to the same format as bing/google
# convert to the same format as bing
contexts = []

if json_content.get("answer_box"):
Expand Down Expand Up @@ -266,14 +271,14 @@ def search_with_searchapi(query: str, subscription_key: str):
{"name": c["title"], "url": c["link"], "snippet": c.get("snippet", "")}
for c in json_content["organic_results"]
]

if json_content.get("related_questions"):
for question in json_content["related_questions"]:
if question.get("source"):
url = question.get("source").get("link", "")
else:
url = ""
url = ""

snippet = question.get("answer", "")

if url and snippet:
Expand Down
5 changes: 3 additions & 2 deletions web/src/app/components/sources.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const SourceItem: FC<{ source: Source; index: number }> = ({
source,
index,
}) => {
const { id, name, url } = source;
const { name, url } = source;
const id = source.id || url;
const domain = new URL(url).hostname;
return (
<div
Expand Down Expand Up @@ -50,7 +51,7 @@ export const Sources: FC<{ sources: Source[] }> = ({ sources }) => {
{sources.length > 0 ? (
sources.map((item, index) => (
<SourceItem
key={item.id}
key={item.id || item.url}
index={index}
source={item}
></SourceItem>
Expand Down