ferrislucas / promptr

Promptr is a CLI tool that lets you use plain English to instruct GPT3 or GPT4 to make changes to your codebase.
MIT License
902 stars 37 forks source link

SyntaxError: Unexpected token c in JSON #44

Closed HaveF closed 1 year ago

HaveF commented 1 year ago

Hi, Thanks for your repo. I don't know why the below not works:

it is a minimal code to reproduce :

package main

import (
    "crypto/md5"
    "encoding/hex"
)

type Input struct {
    Types     string `json:"types"` // comments!
    Json      string `json:"json"`
    Check_str string `json:"check_str"`
}

func validateInput(input Input) bool {
    expected := getCheckStr(input)

    return expected == input.Check_str
}

func getCheckStr(input Input) string {
    str := input.Types + input.Json
    hash := md5.Sum([]byte(str))
    expected := hex.EncodeToString(hash[:])[0:10]
    return expected
}

prompt: Make sample.go more efficient.

(node:19804) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `promptr --trace-warnings ...` to show where the warning was created)
Execution time: 14615ms
There was an error parsing the model's output:
{
  "operations": [
    {
      "crudOperation": "update",
      "filePath": "sample.go",
      "fileContents": "package main\n\nimport (\n\t"crypto/md5"\n\t"encoding/hex"\n)\n\ntype Input struct {\n\tTypes     string `json:\"types\"` // comments!\n\tJson      string `json:\"json\"`\n\tCheck_str string `json:\"check_str\"`\n}\n\nfunc validateInput(input Input) bool {\n\texpected := getCheckStr(input)\n\n\treturn expected == input.Check_str\n}\n\nfunc getCheckStr(input Input) string {\n\tstr := input.Types + input.Json\n\thash := md5.Sum([]byte(str))\n\texpected := hex.EncodeToString(hash[:])[0:10]\n\treturn expected\n}\n"
    }
  ]
}
Attemping to extract json:
{
  "operations": [
    {
      "crudOperation": "update",
      "filePath": "sample.go",
      "fileContents": "package main\n\nimport (\n\t"crypto/md5"\n\t"encoding/hex"\n)\n\ntype Input struct {\n\tTypes     string `json:\"types\"` // comments!\n\tJson      string `json:\"json\"`\n\tCheck_str string `json:\"check_str\"`\n}\n\nfunc validateInput(input Input) bool {\n\texpected := getCheckStr(input)\n\n\treturn expected == input.Check_str\n}\n\nfunc getCheckStr(input Input) string {\n\tstr := input.Types + input.Json\n\thash := md5.Sum([]byte(str))\n\texpected := hex.EncodeToString(hash[:])[0:10]\n\treturn expected\n}\n"
    }
  ]
}
SyntaxError: Unexpected token c in JSON at position 142
    at JSON.parse (<anonymous>)
    at /snapshot/promptr/dist/index.js
    at w.call (/snapshot/promptr/dist/index.js)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async v.call (/snapshot/promptr/dist/index.js)
    at async /snapshot/promptr/dist/index.js
node:internal/process/task_queues:95
    runMicrotasks();
    ^

TypeError: Cannot read properties of null (reading 'operations')
    at h.call (/snapshot/promptr/dist/index.js)
    at w.call (/snapshot/promptr/dist/index.js)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async v.call (/snapshot/promptr/dist/index.js)
    at async /snapshot/promptr/dist/index.js

Node.js v18.5.0

Btw, I read [a post today]https://github.com/minimaxir/simpleaichat/blob/main/examples/notebooks/simpleaichat_coding.ipynb). maybe we can use tricks in it, like "stop": ["``` ", "```\n"] etc.

ferrislucas commented 1 year ago

Hi @HaveF thanks for reporting the issue! Thanks for linking that code too, that’s interesting.

The json in the model’s response is probably not syntactically correct, so it’s not being parsed correctly. OpenAI just released some new features that may fix this problem once I get them integrated: https://news.ycombinator.com/item?id=36330972

If that works then this problem should be solved. I’ll mention this issue in the PR so that you’re notified when the fix is merged in.

For now, if you have GPT4 access then try that as GPT4 is much better at responding with valid json. You could also retry a few times. I’ve seen examples of this error resolving with a few attempts.

I’m excited to work on this, so hopefully it shouldn’t be too long before this is fixed.

Take care!

HaveF commented 1 year ago

Oh, yes. This area develop too fast. I believe function call will fit your logic well, especially the function_call: "none" part. Don't bother to notified me, I'm watching the release of the tools :D

Thanks for your tools again!