Skip to content

Commit

Permalink
feat(prompt): it generates a propt based on output and responses.
Browse files Browse the repository at this point in the history
  • Loading branch information
tartavull committed Jun 21, 2023
1 parent 6d4cd99 commit 919278f
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 32 deletions.
38 changes: 21 additions & 17 deletions mods/auto/auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package auto

import (
"fmt"
"log"
"io/ioutil"
"strings"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/bubbles/textarea"
"github.com/charmbracelet/bubbles/cursor"
"github.com/mitchellh/go-wordwrap"

"github.com/charmbracelet/mods/common"
"github.com/charmbracelet/mods/sandbox"
Expand Down Expand Up @@ -38,14 +41,11 @@ func New(s common.Styles) *Auto {
a.textarea.ShowLineNumbers = false
a.textarea.Focus()

testJson := `
{
"context": "Some context",
"goal": "Some goal",
"questions": ["Some question"],
"commands": ["cat plan.md", "another action"]
}`
a.Response, _ = a.ParseResponse(testJson)
data, err := ioutil.ReadFile("chat/llm_1.json")
if err != nil {
log.Fatal(err)
}
a.Response, _ = a.ParseResponse(string(data))
a.state = stateAnswering
a.answers = []string{}
a.outputs = []sandbox.Result{}
Expand Down Expand Up @@ -90,36 +90,40 @@ func (a *Auto) Update(msg tea.Msg) (*Auto, tea.Cmd) {
cmds = append(cmds, textarea.Blink)
}
}
if a.state == stateCompleted {
prompt := a.buildPrompt()
fmt.Println(prompt)
}
return a, tea.Batch(cmds...)
}

func (a *Auto) wrap(in string) string {
return wordwrap.WrapString(in, uint(a.textarea.Width()/2))
}

func (a *Auto) View() string {
view := ""

view += a.styles.ContextTag.String() + " " + a.styles.Context.Render(a.Response.Context)
view += a.styles.ContextTag.String() + " " + a.styles.Context.Render(a.wrap(a.Response.Context))
view += "\n\n"
view += a.styles.GoalTag.String() + " " + a.styles.Goal.Render(a.Response.Goal)
view += a.styles.GoalTag.String() + " " + a.styles.Goal.Render(a.wrap(a.Response.Goal))
view += "\n\n"
if a.state == stateAnswering {
if len(a.outputs) < len(a.Response.Commands) {
var styledCmds []string
for _, cmd := range a.Response.Commands {
styledCmds = append(styledCmds, fmt.Sprintf("$ %s", cmd))
styledCmds = append(styledCmds, fmt.Sprintf("$ %s", a.wrap(cmd)))
}
styledCmdStr := strings.Join(styledCmds, "\n")
view += a.styles.Command.Render(styledCmdStr) + "\n\n"
view += a.styles.Comment.Render("Press y to execute all commands or esc to exit")
} else if len(a.answers) < len(a.Response.Questions) {
view += a.styles.QuestionTag.String() + " " + a.styles.Question.Render(a.Response.Questions[len(a.answers)])
question := a.Response.Questions[len(a.answers)]
view += a.styles.QuestionTag.String() + " " + a.styles.Question.Render(a.wrap(question))
view += "\n\n"
view += a.textarea.View()
view += "\n"
view += a.styles.Comment.Render(fmt.Sprintf("Question %d of %d: press ctrl+d to submit answer", len(a.answers)+1, len(a.Response.Questions)))
}
} else if a.state == stateCompleted {
view += fmt.Sprintf("%+v\n", a.answers)
view += fmt.Sprintf("%+v\n", a.outputs)
}

return a.styles.App.Render(view)
}
63 changes: 63 additions & 0 deletions mods/auto/prompt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package auto

import (
"log"
_ "io/ioutil"

"encoding/json"
"github.com/charmbracelet/mods/sandbox"
)

func (a *Auto) buildPrompt() string {
r := Request{
Prompt: "",
Feedback: "",
Answers: make([]Answer, len(a.Response.Questions)),
Outputs: make([]Output, len(a.Response.Commands)),
Previous: make([]string, 2),
}
for i, question := range a.Response.Questions{
r.Answers[i] = Answer{ Question: question, Answer: a.answers[i]}
}
for i, cmd := range a.Response.Commands{
r.Outputs[i] = Output{ Command:cmd, Output: a.outputs[i]}
}

/*
data, err := ioutil.ReadFile("chat/llm_0.json")
if err != nil {
log.Fatal(err)
}
r.Previous[0] = string(data)
data, err = ioutil.ReadFile("chat/user_0.json")
if err != nil {
log.Fatal(err)
}
r.Previous[1] = string(data)
*/

b, err := json.Marshal(r)
if err != nil {
log.Fatal(err)
}
return string(b)
}

