-
Notifications
You must be signed in to change notification settings - Fork 11
/
completion.go
51 lines (43 loc) · 1.16 KB
/
completion.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
package go_ernie
import (
"context"
"fmt"
"net/http"
)
const completionURL = "/rpc/2.0/ai_custom/v1/wenxinworkshop/completions/"
// CompletionRequest 文本续写模型
type CompletionRequest struct {
Model string `json:"-"`
Prompt string `json:"prompt"`
Stream bool `json:"stream,omitempty"`
UserId string `json:"user_id,omitempty"`
}
type CompletionResponse struct {
Id string `json:"id"`
Object string `json:"object"`
Created int `json:"created"`
SentenceId int `json:"sentence_id"`
IsEnd bool `json:"is_end"`
Result string `json:"result"`
IsSafe bool `json:"is_safe"`
Usage ErnieUsage `json:"usage"`
APIError
}
func (c *Client) CreateCompletion(
ctx context.Context,
request CompletionRequest,
) (response CompletionResponse, err error) {
if request.Stream {
err = ErrChatCompletionStreamNotSupported
return
}
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(fmt.Sprintf("%s%s", completionURL, request.Model)), withBody(request))
if err != nil {
return
}
err = c.sendRequest(req, &response)
if response.ErrorCode != 0 {
err = &response.APIError
}
return
}