Update main.py
Browse files
main.py
CHANGED
|
@@ -9,36 +9,41 @@ 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-
|
| 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
|
| 20 |
SYSTEM_PROMPT = """
|
| 21 |
-
You are "Binglity-Lite,
|
| 22 |
-
|
| 23 |
-
**
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
* **
|
| 31 |
-
|
| 32 |
-
**
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
**
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
"""
|
| 43 |
|
| 44 |
|
|
@@ -46,7 +51,7 @@ Your final response must strictly adhere to this two-part structure:
|
|
| 46 |
app = FastAPI(
|
| 47 |
title="Binglity-Lite API",
|
| 48 |
description="A web search-powered, streaming-capable chat completions API.",
|
| 49 |
-
version="1.
|
| 50 |
)
|
| 51 |
|
| 52 |
# --- Pydantic Models for OpenAI Compatibility ---
|
|
@@ -63,14 +68,11 @@ class ChatCompletionRequest(BaseModel):
|
|
| 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":
|
| 73 |
-
headers=headers
|
| 74 |
)
|
| 75 |
response.raise_for_status()
|
| 76 |
return response.json()
|
|
|
|
| 9 |
from typing import List, Dict, Any, Optional, AsyncGenerator
|
| 10 |
|
| 11 |
# --- Configuration ---
|
| 12 |
+
INFERENCE_API_KEY = os.environ.get("INFERENCE_API_KEY", "your-inference-api-key")
|
|
|
|
| 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 |
"""
|
| 48 |
|
| 49 |
|
|
|
|
| 51 |
app = FastAPI(
|
| 52 |
title="Binglity-Lite API",
|
| 53 |
description="A web search-powered, streaming-capable chat completions API.",
|
| 54 |
+
version="1.2.0",
|
| 55 |
)
|
| 56 |
|
| 57 |
# --- Pydantic Models for OpenAI Compatibility ---
|
|
|
|
| 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()
|