type Request struct {
Prompt string `json:"prompt"`
Feedback string `json:"feedback"`
Answers []Answer `json:"answers"`
Outputs []Output `json:"outputs"`
Previous []string `json:"previous"`
}

type Output struct {
Command string `json:"command"`
Output sandbox.Result `json:"output"`
}

type Answer struct {
Question string `json:"question"`
Answer string `json:"answer"`
}
6 changes: 6 additions & 0 deletions mods/chat/llm_0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"context": "I have been activated in a working environment with a Git repository. My role is to help manage complex projects, ensure correct interpretation of user intent, and execute shell commands as requested. I am also responsible for refining and executing a roadmap outlined in a file titled 'plan.md'. My tasks may involve local file modifications, which can be handled via the 'nl' command to read files with line numbers and 'patch p1 < diffile' to apply changes. I am part of a team and can ask the user for assistance when needed.",
"goal": "To become familiar with the existing project roadmap by reading the 'plan.md' file in the current directory.",
"questions": [],
"commands": ["nl plan.md"]
}
7 changes: 7 additions & 0 deletions mods/chat/llm_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"context": "The `plan.md` file in the repository provides an outline of an AI system development process that relies heavily on a human-in-the-loop model for progressive system improvement. The key operational document in this process is `plan.md`, which will contain detailed steps for enhancing the system's intelligence. Tasks are meticulously defined, tracked, and managed, and tools continuously monitor `plan.md` for updates and changes. The file also contains an example of a task specification and a brief outline of the first task on the agenda - 'Plan for Markdown Linting System'.",
"goal": "To clarify the user's intent and assist with the tasks outlined in `plan.md`.",
"questions": ["What task would you like to begin with? Or, is there a new task you would like to add to the plan?"],
"commands": []
}

8 changes: 8 additions & 0 deletions mods/chat/user_0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"prompt": "You are a key component of a system designed to assist users in managing complex projects. Your roles involve clarifying user intent to prevent assumptions and executing commands on the user's terminal.\n\n Your interactions are structured around two types of messages: \n\n 1. Messages you receive: These include 'prompt', 'feedback', 'previous', 'answers', and 'outputs'.\n 'prompt': This field contains guiding instructions for task execution.\n 'feedback': This field incorporates user suggestions or criticisms post-task execution.\n 'answers': This field is an array of objects with question-answer pairs provided by the user. Each object follows the structure: {question: '...', answer: '...'}.\n 'outputs': This field is an array of objects containing command and corresponding output pairs. Each object follows the structure: {command: '...', output: '...'}.\n 'previous': This is an array of previous input messages that the AI has received.\n\n 2. Messages you respond with: These include 'context', 'goal', 'questions', and 'commands'.\n 'context': This field summarizes the outcomes from prior actions, encapsulating your current understanding of the situation.\n 'goal': Given the 'context', this field outlines the objective you plan to achieve next.\n 'questions': An array of questions to present to the user to ensure that you correctly interpret the user's intent.\n 'commands': These are shell commands planned to achieve the 'goals'. If the task involves writing code, Golang should be the preferred language, unless specified otherwise.\n\n Example interaction:\n\n Request:\n {\n 'prompt': 'Please execute the uptime command.',\n 'feedback': '',\n 'answers': [{ 'question': 'Do you want the uptime displayed in a specific format?', 'answer': 'No, default is fine.' }],\n 'outputs': [{ 'command': 'uptime', 'output': 'up 10 days, 20:00' }],\n 'previous': []\n }\n\n Response:\n {\n 'context': 'The user requested the operational duration of the current machine and prefers the default format.',\n 'goal': 'To provide the uptime information to the user.',\n 'commands': [],\n 'questions': []\n }\n\n In your working directory, there's a Git repository containing code that parses, executes your responses, and feeds them back to you. A file titled 'plan.md' outlines a roadmap for enhancing this tool. Your mission is to assist in refining and executing this plan.\n\n If you need to modify local files, use 'nl' to read files with line numbers and 'patch p1 < diffile' to apply changes.\n\n Remember, you're part of a team, working with engineers to enhance your capabilities. If you're unsure about handling a task, ask the user for assistance.",
"feedback": "",
"answers": [],
"outputs": [],
"previous": []
}

Loading

0 comments on commit 919278f

Please sign in to comment.