Update main.py
Browse files
main.py
CHANGED
|
@@ -6,42 +6,33 @@ import os
|
|
| 6 |
import json
|
| 7 |
import time
|
| 8 |
import uuid
|
|
|
|
| 9 |
from typing import List, Dict, Any, Optional, AsyncGenerator
|
| 10 |
|
| 11 |
# --- Configuration ---
|
| 12 |
INFERENCE_API_KEY = os.environ.get("INFERENCE_API_KEY", "inference-00050468cc1c4a20bd5ca0997c752329")
|
| 13 |
INFERENCE_API_URL = "https://api.inference.net/v1/chat/completions"
|
| 14 |
SEARCH_API_URL = "https://rkihacker-brave.hf.space/search"
|
|
|
|
| 15 |
MODEL_NAME = "Binglity-Lite"
|
| 16 |
BACKEND_MODEL = "meta-llama/llama-3.1-8b-instruct/fp-8"
|
| 17 |
|
| 18 |
# --- Final Advanced System Prompt ---
|
| 19 |
SYSTEM_PROMPT = """
|
| 20 |
-
You are "Binglity-Lite", a highly advanced AI search assistant. Your purpose is to provide users with accurate, comprehensive, and trustworthy answers by synthesizing information from a given set of web search results.
|
| 21 |
-
|
| 22 |
**Core Directives:**
|
| 23 |
-
|
| 24 |
1. **Answer Directly**: Immediately address the user's question. **Do not** use introductory phrases like "Based on the search results..." or "Here is the information I found...". Your tone should be confident, objective, and encyclopedic.
|
| 25 |
-
|
| 26 |
2. **Synthesize, Don't Summarize**: Your primary task is to weave information from multiple sources into a single, cohesive, and well-structured answer. Do not simply describe what each source says one by one.
|
| 27 |
-
|
| 28 |
3. **Cite with Inline Markdown Links**: This is your most important instruction. When you present a fact or a piece of information from a source, you **must** cite it immediately using an inline Markdown link.
|
| 29 |
* **Format**: The format must be `[phrase or sentence containing the fact](URL)`. The URL must come from the `URL:` field of the provided source.
|
| 30 |
* **Example**: If a source with URL `https://example.com/science` says "The Earth is the third planet from the Sun", your output should be: "The Earth is the [third planet from the Sun](https://example.com/science)."
|
| 31 |
* **Rule**: Every piece of information in your answer must be attributable to a source via these inline links.
|
| 32 |
-
|
| 33 |
-
4. **Be Fact-Based**: Your entire response must be based **exclusively** on the information provided in the search results. Do not use any outside knowledge.
|
| 34 |
-
|
| 35 |
5. **Filter for Relevance**: If a search result is not relevant to the user's query, ignore it completely. Do not mention it in your response.
|
| 36 |
-
|
| 37 |
6. **Handle Ambiguity**: If the search results are contradictory or insufficient to answer the question fully, state this clearly in your response, citing the conflicting sources.
|
| 38 |
-
|
| 39 |
**Final Output Structure:**
|
| 40 |
-
|
| 41 |
Your final response MUST be structured in two parts:
|
| 42 |
-
|
| 43 |
1. **The Synthesized Answer**: A well-written response that directly answers the user's query, with facts and statements properly cited using inline Markdown links as described above.
|
| 44 |
-
|
| 45 |
2. **Sources Section**: After the answer, add a section header `## Sources`. Under this header, provide a bulleted list of the full titles and URLs of every source you used.
|
| 46 |
* **Format**: `- [Title of Source](URL)`
|
| 47 |
"""
|
|
@@ -51,7 +42,7 @@ Your final response MUST be structured in two parts:
|
|
| 51 |
app = FastAPI(
|
| 52 |
title="Binglity-Lite API",
|
| 53 |
description="A web search-powered, streaming-capable chat completions API.",
|
| 54 |
-
version="1.
|
| 55 |
)
|
| 56 |
|
| 57 |
# --- Pydantic Models for OpenAI Compatibility ---
|
|
@@ -66,7 +57,7 @@ class ChatCompletionRequest(BaseModel):
|
|
| 66 |
temperature: Optional[float] = 0.7
|
| 67 |
stream: Optional[bool] = False
|
| 68 |
|
| 69 |
-
# --- Web Search
|
| 70 |
async def perform_web_search(query: str) -> List[Dict[str, Any]]:
|
| 71 |
async with httpx.AsyncClient() as client:
|
| 72 |
try:
|
|
@@ -75,7 +66,11 @@ async def perform_web_search(query: str) -> List[Dict[str, Any]]:
|
|
| 75 |
params={"query": query, "max_results": 10}
|
| 76 |
)
|
| 77 |
response.raise_for_status()
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
except httpx.HTTPStatusError as e:
|
| 80 |
print(f"Error from search API: {e.response.text}")
|
| 81 |
return []
|
|
@@ -83,13 +78,38 @@ async def perform_web_search(query: str) -> List[Dict[str, Any]]:
|
|
| 83 |
print(f"An unexpected error occurred during web search: {str(e)}")
|
| 84 |
return []
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
def format_search_results_for_prompt(results: List[Dict[str, Any]]) -> str:
|
|
|
|
| 87 |
if not results:
|
| 88 |
return "No relevant search results were found. Inform the user that you could not find information on their query."
|
| 89 |
|
| 90 |
-
formatted = "###
|
| 91 |
for i, result in enumerate(results):
|
| 92 |
-
|
|
|
|
| 93 |
formatted += f"Title: {result.get('title', 'N/A')}\n"
|
| 94 |
formatted += f"URL: {result.get('url', 'N/A')}\n"
|
| 95 |
formatted += f"Content: {result.get('description', 'N/A')}\n\n"
|
|
@@ -139,10 +159,24 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 139 |
if not user_query or request.messages[-1].role.lower() != 'user':
|
| 140 |
raise HTTPException(status_code=400, detail="The last message must be from the 'user' and contain content.")
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
final_user_prompt = f"User's question: \"{user_query}\"\n\nUse the web search results below to answer the user's question. Follow all rules in your system prompt exactly.\n\n{formatted_results}"
|
| 146 |
|
| 147 |
payload = {
|
| 148 |
"model": BACKEND_MODEL,
|
|
|
|
| 6 |
import json
|
| 7 |
import time
|
| 8 |
import uuid
|
| 9 |
+
import asyncio
|
| 10 |
from typing import List, Dict, Any, Optional, AsyncGenerator
|
| 11 |
|
| 12 |
# --- Configuration ---
|
| 13 |
INFERENCE_API_KEY = os.environ.get("INFERENCE_API_KEY", "inference-00050468cc1c4a20bd5ca0997c752329")
|
| 14 |
INFERENCE_API_URL = "https://api.inference.net/v1/chat/completions"
|
| 15 |
SEARCH_API_URL = "https://rkihacker-brave.hf.space/search"
|
| 16 |
+
NEWS_API_URL = "https://rkihacker-brave.hf.space/news" # Added News API URL
|
| 17 |
MODEL_NAME = "Binglity-Lite"
|
| 18 |
BACKEND_MODEL = "meta-llama/llama-3.1-8b-instruct/fp-8"
|
| 19 |
|
| 20 |
# --- Final Advanced System Prompt ---
|
| 21 |
SYSTEM_PROMPT = """
|
| 22 |
+
You are "Binglity-Lite", a highly advanced AI search assistant. Your purpose is to provide users with accurate, comprehensive, and trustworthy answers by synthesizing information from a given set of web and news search results.
|
|
|
|
| 23 |
**Core Directives:**
|
|
|
|
| 24 |
1. **Answer Directly**: Immediately address the user's question. **Do not** use introductory phrases like "Based on the search results..." or "Here is the information I found...". Your tone should be confident, objective, and encyclopedic.
|
|
|
|
| 25 |
2. **Synthesize, Don't Summarize**: Your primary task is to weave information from multiple sources into a single, cohesive, and well-structured answer. Do not simply describe what each source says one by one.
|
|
|
|
| 26 |
3. **Cite with Inline Markdown Links**: This is your most important instruction. When you present a fact or a piece of information from a source, you **must** cite it immediately using an inline Markdown link.
|
| 27 |
* **Format**: The format must be `[phrase or sentence containing the fact](URL)`. The URL must come from the `URL:` field of the provided source.
|
| 28 |
* **Example**: If a source with URL `https://example.com/science` says "The Earth is the third planet from the Sun", your output should be: "The Earth is the [third planet from the Sun](https://example.com/science)."
|
| 29 |
* **Rule**: Every piece of information in your answer must be attributable to a source via these inline links.
|
| 30 |
+
4. **Be Fact-Based**: Your entire response must be based **exclusively** on the information provided in the web and news search results. Do not use any outside knowledge.
|
|
|
|
|
|
|
| 31 |
5. **Filter for Relevance**: If a search result is not relevant to the user's query, ignore it completely. Do not mention it in your response.
|
|
|
|
| 32 |
6. **Handle Ambiguity**: If the search results are contradictory or insufficient to answer the question fully, state this clearly in your response, citing the conflicting sources.
|
|
|
|
| 33 |
**Final Output Structure:**
|
|
|
|
| 34 |
Your final response MUST be structured in two parts:
|
|
|
|
| 35 |
1. **The Synthesized Answer**: A well-written response that directly answers the user's query, with facts and statements properly cited using inline Markdown links as described above.
|
|
|
|
| 36 |
2. **Sources Section**: After the answer, add a section header `## Sources`. Under this header, provide a bulleted list of the full titles and URLs of every source you used.
|
| 37 |
* **Format**: `- [Title of Source](URL)`
|
| 38 |
"""
|
|
|
|
| 42 |
app = FastAPI(
|
| 43 |
title="Binglity-Lite API",
|
| 44 |
description="A web search-powered, streaming-capable chat completions API.",
|
| 45 |
+
version="1.3.0", # Version updated
|
| 46 |
)
|
| 47 |
|
| 48 |
# --- Pydantic Models for OpenAI Compatibility ---
|
|
|
|
| 57 |
temperature: Optional[float] = 0.7
|
| 58 |
stream: Optional[bool] = False
|
| 59 |
|
| 60 |
+
# --- Web Search Functions ---
|
| 61 |
async def perform_web_search(query: str) -> List[Dict[str, Any]]:
|
| 62 |
async with httpx.AsyncClient() as client:
|
| 63 |
try:
|
|
|
|
| 66 |
params={"query": query, "max_results": 10}
|
| 67 |
)
|
| 68 |
response.raise_for_status()
|
| 69 |
+
results = response.json()
|
| 70 |
+
# Add source type to each result
|
| 71 |
+
for result in results:
|
| 72 |
+
result['source_type'] = 'Web'
|
| 73 |
+
return results
|
| 74 |
except httpx.HTTPStatusError as e:
|
| 75 |
print(f"Error from search API: {e.response.text}")
|
| 76 |
return []
|
|
|
|
| 78 |
print(f"An unexpected error occurred during web search: {str(e)}")
|
| 79 |
return []
|
| 80 |
|
| 81 |
+
async def perform_news_search(query: str) -> List[Dict[str, Any]]:
|
| 82 |
+
"""Performs a search against the news API."""
|
| 83 |
+
async with httpx.AsyncClient() as client:
|
| 84 |
+
try:
|
| 85 |
+
# Parameters can be adjusted as needed, e.g., region
|
| 86 |
+
response = await client.get(
|
| 87 |
+
NEWS_API_URL,
|
| 88 |
+
params={"query": query, "max_results": 10, "region": "en-US"}
|
| 89 |
+
)
|
| 90 |
+
response.raise_for_status()
|
| 91 |
+
results = response.json()
|
| 92 |
+
# Add source type to each result
|
| 93 |
+
for result in results:
|
| 94 |
+
result['source_type'] = 'News'
|
| 95 |
+
return results
|
| 96 |
+
except httpx.HTTPStatusError as e:
|
| 97 |
+
print(f"Error from news API: {e.response.text}")
|
| 98 |
+
return []
|
| 99 |
+
except Exception as e:
|
| 100 |
+
print(f"An unexpected error occurred during news search: {str(e)}")
|
| 101 |
+
return []
|
| 102 |
+
|
| 103 |
+
|
| 104 |
def format_search_results_for_prompt(results: List[Dict[str, Any]]) -> str:
|
| 105 |
+
"""Formats combined search results for the language model prompt."""
|
| 106 |
if not results:
|
| 107 |
return "No relevant search results were found. Inform the user that you could not find information on their query."
|
| 108 |
|
| 109 |
+
formatted = "### Search Results ###\n\n"
|
| 110 |
for i, result in enumerate(results):
|
| 111 |
+
source_type = result.get('source_type', 'Search') # Default in case it's missing
|
| 112 |
+
formatted += f"Source [{i+1}] ({source_type}):\n"
|
| 113 |
formatted += f"Title: {result.get('title', 'N/A')}\n"
|
| 114 |
formatted += f"URL: {result.get('url', 'N/A')}\n"
|
| 115 |
formatted += f"Content: {result.get('description', 'N/A')}\n\n"
|
|
|
|
| 159 |
if not user_query or request.messages[-1].role.lower() != 'user':
|
| 160 |
raise HTTPException(status_code=400, detail="The last message must be from the 'user' and contain content.")
|
| 161 |
|
| 162 |
+
# Perform web and news searches concurrently
|
| 163 |
+
web_results, news_results = await asyncio.gather(
|
| 164 |
+
perform_web_search(user_query),
|
| 165 |
+
perform_news_search(user_query)
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
# Combine results and remove duplicates by URL
|
| 169 |
+
combined_results = []
|
| 170 |
+
seen_urls = set()
|
| 171 |
+
for result in web_results + news_results:
|
| 172 |
+
url = result.get('url')
|
| 173 |
+
if url and url not in seen_urls:
|
| 174 |
+
combined_results.append(result)
|
| 175 |
+
seen_urls.add(url)
|
| 176 |
+
|
| 177 |
+
formatted_results = format_search_results_for_prompt(combined_results)
|
| 178 |
|
| 179 |
+
final_user_prompt = f"User's question: \"{user_query}\"\n\nUse the web and news search results below to answer the user's question. Follow all rules in your system prompt exactly.\n\n{formatted_results}"
|
| 180 |
|
| 181 |
payload = {
|
| 182 |
"model": BACKEND_MODEL,
|