Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import spaces
|
|
2 |
import json
|
3 |
import subprocess
|
4 |
import os
|
|
|
5 |
from llama_cpp import Llama
|
6 |
from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
|
7 |
from llama_cpp_agent.providers import LlamaCppPythonProvider
|
@@ -10,6 +11,52 @@ from llama_cpp_agent.chat_history.messages import Roles
|
|
10 |
import gradio as gr
|
11 |
from huggingface_hub import hf_hub_download
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
llm = None
|
14 |
llm_model = None
|
15 |
|
@@ -134,6 +181,17 @@ def respond(
|
|
134 |
settings.repeat_penalty = repeat_penalty
|
135 |
settings.stream = True
|
136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
messages = BasicChatHistory()
|
138 |
|
139 |
# history์ ๊ฐ ํญ๋ชฉ์ด dict ํ์์ผ๋ก {'user': <user_message>, 'assistant': <assistant_message>} ํํ๋ผ๊ณ ๊ฐ์
|
|
|
2 |
import json
|
3 |
import subprocess
|
4 |
import os
|
5 |
+
import requests # โ Brave Search API ํธ์ถ ์ํด ์ถ๊ฐ
|
6 |
from llama_cpp import Llama
|
7 |
from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
|
8 |
from llama_cpp_agent.providers import LlamaCppPythonProvider
|
|
|
11 |
import gradio as gr
|
12 |
from huggingface_hub import hf_hub_download
|
13 |
|
14 |
+
##############################################################################
|
15 |
+
# Brave Web Search ์ฐ๋์ฉ ์ถ๊ฐ ์ฝ๋
|
16 |
+
##############################################################################
|
17 |
+
SERPHOUSE_API_KEY = os.getenv("SERPHOUSE_API_KEY", "")
|
18 |
+
|
19 |
+
def do_web_search(query: str) -> str:
|
20 |
+
"""
|
21 |
+
Brave Web Search API๋ฅผ ์ด์ฉํ์ฌ query ๊ฒ์ ํ,
|
22 |
+
์ต๋ 10๊ฐ ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ title/url/description ํํ๋ก ๋งํฌ๋ค์ด์ผ๋ก ์์ฝ ๋ฐํ.
|
23 |
+
"""
|
24 |
+
try:
|
25 |
+
url = "https://api.search.brave.com/res/v1/web/search"
|
26 |
+
params = {
|
27 |
+
"q": query,
|
28 |
+
"count": 10,
|
29 |
+
"search_lang": "en"
|
30 |
+
}
|
31 |
+
headers = {
|
32 |
+
"Accept": "application/json",
|
33 |
+
"Accept-Encoding": "gzip",
|
34 |
+
# Brave API ํค ( SERPHOUSE_API_KEY ๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉ )
|
35 |
+
"X-Subscription-Token": SERPHOUSE_API_KEY,
|
36 |
+
}
|
37 |
+
response = requests.get(url, headers=headers, params=params, timeout=30)
|
38 |
+
response.raise_for_status()
|
39 |
+
data = response.json()
|
40 |
+
web_data = data.get("web", {})
|
41 |
+
results = web_data.get("results", [])
|
42 |
+
|
43 |
+
if not results:
|
44 |
+
return "No results from Brave Search."
|
45 |
+
|
46 |
+
lines = []
|
47 |
+
lines.append("## Brave Search Results\n")
|
48 |
+
for i, item in enumerate(results, start=1):
|
49 |
+
title = item.get("title", "Untitled")
|
50 |
+
link = item.get("url", "")
|
51 |
+
snippet = item.get("description", "")
|
52 |
+
lines.append(f"**{i}. {title}**\n\n{snippet}\n\n[{link}]({link})\n\n---\n")
|
53 |
+
return "\n".join(lines)
|
54 |
+
except Exception as e:
|
55 |
+
return f"Brave Search Error: {str(e)}"
|
56 |
+
|
57 |
+
##############################################################################
|
58 |
+
# ์ดํ ์๋ณธ ์ฝ๋
|
59 |
+
##############################################################################
|
60 |
llm = None
|
61 |
llm_model = None
|
62 |
|
|
|
181 |
settings.repeat_penalty = repeat_penalty
|
182 |
settings.stream = True
|
183 |
|
184 |
+
# --------------------------------------------------------------------------------------
|
185 |
+
# ์ฌ๊ธฐ์ Brave Web Search๋ฅผ ์ํํ์ฌ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ system_message์ ์ถ๊ฐ (๊ธฐ๋ณธ ์ ์ฉ)
|
186 |
+
# --------------------------------------------------------------------------------------
|
187 |
+
# 1) ์ ์ ๋ฉ์์ง์ ๋ํด Brave ๊ฒ์
|
188 |
+
search_results = do_web_search(message)
|
189 |
+
|
190 |
+
# 2) system_message ๋์ ๊ฒ์ ๊ฒฐ๊ณผ ์ถ๊ฐ
|
191 |
+
# ํ์์ ๋ฐ๋ผ ์๋ด ๋ฌธ๊ตฌ๋ ๋ง๋ถ์ผ ์ ์์
|
192 |
+
agent.system_prompt += f"\n\n[Brave Search Results for '{message}']\n{search_results}\n"
|
193 |
+
# --------------------------------------------------------------------------------------
|
194 |
+
|
195 |
messages = BasicChatHistory()
|
196 |
|
197 |
# history์ ๊ฐ ํญ๋ชฉ์ด dict ํ์์ผ๋ก {'user': <user_message>, 'assistant': <assistant_message>} ํํ๋ผ๊ณ ๊ฐ์
|