-
Notifications
You must be signed in to change notification settings - Fork 7
/
completion.go
62 lines (56 loc) · 2.27 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
52
53
54
55
56
57
58
59
60
61
62
package openai
import "context"
// Well-known completion models
const (
CompletionModelDavinci3 = "text-davinci-003"
CompletionModelDavinci2 = "text-davinci-002"
CompletionModelCurie1 = "text-curie-001"
CompletionModelBabbage1 = "text-babbage-001"
CompletionModelAda1 = "text-ada-001"
CompletionModelCodeDavinci2 = "code-davinci-002"
)
// Complete calls the non-chat Completion endpoints for non-chatgpt completion
// models.
func (c Client) Complete(ctx context.Context, req CompleteReq) (*CompleteRes, error) {
var res CompleteRes
err := c.c.R().Post("completions").JSON(req).Do(ctx).JSON(&res)
if err != nil {
return nil, err
}
return &res, nil
}
// CompleteReq holds all the inputs for a completion request.
type CompleteReq struct {
Model string `json:"model"`
Prompt string `json:"prompt"`
Suffix *string `json:"suffix,omitempty"`
MaxTokens *int `json:"max_tokens,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
TopP *int `json:"top_p,omitempty"`
N *int `json:"n,omitempty"`
Logprobs *int `json:"logprobs,omitempty"`
Echo *bool `json:"echo,omitempty"`
Stop *string `json:"stop,omitempty"`
PresencePentalty *float64 `json:"presence_pentalty,omitempty"`
FrequencyPentalty *float64 `json:"frequency_pentalty,omitempty"`
BestOf *int `json:"best_of,omitempty"`
LogitBias *map[string]any `json:"logit_bias,omitempty"`
User *string `json:"user,omitempty"`
}
// CompleteRes represents the final completion(s) from OpenAI.
type CompleteRes struct {
Choices []CompleteChoice `json:"choices"`
Created int `json:"created"`
ID string `json:"id"`
Model string `json:"model"`
Object string `json:"object"`
Usage Usage `json:"usage"`
}
// CompleteChoice is the representation of the individual choices returned by
// OpenAI.
type CompleteChoice struct {
Text string `json:"text"`
Index int `json:"index"`
LogProbs any `json:"log_probs"`
FinishReason string `json:"finish_reason"`
}