forked from 0x9ef/openai-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedits.go
67 lines (63 loc) · 2.01 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
66
67
// Copyright (c) 2022 0x9ef. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
package openai
import (
"context"
"net/http"
)
type EditOptions struct {
// ID of the model to use.
Model Model `json:"model" binding:"required"`
// The input text to use as a starting point for the edit.
Input string `json:"input" binding:"required"`
// The instruction that tells the model how to edit the prompt.
Instruction string `json:"instruction" binding:"required"`
// How many edits to generate for the input and instruction.
// Defaults to 1.
N int `json:"n,omitempty"`
// 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.
Temperature float32 `json:"temperature,omitempty"`
}
type EditResponse struct {
Id string `json:"id"`
Object string `json:"object"`
Created int `json:"created"`
Choices []struct {
Text string `json:"text"`
Index int `json:"index"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`
Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
} `json:"usage"`
}
// Edit given a prompt and an instruction, the model will return an edited version of the prompt.
//
// Docs: https://beta.openai.com/docs/api-reference/edits
func (e *Engine) Edit(ctx context.Context, opts *EditOptions) (*EditResponse, error) {
if err := e.validate.StructCtx(ctx, opts); err != nil {
return nil, err
}
url := e.apiBaseURL + "/edits"
r, err := marshalJson(opts)
if err != nil {
return nil, err
}
req, err := e.newReq(ctx, http.MethodPost, url, "json", r)
if err != nil {
return nil, err
}
resp, err := e.doReq(req)
if err != nil {
return nil, err
}
var jsonResp EditResponse
if err := unmarshal(resp, &jsonResp); err != nil {
return nil, err
}
return &jsonResp, nil
}