-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
69 lines (61 loc) · 1.73 KB
/
main.go
File metadata and controls
69 lines (61 loc) · 1.73 KB
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
package main
import (
"fmt"
"time"
"github.com/leverly/ChatGLM/client"
)
const API_KEY = "XXX.XXX"
func main() {
model := "chatglm_6b"
prompt := []client.Message{
{Role: "user", Content: "你好"},
{Role: "assistant", Content: "我是人工智能助手"},
{Role: "user", Content: "你叫什么名字"},
{Role: "assistant", Content: "我叫chatGLM"},
{Role: "user", Content: "你都可以做些什么事"},
}
//Invoke(model, prompt)
SSEInvoke(model, prompt)
}
func SSEInvoke(model string, prompt []client.Message) {
proxy := client.NewChatGLMClient(API_KEY, 30*time.Second)
err := proxy.SSEInvoke(model, 0.2, prompt, &StreamCallback{})
if err != nil {
fmt.Println("SSEInvoke Error:", err)
return
}
}
func Invoke(model string, prompt []client.Message) {
proxy := client.NewChatGLMClient(API_KEY, 30*time.Second)
response, err := proxy.Invoke(model, 0.2, prompt)
if err != nil {
fmt.Println("Invoke Error:", err)
return
}
fmt.Printf("Invoke Response:%s\n", (*response.Choices)[0].Content)
}
func AsyncInvoke(model string, prompt []client.Message) {
proxy := client.NewChatGLMClient(API_KEY, 30*time.Second)
taskId, err := proxy.AsyncInvoke(model, 0.2, prompt)
if err != nil {
fmt.Println("Async Invoke Error:", err)
return
}
fmt.Println("Async Invoke Task:", taskId)
for true {
response, err := proxy.AsyncInvokeTask(model, taskId)
if err != nil {
fmt.Println("Async Invoke Task Error:", err)
return
}
if response.TaskStatus == client.TaskStatusSuccess {
fmt.Printf("AsyncInvoke Response:%s\n", (*response.Choices)[0].Content)
break
} else if response.TaskStatus == client.TaskStatusFail {
fmt.Println("Check Task Status Failed")
return
} else {
time.Sleep(200 * time.Millisecond)
}
}
}