-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathollama.ai.go
54 lines (45 loc) · 1.06 KB
/
ollama.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
package main
import (
"encoding/json"
"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
)
type OllamaAi struct {
Config AiConfig
}
type OllamaAiReqBody struct {
Model string `json:"model"`
Messages []AiReqBodyMessage `json:"messages"`
Stream bool `json:"stream"`
}
type OllamaAiRes struct {
Message AiReqBodyMessage `json:"message"`
}
func (ai OllamaAi) before() {
log.Info("请求大模型中......")
}
func (ai OllamaAi) after() {
log.Info("请求完成......")
}
func (ai OllamaAi) request(client *resty.Client, messages []AiReqBodyMessage) AiReqBodyMessage {
resp, err := client.R().
SetHeader("Content-Type", "application/json").
SetBody(OllamaAiReqBody{
Model: ai.Config.Model,
Messages: messages,
Stream: false,
}).
Post(ai.Config.BaseUrl + "/api/chat")
if err != nil {
log.Fatal(err)
}
if resp.StatusCode() != 200 {
log.Fatal(resp)
}
result := OllamaAiRes{}
jsonErr := json.Unmarshal(resp.Body(), &result)
if jsonErr != nil {
log.Fatal(jsonErr)
}
return result.Message
}