-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
143 lines (118 loc) · 3.24 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
)
type Request struct {
Model string `json:"model"`
Prompt string `json:"prompt"`
System *string `json:"system,omitempty"`
Template *string `json:"template,omitempty"`
Stream bool `json:"stream"`
Options struct {
Temperature float64 `json:"temperature"`
} `json:"options"`
}
type Response struct {
Model string `json:"model"`
CreatedAt time.Time `json:"created_at"`
Response string `json:"response"`
Done bool `json:"done"`
Context []int `json:"context"`
TotalDuration int `json:"total_duration"`
LoadDuration int `json:"load_duration"`
PromptEvalCount int `json:"prompt_eval_count"`
PromptEvalDuration int `json:"prompt_eval_duration"`
EvalCount int `json:"eval_count"`
EvalDuration int `json:"eval_duration"`
}
type Config struct {
URL string `json:"url"`
System string `json:"system"`
Prompt string `json:"prompt"`
Template string `json:"template"`
Model string `json:"model"`
Temperature float64 `json:"temperature"`
}
func main() {
pathConfig := flag.String("config", "./properties.json", "path to config file")
prompt := flag.String("prompt", "", "prompt to use")
model := flag.String("model", "", "model to use")
template := flag.String("template", "@", "template: you need assign '' for put empty this field")
temperature := flag.Float64("temperature", -1, "temperature to use")
system := flag.String("system", "", "system to use")
flag.Parse()
var config Config
contentConfigJson, err := os.ReadFile(*pathConfig)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(contentConfigJson, &config)
if err != nil {
log.Fatal(err)
}
var requestBody Request
requestBody.Model = config.Model
if *model != "" {
requestBody.Model = *model
}
if config.Prompt == "" {
requestBody.Prompt = *prompt
} else {
requestBody.Prompt = fileToString(config.Prompt) + " " + *prompt
}
requestBody.System = nil
if config.System != "" {
tmp := fileToString(config.System)
requestBody.System = &tmp
}
if *system != "" {
requestBody.System = system
}
requestBody.Template = nil
if config.Template != "" {
tmp := fileToString(config.Template)
requestBody.Template = &tmp
}
if *template != "@" {
requestBody.Template = template
}
requestBody.Stream = false
requestBody.Options.Temperature = config.Temperature
if *temperature >= 0.0 {
requestBody.Options.Temperature = *temperature
}
marshalled, err := json.Marshal(requestBody)
if err != nil {
log.Fatal(err)
}
req, err := http.NewRequest(http.MethodPost, config.URL, bytes.NewReader(marshalled))
if err != nil {
log.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
var responseBody Response
err = json.NewDecoder(resp.Body).Decode(&responseBody)
if err != nil {
log.Fatal(err)
}
fmt.Println(responseBody.Response)
}
func fileToString(filename string) string {
b, err := os.ReadFile(filename) // just pass the file name
if err != nil {
return ""
}
return string(b)
}