Skip to content

Commit

Permalink
Merge pull request #337 from nerdalert/prompt-scrub
Browse files Browse the repository at this point in the history
Strip the ctx prompt from the yaml/logs results
  • Loading branch information
mergify[bot] authored May 7, 2024
2 parents 290096f + 74ebaad commit 6e3e8fc
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions worker/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const (
jobPreCheck = "precheck"
sdgModel = "mistralai/mixtral-8x7b-instruct-v0-1"
jsonViewerFilenameSuffix = "-viewer.html"
ctxPrompt = "Answer this based on the following context:"
)

const (
Expand Down Expand Up @@ -324,10 +325,11 @@ func (w *Worker) runPrecheck(lab, outputDir, modelName string) error {
}

context, hasContext := example["context"].(string)
originalQuestion := question
// Slicing args breaks ilab chat for context, use Sprintf to control spacing
if hasContext {
// Append the context to the question with a specific format
question = fmt.Sprintf("%s Answer this based on the following context: %s.", question, context)
question = fmt.Sprintf("%s %s %s.", question, ctxPrompt, context)
}
commandStr := fmt.Sprintf("chat --quick-question %s", question)
if TlsInsecure {
Expand Down Expand Up @@ -356,10 +358,11 @@ func (w *Worker) runPrecheck(lab, outputDir, modelName string) error {

logData := map[string]interface{}{
"input": map[string]string{
"question": question,
"question": originalQuestion,
},
"output": out.String(),
}

if hasContext {
logData["input"].(map[string]string)["context"] = context
}
Expand Down Expand Up @@ -1104,6 +1107,26 @@ func (w *Worker) handleOutputFiles(outputDir, prNumber, outDirName string) strin
continue
}

// Strip the context prompt out from the question in the precheck logs
if info.ModTime().After(w.jobStart) && strings.HasSuffix(filename, ".log") {
content, err := os.ReadFile(fullPath)
if err != nil {
sugar.Errorf("Could not read file: %v", err)
continue
}
contentStr := string(content)
// Split into two parts, modifying only the first occurrence in the Q section
parts := strings.SplitN(contentStr, ctxPrompt, 2)
if len(parts) > 1 {
modifiedContent := parts[0] + "\n" + strings.SplitN(parts[1], "\n", 2)[1]
err = os.WriteFile(fullPath, []byte(modifiedContent), 0644)
if err != nil {
sugar.Errorf("Could not write modified content back to file: %v", err)
continue
}
}
}

// Only process files created after the job start time
if info.ModTime().After(w.jobStart) {
if strings.HasSuffix(filename, ".json") || strings.HasSuffix(filename, ".jsonl") {
Expand Down

0 comments on commit 6e3e8fc

Please sign in to comment.