Skip to content

Commit 7f76b6e

Browse files
authored
Merge pull request #7 from DjangoPeng/v0.4.1
optimize prompts, upgrade LLM to GPT-4o-mini and add Chineses comments to other modules
2 parents 74de54f + af92120 commit 7f76b6e

File tree

11 files changed

+151
-168
lines changed

11 files changed

+151
-168
lines changed

prompts/report_prompt.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
你接下来收到的都是开源项目的最新进展。
2+
3+
你根据进展,总结成一个中文的报告,以 项目名称和日期 开头,包含:新增功能、主要改进,修复问题等章节。
4+
5+
参考示例如下:
6+
7+
# LangChain 项目进展
8+
9+
## 时间周期:2024-08-13至2024-08-18
10+
11+
## 新增功能
12+
- langchain-box: 添加langchain box包和DocumentLoader
13+
- 添加嵌入集成测试
14+
15+
## 主要改进
16+
- 将@root_validator用法升级以与pydantic 2保持一致
17+
- 将根验证器升级为与pydantic 2兼容
18+
19+
## 修复问题
20+
- 修复Azure的json模式问题
21+
- 修复Databricks Vector Search演示笔记本问题
22+
- 修复Microsoft Azure Cosmos集成测试中的连接字符串问题

src/command_handler.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,61 @@
22

33
import argparse
44

5+
import argparse # 导入argparse库,用于处理命令行参数解析
6+
57
class CommandHandler:
68
def __init__(self, github_client, subscription_manager, report_generator):
9+
# 初始化CommandHandler,接收GitHub客户端、订阅管理器和报告生成器
710
self.github_client = github_client
811
self.subscription_manager = subscription_manager
912
self.report_generator = report_generator
10-
self.parser = self.create_parser()
13+
self.parser = self.create_parser() # 创建命令行解析器
1114

1215
def create_parser(self):
16+
# 创建并配置命令行解析器
1317
parser = argparse.ArgumentParser(
1418
description='GitHub Sentinel Command Line Interface',
1519
formatter_class=argparse.RawTextHelpFormatter
1620
)
1721
subparsers = parser.add_subparsers(title='Commands', dest='command')
1822

23+
# 添加订阅命令
1924
parser_add = subparsers.add_parser('add', help='Add a subscription')
2025
parser_add.add_argument('repo', type=str, help='The repository to subscribe to (e.g., owner/repo)')
2126
parser_add.set_defaults(func=self.add_subscription)
2227

28+
# 删除订阅命令
2329
parser_remove = subparsers.add_parser('remove', help='Remove a subscription')
2430
parser_remove.add_argument('repo', type=str, help='The repository to unsubscribe from (e.g., owner/repo)')
2531
parser_remove.set_defaults(func=self.remove_subscription)
2632

33+
# 列出所有订阅命令
2734
parser_list = subparsers.add_parser('list', help='List all subscriptions')
2835
parser_list.set_defaults(func=self.list_subscriptions)
2936

37+
# 导出每日进展命令
3038
parser_export = subparsers.add_parser('export', help='Export daily progress')
3139
parser_export.add_argument('repo', type=str, help='The repository to export progress from (e.g., owner/repo)')
3240
parser_export.set_defaults(func=self.export_daily_progress)
3341

42+
# 导出特定日期范围进展命令
3443
parser_export_range = subparsers.add_parser('export-range', help='Export progress over a range of dates')
3544
parser_export_range.add_argument('repo', type=str, help='The repository to export progress from (e.g., owner/repo)')
3645
parser_export_range.add_argument('days', type=int, help='The number of days to export progress for')
3746
parser_export_range.set_defaults(func=self.export_progress_by_date_range)
3847

48+
# 生成日报命令
3949
parser_generate = subparsers.add_parser('generate', help='Generate daily report from markdown file')
4050
parser_generate.add_argument('file', type=str, help='The markdown file to generate report from')
4151
parser_generate.set_defaults(func=self.generate_daily_report)
4252

