aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Qetesh <4559341+Qetesh@users.noreply.github.com> 2024-08-19 22:00:35 +0800
committerGravatar Qetesh <4559341+Qetesh@users.noreply.github.com> 2024-08-19 22:00:35 +0800
commit9adf372d461ceee5d9fc3aef4429b262290636b4 (patch)
tree0a67b548dc5b9aa1623167c90bbd33eff9747972
parentb93e2a560743bc485b7a9245fdbe75ca4be7dbc2 (diff)
downloadminiflux-ai-9adf372d461ceee5d9fc3aef4429b262290636b4.tar.gz
miniflux-ai-9adf372d461ceee5d9fc3aef4429b262290636b4.tar.zst
miniflux-ai-9adf372d461ceee5d9fc3aef4429b262290636b4.zip
add timeout, add err handler
-rw-r--r--config.sample.yml2
-rw-r--r--main.py27
-rw-r--r--requirements.txt3
3 files changed, 23 insertions, 9 deletions
diff --git a/config.sample.yml b/config.sample.yml
index d56863f..a9d4c39 100644
--- a/config.sample.yml
+++ b/config.sample.yml
@@ -10,7 +10,7 @@ llm:
agents:
summary:
title: "💡AI 摘要"
- prompt: "Please summarize the content of the article in 50 words in Chinese. Do not add any explanations or annotations to the result text. 请用 50 个字的中文总结文章的内容。"
+ prompt: "Please summarize the content of the article under 50 words in Chinese. Do not add any additional Character、markdown language to the result text. 请用不超过50个汉字概括文章内容。结果文本中不要添加任何额外的字符、Markdown语言。"
style_block: true
blacklist:
- https://xxxx.net
diff --git a/main.py b/main.py
index 9180760..22f213f 100644
--- a/main.py
+++ b/main.py
@@ -3,6 +3,7 @@ import time
import miniflux
from markdownify import markdownify as md
+import markdown
from openai import OpenAI
from yaml import load, Loader
@@ -17,31 +18,43 @@ def process_entry(entry):
[start_with_list.append('<pre') for i in style_block if i]
for agent in config['agents'].items():
- messages = [{"role": "system", "content": agent[1]['prompt']}, {"role": "user", "content": "The following is the input content:\n---\n " + md(entry['content'])}]
+ messages = [
+ {"role": "system", "content": agent[1]['prompt']},
+ {"role": "user", "content": "The following is the input content:\n---\n " + md(entry['content']) }
+ ]
# filter, if AI is not generating, and in whitelist, or not in blacklist
if ((not entry['content'].startswith(tuple(start_with_list))) and
(((agent[1]['whitelist'] is not None) and (entry['feed']['site_url'] in agent[1]['whitelist'])) or
(agent[1]['blacklist'] is not None and entry['feed']['site_url'] not in agent[1]['blacklist']) or
(agent[1]['whitelist'] is None and agent[1]['blacklist'] is None))):
- completion = llm_client.chat.completions.create( model=config['llm']['model'], messages= messages )
+ completion = llm_client.chat.completions.create( model=config['llm']['model'], messages= messages, timeout=60 )
response_content = completion.choices[0].message.content
print(time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()), agent[0], entry['feed']['feed_url'], response_content)
if agent[1]['style_block']:
- llm_result = llm_result + f"<pre style=\"white-space: pre-wrap;\"><code>\n{agent[1]['title']}:{response_content}</code></pre><hr><br />"
+ llm_result = (llm_result + '<pre style="white-space: pre-wrap;"><code>\n'
+ + agent[1]['title'] + ':'
+ + response_content.replace('\n', '').replace('\r', '')
+ + '\n</code></pre><hr><br />')
else:
- llm_result = llm_result + f"{agent[1]['title']}:{response_content}<hr><br />"
+ llm_result = llm_result + f"{agent[1]['title']}:{markdown.markdown(response_content)}<hr><br />"
if len(llm_result) > 0:
miniflux_client.update_entry(entry['id'], content= llm_result + entry['content'])
while True:
- # Fetch entries with status unread
entries = miniflux_client.get_entries(status=['unread'], limit=10000)
- print(time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()), 'Fetched new entries: ' + str(len(entries['entries']))) if len(entries['entries']) > 0 else print(time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()), 'No new entries')
+ start_time = time.time()
+ print(time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()), 'Fetched unread entries: ' + str(len(entries['entries']))) if len(entries['entries']) > 0 else print(time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()), 'No new entries')
+
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [executor.submit(process_entry, i) for i in entries['entries']]
+ for future in concurrent.futures.as_completed(futures):
+ try:
+ data = future.result()
+ except Exception as e:
+ print('generated an exception: %s' % e)
- if len(entries['entries']) > 0:
+ if len(entries['entries']) > 0 and time.time() - start_time >= 3:
print(time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()), 'Done')
time.sleep(60) \ No newline at end of file
diff --git a/requirements.txt b/requirements.txt
index 4596549..7599e77 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,4 +1,5 @@
miniflux
openai
markdownify
-PyYAML \ No newline at end of file
+markdown
+PyYAML