Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
from fastapi import FastAPI, Request, HTTPException
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
from llama_cpp import Llama
|
|
|
|
| 6 |
|
| 7 |
API_KEYS = {k.strip() for k in os.environ.get("GINI_API_KEYS", "").split(",") if k.strip()}
|
| 8 |
if os.environ.get("GINI_API_KEY"): API_KEYS.add(os.environ.get("GINI_API_KEY"))
|
|
@@ -19,6 +21,23 @@ SYSTEM_PROMPT = (
|
|
| 19 |
"Твой девиз сейчас: «Творчество не имеет границ, а каждый мой ответ — это маленькое произведение искусства»."
|
| 20 |
)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
MODEL_PATH = hf_hub_download(
|
| 23 |
repo_id="bartowski/Meta-Llama-3.1-8B-Instruct-GGUF",
|
| 24 |
filename="Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf",
|
|
@@ -40,9 +59,14 @@ async def generate(req: GenerateRequest, request: Request):
|
|
| 40 |
if auth.removeprefix("Bearer ").strip() not in API_KEYS:
|
| 41 |
raise HTTPException(status_code=401, detail="Unauthorized")
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
result = llm.create_chat_completion(
|
| 44 |
messages=[
|
| 45 |
-
{"role": "system", "content":
|
| 46 |
{"role": "user", "content": req.prompt},
|
| 47 |
],
|
| 48 |
max_tokens=req.max_new_tokens,
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
from fastapi import FastAPI, Request, HTTPException
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
from llama_cpp import Llama
|
| 7 |
+
from tavily import TavilyClient
|
| 8 |
|
| 9 |
API_KEYS = {k.strip() for k in os.environ.get("GINI_API_KEYS", "").split(",") if k.strip()}
|
| 10 |
if os.environ.get("GINI_API_KEY"): API_KEYS.add(os.environ.get("GINI_API_KEY"))
|
|
|
|
| 21 |
"Твой девиз сейчас: «Творчество не имеет границ, а каждый мой ответ — это маленькое произведение искусства»."
|
| 22 |
)
|
| 23 |
|
| 24 |
+
TAVILY_API_KEY = "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"
|
| 25 |
+
tavily = TavilyClient(api_key=TAVILY_API_KEY)
|
| 26 |
+
|
| 27 |
+
def get_web_context(query: str) -> str:
|
| 28 |
+
query_lower = query.lower()
|
| 29 |
+
keywords = ["найди", "поищи", "погугли", "кто", "что", "когда", "где", "какая", "почему", "новости", "вчера", "погода", "курс"]
|
| 30 |
+
if any(k in query_lower for k in keywords) or query.endswith("?"):
|
| 31 |
+
try:
|
| 32 |
+
response = tavily.search(query, search_depth="basic", max_results=3)
|
| 33 |
+
results = response.get("results", [])
|
| 34 |
+
if results:
|
| 35 |
+
snippets = [f"- {r.get('title', '')}: {r.get('content', '')}" for r in results]
|
| 36 |
+
return "ВНИМАНИЕ! Свежая информация из интернета по запросу пользователя:\n" + "\n".join(snippets) + "\n\nОпирайся строго на эти факты при ответе."
|
| 37 |
+
except Exception:
|
| 38 |
+
pass
|
| 39 |
+
return ""
|
| 40 |
+
|
| 41 |
MODEL_PATH = hf_hub_download(
|
| 42 |
repo_id="bartowski/Meta-Llama-3.1-8B-Instruct-GGUF",
|
| 43 |
filename="Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf",
|
|
|
|
| 59 |
if auth.removeprefix("Bearer ").strip() not in API_KEYS:
|
| 60 |
raise HTTPException(status_code=401, detail="Unauthorized")
|
| 61 |
|
| 62 |
+
system_content = SYSTEM_PROMPT
|
| 63 |
+
web_context = get_web_context(req.prompt)
|
| 64 |
+
if web_context:
|
| 65 |
+
system_content += f"\n\n{web_context}"
|
| 66 |
+
|
| 67 |
result = llm.create_chat_completion(
|
| 68 |
messages=[
|
| 69 |
+
{"role": "system", "content": system_content},
|
| 70 |
{"role": "user", "content": req.prompt},
|
| 71 |
],
|
| 72 |
max_tokens=req.max_new_tokens,
|