Skip to content

Commit

Permalink
add: 支持Openai
Browse files Browse the repository at this point in the history
  • Loading branch information
muyu66 committed Apr 7, 2024
1 parent 14990ef commit 97c41ee
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 184 deletions.
121 changes: 56 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
# Git to DailyReport

将Git日志通过大模型自动转换成每日工作报告

[![Release](https://img.shields.io/github/release/muyu66/git-to-dailyreport.svg?style=flat-square)](https://github.com/muyu66/git-to-dailyreport/releases)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
<div align="center">
<h1> Git to DailyReport </h1>
将Git日志通过大模型自动转换成每日/周工作报告
</div>

<div align="center">
<a href="https://github.com/muyu66/git-to-dailyreport/releases">
<img src="https://img.shields.io/github/release/muyu66/git-to-dailyreport.svg?style=flat-square" />
</a>
<a href="https://opensource.org/licenses/MIT">
<img src="https://img.shields.io/badge/License-MIT-green.svg">
</a>
</div>

## 特点

Expand All @@ -18,22 +25,48 @@
* 支持周报
* 支持终端显示工作报告,亦或是生成文本文件
* 支持AI Flow工作方式,提供模型文本准确度
* 已接入OpenAI,并可以支持国内代理地址
* 已接入ollama

![](public/pic1.gif "to boss")

## 一键开始
## 开始使用

1. **申请AI大模型**

```markdown
提供 Ollama、阿里云DashScope、OpenAI三种接入方式
```

Ollama:前往 [Ollama Github](https://github.com/ollama/ollama) 安装

阿里云DashScope:前往 [DashScope主页](https://dashscope.aliyun.com) 开通相关功能

OpenAI: 前往 [OpenAI Doc](https://platform.openai.com/api-keys) 获取ApiKey。或者尝试免费的国内代理 [Chatanywhere](https://github.com/chatanywhere/GPT_API_free)

2. **配置**

```markdown
根据配置文件 `config.sample.yaml` 配置相关参数,并重命名为`config.yaml`放置于 report.exe 同目录下
```

3. **运行在 Windows**

```shell
.\一键生成日报.bat
```

Windows
```shell
.\一键生成周报.bat
```

双击 一键生成日报.bat
OR
双击 一键生成周报.bat
4. **运行在 Linux Or Docker**

* 开通阿里大模型 https://dashscope.aliyun.com
* 配置文件 需要将`config.sample.yaml`重命名为`config.yaml`,并填写配置
* 确保`config.yaml``report.exe`在同一个目录下
```markdown
// TODO:
```

## 命令行使用
## 高级使用

$ .\bin\report.exe

Expand All @@ -45,6 +78,14 @@ Windows
|----|-----|----------|--------|
| -c | day | day week | 工作报告周期 |

## 编译 Win64 (可选)

需要 go 1.22+

```shell
.\build.bat # 执行编译脚本
```

## 效果展示

日报:
Expand Down Expand Up @@ -105,54 +146,6 @@ Best regards,
[Your Title]
```

## 编译 Win64

.\build.bat # 编译脚本
.\bin\report.exe # 开始使用

## 配置文件示例

config.yaml (与report.exe同目录)

```yaml
ai:
# [aliyun|ollama]
name: aliyun
ak: sk-xxxxxxxxxxxxxxxxxxx
# qwen1.5-72b-chat ⭐⭐⭐⭐
# qwen1.5-14b-chat ⭐⭐⭐
# qwen1.5-7b-chat ⭐⭐
# qwen-1.8b-chat ⭐
# qwen-plus 未知
# qwen-turbo 未知
# qwen-max-1201 未知
# qwen-max-longcontext ⭐⭐⭐⭐
model: qwen-max-longcontext
git:
# 不会空则以此username为准,防止误领别人的工作
# 为空字符串则自动获取
username:
repo:
# [数组]
# 自动遍历目录下的所有git仓库
# 可以填项目集中的目录: C:\Web
# 也可以填具体仓库目录: C:\Web\niubiGame
- C:\Web
report:
# 报告模式
# [normal 详细叙述] [simple 简单概要]
mode: normal
# 间隔多少天汇报一次,默认一天报告一次
# 1=只含今天,2=今天和昨天
intervalDay: 1
# 输出模式
# file 输出文本文件
# print 打印到终端
out: file
# 报告语言 [chs|en]
lang: chs
```
## Roadmap

* 将工作区、Stage区纳入上报的工作范围
Expand All @@ -164,8 +157,6 @@ report:
* 将会提交的工作范围简要报告给使用者
* 应对加班等可能跨天的日报
* tty模式配置向导
* 支持本地大模型
* 接入Openai

## Contributors

Expand Down
19 changes: 15 additions & 4 deletions ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,26 @@ import (
)

type Ai interface {
before()
request(*resty.Client, []AiReqBodyMessage) AiReqBodyMessage
after()
}

func AiFactory(aiName string) (Ai, error) {
type AiConfig struct {
Model string
BaseUrl string
ApiKey string
MaxInputTokens uint32
}

func AiFactory(aiName string, aiConf AiConfig) (Ai, error) {
switch aiName {
case "aliyun":
return AliyunAi{}, nil
case "aliyun-dashscope":
return AliyunAi{Config: aiConf}, nil
case "ollama":
return OllamaAi{}, nil
return OllamaAi{Config: aiConf}, nil
case "openai":
return OpenAi{Config: aiConf}, nil
default:
return nil, errors.New("找不到合适的AI引擎")
}
Expand Down
12 changes: 7 additions & 5 deletions ai_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
)

func flow(ai Ai, client *resty.Client, prompt string) AiReqBodyMessage {
log.Info("开始AI-FLOW......")
var flowPrompts = getFlowPrompt()

log.Info("开启AI-FLOW......")
var userMsgs = []AiReqBodyMessage{
{
Role: "system",
Content: "你是一个擅于帮助程序员分析GIT日志,并整理归纳今天工作内容的助理。",
Content: flowPrompts[0],
},
{
Role: "user",
Expand All @@ -22,7 +24,7 @@ func flow(ai Ai, client *resty.Client, prompt string) AiReqBodyMessage {
var messagesBoss = []AiReqBodyMessage{
{
Role: "system",
Content: "你是一个程序员的领导,你要审查他提交的今日工作报告,并提出修改意见。",
Content: flowPrompts[1],
},
{
Role: "user",
Expand All @@ -34,9 +36,9 @@ func flow(ai Ai, client *resty.Client, prompt string) AiReqBodyMessage {
userMsgs = append(userMsgs, msgs1)
userMsgs = append(userMsgs, AiReqBodyMessage{
Role: "user",
Content: "我将你的报告提交给了我的领导,他反馈了一些修改建议,请你进行补充,并重新提交。建议如下:" + msgs2.Content,
Content: flowPrompts[2] + msgs2.Content,
})
var res = ai.request(client, userMsgs)
log.Info("已结束AI-FLOW......")
log.Info("已关闭AI-FLOW......")
return res
}
18 changes: 12 additions & 6 deletions aliyun.ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,31 @@ import (
)

type AliyunAi struct {
Config AiConfig
}

func (ai AliyunAi) request(client *resty.Client, messages []AiReqBodyMessage) AiReqBodyMessage {
func (ai AliyunAi) before() {
log.Info("请求大模型中......")
}

func (ai AliyunAi) after() {
log.Info("请求完成......")
}

func (ai AliyunAi) request(client *resty.Client, messages []AiReqBodyMessage) AiReqBodyMessage {
resp, err := client.R().
SetHeader("Content-Type", "application/json").
SetAuthToken("Bearer " + getAiAkConf()).
SetAuthToken(ai.Config.ApiKey).
SetBody(AiReqBody{
Model: getAiModelConf(),
Model: ai.Config.Model,
Input: AiReqBodyInput{
Messages: messages,
},
Parameters: AiReqBodyParameters{
ResultFormat: Message,
},
}).
Post("https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation")
Post(ai.Config.BaseUrl + "/api/v1/services/aigc/text-generation/generation")

if err != nil {
log.Fatal(err)
Expand All @@ -32,8 +40,6 @@ func (ai AliyunAi) request(client *resty.Client, messages []AiReqBodyMessage) Ai
log.Fatal(resp)
}

log.Info("请求完成......")

result := AiRes{}
jsonErr := json.Unmarshal(resp.Body(), &result)
if jsonErr != nil {
Expand Down
33 changes: 20 additions & 13 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
package main

import "github.com/spf13/viper"

func getAiNameConf() string {
return viper.GetString("ai.name")
}

func getAiAkConf() string {
return viper.GetString("ai.ak")
}

func getAiModelConf() string {
return viper.GetString("ai.model")
}
import (
"github.com/spf13/viper"
"strconv"
)

func getGitUsernameConf() string {
return viper.GetString("git.username")
Expand Down Expand Up @@ -41,3 +32,19 @@ func getReportLangConf() string {
func getReportFlowConf() bool {
return viper.GetBool("report.flow")
}

func getAiConf() AiConfig {
usedAi := getUseAiConf()
m := viper.GetStringMapString("ai." + usedAi)
maxInputTokens, _ := strconv.ParseUint(m["maxinputtokens"], 10, 32)
return AiConfig{
Model: m["model"],
BaseUrl: m["baseurl"],
ApiKey: m["apikey"],
MaxInputTokens: uint32(maxInputTokens),
}
}

func getUseAiConf() string {
return viper.GetString("useAi")
}
39 changes: 25 additions & 14 deletions config.sample.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
# 对于不使用的接口,可以不填
ai:
# [aliyun|ollama]
name: aliyun
ak: sk-xxxxxxxxxxxxxxxxxxx
# qwen1.5-72b-chat ⭐⭐⭐⭐
# qwen1.5-14b-chat ⭐⭐⭐
# qwen1.5-7b-chat ⭐⭐
# qwen-1.8b-chat ⭐
# qwen-plus 未知
# qwen-turbo 未知
# qwen-max-1201 未知
# qwen-max-longcontext ⭐⭐⭐⭐
model: qwen-max-longcontext
aliyun-dashscope:
model: qwen1.5-72b-chat
apiKey: sk-xxxxxxxxxxxxxxxxxxx
baseUrl: https://dashscope.aliyuncs.com
# 请按当前大模型支持的TOKEN数量来输入,如有节省TOKEN考虑,可以设小。TOKEN数量可以查阅相关文档
maxInputTokens: 6000
ollama:
model: qwen:latest
apiKey:
baseUrl: http://localhost:11434
# 同上
maxInputTokens: 6000
openai:
model: gpt-3.5-turbo
apiKey: sk-xxxxxxxxxxxxxxxxxxx
baseUrl: https://api.chatanywhere.com.cn
# 同上
maxInputTokens: 6000
# 选择使用的AI接口
useAi: openai
git:
# 不会空则以此username为准,防止误领别人的工作
# 为空字符串则自动获取
Expand All @@ -31,6 +40,8 @@ report:
# 输出模式
# file 输出文本文件
# print 打印到终端
out: file
out: print
# 报告语言 [chs|en]
lang: chs
lang: chs
# 是否开启AI-FLOW以提高报告精准度 (会额外消耗小部分TOKEN和时间)
flow: true
Loading

0 comments on commit 97c41ee

Please sign in to comment.