Skip to content

Commit

Permalink
sync copilot code
Browse files Browse the repository at this point in the history
  • Loading branch information
shaowenchen committed Jul 23, 2024
1 parent 3a09c9f commit a6c70ca
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/cli/copilot/copilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func CreateCopilot(logger *log.Logger, opt option.CopilotOption) {
break
}
input = strings.TrimSpace(input)
pr, exitCode, err := copilot.RunPipeline(logger, chat, &history, pipelinerunsManager, input, "copilot")
pr, exitCode, err := copilot.RunPipeline(logger, chat, &history, pipelinerunsManager, input, nil)
output := ""
if exitCode == copilot.ExitCodeIntentionEmpty {
output = "I can not understand your input:" + input + ", please help me to solve it, use following intention:\n " + pipelinerunsManager.GetForIntent()
Expand Down
22 changes: 18 additions & 4 deletions pkg/copilot/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ Again:
logger.Error.Printf("llm chatParameters error: %v\n", err)
return
}
clusterEnums := []string{}
for _, cluster := range clusters {
clusterEnums = append(clusterEnums, cluster.Name)
}
// clean string
start := -1
end := -1
Expand Down Expand Up @@ -238,17 +242,27 @@ Again:
}

for k, v := range pipeline.Spec.Variables {
if k == "nameRef" {
v.Enums = clusterEnums
}
if v.Value != "" {
pr.Spec.Variables[k] = v.Value
} else if _, ok := outputVars[k]; ok {
outV := outputVars[k]
pr.Spec.Variables[k] = ""
// if in enum keep it
for _, enum := range v.Enums {
if outV == enum {
if len(v.Enums) > 0 {
found := false
for _, enum := range v.Enums {
if outV == enum {
found = true
break
}
}
if found {
pr.Spec.Variables[k] = outV
break
}
} else {
pr.Spec.Variables[k] = outV
}
} else {
pr.Spec.Variables[k] = ""
Expand Down
13 changes: 10 additions & 3 deletions pkg/copilot/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,23 @@ func GetParametersPrompt(pipeline opsv1.Pipeline, clusters []opsv1.Cluster) stri
if len(pipeline.Spec.Variables) >= 0 {
desc.WriteString("It requires the following parameters(if enum provided, choose one of them):\n")
}
clusterEnums := []string{}
clusterEnum := []string{}
for _, cluster := range clusters {
clusterEnums = append(clusterEnums, cluster.Name)
clusterEnum = append(clusterEnum, cluster.Name)
}
for k, _ := range pipeline.Spec.Variables {
vt := pipeline.Spec.Variables[k]
if k == "nameRef" {
vt.Enums = clusterEnums
vt.Enums = clusterEnum
}
vStr, _ := json.Marshal(vt)
parmDesc := fmt.Sprintf("\t- %s \t %s\n", k, string(vStr))
desc.WriteString(parmDesc)
}
clustersInfo := ""
for i := 0; i < len(clusters); i++ {
clustersInfo += fmt.Sprintf("- %s, desc: %s\n", clusters[i].Name, clusters[i].Spec.Desc)
}
outputScheme := map[string]string{}
for key, value := range pipeline.Spec.Variables {
if value.Value != "" {
Expand All @@ -57,6 +61,9 @@ func GetParametersPrompt(pipeline opsv1.Pipeline, clusters []opsv1.Cluster) stri
-understand < Workflow > and output as required.
-understand the pipeline description and parameters provided.
< Clusters information >
` + clustersInfo + `
< Workflow >
1. According to the following pipeline definition, accurately extract the appropriate parameters from the user input, and pay attention to the data type of the parameters.
two。. Please make sure that the parameters you extract strictly follow the definition of the pipeline and contain only the information explicitly mentioned in the user's input.
Expand Down
12 changes: 9 additions & 3 deletions pkg/copilot/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const ExitCodeDefault = 0
const ExitCodeIntentionEmpty = 1
const ExitCodeParametersNotFound = 2

func RunPipeline(logger *log.Logger, chat func(string, string, *RoleContentList) (string, error), history *RoleContentList, pipelinerunsManager *PipelineRunsManager, input string, creator string) (prResult *opsv1.PipelineRun, exit int, err error) {
func RunPipeline(logger *log.Logger, chat func(string, string, *RoleContentList) (string, error), history *RoleContentList, pipelinerunsManager *PipelineRunsManager, input string, extraVariables map[string]string) (prResult *opsv1.PipelineRun, exit int, err error) {
exit = ExitCodeDefault
pipelines, err := pipelinerunsManager.GetPipelines()
if err != nil {
Expand All @@ -22,7 +22,7 @@ func RunPipeline(logger *log.Logger, chat func(string, string, *RoleContentList)
logger.Debug.Println("available pipelines num: ", len(pipelines))
// chat intention
history.WithHistory(0)
_, pipeline, prResult, err := ChatIntention(logger, chat, GetIntentionPrompt, pipelines, history, input, 1)
_, pipeline, prResult, err := ChatIntention(logger, chat, GetIntentionPrompt, pipelines, history, input, 3)
if err != nil {
return
}
Expand All @@ -32,7 +32,7 @@ func RunPipeline(logger *log.Logger, chat func(string, string, *RoleContentList)
}
// chat parameters
history.WithHistory(0)
ChatParameters(logger, chat, GetParametersPrompt, pipelines, clusters, history, pipeline, prResult, input, 1)
ChatParameters(logger, chat, GetParametersPrompt, pipelines, clusters, history, pipeline, prResult, input, 3)
if pipeline.Spec.Variables != nil {
variables := map[string]string{}
for k, _ := range pipeline.Spec.Variables {
Expand All @@ -43,6 +43,12 @@ func RunPipeline(logger *log.Logger, chat func(string, string, *RoleContentList)
variables[k] = ""
}
}
// merge extra variables
for k, v := range extraVariables {
if val, ok := prResult.Spec.Variables[k]; !ok || val == "" {
variables[k] = v
}
}
prResult.Spec.Variables = variables
}
// skip run pr
Expand Down

0 comments on commit a6c70ca

Please sign in to comment.