2023-05-08 20:14:19 -04:00
|
|
|
package llm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"text/template"
|
2023-09-04 16:44:59 -04:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
2023-05-08 20:14:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func (req DiffCommentRequest) String() string {
|
|
|
|
return req.MustGetPrompt()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustGetPrompt only returns the prompt, but panics if the data in the request cannot populate the template.
|
|
|
|
func (req DiffCommentRequest) MustGetPrompt() string {
|
|
|
|
prompt, err := req.GetPrompt()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return prompt
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPrompt converts the information in the request to a prompt for an LLM.
|
|
|
|
func (req DiffCommentRequest) GetPrompt() (string, error) {
|
|
|
|
tmpl, err := template.ParseFiles("./llm/prompts/comment-diff-request.tmpl")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var result bytes.Buffer
|
|
|
|
err = tmpl.Execute(&result, req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String is a string representation of DiffCommentResponse.
|
|
|
|
func (res DiffCommentResponse) String() string {
|
|
|
|
out := ""
|
|
|
|
if res.Type == ResponseAnswer {
|
|
|
|
out += "Type: Answer\n"
|
2023-09-04 16:44:59 -04:00
|
|
|
out += res.Response
|
2023-05-08 20:14:19 -04:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
out += "Type: Code Change\n"
|
|
|
|
|
|
|
|
out += "Response:\n"
|
2023-09-04 16:44:59 -04:00
|
|
|
out += res.Response + "\n\n"
|
2023-05-08 20:14:19 -04:00
|
|
|
out += "Files:\n"
|
|
|
|
out += res.File.Path + ":\n```\n"
|
|
|
|
out += res.File.Contents + "\n```\n"
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2023-09-04 16:44:59 -04:00
|
|
|
func ParseDiffCommentResponse(llmResponse string) (DiffCommentResponse, error) {
|
|
|
|
var response DiffCommentResponse
|
|
|
|
err := yaml.Unmarshal([]byte(llmResponse), &response)
|
|
|
|
return response, err
|
2023-05-08 20:14:19 -04:00
|
|
|
}
|