-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
96 lines (85 loc) · 2.44 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package chatglm
import (
"fmt"
"os"
"strings"
"time"
)
type ModelAPI struct {
Model string
Prompt []map[string]interface{}
Messages []map[string]interface{}
TopP float64
Temperature float64
}
const (
//BaseURL = "https://open.bigmodel.cn/api/paas/v3/model-api"
BaseURL = "https://open.bigmodel.cn/api/paas/v4/chat/completions"
InvokeTypeSync = "invoke"
InvokeTypeAsync = "async-invoke"
InvokeTypeSSE = "sse-invoke"
APITimeout = 300 * time.Second
ChatGLMLite = "chatglm_lite"
ChatGLMStd = "chatglm_std"
ChatGLMPro = "chatglm_pro"
)
func (m ModelAPI) Invoke(apiKey string) (map[string]interface{}, error) {
token, err := generateToken(apiKey)
if err != nil {
return nil, err
}
//return post(buildAPIURL(m.Model, InvokeTypeSync), token, m.buildParams(), APITimeout)
return post(BaseURL, token, m.buildParams(), APITimeout)
}
func (m ModelAPI) AsyncInvoke(apiKey string) (map[string]interface{}, error) {
token, err := generateToken(apiKey)
if err != nil {
return nil, err
}
return post(buildAPIURL(m.Model, InvokeTypeAsync), token, m.buildParams(), APITimeout)
}
func (m ModelAPI) SSEInvoke(apiKey string) (*SSEClient, error) {
token, err := generateToken(apiKey)
if err != nil {
return nil, err
}
resp, err := stream(buildAPIURL(m.Model, InvokeTypeSSE), token, m.buildParams(), APITimeout)
if err != nil {
return nil, err
}
eventSource := strings.NewReader(string(resp.Body()))
return NewSSEClient(eventSource, "utf-8"), nil
}
func QueryAsyncInvokeResult(apiKey, taskID string) (map[string]interface{}, error) {
token, err := generateToken(apiKey)
if err != nil {
return nil, err
}
return get(buildGetAPIURL(InvokeTypeAsync, taskID), token, APITimeout)
}
func (m ModelAPI) buildParams() map[string]interface{} {
params := make(map[string]interface{})
params["prompt"] = m.Prompt
params["messages"] = m.Messages
params["model"] = m.Model
params["top_p"] = m.TopP
params["temperature"] = m.Temperature
return params
}
func buildAPIURL(module, invokeMethod string) string {
url := getBaseURL()
//return fmt.Sprintf("%s/%s/%s", url, module, invokeMethod)
return fmt.Sprintf("%s/%s", url, module)
}
func buildGetAPIURL(invokeMethod, taskID string) string {
url := getBaseURL()
return fmt.Sprintf("%s/-/%s/%s", url, invokeMethod, taskID)
}
func getBaseURL() string {
var url string
url = os.Getenv("ZHIPUAI_MODEL_API_URL")
if url == "" {
url = BaseURL
}
return url
}