added enhanced prompt (#332)

This commit is contained in:
Cole Lashley 2024-02-25 11:03:15 -08:00 committed by GitHub
parent 7a4e1593b4
commit 85d4259937
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -16,6 +16,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@ -2585,13 +2586,18 @@ func updateAsstResponseAndWriteToUpdateBus(ctx context.Context, cmd *sstore.CmdT
scbus.MainUpdateBus.DoScreenUpdate(cmd.ScreenId, update) scbus.MainUpdateBus.DoScreenUpdate(cmd.ScreenId, update)
} }
func getCmdInfoEngineeredPrompt(userQuery string, curLineStr string) string { func getCmdInfoEngineeredPrompt(userQuery string, curLineStr string, shellType string, osType string) string {
rtn := "You are an expert on the command line terminal. Your task is to help me write a command." promptBase := "You are an AI assistant with deep expertise in command line interfaces, CLI programs, and shell scripting. Your task is to help the user to fix an existing command that will be provided, or if no command is provided, help write a new command that the user requires. Feel free to provide appropriate context, but try to keep your answers short and to the point as the user is asking for help because they are trying to get a task done immediately."
if curLineStr != "" { promptBase = promptBase + " The user is current using the \"" + shellType + "\" shell on " + osType + "."
rtn += "My current command is: " + curLineStr promptCurrentCommand := ""
if strings.TrimSpace(curLineStr) != "" {
// Enclose the command in triple backticks to format it as a code block.
promptCurrentCommand = " The user is currently working with the command: ```\n" + curLineStr + "\n```\n\n"
} }
rtn += ". My question is: " + userQuery + "." promptFormattingInstruction := "Please ensure any command line suggestions or code snippets that are meant to be run by the user are enclosed in triple backquotes for easy copy and paste into the terminal."
return rtn promptQuestion := " The user's question is:\n\n" + userQuery + ""
return promptBase + promptCurrentCommand + promptFormattingInstruction + promptQuestion
} }
func doOpenAICmdInfoCompletion(cmd *sstore.CmdType, clientId string, opts *sstore.OpenAIOptsType, prompt []packet.OpenAIPromptMessageType, curLineStr string) { func doOpenAICmdInfoCompletion(cmd *sstore.CmdType, clientId string, opts *sstore.OpenAIOptsType, prompt []packet.OpenAIPromptMessageType, curLineStr string) {
@ -2641,7 +2647,6 @@ func doOpenAICmdInfoCompletion(cmd *sstore.CmdType, clientId string, opts *sstor
doneWaitingForPackets = true doneWaitingForPackets = true
asstOutputPk.Error = "timeout waiting for server response" asstOutputPk.Error = "timeout waiting for server response"
updateAsstResponseAndWriteToUpdateBus(ctx, cmd, asstMessagePk, asstOutputMessageID) updateAsstResponseAndWriteToUpdateBus(ctx, cmd, asstMessagePk, asstOutputMessageID)
break
case pk, ok := <-ch: case pk, ok := <-ch:
if ok { if ok {
// got a packet // got a packet
@ -2667,11 +2672,9 @@ func doOpenAICmdInfoCompletion(cmd *sstore.CmdType, clientId string, opts *sstor
} }
asstMessagePk.AssistantResponse = asstOutputPk asstMessagePk.AssistantResponse = asstOutputPk
updateAsstResponseAndWriteToUpdateBus(ctx, cmd, asstMessagePk, asstOutputMessageID) updateAsstResponseAndWriteToUpdateBus(ctx, cmd, asstMessagePk, asstOutputMessageID)
} else { } else {
// channel closed // channel closed
doneWaitingForPackets = true doneWaitingForPackets = true
break
} }
} }
} }
@ -2739,7 +2742,6 @@ func doOpenAIStreamCompletion(cmd *sstore.CmdType, clientId string, opts *sstore
return return
} }
doneWaitingForPackets = true doneWaitingForPackets = true
break
case pk, ok := <-ch: case pk, ok := <-ch:
if ok { if ok {
// got a packet // got a packet
@ -2755,7 +2757,6 @@ func doOpenAIStreamCompletion(cmd *sstore.CmdType, clientId string, opts *sstore
} else { } else {
// channel closed // channel closed
doneWaitingForPackets = true doneWaitingForPackets = true
break
} }
} }
} }
@ -2779,6 +2780,14 @@ func BuildOpenAIPromptArrayWithContext(messages []*packet.OpenAICmdInfoChatMessa
return rtn return rtn
} }
func GetOsTypeFromRuntime() string {
osVal := runtime.GOOS
if osVal == "darwin" {
osVal = "macos"
}
return osVal
}
func OpenAICommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (scbus.UpdatePacket, error) { func OpenAICommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (scbus.UpdatePacket, error) {
ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen) ids, err := resolveUiIds(ctx, pk, R_Session|R_Screen)
if err != nil { if err != nil {
@ -2825,7 +2834,8 @@ func OpenAICommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (scbus
} }
curLineStr := defaultStr(pk.Kwargs["curline"], "") curLineStr := defaultStr(pk.Kwargs["curline"], "")
userQueryPk := &packet.OpenAICmdInfoChatMessage{UserQuery: promptStr, MessageID: sstore.ScreenMemGetCmdInfoMessageCount(cmd.ScreenId)} userQueryPk := &packet.OpenAICmdInfoChatMessage{UserQuery: promptStr, MessageID: sstore.ScreenMemGetCmdInfoMessageCount(cmd.ScreenId)}
engineeredQuery := getCmdInfoEngineeredPrompt(promptStr, curLineStr) osType := GetOsTypeFromRuntime()
engineeredQuery := getCmdInfoEngineeredPrompt(promptStr, curLineStr, ids.Remote.ShellType, osType)
userQueryPk.UserEngineeredQuery = engineeredQuery userQueryPk.UserEngineeredQuery = engineeredQuery
writePacketToUpdateBus(ctx, cmd, userQueryPk) writePacketToUpdateBus(ctx, cmd, userQueryPk)
prompt := BuildOpenAIPromptArrayWithContext(sstore.ScreenMemGetCmdInfoChat(cmd.ScreenId).Messages) prompt := BuildOpenAIPromptArrayWithContext(sstore.ScreenMemGetCmdInfoChat(cmd.ScreenId).Messages)