File tree 15 files changed +453
-0
lines changed
15 files changed +453
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Binaries for programs and plugins
2
+ * .exe
3
+ * .exe~
4
+ * .dll
5
+ * .so
6
+ * .dylib
7
+
8
+ # Test binary, built with `go test -c`
9
+ * .test
10
+
11
+ # Output of the go coverage tool, specifically when used with LiteIDE
12
+ * .out
13
+
14
+ # Dependency directories (remove the comment below to include it)
15
+ # vendor/
16
+
17
+ # Auth token for tests
18
+ .openai-token
19
+ .idea
20
+
21
+ src /key.go
Original file line number Diff line number Diff line change
1
+ # go-openai
2
+
3
+ OpenAI wrapper for Go
Original file line number Diff line number Diff line change
1
+ package completions
2
+
3
+ type Param struct {
4
+ Model string `json:"model"`
5
+ Prompt any `json:"prompt,omitempty"`
6
+ Suffix string `json:"suffix,omitempty"`
7
+ MaxTokens int `json:"max_tokens,omitempty"`
8
+ Temperature float32 `json:"temperature,omitempty"`
9
+ TopP float32 `json:"top_p,omitempty"`
10
+ N int `json:"n,omitempty"`
11
+ Stream bool `json:"stream,omitempty"`
12
+ LogProbs int `json:"logprobs,omitempty"`
13
+ Echo bool `json:"echo,omitempty"`
14
+ Stop any `json:"stop,omitempty"`
15
+ PresencePenalty float32 `json:"presence_penalty,omitempty"`
16
+ FrequencyPenalty float32 `json:"frequency_penalty,omitempty"`
17
+ BestOf int `json:"best_of,omitempty"`
18
+ LogitBias map [string ]int `json:"logit_bias,omitempty"`
19
+ User string `json:"user,omitempty"`
20
+ }
Original file line number Diff line number Diff line change
1
+ package completions
2
+
3
+ import (
4
+ "configuration"
5
+ "request_handler"
6
+ )
7
+
8
+ func Create (c * configuration.Configuration , p Param ) (response Response , err error ) {
9
+ r := NewRequest (c , p )
10
+ err = request_handler .Perform (r , & response )
11
+ return
12
+ }
Original file line number Diff line number Diff line change
1
+ package completions
2
+
3
+ import (
4
+ "bytes"
5
+ "configuration"
6
+ "encoding/json"
7
+ "fmt"
8
+ "io"
9
+ "net/http"
10
+ "request"
11
+ )
12
+
13
+ // Request https://platform.openai.com/docs/api-reference/completions
14
+ type Request struct {
15
+ c * configuration.Configuration
16
+ p Param
17
+ }
18
+
19
+ func NewRequest (c * configuration.Configuration , p Param ) * Request {
20
+ return & Request {c : c , p : p }
21
+ }
22
+
23
+ func (c * Request ) Method () string {
24
+ return http .MethodPost
25
+ }
26
+
27
+ func (c * Request ) Host () string {
28
+ return request .BaseURL
29
+ }
30
+
31
+ func (c * Request ) Path () string {
32
+ return "/v1/completions"
33
+ }
34
+
35
+ func (c * Request ) URL () string {
36
+ return c .Host () + c .Path ()
37
+ }
38
+
39
+ func (c * Request ) Headers () map [string ]string {
40
+ headers := map [string ]string {
41
+ "Content-Type" : "application/json" ,
42
+ }
43
+ for k , v := range c .c .Headers () {
44
+ headers [k ] = v
45
+ }
46
+ return headers
47
+ }
48
+
49
+ func (c * Request ) Body () io.Reader {
50
+ marshal , err := json .Marshal (c .p )
51
+ if err != nil {
52
+ return nil
53
+ }
54
+ fmt .Println (string (marshal ))
55
+ return bytes .NewBuffer (marshal )
56
+ }
Original file line number Diff line number Diff line change
1
+ package completions
2
+
3
+ type Response struct {
4
+ Id string `json:"id"`
5
+ Object string `json:"object"`
6
+ Created int `json:"created"`
7
+ Model string `json:"model"`
8
+ Usage Usage `json:"usage,omitempty"`
9
+ Choices []Choices `json:"choices"`
10
+ }
11
+
12
+ type Choices struct {
13
+ Text string `json:"text,omitempty"`
14
+ Index int `json:"index,omitempty"`
15
+ Logprobs Logprobs `json:"logprobs,omitempty"`
16
+ FinishReason string `json:"finish_reason,omitempty"`
17
+ }
18
+
19
+ type Logprobs struct {
20
+ Tokens []string `json:"tokens,omitempty"`
21
+ TokenLogprobs []float32 `json:"token_logprobs,omitempty"`
22
+ TopLogprobs []map [string ]float32 `json:"top_logprobs,omitempty"`
23
+ TextOffset []int `json:"text_offset,omitempty"`
24
+ }
25
+
26
+ type Usage struct {
27
+ PromptTokens int `json:"prompt_tokens,omitempty"`
28
+ CompletionTokens int `json:"completion_tokens,omitempty"`
29
+ TotalTokens int `json:"total_tokens,omitempty"`
30
+ }
Original file line number Diff line number Diff line change
1
+ package configuration
2
+
3
+ import "fmt"
4
+
5
+ type Configuration struct {
6
+ ApiKey string
7
+ Organization string
8
+ }
9
+
10
+ func NewConfiguration (apiKey string ) * Configuration {
11
+ return & Configuration {
12
+ ApiKey : apiKey ,
13
+ }
14
+ }
15
+
16
+ func NewConfigurationWithOrg (apiKey string , organization string ) * Configuration {
17
+ return & Configuration {
18
+ ApiKey : apiKey ,
19
+ Organization : organization ,
20
+ }
21
+ }
22
+
23
+ func (c * Configuration ) Headers () map [string ]string {
24
+ var header = map [string ]string {
25
+ "Authorization" : fmt .Sprintf ("Bearer %s" , c .ApiKey ),
26
+ }
27
+ if len (c .Organization ) != 0 {
28
+ header ["OpenAI-Organization" ] = c .Organization
29
+ }
30
+ return header
31
+ }
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "completions"
5
+ "configuration"
6
+ "encoding/json"
7
+ "fmt"
8
+ )
9
+
10
+ func main () {
11
+ fmt .Println ("Hello, World" )
12
+
13
+ key := "sk-xxxx"
14
+ c := configuration .NewConfiguration (key )
15
+
16
+ p := completions.Param {
17
+ Model : "text-davinci-003" ,
18
+ Prompt : "今天是几号?" ,
19
+ Temperature : 1 ,
20
+ MaxTokens : 1024 ,
21
+ }
22
+
23
+ response , err := completions .Create (c , p )
24
+ if err != nil {
25
+ fmt .Println (err .Error ())
26
+ return
27
+ }
28
+ marshal , err := json .Marshal (response )
29
+ if err != nil {
30
+ return
31
+ }
32
+ fmt .Println (string (marshal ))
33
+
34
+ //
35
+ //// list, err := models.List(c)
36
+ //model, err := models.Retrieve(c, "babbage")
37
+ //
38
+ //if err != nil {
39
+ // fmt.Println(err.Error())
40
+ // return
41
+ //}
42
+ //fmt.Println(model)
43
+
44
+ }
Original file line number Diff line number Diff line change
1
+ package models
2
+
3
+ import (
4
+ "configuration"
5
+ "io"
6
+ "net/http"
7
+ "request"
8
+ )
9
+
10
+ // ListModelsRequest https://platform.openai.com/docs/api-reference/models/list
11
+ type ListModelsRequest struct {
12
+ c * configuration.Configuration
13
+ }
14
+
15
+ func NewListModelsRequest (c * configuration.Configuration ) * ListModelsRequest {
16
+ return & ListModelsRequest {c : c }
17
+ }
18
+
19
+ func (l * ListModelsRequest ) Method () string {
20
+ return http .MethodGet
21
+ }
22
+
23
+ func (l * ListModelsRequest ) Host () string {
24
+ return request .BaseURL
25
+ }
26
+
27
+ func (l * ListModelsRequest ) Path () string {
28
+ return "/v1/models"
29
+ }
30
+
31
+ func (l * ListModelsRequest ) URL () string {
32
+ return l .Host () + l .Path ()
33
+ }
34
+
35
+ func (l * ListModelsRequest ) Headers () map [string ]string {
36
+ headers := map [string ]string {
37
+ "Content-Type" : "application/json" ,
38
+ }
39
+ for k , v := range l .c .Headers () {
40
+ headers [k ] = v
41
+ }
42
+ return headers
43
+ }
44
+
45
+ func (l * ListModelsRequest ) Body () io.Reader {
46
+ return nil
47
+ }
Original file line number Diff line number Diff line change
1
+ package models
2
+
3
+ import (
4
+ "configuration"
5
+ "request_handler"
6
+ )
7
+
8
+ func List (c * configuration.Configuration ) (response Response , err error ) {
9
+ r := NewListModelsRequest (c )
10
+ err = request_handler .Perform (r , & response )
11
+ return
12
+ }
13
+
14
+ func Retrieve (c * configuration.Configuration , id string ) (model Model , err error ) {
15
+ r := NewRetrieveModelRequest (c , id )
16
+ err = request_handler .Perform (r , & model )
17
+ return
18
+ }
Original file line number Diff line number Diff line change
1
+ package models
2
+
3
+ type Model struct {
4
+ Id string `json:"id"`
5
+ Object string `json:"object"`
6
+ Created int64 `json:"created"`
7
+ OwnedBy string `json:"owned_by"`
8
+ Root string `json:"root"`
9
+ Parent string `json:"parent"`
10
+ Permission []Permission `json:"permission"`
11
+ }
12
+
13
+ type Permission struct {
14
+ Id string `json:"id"`
15
+ Object string `json:"object"`
16
+ Created int `json:"created"`
17
+ AllowCreateEngine bool `json:"allow_create_engine"`
18
+ AllowSampling bool `json:"allow_sampling"`
19
+ AllowLogprobs bool `json:"allow_logprobs"`
20
+ AllowSearchIndices bool `json:"allow_search_indices"`
21
+ AllowView bool `json:"allow_view"`
22
+ AllowFineTuning bool `json:"allow_fine_tuning"`
23
+ Organization string `json:"organization"`
24
+ Group interface {} `json:"group"`
25
+ IsBlocking bool `json:"is_blocking"`
26
+ }
27
+
28
+ type Response struct {
29
+ Models []Model `json:"data"`
30
+ }
Original file line number Diff line number Diff line change
1
+ package models
2
+
3
+ import (
4
+ "configuration"
5
+ "fmt"
6
+ "io"
7
+ "net/http"
8
+ "request"
9
+ )
10
+
11
+ // RetrieveModelRequest https://platform.openai.com/docs/api-reference/models/retrieve
12
+ type RetrieveModelRequest struct {
13
+ c * configuration.Configuration
14
+ modelID string
15
+ }
16
+
17
+ func NewRetrieveModelRequest (c * configuration.Configuration , modelID string ) * RetrieveModelRequest {
18
+ return & RetrieveModelRequest {c : c , modelID : modelID }
19
+ }
20
+
21
+ func (r * RetrieveModelRequest ) Method () string {
22
+ return http .MethodGet
23
+ }
24
+
25
+ func (r * RetrieveModelRequest ) Host () string {
26
+ return request .BaseURL
27
+ }
28
+
29
+ func (r * RetrieveModelRequest ) Path () string {
30
+ return fmt .Sprintf ("/v1/models/%s" , r .modelID )
31
+ }
32
+
33
+ func (r * RetrieveModelRequest ) URL () string {
34
+ return r .Host () + r .Path ()
35
+ }
36
+
37
+ func (r * RetrieveModelRequest ) Headers () map [string ]string {
38
+ headers := map [string ]string {
39
+ "Content-Type" : "application/json" ,
40
+ }
41
+ for k , v := range r .c .Headers () {
42
+ headers [k ] = v
43
+ }
44
+ return headers
45
+ }
46
+
47
+ func (r * RetrieveModelRequest ) Body () io.Reader {
48
+ return nil
49
+ }
You can’t perform that action at this time.
0 commit comments