-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompletion.go
More file actions
87 lines (73 loc) · 3.11 KB
/
completion.go
File metadata and controls
87 lines (73 loc) · 3.11 KB
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
package openaiclient
import "github.com/go-zoox/core-utils/fmt"
// CreateCompletionRequest ...
type CreateCompletionRequest struct {
// Model is ID of the model to use.
// You can use the List models API to see all of your available models,
// or see our Model overview for descriptions of them.
// model is required.
Model string `json:"model"`
// prompt is the prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.
// Note that <|endoftext|> is the document separator that the model sees during training,
// so if a prompt is not specified the model will generate as if from the beginning of a new document.
Prompt string `json:"prompt"`
// MaxTokens is the maximum number of tokens to generate in the completion.
// The token count of your prompt plus max_tokens cannot exceed the model's context length.
// Most models have a context length of 2048 tokens (except for the newest models, which support 4096).
MaxTokens int `json:"max_tokens"`
// Temperature means What sampling temperature to use, between 0 and 2.
// Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
// We generally recommend altering this or top_p but not both.
Temperature float64 `json:"temperature"`
// Stream whether to stream back partial progress.
// If set, tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message.
Stream bool `json:"stream"`
// Stop is up to 4 sequences where the API will stop generating further tokens.
// The returned text will not contain the stop sequence.
Stop string `json:"stop"`
// Suffix string `json:"suffix"`
// TopP int `json:"top_p"`
// N int `json:"n"`
// Logprobs bool `json:"logprobs"`
}
// CreateCompletionResponse ...
type CreateCompletionResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Model string `json:"model"`
Choices []struct {
Text string `json:"text"`
Index int `json:"index"`
Logprobs int `json:"logprobs"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`
Usage struct {
PromptTokens int `json:"prompt_token"`
CompletionTokens int `json:"completion_token"`
TotalTokens int `json:"total_tokens"`
} `json:"usage"`
}
// reference: https://platform.openai.com/docs/api-reference/completions/create
func (c *client) CreateCompletion(cfg *CreateCompletionRequest) (*CreateCompletionResponse, error) {
if cfg.Temperature == 0 {
cfg.Temperature = 0.8
}
var apiPath string
switch c.cfg.APIType {
case APITypeOpenAI:
// /completions
apiPath = fmt.Sprintf("/%s", ResourceCompletion)
case APITypeAzure:
// openai/deployments/{deployment_id}/completions
apiPath = fmt.Sprintf("/openai/deployments/%s/%s", c.cfg.AzureDeployment, ResourceCompletion)
}
resp, err := c.post(apiPath, cfg)
if err != nil {
return nil, err
}
var response CreateCompletionResponse
if err := resp.UnmarshalJSON(&response); err != nil {
return nil, err
}
return &response, nil
}