-
Notifications
You must be signed in to change notification settings - Fork 6
/
ask-llm.go
164 lines (148 loc) · 3.54 KB
/
ask-llm.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"time"
)
var (
LLMAPIBaseURL = os.Getenv("LLM_API_BASE_URL")
LLMAPIKey = os.Getenv("LLM_API_KEY")
LLMChatModel = os.Getenv("LLM_CHAT_MODEL")
LLMStreaming = os.Getenv("LLM_STREAMING") != "no"
LLMDebug = os.Getenv("LLM_DEBUG")
)
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
type ChatRequest struct {
Messages []Message `json:"messages"`
Model string `json:"model"`
Stop []string `json:"stop"`
MaxTokens int `json:"max_tokens"`
Temperature float64 `json:"temperature"`
Stream bool `json:"stream"`
}
type Choice struct {
Message struct {
Content string `json:"content"`
} `json:"message"`
}
func chat(messages []Message, handler func(string)) (string, error) {
url := fmt.Sprintf("%s/chat/completions", LLMAPIBaseURL)
authHeader := ""
if LLMAPIKey != "" {
authHeader = fmt.Sprintf("Bearer %s", LLMAPIKey)
}
stream := LLMStreaming && handler != nil
requestBody := ChatRequest{
Messages: messages,
Model: LLMChatModel,
Stop: []string{"<|im_end|>", "<|end|>", "<|eot_id|>"},
MaxTokens: 200,
Temperature: 0,
Stream: stream,
}
jsonBody, err := json.Marshal(requestBody)
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
if authHeader != "" {
req.Header.Set("Authorization", authHeader)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("HTTP error: %d %s", resp.StatusCode, resp.Status)
}
if !stream {
var data struct {
Choices []Choice `json:"choices"`
}
err := json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return "", err
}
answer := data.Choices[0].Message.Content
if handler != nil {
handler(answer)
}
return answer, nil
} else {
answer := ""
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "data: ") {
payload := line[6:]
var data struct {
Choices []struct {
Delta struct {
Content string `json:"content"`
} `json:"delta"`
} `json:"choices"`
}
err := json.Unmarshal([]byte(payload), &data)
if err != nil {
return "", err
}
partial := data.Choices[0].Delta.Content
answer += partial
if handler != nil {
handler(partial)
}
}
}
if err := scanner.Err(); err != nil {
return "", err
}
return answer, nil
}
}
const SystemPrompt = "Answer the question politely and concisely."
func main() {
fmt.Printf("Using LLM at %s.\n", LLMAPIBaseURL)
fmt.Println("Press Ctrl+D to exit.")
fmt.Println()
messages := []Message{{Role: "system", Content: SystemPrompt}}
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print(">> ")
scanner.Scan()
question := scanner.Text()
if question == "" {
break
}
messages = append(messages, Message{Role: "user", Content: question})
handler := func(partial string) {
fmt.Print(partial)
}
start := time.Now()
answer, err := chat(messages, handler)
if err != nil {
fmt.Println("Error:", err)
break
}
messages = append(messages, Message{Role: "assistant", Content: answer})
fmt.Println()
elapsed := time.Since(start)
if LLMDebug != "" {
fmt.Printf("[%d ms]\n", elapsed.Milliseconds())
}
fmt.Println()
}
}