-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathai.go
71 lines (59 loc) · 1.43 KB
/
ai.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
package main
import (
"errors"
"github.com/go-resty/resty/v2"
)
type Ai interface {
before()
request(*resty.Client, []AiReqBodyMessage) AiReqBodyMessage
after()
}
type AiConfig struct {
Model string
BaseUrl string
ApiKey string
MaxInputTokens uint32
}
func AiFactory(aiName string, aiConf AiConfig) (Ai, error) {
switch aiName {
case "aliyun-dashscope":
return AliyunAi{Config: aiConf}, nil
case "ollama":
return OllamaAi{Config: aiConf}, nil
case "openai":
return OpenAi{Config: aiConf}, nil
default:
return nil, errors.New("找不到合适的AI引擎")
}
}
type AiReqBody struct {
Model string `json:"model"`
Input AiReqBodyInput `json:"input"`
Parameters AiReqBodyParameters `json:"parameters"`
}
type AiReqBodyInput struct {
Messages []AiReqBodyMessage `json:"messages"`
}
type AiReqBodyMessage struct {
Role string `json:"role"`
Content string `json:"content"`
}
type AiResultFormat string
// 使用 iota 为枚举成员分配值
const (
Message AiResultFormat = "message"
Text AiResultFormat = "text"
)
type AiReqBodyParameters struct {
ResultFormat AiResultFormat `json:"result_format"`
}
type AiResChoice struct {
Message AiReqBodyMessage `json:"message"`
//FinishReason string `json:"finish_reason"`
}
type AiRes struct {
Output struct {
Choices []AiResChoice `json:"choices"`
} `json:"output"`
RequestID string `json:"request_id"`
}