-
Notifications
You must be signed in to change notification settings - Fork 4
/
edits.go
65 lines (57 loc) · 2.3 KB
/
edits.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
package openai
import (
"context"
"encoding/json"
"github.com/fabiustech/openai/models"
"github.com/fabiustech/openai/objects"
"github.com/fabiustech/openai/routes"
)
// EditsRequest contains all relevant fields for requests to the edits endpoint.
type EditsRequest struct {
Model models.Edit `json:"model"`
// Input is the input text to use as a starting point for the edit.
// Defaults to "".
Input string `json:"input,omitempty"`
// Instruction is the instruction that tells the model how to edit the prompt.
Instruction string `json:"instruction,omitempty"`
// N specifies how many edits to generate for the input and instruction.
// Defaults to 1.
N int `json:"n,omitempty"`
// Temperature specifies what sampling temperature to use. Higher values means the model will take more risks.
// Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer. OpenAI
// generally recommends altering this or top_p but not both.
//
// More on sampling temperature: https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277
//
// Defaults to 1.
Temperature *float64 `json:"temperature,omitempty"`
// TopP specifies an alternative to sampling with temperature, called nucleus sampling, where the model considers
// the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10%
// probability mass are considered. OpenAI generally recommends altering this or temperature but not both.
// Defaults to 1.
TopP *float64 `json:"top_p,omitempty"`
}
// EditsChoice represents one of possible edits.
type EditsChoice struct {
Text string `json:"text"`
Index int `json:"index"`
}
// EditsResponse represents a response structure for Edits API.
type EditsResponse struct {
Object objects.Object `json:"object"` // "edit"
Created uint64 `json:"created"`
Usage *Usage `json:"usage"`
Choices []*EditsChoice `json:"choices"`
}
// CreateEdit creates a new edit for the provided input, instruction, and parameters.
func (c *Client) CreateEdit(ctx context.Context, er *EditsRequest) (*EditsResponse, error) {
var b, err = c.post(ctx, routes.Edits, er)
if err != nil {
return nil, err
}
var resp = &EditsResponse{}
if err = json.Unmarshal(b, resp); err != nil {
return nil, err
}
return resp, nil
}