forked from franciscoescher/goopenai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompletions.go
66 lines (58 loc) · 2.48 KB
/
completions.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
package goopenai
import (
"context"
"encoding/json"
)
const COMPLETIONS_URL = "https://api.openai.com/v1/chat/completions"
type CreateCompletionsRequest struct {
Model string `json:"model,omitempty"`
Messages []Message `json:"messages,omitempty"`
Prompt StrArray `json:"prompt,omitempty"`
Suffix string `json:"suffix,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
Temperature float64 `json:"temperature,omitempty"`
TopP float64 `json:"top_p,omitempty"`
N int `json:"n,omitempty"`
Stream bool `json:"stream,omitempty"`
LogProbs int `json:"logprobs,omitempty"`
Echo bool `json:"echo,omitempty"`
Stop StrArray `json:"stop,omitempty"`
PresencePenalty float64 `json:"presence_penalty,omitempty"`
FrequencyPenalty float64 `json:"frequency_penalty,omitempty"`
BestOf int `json:"best_of,omitempty"`
LogitBias map[string]string `json:"logit_bias,omitempty"`
User string `json:"user,omitempty"`
}
func (c *Client) CreateCompletionsRaw(ctx context.Context, r CreateCompletionsRequest) ([]byte, error) {
return c.Post(ctx, COMPLETIONS_URL, r)
}
func (c *Client) CreateCompletions(ctx context.Context, r CreateCompletionsRequest) (response CreateCompletionsResponse, err error) {
raw, err := c.CreateCompletionsRaw(ctx, r)
if err != nil {
return response, err
}
err = json.Unmarshal(raw, &response)
return response, err
}
type CreateCompletionsResponse struct {
ID string `json:"id,omitempty"`
Object string `json:"object,omitempty"`
Created int `json:"created,omitempty"`
Model string `json:"model,omitempty"`
Choices []struct {
Message struct {
Role string `json:"role,omitempty"`
Content string `json:"content,omitempty"`
} `json:"message"`
Text string `json:"text,omitempty"`
Index int `json:"index,omitempty"`
Logprobs interface{} `json:"logprobs,omitempty"`
FinishReason string `json:"finish_reason,omitempty"`
} `json:"choices,omitempty"`
Usage struct {
PromptTokens int `json:"prompt_tokens,omitempty"`
CompletionTokens int `json:"completion_tokens,omitempty"`
TotalTokens int `json:"total_tokens,omitempty"`
} `json:"usage,omitempty"`
Error Error `json:"error,omitempty"`
}