yangtb24 commited on
Commit
3c9685c
·
verified ·
1 Parent(s): 072a6ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -14,6 +14,7 @@ AI_API_ENDPOINT = os.environ.get('AI_API_ENDPOINT')
14
  AI_API_KEY = os.environ.get('AI_API_KEY')
15
  AI_MODEL = os.environ.get('AI_MODEL')
16
  PHP_PROXY_URL = os.environ.get('PHP_PROXY_URL')
 
17
 
18
  if not all([TELEGRAM_BOT_TOKEN, AI_API_ENDPOINT, AI_API_KEY, AI_MODEL]):
19
  raise ValueError("请设置所有必要的环境变量")
@@ -91,8 +92,8 @@ TOOLS = [
91
  {
92
  "type": "function",
93
  "function": {
94
- "name": "search_bing",
95
- "description": "使用必应搜索查询",
96
  "parameters": {
97
  "type": "object",
98
  "properties": {
@@ -115,28 +116,25 @@ def get_current_datetime():
115
  def generate_random_number(min, max):
116
  return random.randint(min, max)
117
 
118
- def search_bing(query):
119
- search_api_url = "https://api.bing.microsoft.com/v7.0/search"
120
- bing_api_key = os.environ.get("BING_API_KEY")
121
- if not bing_api_key:
122
- return "请设置 BING_API_KEY 环境变量"
123
- headers = {"Ocp-Apim-Subscription-Key": bing_api_key}
124
- params = {"q": query}
125
  try:
126
- response = requests.get(search_api_url, headers=headers, params=params)
127
  response.raise_for_status()
128
  search_results = response.json()
129
- if search_results and search_results.get("webPages") and search_results.get("webPages").get("value"):
130
- results = search_results["webPages"]["value"][:3]
131
  formatted_results = []
132
  for result in results:
133
- formatted_results.append(f"[{result['name']}]({result['url']})")
134
  return "\n".join(formatted_results)
135
  else:
136
  return "没有找到相关结果"
137
  except requests.exceptions.RequestException as e:
138
  return f"搜索失败: {e}"
139
 
 
140
  def make_telegram_request(method, data=None):
141
  url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/{method}"
142
  if PHP_PROXY_URL:
@@ -360,8 +358,8 @@ async def handleAiResponse(ai_data, chatId, history, thinking_message_id):
360
  tool_result = get_current_datetime()
361
  elif function_name == 'generate_random_number':
362
  tool_result = generate_random_number(arguments.get('min'), arguments.get('max'))
363
- elif function_name == 'search_bing':
364
- tool_result = search_bing(arguments.get('query'))
365
 
366
  if tool_result is not None:
367
  history.append({
 
14
  AI_API_KEY = os.environ.get('AI_API_KEY')
15
  AI_MODEL = os.environ.get('AI_MODEL')
16
  PHP_PROXY_URL = os.environ.get('PHP_PROXY_URL')
17
+ SEARXNG_URL = os.environ.get('SEARXNG_URL', 'https://sousuo.emoe.top') # 默认值是你的 URL
18
 
19
  if not all([TELEGRAM_BOT_TOKEN, AI_API_ENDPOINT, AI_API_KEY, AI_MODEL]):
20
  raise ValueError("请设置所有必要的环境变量")
 
92
  {
93
  "type": "function",
94
  "function": {
95
+ "name": "search_searxng",
96
+ "description": "使用SearXNG搜索查询",
97
  "parameters": {
98
  "type": "object",
99
  "properties": {
 
116
  def generate_random_number(min, max):
117
  return random.randint(min, max)
118
 
119
+ def search_searxng(query):
120
+ search_url = f"{SEARXNG_URL}/search"
121
+ params = {"q": query, "format": "json"}
 
 
 
 
122
  try:
123
+ response = requests.get(search_url, params=params)
124
  response.raise_for_status()
125
  search_results = response.json()
126
+ if search_results and search_results.get("results"):
127
+ results = search_results["results"][:3]
128
  formatted_results = []
129
  for result in results:
130
+ formatted_results.append(f"[{result['title']}]({result['url']})")
131
  return "\n".join(formatted_results)
132
  else:
133
  return "没有找到相关结果"
134
  except requests.exceptions.RequestException as e:
135
  return f"搜索失败: {e}"
136
 
137
+
138
  def make_telegram_request(method, data=None):
139
  url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/{method}"
140
  if PHP_PROXY_URL:
 
358
  tool_result = get_current_datetime()
359
  elif function_name == 'generate_random_number':
360
  tool_result = generate_random_number(arguments.get('min'), arguments.get('max'))
361
+ elif function_name == 'search_searxng':
362
+ tool_result = search_searxng(arguments.get('query'))
363
 
364
  if tool_result is not None:
365
  history.append({