File size: 2,507 Bytes
e51e040
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4afe46c
e51e040
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21b4bd9
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from search import search_n_browse, search_with_content
from website_viewer import fetch_website_content
from typing import Dict, List


app = FastAPI(title="VerboAI Search Engine API", description="""
VerboAI Search Engine API provides three core functionalities:
1. Web search with browsing capabilities
2. Website content extraction with structured data (title, markdown, links, and images)
3. Content-based search that returns the top N most relevant results from websites

The API is built with FastAPI, supports CORS, and is ready for cloud deployment. Powered by VerboAI.
""", version="1.0.0", docs_url="/", redoc_url="/redoc")

# Enable CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

class SearchRequest(BaseModel):
    search_phrase: str

class SearchResponse(BaseModel):
    results: dict

class WebsiteViewRequest(BaseModel):
    url: str




class WebsiteViewResponse(BaseModel):
    title: str
    markdown: str
    links: Dict[str, List[Dict]] = {}    
    images: Dict[str, List[Dict]] = {}
    url: str

class SearchWithContentRequest(BaseModel):
    search_phrase: str
    top_n: int = 5

class SearchWithContentResponse(BaseModel):
    results: dict

@app.post("/search", response_model=SearchResponse)
async def search(request: SearchRequest):
    try:
        results = await search_n_browse(request.search_phrase)
        return SearchResponse(results=results)
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/websiteView", response_model=WebsiteViewResponse)
async def view_website(request: WebsiteViewRequest):
    try:
        
        website_content = await fetch_website_content(request.url)
        return WebsiteViewResponse(**website_content)
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/searchWithContent", response_model=SearchWithContentResponse)
async def search_with_content_endpoint(request: SearchWithContentRequest):
    try:
        results = await search_with_content(request.search_phrase, request.top_n)
        return SearchWithContentResponse(results=results)
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@app.get("/health")
async def health_check():
    return {"status": "healthy"}