Skip to content

Commit

Permalink
fix: skip anonymous functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaostudy committed Dec 26, 2023
1 parent bd1ffdf commit 9e67283
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
28 changes: 16 additions & 12 deletions bigmodel/chatgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type chunk struct {
Delta struct {
Content string `json:"content"`
} `json:"delta"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`
}

Expand Down Expand Up @@ -100,8 +101,11 @@ func (gpt *ChatGPT) Chat(query string) chan Result {
}
defer resp.Body.Close()

// TODO: response status exception

// read response stream data
buf := make([]byte, 4096)
buf := make([]byte, 1024)
var chunks, tmp string
for {
n, err := resp.Body.Read(buf)
if err == io.EOF {
Expand All @@ -113,23 +117,23 @@ func (gpt *ChatGPT) Chat(query string) chan Result {
return
}

chunks := string(buf[:n])
data := new(chunk)
for _, chunk := range strings.Split(chunks, "\n\n") {
if !strings.HasPrefix(chunk, "data: ") {
continue
}
chunk = strings.TrimPrefix(chunk, "data: ")
chunks, tmp = tmp+string(buf[:n]), ""
for _, chk := range strings.Split(chunks, "\n\n") {
chk = strings.TrimPrefix(chk, "data: ")

// done
if chunk == "[DONE]" {
if chk == "[DONE]" {
out <- Result{Type: TypeDone}
return
}

err := json.Unmarshal([]byte(chunk), data)
data := new(chunk)
err := json.Unmarshal([]byte(chk), data)
if err != nil {
out <- Result{Type: TypeError, Content: err.Error()}
tmp += chk
continue
}
if len(data.Choices) == 0 {
out <- Result{Type: TypeError, Content: "response data error"}
return
}
out <- Result{Type: TypeData, Content: data.Choices[0].Delta.Content}
Expand Down
7 changes: 3 additions & 4 deletions diagnostic/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,15 @@ func (diag *Diag) Analyze(pnc, stack string, funs []*Function) {
case bigmodel.TypeData:
print(ans.Content)
case bigmodel.TypeDone:
close(answer)
finish = true
case bigmodel.TypeError:
close(answer)
log.Fatalln("chatgpt response error:", ans.Content)
default:
close(answer)
log.Fatalln("chatgpt response unknown type:", ans.Type)
}
}
close(answer)
println()
}

func buildFuncListDescription(funs []*Function) string {
Expand All @@ -129,7 +128,7 @@ func buildFuncListDescription(funs []*Function) string {
}

func buildFileFunctionsDescription(file string, funs []*Function) string {
desc := strings.TrimPrefix(file, root) + ":\n```go\n"
desc := file + ":\n```go\n"
for _, f := range funs {
desc += f.Source + "\n"
}
Expand Down
12 changes: 9 additions & 3 deletions diagnostic/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,22 @@ func ReadFuncSource(frame *runtime.Frame) *Function {
}
}

log.Fatalln("function source code not found")
log.Printf("the source code of function %s cannot be found in %s", fun, file)
return nil
}

func GetFuncList(frames *runtime.Frames) (funs []*Function) {
set := map[string]struct{}{}
for {
frame, more := frames.Next()
if strings.HasPrefix(frame.File, root) {
fun := ReadFuncSource(&frame)
funs = append(funs, fun)
if _, ok := set[frame.Function]; !ok {
fun := ReadFuncSource(&frame)
if fun != nil {
funs = append(funs, fun)
set[frame.Function] = struct{}{}
}
}
}
if !more {
break
Expand Down
4 changes: 3 additions & 1 deletion example/math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ func Mul(a, b int) int {
}

func Div(a, b int) int {
return a / b
return func(a, b int) int {
return a / b
}(a, b)
}

0 comments on commit 9e67283

Please sign in to comment.