-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathernie_bot.go
126 lines (110 loc) · 4.2 KB
/
ernie_bot.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
package go_ernie
import (
"context"
"errors"
"net/http"
)
const (
MessageRoleUser = "user"
MessageRoleAssistant = "assistant"
MessageRoleFunction = "function"
)
var (
ErrChatCompletionStreamNotSupported = errors.New("streaming is not supported with this method, please use CreateChatCompletionStream") //nolint:lll
)
const ernieBotURL = "/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions"
type ErnieFunctionCall struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
Thoughts string `json:"thoughts"`
}
type ErnieFunctionExample struct {
Role string `json:"role"`
Content string `json:"content"`
Name string `json:"name,omitempty"`
FunctionCall ErnieFunctionCall `json:"function_call,omitempty"`
}
type ErnieFunction struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters any `json:"parameters"`
Responses any `json:"responses"`
Examples []ErnieFunctionExample `json:"examples,omitempty"`
}
type ChatCompletionMessage struct {
Role string `json:"role"`
Content string `json:"content"`
Name string `json:"name,omitempty"`
FunctionCall *ErnieFunctionCall `json:"function_call,omitempty"`
}
type ErnieBotRequest struct {
Messages []ChatCompletionMessage `json:"messages"`
Temperature float32 `json:"temperature,omitempty"`
TopP float32 `json:"top_p,omitempty"`
PresencePenalty float32 `json:"presence_penalty,omitempty"`
Stream bool `json:"stream"`
UserId string `json:"user_id,omitempty"`
PenaltyScore float32 `json:"penalty_score,omitempty"`
Functions []ErnieFunction `json:"functions,omitempty"`
System string `json:"system,omitempty"`
Stop []string `json:"stop,omitempty"`
DisableSearch bool `json:"disable_search,omitempty"`
EnableCitation bool `json:"enable_citation,omitempty"`
}
type ErniePluginUsage struct {
Name string `json:"name"`
ParseTokens int `json:"parse_tokens"`
AbstractTokens int `json:"abstract_tokens"`
SearchTokens int `json:"search_tokens"`
TotalTokens int `json:"total_tokens"`
}
type ErnieUsage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
Plugins []ErniePluginUsage `json:"plugins"`
}
type ErnieSearchInfoResult struct {
Index int `json:"index"`
Url string `json:"url"`
Title string `json:"title"`
DataSourceId string `json:"data_source_id"`
}
type ErnieSearchInfo struct {
IsBeset bool `json:"is_beset"`
ReWrite string `json:"re_write"`
SearchResults []ErnieSearchInfoResult `json:"search_results"`
}
type ErnieBotResponse struct {
Id string `json:"id"`
Object string `json:"object"`
Created int `json:"created"`
SentenceId int `json:"sentence_id"`
IsEnd bool `json:"is_end"`
IsTruncated bool `json:"is_truncated"`
Result string `json:"result"`
NeedClearHistory bool `json:"need_clear_history"`
Usage ErnieUsage `json:"usage"`
FunctionCall ErnieFunctionCall `json:"function_call"`
BanRound int `json:"ban_round"`
SearchInfo ErnieSearchInfo `json:"search_info"`
APIError
}
func (c *Client) CreateErnieBotChatCompletion(
ctx context.Context,
request ErnieBotRequest,
) (response ErnieBotResponse, err error) {
if request.Stream {
err = ErrChatCompletionStreamNotSupported
return
}
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(ernieBotURL), withBody(request))
if err != nil {
return
}
err = c.sendRequest(req, &response)
if response.ErrorCode != 0 {
err = &response.APIError
}
return
}