Skip to content

Commit

Permalink
Add previous answer context with GPT queries
Browse files Browse the repository at this point in the history
  • Loading branch information
noboruma committed Apr 11, 2023
1 parent 1be9083 commit 1cfd3a7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
9 changes: 6 additions & 3 deletions ais/chatgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ type PromptRequest struct {
Messages []Message `json:"messages"`
}

func SendPrompt(prompt string, max_tokens int) ([]string, error) {
func SendPrompt(prompt, prev_ans string, max_tokens int) ([]string, error) {
res := []string{}
url := "https://api.openai.com/v1/chat/completions"

PromptRequest := PromptRequest{
Model: "gpt-3.5-turbo",
Messages: []Message{{Role: "user", Content: prompt}},
Model: "gpt-3.5-turbo",
Messages: []Message{
{Role: "assistant", Content: prev_ans},
{Role: "user", Content: prompt},
},
}

b, err := json.Marshal(PromptRequest)
Expand Down
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func prepareInputSection(app *tview.Application, chatView *tview.TextView, copy_
SetLabel(prompt.DefaultPrompt).
SetPlaceholder("E.g. why is 42 the answer")

prev_ans := ""

var sb strings.Builder
inputTextArea.SetDoneFunc(func(key tcell.Key) {
if key == tcell.KeyEnter {
Expand All @@ -65,7 +67,7 @@ func prepareInputSection(app *tview.Application, chatView *tview.TextView, copy_
go func() {
defer inputTextArea.SetDisabled(false)
defer func() { spinctl <- struct{}{} }()
responses, err := ais.SendPrompt(inputTextArea.GetText(), 100)
responses, err := ais.SendPrompt(inputTextArea.GetText(), prev_ans, 100)
if err != nil {
errorLog(err.Error())
return
Expand All @@ -76,7 +78,8 @@ func prepareInputSection(app *tview.Application, chatView *tview.TextView, copy_
sb.WriteString(inputTextArea.GetText())
sb.WriteString("\n")

sections := str.ExtractMarkdownSections(strings.Join(responses, "\n"))
prev_ans = strings.Join(responses, "\n")
sections := str.ExtractMarkdownSections(prev_ans)
for i := range sections {
if sections[i].Markdown {
id := copy_clipboards.Append(sections[i].Content)
Expand Down

0 comments on commit 1cfd3a7

Please sign in to comment.