forked from majelisIT/telegram-openai-codex-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codex.go
89 lines (74 loc) · 2.1 KB
/
codex.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/gojek/heimdall/v7/httpclient"
)
type CodexSuggestion struct {
ID string `json:"id"`
Object string `json:"object"`
Created int `json:"created"`
Model string `json:"model"`
Choices []struct {
Text string `json:"text"`
Index int `json:"index"`
Logprobs interface{} `json:"logprobs"`
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"`
}
type CodexRequest struct {
Model string `json:"model"`
Prompt string `json:"prompt"`
Temperature float64 `json:"temperature"`
MaxTokens int `json:"max_tokens"`
TopP int `json:"top_p"`
FrequencyPenalty int `json:"frequency_penalty"`
PresencePenalty int `json:"presence_penalty"`
}
type OpenApiCodex interface {
GetCodexSuggestion(prompt string) (suggestion CodexSuggestion, err error)
}
type CodexApi struct {
apiKey string
}
func NewCodexApi(apiKey string) OpenApiCodex {
return &CodexApi{
apiKey: apiKey,
}
}
func (c *CodexApi) GetCodexSuggestion(prompt string) (suggestion CodexSuggestion, err error) {
timeout := 100 * time.Second
client := httpclient.NewClient(httpclient.WithHTTPTimeout(timeout))
payloadBody := CodexRequest{
Model: "text-davinci-003",
Prompt: prompt,
Temperature: 0.7,
MaxTokens: 500,
TopP: 1,
FrequencyPenalty: 0,
PresencePenalty: 0,
}
payloadByte, err := json.Marshal(payloadBody)
if err != nil {
panic(err)
}
reqHeader := http.Header{}
reqHeader.Set("Authorization", fmt.Sprintf("Bearer %s", c.apiKey))
reqHeader.Set("Content-Type", "application/json")
res, err := client.Post("https://api.openai.com/v1/completions", bytes.NewBuffer(payloadByte), reqHeader)
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(res.Body)
err = json.Unmarshal(body, &suggestion)
return
}