Created
March 10, 2023 17:57
-
-
Save ivershuo/140cb62af534b946749cdb7eeae8fd3e to your computer and use it in GitHub Desktop.
一个chatbash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"os" | |
"os/exec" | |
"regexp" | |
"strings" | |
"github.com/sashabaranov/go-openai" | |
) | |
var client *openai.Client | |
var messages []openai.ChatCompletionMessage | |
var ctx = context.Background() | |
func init() { | |
key := os.Getenv("OPENAI_KEY") | |
if key == "" { | |
println("没有配置key,可在终端中使用以下方式配置:\nexport OPENAI_KEY=sk-xxxxxx") | |
return | |
} | |
client = openai.NewClient(key) | |
messages = append(messages, openai.ChatCompletionMessage{ | |
Role: openai.ChatMessageRoleSystem, | |
Content: "You are a computer maintenance assistant, skilled in using bash to help users solve computer problems. The current user is using the Mac OS system.", | |
}) | |
} | |
func main() { | |
q := strings.Join(os.Args[1:], "") | |
if q == "" { | |
fmt.Println("您没有输入任何指令") | |
} | |
// 将需求发送给openai | |
messages = append(messages, openai.ChatCompletionMessage{ | |
Role: openai.ChatMessageRoleUser, | |
Content: q + "\n要实现上面的功能该使用怎样的bash命令?\n请注意:在回答中仅仅包含bash代码内容,回复格式:```bash\n#code here\n```,回复内容以“```”开头,“```”结尾。如果上面的功能无法使用明确的bash指令实现,请回复“IDONTKNOW”。", | |
}) | |
resp, err := client.CreateChatCompletion( | |
ctx, | |
openai.ChatCompletionRequest{ | |
Model: openai.GPT3Dot5Turbo, | |
Messages: messages, | |
Temperature: 0.1, | |
}, | |
) | |
if err != nil { | |
fmt.Println("发生了一点错误," + err.Error()) | |
return | |
} | |
// 获取到openai给出的结果 | |
respText := resp.Choices[0].Message.Content | |
if respText == "IDONTKNOW" { | |
fmt.Println("你在想啥呢!不要为难你的电脑了😓。") | |
return | |
} | |
messages = append(messages, openai.ChatCompletionMessage{ | |
Role: openai.ChatMessageRoleAssistant, | |
Content: respText, | |
}) | |
// 解析bash指令 | |
re := regexp.MustCompile("```bash\n(.*?)\n```") | |
matches := re.FindStringSubmatch(respText) | |
bashCode := matches[1] | |
// 运行bash | |
cmd := exec.Command("bash", "-c", bashCode) | |
output, err := cmd.Output() | |
bashRunRet := "" | |
// fmt.Println("output:" + string(output)) | |
// 将运行结果添加到对话中,以便后续openai理解 | |
if err == nil { | |
bashRunRet = string(output) | |
messages = append(messages, openai.ChatCompletionMessage{ | |
Role: openai.ChatMessageRoleUser, | |
Content: fmt.Sprintf("返回信息如下:%s\n我不是很懂计算机,请帮忙解释一下这个结果,以便我能听懂,说得越简单易懂就好,如果可能,请使用《银河系漫游指南》中马文的说话语气回复我,但是你不要给我透露你使用的马文的语气。", string(output)), | |
}) | |
} else { | |
bashRunRet = err.Error() | |
messages = append(messages, openai.ChatCompletionMessage{ | |
Role: openai.ChatMessageRoleUser, | |
Content: fmt.Sprintf("我运行 \"%s\" 命令之后获得了以下错误信息:%s\n请帮忙解释说明了什么?", bashCode, err.Error()), | |
}) | |
} | |
// 通过openai给出最终说明 | |
resp2, err := client.CreateChatCompletion( | |
ctx, | |
openai.ChatCompletionRequest{ | |
Model: openai.GPT3Dot5Turbo, | |
Messages: messages, | |
Temperature: 0.1, | |
}, | |
) | |
// fmt.Printf("%#v", resp2) | |
if err != nil { | |
fmt.Println("AI罢工了,只获得了以下的结果,希望你能看得懂\n" + bashRunRet + "\n" + err.Error()) | |
return | |
} | |
fmt.Println(resp2.Choices[0].Message.Content) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment