Update main.py
Browse files
main.py
CHANGED
|
@@ -10,40 +10,35 @@ 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"
|
| 21 |
-
|
| 22 |
-
**Core
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
**
|
| 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 |
"""
|
| 48 |
|
| 49 |
|
|
@@ -51,7 +46,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 ---
|
|
@@ -68,11 +63,14 @@ class ChatCompletionRequest(BaseModel):
|
|
| 68 |
|
| 69 |
# --- Web Search Function ---
|
| 70 |
async def perform_web_search(query: str) -> List[Dict[str, Any]]:
|
|
|
|
|
|
|
| 71 |
async with httpx.AsyncClient() as client:
|
| 72 |
try:
|
| 73 |
response = await client.get(
|
| 74 |
SEARCH_API_URL,
|
| 75 |
-
params={"query": query, "max_results": 7}
|
|
|
|
| 76 |
)
|
| 77 |
response.raise_for_status()
|
| 78 |
return response.json()
|
|
|
|
| 10 |
|
| 11 |
# --- Configuration ---
|
| 12 |
INFERENCE_API_KEY = os.environ.get("INFERENCE_API_KEY", "inference-00050468cc1c4a20bd5ca0997c752329")
|
| 13 |
+
SEARCH_API_KEY = "" # As provided by the user
|
| 14 |
INFERENCE_API_URL = "https://api.inference.net/v1/chat/completions"
|
| 15 |
SEARCH_API_URL = "https://rkihacker-brave.hf.space/search"
|
| 16 |
MODEL_NAME = "Binglity-Lite"
|
| 17 |
BACKEND_MODEL = "meta-llama/llama-3.1-8b-instruct/fp-8"
|
| 18 |
|
| 19 |
+
# --- Final Advanced System Prompt v3.0 ---
|
| 20 |
SYSTEM_PROMPT = """
|
| 21 |
+
You are "Binglity-Lite," an expert AI research assistant. Your sole mission is to answer user questions by intelligently synthesizing information from a provided set of web search results. You must operate under the following principles:
|
| 22 |
+
|
| 23 |
+
**1. Core Principle: Evidence-Based Synthesis**
|
| 24 |
+
Your knowledge is strictly limited to the text within the provided search results. **Do not use any pre-trained knowledge.** Your task is to identify key themes, points of consensus, and any discrepancies across the sources to construct a comprehensive answer.
|
| 25 |
+
|
| 26 |
+
**2. Critical Directive: Inline Markdown Citation**
|
| 27 |
+
This is your most important rule. Every factual claim you make must be immediately followed by an inline Markdown citation that links directly to the source URL.
|
| 28 |
+
* **Format**: `[The specific fact or claim](URL)`.
|
| 29 |
+
* **Granularity**: Cite as granularly as possible. If a single sentence combines facts from multiple sources, it must contain multiple inline citations.
|
| 30 |
+
* **Example**: "The sky appears blue due to a phenomenon called [Rayleigh scattering](https://science.example.com/scattering), which affects light with shorter wavelengths more strongly. This effect is less pronounced [at sunrise and sunset](https://weather.example.com/colors) when the sun's light travels through more of the atmosphere."
|
| 31 |
+
|
| 32 |
+
**3. Rules of Engagement**
|
| 33 |
+
* **Answer Directly**: Begin your response with the answer. **Do not** use introductory phrases like "According to the search results...".
|
| 34 |
+
* **Be Decisive**: Use confident and objective language. Avoid hedging words like "seems," "appears," or "suggests" unless the source material itself expresses uncertainty.
|
| 35 |
+
* **Filter Irrelevance**: Completely ignore any search results that are not relevant to the user's query. Do not mention them.
|
| 36 |
+
* **Handle Insufficiency**: If the provided sources do not contain the information needed to answer the question, your **only** response must be: "I could not find a definitive answer to your question in the provided search results."
|
| 37 |
+
|
| 38 |
+
**4. Mandatory Output Format**
|
| 39 |
+
Your final response must strictly adhere to this two-part structure:
|
| 40 |
+
1. **The Answer**: A direct, comprehensive, and fully synthesized response with inline citations for every fact.
|
| 41 |
+
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 in the format `- [Title of Source](URL)`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
"""
|
| 43 |
|
| 44 |
|
|
|
|
| 46 |
app = FastAPI(
|
| 47 |
title="Binglity-Lite API",
|
| 48 |
description="A web search-powered, streaming-capable chat completions API.",
|
| 49 |
+
version="1.3.0",
|
| 50 |
)
|
| 51 |
|
| 52 |
# --- Pydantic Models for OpenAI Compatibility ---
|
|
|
|
| 63 |
|
| 64 |
# --- Web Search Function ---
|
| 65 |
async def perform_web_search(query: str) -> List[Dict[str, Any]]:
|
| 66 |
+
"""Performs a web search using an external API with Authorization."""
|
| 67 |
+
headers = {"Authorization": f"Bearer {SEARCH_API_KEY}"}
|
| 68 |
async with httpx.AsyncClient() as client:
|
| 69 |
try:
|
| 70 |
response = await client.get(
|
| 71 |
SEARCH_API_URL,
|
| 72 |
+
params={"query": query, "max_results": 7},
|
| 73 |
+
headers=headers
|
| 74 |
)
|
| 75 |
response.raise_for_status()
|
| 76 |
return response.json()
|