Skip to content

Commit

Permalink
feat: allow edit between generating
Browse files Browse the repository at this point in the history
  • Loading branch information
williamfzc committed Mar 18, 2023
1 parent d1e0214 commit ccfc46c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 22 deletions.
48 changes: 27 additions & 21 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package chat_gpt_ppt

import (
"fmt"
"io/fs"
"log"
"os"

"github.com/abiosoft/ishell/v2"
)

type ApiConfig struct {
Expand All @@ -18,19 +18,7 @@ type ApiConfig struct {

var logger = log.Default()

func GenAndRender(config ApiConfig) error {
content, err := GenAndRenderString(config)
if err != nil {
return err
}
err = os.WriteFile(config.OutputFile, []byte(content), fs.ModePerm)
if err != nil {
return err
}
return nil
}

func GenAndRenderString(config ApiConfig) (string, error) {
func GenAndRenderString(shellContext *ishell.Context, config ApiConfig) (string, error) {
// init client
c := GetClient(config.ClientType)
if c == nil {
Expand All @@ -43,30 +31,31 @@ func GenAndRenderString(config ApiConfig) (string, error) {
return "", fmt.Errorf("no renderer named: %v", config.RendererType)
}
if config.RendererBin != "" {
logger.Printf("set renderer bin: %v\n", config.RendererBin)
shellContext.Printf("set renderer bin: %v\n", config.RendererBin)
renderer.SetBinPath(config.RendererBin)
}

// prepare
logger.Println("start preparing ...")
shellContext.Println("start preparing ...")
err := c.Prepare(config.Topics)
if err != nil {
return "", err
}

// fill
logger.Println("start generating ...")
shellContext.Println("start generating ...")
topics := make([]*Topic, 0)
for _, eachTopic := range config.Topics {
resp, err := c.FillTopic(eachTopic)
finalTopic, err := getFinalTopic(shellContext, c, eachTopic)
if err != nil {
return "", err
}
topics = append(topics, resp)
topics = append(topics, finalTopic)
}

// renderer
logger.Println("start rendering")
shellContext.Println("start rendering ...")
shellContext.Stop()
for _, eachTopic := range topics {
renderer.AddTopic(eachTopic)
}
Expand All @@ -76,3 +65,20 @@ func GenAndRenderString(config ApiConfig) (string, error) {
}
return str, nil
}

func getFinalTopic(shellContext *ishell.Context, c Client, eachTopic string) (*Topic, error) {
resp, err := c.FillTopic(eachTopic)
if err != nil {
return nil, err
}

shellContext.Println("Here is your response, type any key to continue, type 'n' to edit", resp.ToMarkdown())
ok := shellContext.ReadLine()
if ok != "n" {
return resp, nil
} else {
shellContext.Println("You can enter a new topic to regenerate this page.")
newTopic := shellContext.ReadLine()
return getFinalTopic(shellContext, c, newTopic)
}
}
17 changes: 16 additions & 1 deletion cmd/cgp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package main

import (
"flag"
"io/fs"
"os"
"strings"

"github.com/abiosoft/ishell/v2"
cgp "github.com/williamfzc/chat-gpt-ppt"
)

Expand Down Expand Up @@ -33,7 +35,20 @@ func main() {
RendererBin: *rendererBin,
ClientType: *clientType,
}
err = cgp.GenAndRender(config)

shell := ishell.New()
cmd := &ishell.Cmd{
Name: "gen",
Help: "gen",
Func: func(c *ishell.Context) {
content, err := cgp.GenAndRenderString(c, config)
panicIfErr(err)
err = os.WriteFile(config.OutputFile, []byte(content), fs.ModePerm)
panicIfErr(err)
},
}
shell.AddCmd(cmd)
err = shell.Process("gen")
panicIfErr(err)
}

Expand Down
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ require (
)

require (
github.com/abiosoft/ishell/v2 v2.0.2 // indirect
github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
24 changes: 24 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
github.com/abiosoft/ishell/v2 v2.0.2 h1:5qVfGiQISaYM8TkbBl7RFO6MddABoXpATrsFbVI+SNo=
github.com/abiosoft/ishell/v2 v2.0.2/go.mod h1:E4oTCXfo6QjoCart0QYa5m9w4S+deXs/P/9jA77A9Bs=
github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db h1:CjPUSXOiYptLbTdr1RceuZgSFDQ7U15ITERUGrUORx8=
github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db/go.mod h1:rB3B4rKii8V21ydCbIzH5hZiCQE7f5E9SzUb/ZZx530=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.12.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BMXYYRWTLOJKlh+lOBt6nUQgXAfB7oVIQt5cNreqSLI=
github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:rZfgFAXFS/z/lEd6LJmf9HVZ1LkgYiHx5pHhV5DR16M=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sashabaranov/go-gpt3 v1.3.1 h1:ACQOAVX5CAV5rHt0oJOBMKo9BNcqVnmxEdjVxcjVAzw=
github.com/sashabaranov/go-gpt3 v1.3.1/go.mod h1:BIZdbwdzxZbCrcKGMGH6u2eyGe1xFuX9Anmh3tCP8lQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down

0 comments on commit ccfc46c

Please sign in to comment.