khronoz commited on
Commit
58e210e
·
1 Parent(s): 4bb5ec7

Update Search Frontend & API

Browse files
backend/backend/app/api/routers/search.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+
3
+ from app.utils.index import get_index
4
+ from fastapi import APIRouter, Depends, HTTPException, Request, status
5
+ from llama_index import VectorStoreIndex
6
+ from llama_index.postprocessor import SimilarityPostprocessor
7
+ from llama_index.retrievers import VectorIndexRetriever
8
+
9
+ search_router = r = APIRouter()
10
+
11
+ """
12
+ This router is for search functionality which consist of query engine.
13
+ The query engine is used to query the index.
14
+ It is similar to query except that it does not return the formulated response.
15
+ Instead it returns the relevant information from the index.
16
+ """
17
+
18
+
19
+ @r.get("")
20
+ async def search(
21
+ request: Request,
22
+ index: VectorStoreIndex = Depends(get_index),
23
+ ):
24
+ query = request.query_params.get("search")
25
+ logger = logging.getLogger("uvicorn")
26
+ logger.info(f"Search: {query}")
27
+ if query is None:
28
+ raise HTTPException(
29
+ status_code=status.HTTP_400_BAD_REQUEST,
30
+ detail="No search info provided",
31
+ )
32
+
33
+ # configure retriever
34
+ retriever = VectorIndexRetriever(
35
+ index=index,
36
+ similarity_top_k=10,
37
+ )
38
+ # configure postprocessor
39
+ node_postprocessors = [SimilarityPostprocessor(similarity_cutoff=0.7)]
40
+
41
+ # retrieve results
42
+ query_results = retriever.retrieve(query, node_postprocessors=node_postprocessors)
43
+
44
+ # TODO: get only relevant info from response such as references and not the whole thing without using LLM to formulate response
45
+ results = query_results.to_dict()
46
+ return results
frontend/app/components/ui/search/useSearch.tsx CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import { useState, useEffect } from "react";
2
 
3
  interface SearchResult {
@@ -12,6 +14,8 @@ interface UseSearchResult {
12
  handleSearch: (query: string) => Promise<void>;
13
  }
14
 
 
 
15
  const useSearch = (): UseSearchResult => {
16
  const [searchResults, setSearchResults] = useState<SearchResult[]>([]);
17
  const [isLoading, setIsLoading] = useState(false);
@@ -19,10 +23,15 @@ const useSearch = (): UseSearchResult => {
19
  const handleSearch = async (query: string): Promise<void> => {
20
  setIsLoading(true);
21
 
22
- // Perform your search logic here
23
- // Replace the following with your actual search API or function
 
 
 
 
24
  try {
25
- const response = await fetch(`/api/search?query=${query}`);
 
26
  const data = await response.json();
27
  setSearchResults(data);
28
  } catch (error) {
 
1
+ "use client";
2
+
3
  import { useState, useEffect } from "react";
4
 
5
  interface SearchResult {
 
14
  handleSearch: (query: string) => Promise<void>;
15
  }
16
 
17
+ const search_api = process.env.NEXT_PUBLIC_SEARCH_API;
18
+
19
  const useSearch = (): UseSearchResult => {
20
  const [searchResults, setSearchResults] = useState<SearchResult[]>([]);
21
  const [isLoading, setIsLoading] = useState(false);
 
23
  const handleSearch = async (query: string): Promise<void> => {
24
  setIsLoading(true);
25
 
26
+ if (!search_api) {
27
+ console.error("Search API is not defined");
28
+ setIsLoading(false);
29
+ return;
30
+ }
31
+ // Perform search logic here
32
  try {
33
+ console.log("Searching for:", query);
34
+ const response = await fetch(`${search_api}?query=${query}`);
35
  const data = await response.json();
36
  setSearchResults(data);
37
  } catch (error) {