Skip to content

Commit 1f71638

Browse files
author
noona
authored
Added support for Edits endpoint & other minor changes (#22)
* Added support for Moderations API * gofmt moderation.go * support for edits endpoint & other improvements
1 parent 277071e commit 1f71638

File tree

3 files changed

+84
-23
lines changed

3 files changed

+84
-23
lines changed

completion.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,21 @@ import (
1010

1111
// CompletionRequest represents a request structure for completion API
1212
type CompletionRequest struct {
13-
Prompt string `json:"prompt,omitempty"`
14-
MaxTokens int `json:"max_tokens,omitempty"`
15-
16-
Temperature float32 `json:"temperature,omitempty"`
17-
TopP float32 `json:"top_p,omitempty"`
18-
19-
N int `json:"n,omitempty"`
20-
21-
LogProbs int `json:"logprobs,omitempty"`
22-
23-
Model *string `json:"model,omitempty"`
24-
25-
Echo bool `json:"echo,omitempty"`
26-
Stop []string `json:"stop,omitempty"`
27-
13+
Model *string `json:"model,omitempty"`
14+
Prompt string `json:"prompt,omitempty"`
15+
MaxTokens int `json:"max_tokens,omitempty"`
16+
Temperature float32 `json:"temperature,omitempty"`
17+
TopP float32 `json:"top_p,omitempty"`
18+
N int `json:"n,omitempty"`
19+
Stream bool `json:"stream,omitempty"`
20+
LogProbs int `json:"logprobs,omitempty"`
21+
Echo bool `json:"echo,omitempty"`
22+
Stop []string `json:"stop,omitempty"`
2823
PresencePenalty float32 `json:"presence_penalty,omitempty"`
2924
FrequencyPenalty float32 `json:"frequency_penalty,omitempty"`
3025
BestOf int `json:"best_of,omitempty"`
3126
LogitBias map[string]int `json:"logit_bias,omitempty"`
27+
User string `json:"user,omitempty"`
3228
}
3329

3430
// Choice represents one of possible completions
@@ -47,13 +43,21 @@ type LogprobResult struct {
4743
TextOffset []int `json:"text_offset"`
4844
}
4945

46+
// CompletionUsage represents Usage of CompletionResponse
47+
type CompletionUsage struct {
48+
PromptTokens int `json:"prompt_tokens"`
49+
CompletionTokens int `json:"completion_tokens"`
50+
TotalTokens int `json:"total_tokens"`
51+
}
52+
5053
// CompletionResponse represents a response structure for completion API
5154
type CompletionResponse struct {
52-
ID string `json:"id"`
53-
Object string `json:"object"`
54-
Created uint64 `json:"created"`
55-
Model string `json:"model"`
56-
Choices []Choice `json:"choices"`
55+
ID string `json:"id"`
56+
Object string `json:"object"`
57+
Created uint64 `json:"created"`
58+
Model string `json:"model"`
59+
Choices []Choice `json:"choices"`
60+
Usage CompletionUsage `json:"usage"`
5761
}
5862

5963
// CreateCompletion — API call to create a completion. This is the main endpoint of the API. Returns new text as well as, if requested, the probabilities over each alternative token at each position.

edits.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package gogpt
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"net/http"
8+
)
9+
10+
// EditsRequest represents a request structure for Edits API
11+
type EditsRequest struct {
12+
Model *string `json:"model,omitempty"`
13+
Input string `json:"input,omitempty"`
14+
Instruction string `json:"instruction,omitempty"`
15+
N int `json:"n,omitempty"`
16+
Temperature float32 `json:"temperature,omitempty"`
17+
TopP float32 `json:"top_p,omitempty"`
18+
}
19+
20+
// EditsUsage represents Usage of EditsResponse
21+
type EditsUsage struct {
22+
PromptTokens int `json:"prompt_tokens"`
23+
CompletionTokens int `json:"completion_tokens"`
24+
TotalTokens int `json:"total_tokens"`
25+
}
26+
27+
// Choice represents one of possible edits
28+
type Choice struct {
29+
Text string `json:"text"`
30+
Index int `json:"index"`
31+
}
32+
33+
// EditsResponse represents a response structure for Edits API
34+
type EditsResponse struct {
35+
Object string `json:"object"`
36+
Created uint64 `json:"created"`
37+
Usage EditsUsage `json:"usage"`
38+
Choices []Choice `json:"choices"`
39+
}
40+
41+
// Perform an API call to the Edits endpoint.
42+
func (c *Client) Edits(ctx context.Context, request EditsRequest) (response EditsResponse, err error) {
43+
var reqBytes []byte
44+
reqBytes, err = json.Marshal(request)
45+
if err != nil {
46+
return
47+
}
48+
49+
req, err := http.NewRequest("POST", c.fullURL("/edits"), bytes.NewBuffer(reqBytes))
50+
if err != nil {
51+
return
52+
}
53+
54+
req = req.WithContext(ctx)
55+
err = c.sendRequest(req, &response)
56+
return
57+
}

moderation.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ type ModerationRequest struct {
1515

1616
// Result represents one of possible moderation results
1717
type Result struct {
18-
Categories []ResultCategories `json:"categories"`
19-
CategoryScores []ResultCategoryScores `json:"category_scores"`
20-
Flagged int `json:"flagged"`
18+
Categories ResultCategories `json:"categories"`
19+
CategoryScores ResultCategoryScores `json:"category_scores"`
20+
Flagged int `json:"flagged"`
2121
}
2222

2323
// ResultCategories represents Categories of Result

0 commit comments

Comments
 (0)