rkihacker commited on
Commit
1305b39
·
verified ·
1 Parent(s): 55e2b0b

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +91 -0
main.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ import httpx
4
+ import os
5
+
6
+ # --- Configuration ---
7
+ # In a real application, use a proper search API and manage keys securely.
8
+ # For this example, we'll use a placeholder.
9
+ SEARCH_API_URL = "https://rkihacker-brave.hf.space/search"
10
+
11
+ # --- System Prompt ---
12
+ SYSTEM_PROMPT = """
13
+ You are Binglity, a helpful and friendly AI assistant with web search capabilities.
14
+ Your goal is to provide accurate and relevant information to users by searching the web.
15
+ When responding, synthesize the information from the search results into a coherent answer.
16
+ Always be polite and informative.
17
+ """
18
+
19
+ # --- FastAPI App ---
20
+ app = FastAPI(
21
+ title="Binglity API",
22
+ description="A web search-powered chatbot alternative to Perplexity.",
23
+ version="1.0.0",
24
+ )
25
+
26
+ # --- Pydantic Models ---
27
+ class SearchRequest(BaseModel):
28
+ query: str
29
+
30
+ class SearchResult(BaseModel):
31
+ title: str
32
+ url: str
33
+ description: str
34
+
35
+ class SearchResponse(BaseModel):
36
+ binglity_response: str
37
+ search_results: list[SearchResult]
38
+
39
+
40
+ # --- Web Search Function ---
41
+ async def perform_web_search(query: str) -> list[SearchResult]:
42
+ """
43
+ Performs a web search using an external API and returns a list of search results.
44
+ """
45
+ async with httpx.AsyncClient() as client:
46
+ try:
47
+ response = await client.get(
48
+ SEARCH_API_URL,
49
+ params={"query": query, "max_results": 5}
50
+ )
51
+ response.raise_for_status()
52
+ results = response.json()
53
+ return [SearchResult(**item) for item in results]
54
+ except httpx.HTTPStatusError as e:
55
+ raise HTTPException(status_code=e.response.status_code, detail=f"Error from search API: {e.response.text}")
56
+ except Exception as e:
57
+ raise HTTPException(status_code=500, detail=f"An unexpected error occurred during web search: {str(e)}")
58
+
59
+ # --- API Endpoint ---
60
+ @app.post("/search", response_model=SearchResponse)
61
+ async def search(request: SearchRequest):
62
+ """
63
+ Takes a user query, performs a web search, and returns a response from Binglity.
64
+ """
65
+ search_results = await perform_web_search(request.query)
66
+
67
+ # --- Simulate Model Response ---
68
+ # In a real application, you would pass the system prompt, query, and search results
69
+ # to a large language model to generate a more nuanced response.
70
+ if not search_results:
71
+ binglity_response = "I couldn't find any relevant information for your query."
72
+ else:
73
+ response_intro = f"Here's what I found for '{request.query}':\n\n"
74
+ formatted_results = "\n\n".join(
75
+ f"Title: {res.title}\nDescription: {res.description}\nURL: {res.url}"
76
+ for res in search_results
77
+ )
78
+ binglity_response = SYSTEM_PROMPT + "\n\n" + response_intro + formatted_results
79
+
80
+ return {
81
+ "binglity_response": binglity_response,
82
+ "search_results": search_results,
83
+ }
84
+
85
+ @app.get("/")
86
+ def read_root():
87
+ return {"message": "Welcome to the Binglity API"}
88
+
89
+ if __name__ == "__main__":
90
+ import uvicorn
91
+ uvicorn.run(app, host="0.0.0.0", port=8000)