Adjust read_file so it returns line numbers

This commit is contained in:
Matt Low 2023-11-26 10:43:47 +00:00
parent 9976c59f58
commit 42f7b7aa29

View File

@ -79,12 +79,15 @@ For directories, size represents the number of entries in that directory.`,
"read_file": { "read_file": {
Tool: openai.Tool{Type: "function", Function: openai.FunctionDefinition{ Tool: openai.Tool{Type: "function", Function: openai.FunctionDefinition{
Name: "read_file", Name: "read_file",
Description: `Read the contents of a file relative to the current working directory. Description: `Read the contents of a text file relative to the current working directory.
Result is returned as JSON in the following format: Each line of the file is prefixed with its line number and a tabs (\t) to make
it make it easier to see which lines to change for other modifications.
Example:
{ {
"message": "success", // if successful, or a different message indicating failure "message": "success", // if successful, or a different message indicating failure
"result": "the contents\nof the file\n" "result": "1\tthe contents\n2\tof the file\n"
}`, }`,
Parameters: FunctionParameters{ Parameters: FunctionParameters{
Type: "object", Type: "object",
@ -327,8 +330,15 @@ func ReadFile(path string) string {
if err != nil { if err != nil {
return resultToJson(FunctionResult{Message: fmt.Sprintf("Could not read path: %s", err.Error())}) return resultToJson(FunctionResult{Message: fmt.Sprintf("Could not read path: %s", err.Error())})
} }
lines := strings.Split(string(data), "\n")
content := strings.Builder{}
for i, line := range lines {
content.WriteString(fmt.Sprintf("%d\t%s\n", i+1, line))
}
return resultToJson(FunctionResult{ return resultToJson(FunctionResult{
Result: string(data), Result: content.String(),
}) })
} }