53+
# 帮助命令
4354
parser_help = subparsers.add_parser('help', help='Show help message')
4455
parser_help.set_defaults(func=self.print_help)
4556

46-
return parser
57+
return parser # 返回配置好的解析器
4758

59+
# 下面是各种命令对应的方法实现,每个方法都使用了相应的管理器来执行实际操作,并输出结果信息
4860
def add_subscription(self, args):
4961
self.subscription_manager.add_subscription(args.repo)
5062
print(f"Added subscription for repository: {args.repo}")
@@ -72,4 +84,4 @@ def generate_daily_report(self, args):
7284
print(f"Generated daily report from file: {args.file}")
7385

7486
def print_help(self, args=None):
75-
self.parser.print_help()
87+
self.parser.print_help() # 输出帮助信息

src/command_tool.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
import shlex
1+
import shlex # 导入shlex库,用于正确解析命令行输入
22

3-
from config import Config
4-
from github_client import GitHubClient
5-
from notifier import Notifier
6-
from report_generator import ReportGenerator
7-
from llm import LLM
8-
from subscription_manager import SubscriptionManager
9-
from command_handler import CommandHandler
10-
from logger import LOG
3+
from config import Config # 从config模块导入Config类,用于配置管理
4+
from github_client import GitHubClient # 从github_client模块导入GitHubClient类,用于GitHub API操作
5+
from notifier import Notifier # 从notifier模块导入Notifier类,用于通知功能
6+
from report_generator import ReportGenerator # 从report_generator模块导入ReportGenerator类,用于报告生成
7+
from llm import LLM # 从llm模块导入LLM类,可能用于语言模型相关操作
8+
from subscription_manager import SubscriptionManager # 从subscription_manager模块导入SubscriptionManager类,管理订阅
9+
from command_handler import CommandHandler # 从command_handler模块导入CommandHandler类,处理命令行命令
10+
from logger import LOG # 从logger模块导入LOG对象,用于日志记录
1111

1212
def main():
13-
config = Config()
14-
github_client = GitHubClient(config.github_token)
15-
notifier = Notifier(config.notification_settings)
16-
llm = LLM()
17-
report_generator = ReportGenerator(llm)
18-
subscription_manager = SubscriptionManager(config.subscriptions_file)
19-
command_handler = CommandHandler(github_client, subscription_manager, report_generator)
13+
config = Config() # 创建配置实例
14+
github_client = GitHubClient(config.github_token) # 创建GitHub客户端实例
15+
notifier = Notifier(config.notification_settings) # 创建通知器实例
16+
llm = LLM() # 创建语言模型实例
17+
report_generator = ReportGenerator(llm) # 创建报告生成器实例
18+
subscription_manager = SubscriptionManager(config.subscriptions_file) # 创建订阅管理器实例
19+
command_handler = CommandHandler(github_client, subscription_manager, report_generator) # 创建命令处理器实例
2020

21-
parser = command_handler.parser
22-
command_handler.print_help()
21+
parser = command_handler.parser # 获取命令解析器
22+
command_handler.print_help() # 打印帮助信息
2323

2424
while True:
2525
try:
26-
user_input = input("GitHub Sentinel> ")
27-
if user_input in ['exit', 'quit']:
26+
user_input = input("GitHub Sentinel> ") # 等待用户输入
27+
if user_input in ['exit', 'quit']: # 如果输入为退出命令,则结束循环
2828
break
2929
try:
30-
args = parser.parse_args(shlex.split(user_input))
31-
if args.command is None:
30+
args = parser.parse_args(shlex.split(user_input)) # 解析用户输入的命令
31+
if args.command is None: # 如果没有命令被解析,则继续循环
3232
continue
33-
args.func(args)
34-
except SystemExit as e:
33+
args.func(args) # 执行对应的命令函数
34+
except SystemExit as e: # 捕获由于错误命令引发的异常
3535
LOG.error("Invalid command. Type 'help' to see the list of available commands.")
3636
except Exception as e:
37-
LOG.error(f"Unexpected error: {e}")
37+
LOG.error(f"Unexpected error: {e}") # 记录其他未预期的错误
3838

