FrostIce commited on
Commit
f5af3a0
·
verified ·
1 Parent(s): 33ce76c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -1
app.py CHANGED
@@ -3,6 +3,45 @@ import gradio as gr
3
  from g4f.client import Client
4
 
5
  client = Client()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  # --- Обработчик диалога ---
8
  def respond(message, history):
@@ -48,7 +87,13 @@ def respond(message, history):
48
  responsed = client.chat.completions.create(
49
  model="deepseek-v3",
50
  messages=messages,
51
- web_search=True,
 
 
 
 
 
 
52
 
53
  )
54
  bot_message = responsed.choices[0].message.content
 
3
  from g4f.client import Client
4
 
5
  client = Client()
6
+ # --- Обработчик диалога ---
7
+ def answer_to_bool(text: str) -> bool:
8
+ t = text.strip().lower()
9
+ # Положительные (нужен поиск)
10
+ if any(word in t for word in ("TRUE")):
11
+ return True
12
+ # Всё остальное – считаем, что поиск не нужен
13
+ return False
14
+
15
+ def need_search(message, history) -> bool:
16
+ # Добавляем системное сообщение только при старте истории
17
+ messages = [{"role": "system", "content": """Ты — вспомогательная модель, задача которой **только** решить, нужен ли в данный момент
18
+ поиск в интернете, чтобы дать корректный ответ на запрос пользователя.
19
+
20
+ Ответ дай **коротко** одной из следующих форм:
21
+ * TRUE
22
+ * FALSE
23
+
24
+ Никаких пояснений, объяснений, советов, дополнительных фраз и т.п. – только один из указанных вариантов.
25
+
26
+ """}]
27
+
28
+ for human, assistant in history:
29
+ messages.append({"role": "user", "content": human})
30
+ messages.append({"role": "assistant", "content": assistant})
31
+
32
+ messages.append({"role": "user", "content": message})
33
+
34
+ try:
35
+ responsed = client.chat.completions.create(
36
+ model="deepseek-v3",
37
+ messages=messages,
38
+ temperature=0.0,
39
+ top_p=1.0,
40
+ max_tokens=12, # достаточно, чтобы получить "YES"/"NO"
41
+ stop=["\n"],
42
+
43
+ )
44
+ return answer_to_bool(responsed.choices[0].message.content)
45
 
46
  # --- Обработчик диалога ---
47
  def respond(message, history):
 
87
  responsed = client.chat.completions.create(
88
  model="deepseek-v3",
89
  messages=messages,
90
+ web_search=need_search(human),
91
+ temperature=0.8,
92
+ top_p=0.95,
93
+ max_tokens=300,
94
+ presence_penalty=0.2,
95
+ frequency_penalty=0.1,
96
+
97
 
98
  )
99
  bot_message = responsed.choices[0].message.content