ginipick commited on
Commit
9a25e63
ยท
verified ยท
1 Parent(s): 83f2f34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
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>} ํ˜•ํƒœ๋ผ๊ณ  ๊ฐ€์ •