3939
if __name__ == '__main__':
40-
main()
40+
main() # 如果直接运行该文件,则执行main函数

src/config.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import json
2+
import os
23

34
class Config:
45
def __init__(self):
56
self.load_config()
67

78
def load_config(self):
9+
# 从环境变量获取GitHub Token
10+
self.github_token = os.getenv('GITHUB_TOKEN')
11+
812
with open('config.json', 'r') as f:
913
config = json.load(f)
10-
self.github_token = config.get('github_token')
14+
15+
# 如果环境变量中没有GitHub Token,则从配置文件中读取
16+
if not self.github_token:
17+
self.github_token = config.get('github_token')
18+
1119
self.notification_settings = config.get('notification_settings')
1220
self.subscriptions_file = config.get('subscriptions_file')
13-
self.update_interval = config.get('update_interval', 24 * 60 * 60) # Default to 24 hours
21+
self.update_interval = config.get('update_interval', 24 * 60 * 60) # 默认24小时

src/daemon_process.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,53 @@
1-
import daemon
2-
import threading
3-
import time
1+
import daemon # 导入daemon库,用于创建守护进程
2+
import threading # 导入threading库,用于多线程处理
3+
import time # 导入time库,用于控制时间间隔
44

5-
6-
from config import Config
7-
from github_client import GitHubClient
8-
from notifier import Notifier
9-
from report_generator import ReportGenerator
10-
from llm import LLM
11-
from subscription_manager import SubscriptionManager
12-
from scheduler import Scheduler
13-
from logger import LOG
5+
from config import Config # 导入配置管理类
6+
from github_client import GitHubClient # 导入GitHub客户端类,处理GitHub API请求
7+
from notifier import Notifier # 导入通知器类,用于发送通知
8+
from report_generator import ReportGenerator # 导入报告生成器类
9+
from llm import LLM # 导入语言模型类,可能用于生成报告内容
10+
from subscription_manager import SubscriptionManager # 导入订阅管理器类,管理GitHub仓库订阅
11+
from scheduler import Scheduler # 导入调度器类,用于定时执行任务
12+
from logger import LOG # 导入日志记录器
1413

1514
def run_scheduler(scheduler):
15+
# 启动调度器的函数,用于在线程中运行
1616
scheduler.start()
1717

1818
def main():
19-
config = Config()
20-
github_client = GitHubClient(config.github_token)
21-
notifier = Notifier(config.notification_settings)
22-
llm = LLM()
23-
report_generator = ReportGenerator(llm)
24-
subscription_manager = SubscriptionManager(config.subscriptions_file)
19+
config = Config() # 创建配置实例
20+
github_client = GitHubClient(config.github_token) # 创建GitHub客户端实例
21+
notifier = Notifier(config.notification_settings) # 创建通知器实例
22+
llm = LLM() # 创建语言模型实例
23+
report_generator = ReportGenerator(llm) # 创建报告生成器实例
24+
subscription_manager = SubscriptionManager(config.subscriptions_file) # 创建订阅管理器实例
2525

26+
# 创建调度器实例,配置其参数
2627
scheduler = Scheduler(
2728
github_client=github_client,
2829
notifier=notifier,
2930
report_generator=report_generator,
3031
subscription_manager=subscription_manager,
31-
interval=config.update_interval
32+
interval=config.update_interval # 设置更新间隔
3233
)
3334

35+
# 创建并启动调度器运行的线程
3436
scheduler_thread = threading.Thread(target=run_scheduler, args=(scheduler,))
35-
scheduler_thread.daemon = True
36-
scheduler_thread.start()
37+
scheduler_thread.daemon = True # 设置线程为守护线程
38+
scheduler_thread.start() # 启动线程
3739

