diff --git a/api/go/core/types/chat.go b/api/go/core/types/chat.go index 315031c7..4e10b5b7 100644 --- a/api/go/core/types/chat.go +++ b/api/go/core/types/chat.go @@ -10,8 +10,14 @@ type ApiRequest struct { } type Message struct { - Role string `json:"role"` - Content string `json:"content"` + Role string `json:"role"` + Content string `json:"content"` + FunctionCall FunctionCall `json:"function_call"` +} + +type FunctionCall struct { + Name string `json:"name"` + Arguments string `json:"arguments"` } type ApiResponse struct { diff --git a/api/go/test/data.txt b/api/go/test/data.txt new file mode 100644 index 00000000..abfa444c --- /dev/null +++ b/api/go/test/data.txt @@ -0,0 +1,32 @@ +data: {"id":"chatcmpl-7aKdKN9CaL7g0CI1wJv6PHxqNKZc8","object":"chat.completion.chunk","created":1688893478,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"role":"assistant","content":null,"function_call":{"name":"browser","arguments":""}},"finish_reason":null}]} + +data: {"id":"chatcmpl-7aKdKN9CaL7g0CI1wJv6PHxqNKZc8","object":"chat.completion.chunk","created":1688893478,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"function_call":{"arguments":"{\n"}},"finish_reason":null}]} + +data: {"id":"chatcmpl-7aKdKN9CaL7g0CI1wJv6PHxqNKZc8","object":"chat.completion.chunk","created":1688893478,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"function_call":{"arguments":" "}},"finish_reason":null}]} + +data: {"id":"chatcmpl-7aKdKN9CaL7g0CI1wJv6PHxqNKZc8","object":"chat.completion.chunk","created":1688893478,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"function_call":{"arguments":" \""}},"finish_reason":null}]} + +data: {"id":"chatcmpl-7aKdKN9CaL7g0CI1wJv6PHxqNKZc8","object":"chat.completion.chunk","created":1688893478,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"function_call":{"arguments":"keyword"}},"finish_reason":null}]} + +data: {"id":"chatcmpl-7aKdKN9CaL7g0CI1wJv6PHxqNKZc8","object":"chat.completion.chunk","created":1688893478,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"function_call":{"arguments":"\":"}},"finish_reason":null}]} + +data: {"id":"chatcmpl-7aKdKN9CaL7g0CI1wJv6PHxqNKZc8","object":"chat.completion.chunk","created":1688893478,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"function_call":{"arguments":" \""}},"finish_reason":null}]} + +data: {"id":"chatcmpl-7aKdKN9CaL7g0CI1wJv6PHxqNKZc8","object":"chat.completion.chunk","created":1688893478,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"function_call":{"arguments":"特"}},"finish_reason":null}]} + +data: {"id":"chatcmpl-7aKdKN9CaL7g0CI1wJv6PHxqNKZc8","object":"chat.completion.chunk","created":1688893478,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"function_call":{"arguments":"斯"}},"finish_reason":null}]} + +data: {"id":"chatcmpl-7aKdKN9CaL7g0CI1wJv6PHxqNKZc8","object":"chat.completion.chunk","created":1688893478,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"function_call":{"arguments":"拉"}},"finish_reason":null}]} + +data: {"id":"chatcmpl-7aKdKN9CaL7g0CI1wJv6PHxqNKZc8","object":"chat.completion.chunk","created":1688893478,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"function_call":{"arguments":"股"}},"finish_reason":null}]} + +data: {"id":"chatcmpl-7aKdKN9CaL7g0CI1wJv6PHxqNKZc8","object":"chat.completion.chunk","created":1688893478,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"function_call":{"arguments":"价"}},"finish_reason":null}]} + +data: {"id":"chatcmpl-7aKdKN9CaL7g0CI1wJv6PHxqNKZc8","object":"chat.completion.chunk","created":1688893478,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"function_call":{"arguments":"\"\n"}},"finish_reason":null}]} + +data: {"id":"chatcmpl-7aKdKN9CaL7g0CI1wJv6PHxqNKZc8","object":"chat.completion.chunk","created":1688893478,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"function_call":{"arguments":"}"}},"finish_reason":null}]} + +data: {"id":"chatcmpl-7aKdKN9CaL7g0CI1wJv6PHxqNKZc8","object":"chat.completion.chunk","created":1688893478,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{},"finish_reason":"function_call"}]} + +data: [DONE] + diff --git a/api/go/test/test.go b/api/go/test/test.go index cfe7bfdf..56c6fd3a 100644 --- a/api/go/test/test.go +++ b/api/go/test/test.go @@ -1,14 +1,18 @@ package main import ( + "bufio" + "chatplus/core/types" "chatplus/store/model" "chatplus/store/vo" "chatplus/utils" "context" + "encoding/json" "fmt" "io" "log" "net/http" + "os" "strings" "time" @@ -17,7 +21,8 @@ import ( ) func main() { - testAesEncrypt() + err := extractFunction() + fmt.Println(err) } // Http client 取消操作 @@ -156,3 +161,34 @@ func testAesEncrypt() { } fmt.Println("解密明文:", string(decrypt)) } + +func extractFunction() error { + open, err := os.Open("data.txt") + if err != nil { + return err + } + reader := bufio.NewReader(open) + //var contents = make([]string, 0) + var responseBody = types.ApiResponse{} + //var functionCall = false + for { + line, err := reader.ReadString('\n') + if err != nil { + return err + } + if !strings.Contains(line, "data:") { + continue + } + + err = json.Unmarshal([]byte(line[6:]), &responseBody) + if err != nil || len(responseBody.Choices) == 0 { // 数据解析出错 + return err + } + + if !utils.IsEmptyValue(responseBody.Choices[0].Delta.FunctionCall) { + //functionCall = true + fmt.Println("函数调用") + continue + } + } +} diff --git a/api/go/utils/common.go b/api/go/utils/common.go index f3597012..4924c587 100644 --- a/api/go/utils/common.go +++ b/api/go/utils/common.go @@ -87,3 +87,25 @@ func Ip2Region(searcher *xdb.Searcher, ip string) string { } return fmt.Sprintf("%s-%s-%s", arr[0], arr[2], arr[3]) } + +func IsEmptyValue(obj interface{}) bool { + v := reflect.ValueOf(obj) + switch v.Kind() { + case reflect.Ptr, reflect.Interface: + return v.IsNil() + case reflect.Array, reflect.Slice, reflect.Map, reflect.String: + return v.Len() == 0 + case reflect.Bool: + return !v.Bool() + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return v.Int() == 0 + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + return v.Uint() == 0 + case reflect.Float32, reflect.Float64: + return v.Float() == 0 + case reflect.Complex64, reflect.Complex128: + return v.Complex() == 0 + default: + return reflect.DeepEqual(obj, reflect.Zero(reflect.TypeOf(obj)).Interface()) + } +}