Skip to content

Commit

Permalink
Merge pull request #455 from l3vels/feat/search-api
Browse files Browse the repository at this point in the history
feat: google search suggestions
  • Loading branch information
Chkhikvadze committed Apr 18, 2024
2 parents 27456b9 + cf5378d commit d897f4f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
33 changes: 33 additions & 0 deletions apps/server/controllers/google_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from typing import List

from fastapi import APIRouter, Depends
from fastapi.responses import JSONResponse
from serpapi.google_search import GoogleSearch

router = APIRouter()


# Assuming you have a function to fetch the secret API key
def get_secret_api_key():
# Your implementation to get the API key
return "be37de5344aaa3f5444f1083f4f303a11cce32bd84456a7387a52efeab006e88"


@router.post("/suggestions")
def autocomplete_search(q: str, api_key: str = Depends(get_secret_api_key)):
"""
Perform autocomplete search using Serpapi.
Args:
q (str): The query string for autocomplete search.
api_key (str): Serpapi API key.
Returns:
List[str]: List of autocomplete suggestions.
"""
params = {"engine": "google_autocomplete", "q": q, "api_key": api_key}

search = GoogleSearch(params)
results = search.get_dict()
suggestions = results["suggestions"]
return JSONResponse(content=suggestions)
4 changes: 4 additions & 0 deletions apps/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from controllers.datasource import router as datasource_router
from controllers.file import router as file_router
from controllers.fine_tuning import router as fine_tuning_router
from controllers.google_search import router as google_search_router
from controllers.integrations import router as integrations_router
from controllers.llm import router as llm_router
from controllers.model import router as model_router
Expand Down Expand Up @@ -133,6 +134,9 @@ def jwt_exception_handler(request: Request, exc: AuthJWTException):
app.include_router(integrations_router, prefix="/integrations", include_in_schema=False)
app.include_router(fine_tuning_router, prefix="/fine-tuning", include_in_schema=False)
app.include_router(voice_router, prefix="/voice", include_in_schema=True)
app.include_router(
google_search_router, prefix="/google_search", include_in_schema=False
)


@app.get("/", include_in_schema=False)
Expand Down

0 comments on commit d897f4f

Please sign in to comment.