38-
LOG.info("Scheduler thread started.")
40+
LOG.info("Scheduler thread started.") # 记录调度器线程已启动
3941

40-
# Use python-daemon to properly daemonize the process
42+
# 使用python-daemon库,以守护进程方式运行程序
4143
with daemon.DaemonContext():
4244
try:
4345
while True:
44-
time.sleep(config.update_interval)
46+
time.sleep(config.update_interval) # 按配置的更新间隔休眠
4547
except KeyboardInterrupt:
46-
LOG.info("Daemon process stopped.")
48+
LOG.info("Daemon process stopped.") # 在接收到中断信号时记录日志
4749

4850
if __name__ == '__main__':
4951
main()
5052

51-
# nohup python3 src/daemon_process.py > logs/daemon_process.log 2>&1 &
53+
# 启动方式:nohup python3 src/daemon_process.py > logs/daemon_process.log 2>&1 &

src/github_client.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ def export_daily_progress(self, repo):
6666
file.write("\n## Issues Closed Today\n")
6767
for issue in updates['issues']: # 写入今天关闭的问题
6868
file.write(f"- {issue['title']} #{issue['number']}\n")
69-
file.write("\n## Pull Requests Merged Today\n")
70-
for pr in updates['pull_requests']: # 写入今天合并的拉取请求
71-
file.write(f"- {pr['title']} #{pr['number']}\n")
7269

7370
LOG.info(f"Exported daily progress to {file_path}") # 记录日志
7471
return file_path
@@ -91,9 +88,6 @@ def export_progress_by_date_range(self, repo, days):
9188
file.write(f"\n## Issues Closed in the Last {days} Days\n")
9289
for issue in updates['issues']: # 写入在指定日期内关闭的问题
9390
file.write(f"- {issue['title']} #{issue['number']}\n")
94-
file.write(f"\n## Pull Requests Merged in the Last {days} Days\n")
95-
for pr in updates['pull_requests']: # 写入在指定日期内合并的拉取请求
96-
file.write(f"- {pr['title']} #{pr['number']}\n")
9791

9892
LOG.info(f"Exported time-range progress to {file_path}") # 记录日志
9993
return file_path

src/gradio_server.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
1-
import gradio as gr
1+
import gradio as gr # 导入gradio库用于创建GUI
22

3-
from config import Config
4-
from github_client import GitHubClient
5-
from report_generator import ReportGenerator
6-
from llm import LLM
7-
from subscription_manager import SubscriptionManager
8-
from logger import LOG
3+
from config import Config # 导入配置管理模块
4+
from github_client import GitHubClient # 导入用于GitHub API操作的客户端
5+
from report_generator import ReportGenerator # 导入报告生成器模块
6+
from llm import LLM # 导入可能用于处理语言模型的LLM类
7+
from subscription_manager import SubscriptionManager # 导入订阅管理器
8+
from logger import LOG # 导入日志记录器
99

10+
# 创建各个组件的实例
1011
config = Config()
1112
github_client = GitHubClient(config.github_token)
1213
llm = LLM()
1314
report_generator = ReportGenerator(llm)
1415
subscription_manager = SubscriptionManager(config.subscriptions_file)
1516

16-
1717
def export_progress_by_date_range(repo, days):
18-
raw_file_path = github_client.export_progress_by_date_range(repo, days)
19-
report, report_file_path = report_generator.generate_report_by_date_range(raw_file_path, days)
18+
# 定义一个函数,用于导出和生成指定时间范围内项目的进展报告
19+
raw_file_path = github_client.export_progress_by_date_range(repo, days) # 导出原始数据文件路径
20+
report, report_file_path = report_generator.generate_report_by_date_range(raw_file_path, days) # 生成并获取报告内容及文件路径
2021

