This repository has been archived by the owner on Feb 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternal_api_service.go
147 lines (124 loc) · 2.83 KB
/
external_api_service.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"fmt"
"log"
"strings"
"github.com/parnurzeal/gorequest"
"github.com/tidwall/gjson"
"github.com/wailovet/nuwa"
)
type ExternalApiService struct {
Url string
}
// Free implements NLP
func (e *ExternalApiService) Free() {
}
// IsReady implements NLP
func (e *ExternalApiService) IsReady() bool {
return true
}
// ModelFile implements NLP
func (e *ExternalApiService) ModelFile() string {
return e.Url
}
// Predict implements NLP
func (e *ExternalApiService) Predict(p Prompts, his ChatHistory, opts *PredictOption) (string, error) {
text := fmt.Sprintln(p.Instruct)
if his == nil || len(his) == 0 {
} else {
if his[len(his)-1].Role == "assistant" {
if len(his)-1 > 0 {
his = his[:len(his)-1]
} else {
return "", fmt.Errorf("history is nil")
}
}
for _, v := range his {
if v.Role == "assistant" {
text += fmt.Sprintln(p.AssistantPrefix, v.Content)
} else {
text += fmt.Sprintln(p.UserPrefix, v.Content)
}
}
text += p.AssistantPrefix
}
log.Println(text)
req := gorequest.New()
if !strings.HasSuffix(e.Url, "/") {
e.Url += "/"
}
sendUrl := e.Url + "completion"
postJson := map[string]interface{}{
"prompt": text,
"batch_size": 64,
"as_loop": true,
"n_keep": -1,
"interactive": true,
"stop": []string{"\n### Human:"},
}
if opts != nil {
if opts.BatchSize > 0 {
postJson["batch_size"] = opts.BatchSize
}
if opts.Penalty > 0 {
postJson["penalty"] = opts.Penalty
}
if opts.Temperature > 0 {
postJson["temperature"] = opts.Temperature
}
if opts.TopP > 0 {
postJson["top_p"] = opts.TopP
}
if opts.TopK > 0 {
postJson["top_k"] = opts.TopK
}
if opts.Tokens > 0 {
postJson["n_predict"] = opts.Tokens
}
if opts.Threads > 0 {
postJson["threads"] = opts.Threads
}
if opts.Stop != nil {
postJson["stop"] = opts.Stop
}
} else {
return "", fmt.Errorf("opts is nil")
}
req.Post(sendUrl).Send(nuwa.Helper().JsonEncode(postJson)).End()
nextTokenUrl := e.Url + "next-token"
message := ""
isStop := false
for {
var errs []error
var result string
if isStop {
_, result, errs = req.Get(nextTokenUrl + "?stop=true").End()
} else {
_, result, errs = req.Get(nextTokenUrl).End()
}
if errs != nil {
return "", errs[0]
}
// message += result.data.content;
// if (result.data.stop) {
// console.log("Completed");
// // make sure to add the completion to the prompt.
// prompt += `### Assistant: ${message}`;
// break;
// }
message += gjson.Get(result, "content").String()
if gjson.Get(result, "stop").Bool() {
break
}
log.Println("web.result:", result)
if !isStop {
isStop = opts.StreamFn(message)
}
}
return message, nil
}
// StartUp implements NLP
func (e *ExternalApiService) StartUp(modelfile string) error {
e.Url = modelfile
return nil
}