-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm.go
More file actions
442 lines (398 loc) · 12.7 KB
/
Copy pathllm.go
File metadata and controls
442 lines (398 loc) · 12.7 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package main
import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
)
// llm.go:把「解卦提示词」发给大模型 API,取回中文解读文本。
//
// 提示词本身已由各式的 GenerateAIPrompt / HuCanPrompt 生成(含卦象素材 +
// 解卦规则 + 输出结构要求),故这里把整段提示词作为 user 消息发出即可,
// 另配一句简短的 system 角色设定。支持 OpenAI 兼容与 Anthropic 原生两种协议。
// 解卦统一的 system 角色设定(两种协议共用)。
const interpretSystemPrompt = "你是一位精通周易、奇门遁甲、大六壬的中式术数顾问。" +
"请严格依据用户给出的盘面素材与解卦规则作答,结论须标注依据,不臆造素材中没有的信息。" +
"用中文回答,深入浅出,兼顾传统术数用语与现代通俗表达。"
// ErrLLMNotConfigured 表示未配置可用的解卦 API(apiKey 为空等)。
var ErrLLMNotConfigured = errors.New("未配置解卦 API:请在 config.json 的 llm 段填写 apiKey(或设置环境变量 ZHOUYI_LLM_API_KEY),并将 enabled 置为 true")
// Interpret 用配置好的大模型对一段解卦提示词作答,返回解读文本。
// cfg 为原始 LLMConfig(内部会自动补默认值)。
func Interpret(ctx context.Context, cfg LLMConfig, prompt string) (string, error) {
rc, usable := cfg.resolved()
if !usable {
return "", ErrLLMNotConfigured
}
if strings.TrimSpace(prompt) == "" {
return "", errors.New("提示词为空,无可解之卦")
}
reqCtx, cancel := context.WithTimeout(ctx, time.Duration(rc.TimeoutSec)*time.Second)
defer cancel()
switch rc.Provider {
case "anthropic":
return callAnthropic(reqCtx, rc, prompt)
default:
return callOpenAI(reqCtx, rc, prompt)
}
}
// httpClient 复用一个带超时兜底的客户端(真正的超时由 context 控制)。
var httpClient = &http.Client{Timeout: 0}
// ===== OpenAI 兼容协议(/chat/completions) =====
type openAIChatRequest struct {
Model string `json:"model"`
Messages []openAIMessage `json:"messages"`
Temperature float64 `json:"temperature"`
MaxTokens int `json:"max_tokens"`
}
type openAIMessage struct {
Role string `json:"role"`
Content string `json:"content"`
}
type openAIChatResponse struct {
Choices []struct {
Message openAIMessage `json:"message"`
} `json:"choices"`
Error *struct {
Message string `json:"message"`
Type string `json:"type"`
} `json:"error"`
}
func callOpenAI(ctx context.Context, cfg LLMConfig, prompt string) (string, error) {
body, _ := json.Marshal(openAIChatRequest{
Model: cfg.Model,
Temperature: cfg.Temperature,
MaxTokens: cfg.MaxTokens,
Messages: []openAIMessage{
{Role: "system", Content: interpretSystemPrompt},
{Role: "user", Content: prompt},
},
})
url := cfg.BaseURL + "/chat/completions"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+cfg.APIKey)
raw, status, err := doRequest(req)
if err != nil {
return "", err
}
var out openAIChatResponse
if err := json.Unmarshal(raw, &out); err != nil {
return "", fmt.Errorf("解析响应失败(HTTP %d):%v", status, err)
}
if out.Error != nil {
return "", fmt.Errorf("API 报错(HTTP %d):%s", status, out.Error.Message)
}
if status < 200 || status >= 300 {
return "", fmt.Errorf("API 返回 HTTP %d:%s", status, truncate(string(raw), 300))
}
if len(out.Choices) == 0 {
return "", errors.New("API 未返回任何结果")
}
text := strings.TrimSpace(out.Choices[0].Message.Content)
if text == "" {
return "", errors.New("API 返回内容为空")
}
return text, nil
}
// ===== Anthropic 原生协议(/v1/messages) =====
type anthropicRequest struct {
Model string `json:"model"`
MaxTokens int `json:"max_tokens"`
Temperature float64 `json:"temperature"`
System string `json:"system,omitempty"`
Messages []anthropicMessage `json:"messages"`
}
type anthropicMessage struct {
Role string `json:"role"`
Content string `json:"content"`
}
type anthropicResponse struct {
Content []struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"content"`
Error *struct {
Type string `json:"type"`
Message string `json:"message"`
} `json:"error"`
}
func callAnthropic(ctx context.Context, cfg LLMConfig, prompt string) (string, error) {
body, _ := json.Marshal(anthropicRequest{
Model: cfg.Model,
MaxTokens: cfg.MaxTokens,
Temperature: cfg.Temperature,
System: interpretSystemPrompt,
Messages: []anthropicMessage{
{Role: "user", Content: prompt},
},
})
// baseURL 若已含 /v1 则不重复拼接,兼容用户直接填到 /v1 的情况。
base := cfg.BaseURL
path := "/v1/messages"
if strings.HasSuffix(base, "/v1") {
path = "/messages"
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, base+path, bytes.NewReader(body))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-api-key", cfg.APIKey)
req.Header.Set("anthropic-version", "2023-06-01")
raw, status, err := doRequest(req)
if err != nil {
return "", err
}
var out anthropicResponse
if err := json.Unmarshal(raw, &out); err != nil {
return "", fmt.Errorf("解析响应失败(HTTP %d):%v", status, err)
}
if out.Error != nil {
return "", fmt.Errorf("API 报错(HTTP %d):%s", status, out.Error.Message)
}
if status < 200 || status >= 300 {
return "", fmt.Errorf("API 返回 HTTP %d:%s", status, truncate(string(raw), 300))
}
var sb strings.Builder
for _, c := range out.Content {
if c.Type == "text" {
sb.WriteString(c.Text)
}
}
text := strings.TrimSpace(sb.String())
if text == "" {
return "", errors.New("API 返回内容为空")
}
return text, nil
}
// ===== 公共 =====
func doRequest(req *http.Request) ([]byte, int, error) {
resp, err := httpClient.Do(req)
if err != nil {
// context 超时给一句更友好的提示
if errors.Is(err, context.DeadlineExceeded) {
return nil, 0, errors.New("请求超时:模型未在限定时间内返回,可调大 config.json 的 llm.timeoutSec")
}
return nil, 0, fmt.Errorf("请求失败:%v", err)
}
defer resp.Body.Close()
raw, err := io.ReadAll(resp.Body)
if err != nil {
return nil, resp.StatusCode, fmt.Errorf("读取响应失败:%v", err)
}
return raw, resp.StatusCode, nil
}
func truncate(s string, n int) string {
r := []rune(s)
if len(r) <= n {
return s
}
return string(r[:n]) + "…"
}
// ===== 流式解卦 =====
// InterpretStream 以流式方式调用大模型解卦,每收到一段增量文本就回调 onDelta。
// 双协议各自解析自家的 SSE 流(OpenAI: data: {...delta.content};
// Anthropic: content_block_delta {...delta.text})。
// 返回 nil 表示正常结束;onDelta 收到的是增量片段(非全文)。
func InterpretStream(ctx context.Context, cfg LLMConfig, prompt string, onDelta func(string)) error {
rc, usable := cfg.resolved()
if !usable {
return ErrLLMNotConfigured
}
if strings.TrimSpace(prompt) == "" {
return errors.New("提示词为空,无可解之卦")
}
reqCtx, cancel := context.WithTimeout(ctx, time.Duration(rc.TimeoutSec)*time.Second)
defer cancel()
switch rc.Provider {
case "anthropic":
return streamAnthropic(reqCtx, rc, prompt, onDelta)
default:
return streamOpenAI(reqCtx, rc, prompt, onDelta)
}
}
// openAIStreamRequest 在普通请求基础上加 stream=true。
type openAIStreamRequest struct {
Model string `json:"model"`
Messages []openAIMessage `json:"messages"`
Temperature float64 `json:"temperature"`
MaxTokens int `json:"max_tokens"`
Stream bool `json:"stream"`
}
func streamOpenAI(ctx context.Context, cfg LLMConfig, prompt string, onDelta func(string)) error {
body, _ := json.Marshal(openAIStreamRequest{
Model: cfg.Model,
Temperature: cfg.Temperature,
MaxTokens: cfg.MaxTokens,
Stream: true,
Messages: []openAIMessage{
{Role: "system", Content: interpretSystemPrompt},
{Role: "user", Content: prompt},
},
})
req, err := http.NewRequestWithContext(ctx, http.MethodPost, cfg.BaseURL+"/chat/completions", bytes.NewReader(body))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+cfg.APIKey)
req.Header.Set("Accept", "text/event-stream")
resp, err := httpClient.Do(req)
if err != nil {
return streamReqErr(err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return httpErrFromBody(resp)
}
got := false
err = scanSSE(resp.Body, func(data string) bool {
if data == "[DONE]" {
return false
}
var chunk struct {
Choices []struct {
Delta struct {
Content string `json:"content"`
} `json:"delta"`
} `json:"choices"`
}
if json.Unmarshal([]byte(data), &chunk) != nil {
return true
}
if len(chunk.Choices) > 0 && chunk.Choices[0].Delta.Content != "" {
got = true
onDelta(chunk.Choices[0].Delta.Content)
}
return true
})
if err != nil {
return err
}
if !got {
return errors.New("API 返回内容为空")
}
return nil
}
// anthropicStreamRequest 在普通请求基础上加 stream=true。
type anthropicStreamRequest struct {
Model string `json:"model"`
MaxTokens int `json:"max_tokens"`
Temperature float64 `json:"temperature"`
System string `json:"system,omitempty"`
Messages []anthropicMessage `json:"messages"`
Stream bool `json:"stream"`
}
func streamAnthropic(ctx context.Context, cfg LLMConfig, prompt string, onDelta func(string)) error {
body, _ := json.Marshal(anthropicStreamRequest{
Model: cfg.Model,
MaxTokens: cfg.MaxTokens,
Temperature: cfg.Temperature,
System: interpretSystemPrompt,
Stream: true,
Messages: []anthropicMessage{
{Role: "user", Content: prompt},
},
})
base := cfg.BaseURL
path := "/v1/messages"
if strings.HasSuffix(base, "/v1") {
path = "/messages"
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, base+path, bytes.NewReader(body))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-api-key", cfg.APIKey)
req.Header.Set("anthropic-version", "2023-06-01")
req.Header.Set("Accept", "text/event-stream")
resp, err := httpClient.Do(req)
if err != nil {
return streamReqErr(err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return httpErrFromBody(resp)
}
got := false
err = scanSSE(resp.Body, func(data string) bool {
var ev struct {
Type string `json:"type"`
Delta struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"delta"`
}
if json.Unmarshal([]byte(data), &ev) != nil {
return true
}
if ev.Type == "content_block_delta" && ev.Delta.Text != "" {
got = true
onDelta(ev.Delta.Text)
}
if ev.Type == "message_stop" {
return false
}
return true
})
if err != nil {
return err
}
if !got {
return errors.New("API 返回内容为空")
}
return nil
}
// scanSSE 逐行读取 SSE 流,对每个「data: 」行的负载调用 fn。
// fn 返回 false 表示主动停止扫描(如收到 [DONE] / message_stop)。
func scanSSE(r io.Reader, fn func(data string) bool) error {
sc := bufio.NewScanner(r)
sc.Buffer(make([]byte, 0, 64*1024), 1024*1024) // 放大单行上限,防超长 data 行截断
for sc.Scan() {
line := sc.Text()
if !strings.HasPrefix(line, "data:") {
continue // 跳过 event:/空行/注释行
}
data := strings.TrimSpace(strings.TrimPrefix(line, "data:"))
if data == "" {
continue
}
if !fn(data) {
return nil
}
}
if err := sc.Err(); err != nil {
return streamReqErr(err)
}
return nil
}
// streamReqErr 把底层请求错误翻译成友好提示。
func streamReqErr(err error) error {
if errors.Is(err, context.DeadlineExceeded) {
return errors.New("请求超时:模型未在限定时间内返回,可调大 config.json 的 llm.timeoutSec")
}
return fmt.Errorf("请求失败:%v", err)
}
// httpErrFromBody 读取非 2xx 响应体并构造错误(尝试解析常见错误结构)。
func httpErrFromBody(resp *http.Response) error {
raw, _ := io.ReadAll(resp.Body)
var e struct {
Error *struct {
Message string `json:"message"`
} `json:"error"`
}
if json.Unmarshal(raw, &e) == nil && e.Error != nil && e.Error.Message != "" {
return fmt.Errorf("API 报错(HTTP %d):%s", resp.StatusCode, e.Error.Message)
}
return fmt.Errorf("API 返回 HTTP %d:%s", resp.StatusCode, truncate(string(raw), 300))
}