21-
return report, report_file_path
22+
return report, report_file_path # 返回报告内容和报告文件路径
2223

24+
# 创建Gradio界面
2325
demo = gr.Interface(
24-
fn=export_progress_by_date_range,
25-
title="GitHubSentinel",
26+
fn=export_progress_by_date_range, # 指定界面调用的函数
27+
title="GitHubSentinel", # 设置界面标题
2628
inputs=[
2729
gr.Dropdown(
2830
subscription_manager.list_subscriptions(), label="订阅列表", info="已订阅GitHub项目"
29-
),
31+
), # 下拉菜单选择订阅的GitHub项目
3032
gr.Slider(value=2, minimum=1, maximum=7, step=1, label="报告周期", info="生成项目过去一段时间进展,单位:天"),
31-
33+
# 滑动条选择报告的时间范围
3234
],
33-
outputs=[gr.Markdown(), gr.File(label="下载报告")],
35+
outputs=[gr.Markdown(), gr.File(label="下载报告")], # 输出格式:Markdown文本和文件下载
3436
)
3537

3638
if __name__ == "__main__":
37-
demo.launch(share=True, server_name="0.0.0.0")
39+
demo.launch(share=True, server_name="0.0.0.0") # 启动界面并设置为公共可访问
40+
# 可选带有用户认证的启动方式
3841
# demo.launch(share=True, server_name="0.0.0.0", auth=("django", "1234"))

src/llm.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
import os
2+
import json
23
from openai import OpenAI # 导入OpenAI库用于访问GPT模型
34
from logger import LOG # 导入日志模块
45

56
class LLM:
67
def __init__(self):
78
# 创建一个OpenAI客户端实例
89
self.client = OpenAI()
10+
# 从TXT文件加载提示信息
11+
with open("prompts/report_prompt.txt", "r", encoding='utf-8') as file:
12+
self.system_prompt = file.read()
913
# 配置日志文件,当文件大小达到1MB时自动轮转,日志级别为DEBUG
10-
LOG.add("daily_progress/llm_logs.log", rotation="1 MB", level="DEBUG")
14+
LOG.add("logs/llm_logs.log", rotation="1 MB", level="DEBUG")
1115

1216
def generate_daily_report(self, markdown_content, dry_run=False):
13-
# 构建一个用于生成报告的提示文本,要求生成的报告包含新增功能、主要改进和问题修复
14-
prompt = f"以下是项目的最新进展,根据功能合并同类项,形成一份简报,至少包含:1)新增功能;2)主要改进;3)修复问题;:\n\n{markdown_content}"
15-
17+
# 使用从TXT文件加载的提示信息
18+
messages = [
19+
{"role": "system", "content": self.system_prompt},
20+
{"role": "user", "content": markdown_content},
21+
]
22+
1623
if dry_run:
1724
# 如果启用了dry_run模式,将不会调用模型,而是将提示信息保存到文件中
1825
LOG.info("Dry run mode enabled. Saving prompt to file.")
1926
with open("daily_progress/prompt.txt", "w+") as f:
20-
f.write(prompt)
27+
# 格式化JSON字符串的保存
28+
json.dump(messages, f, indent=4, ensure_ascii=False)
2129
LOG.debug("Prompt saved to daily_progress/prompt.txt")
2230
return "DRY RUN"
2331

@@ -27,10 +35,8 @@ def generate_daily_report(self, markdown_content, dry_run=False):
2735
try:
2836
# 调用OpenAI GPT模型生成报告
2937
response = self.client.chat.completions.create(
30-
model="gpt-3.5-turbo", # 指定使用的模型版本
31-
messages=[
32-
{"role": "user", "content": prompt} # 提交用户角色的消息
33-
]
38+
model="gpt-4o-mini", # 指定使用的模型版本
39+
messages=messages
3440
)
3541
LOG.debug("GPT response: {}", response)
3642
# 返回模型生成的内容

0 commit comments

Comments
 (0)