qingxu98 commited on
Commit
cd145c0
1 Parent(s): 7a4d4ad
crazy_functions/高级功能函数模板.py CHANGED
@@ -1,6 +1,7 @@
1
  from toolbox import CatchException, update_ui
2
  from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
3
- import datetime
 
4
  @CatchException
5
  def 高阶功能模板函数(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
6
  """
@@ -18,12 +19,34 @@ def 高阶功能模板函数(txt, llm_kwargs, plugin_kwargs, chatbot, history, s
18
  for i in range(5):
19
  currentMonth = (datetime.date.today() + datetime.timedelta(days=i)).month
20
  currentDay = (datetime.date.today() + datetime.timedelta(days=i)).day
21
- i_say = f'历史中哪些事件发生在{currentMonth}月{currentDay}日?列举两条并发送相关图片。发送图片时,请使用Markdown,将Unsplash API中的PUT_YOUR_QUERY_HERE替换成描述该事件的一个最重要的单词。'
22
  gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive(
23
  inputs=i_say, inputs_show_user=i_say,
24
  llm_kwargs=llm_kwargs, chatbot=chatbot, history=[],
25
- sys_prompt="当你想发送一张照片时,请使用Markdown, 并且不要有反斜线, 不要用代码块。使用 Unsplash API (https://source.unsplash.com/1280x720/? < PUT_YOUR_QUERY_HERE >)"
26
  )
 
27
  chatbot[-1] = (i_say, gpt_say)
28
  history.append(i_say);history.append(gpt_say)
29
  yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 界面更新
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from toolbox import CatchException, update_ui
2
  from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
3
+ import datetime, re
4
+
5
  @CatchException
6
  def 高阶功能模板函数(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
7
  """
 
19
  for i in range(5):
20
  currentMonth = (datetime.date.today() + datetime.timedelta(days=i)).month
21
  currentDay = (datetime.date.today() + datetime.timedelta(days=i)).day
22
+ i_say = f'历史中哪些事件发生在{currentMonth}月{currentDay}日?用中文列举两条,然后分别给出描述事件的两个英文单词。' + '当你给出关键词时,使用以下json格式:{"KeyWords":[EnglishKeyWord1,EnglishKeyWord2]}。'
23
  gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive(
24
  inputs=i_say, inputs_show_user=i_say,
25
  llm_kwargs=llm_kwargs, chatbot=chatbot, history=[],
26
+ sys_prompt='输出格式示例:1908年,美国消防救援事业发展的“美国消防协会”成立。关键词:{"KeyWords":["Fire","American"]}'
27
  )
28
+ gpt_say = get_images(gpt_say)
29
  chatbot[-1] = (i_say, gpt_say)
30
  history.append(i_say);history.append(gpt_say)
31
  yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 界面更新
32
+
33
+
34
+ def get_images(gpt_say):
35
+ def get_image_by_keyword(keyword):
36
+ import requests
37
+ from bs4 import BeautifulSoup
38
+ response = requests.get(f'https://wallhaven.cc/search?q={keyword}', timeout=2)
39
+ for image_element in BeautifulSoup(response.content, 'html.parser').findAll("img"):
40
+ if "data-src" in image_element: break
41
+ return image_element["data-src"]
42
+
43
+ for keywords in re.findall('{"KeyWords":\[(.*?)\]}', gpt_say):
44
+ keywords = [n.strip('"') for n in keywords.split(',')]
45
+ try:
46
+ description = keywords[0]
47
+ url = get_image_by_keyword(keywords[0])
48
+ img_tag = f"\n\n![{description}]({url})"
49
+ gpt_say += img_tag
50
+ except:
51
+ continue
52
+ return gpt_say