Skip to content

Commit 446bb91

Browse files
committed
fix(ollama): still do the dumb json extraction thing
1 parent a44f074 commit 446bb91

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

filter_ollama.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,19 @@ func OllamaFilter(m string) bool {
108108

109109
var r OllamaResponse
110110
respFunc := func(resp api.GenerateResponse) error {
111-
err = json.Unmarshal([]byte(resp.Response), &r)
111+
// Parse the JSON payload (hopefully)
112+
rex := regexp.MustCompile(`\{[^{}]+\}`)
113+
matches := rex.FindAllStringIndex(resp.Response, -1)
114+
115+
// Find the last json payload in case the model reasons about
116+
// one in the middle of thinking
117+
if len(matches) == 0 {
118+
return fmt.Errorf("did not find a json object in response: %s", resp.Response)
119+
}
120+
start, end := matches[len(matches)-1][0], matches[len(matches)-1][1]
121+
content := resp.Response[start:end]
122+
123+
err = json.Unmarshal([]byte(content), &r)
112124
if err != nil {
113125
err = fmt.Errorf("%w, ollama full response: %s", err, resp.Response)
114126
return err

0 commit comments

Comments
 (0)