Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion answers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type AnswerRequest struct {
Documents [][]string `json:"documents"`
Documents []string `json:"documents"`
Question string `json:"question"`
SearchModel string `json:"search_model"`
Model string `json:"model"`
Expand Down
7 changes: 6 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ func (c *Client) sendRequest(req *http.Request, v interface{}) error {
defer res.Body.Close()

if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusBadRequest {
return fmt.Errorf("error, status code: %d", res.StatusCode)
var errRes ErrorResponse
err = json.NewDecoder(res.Body).Decode(&errRes)
if err != nil || errRes.Error == nil {
return fmt.Errorf("error, status code: %d", res.StatusCode)
}
return fmt.Errorf("error, status code: %d, message: %s", res.StatusCode, errRes.Error.Message)
}

if err = json.NewDecoder(res.Body).Decode(&v); err != nil {
Expand Down
12 changes: 12 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ func TestAPI(t *testing.T) {
t.Fatalf("GetEngine error: %v", err)
}

fileRes, err := c.ListFiles(ctx)
if err != nil {
t.Fatalf("ListFiles error: %v", err)
}

if len(fileRes.Files) > 0 {
_, err = c.GetFile(ctx, fileRes.Files[0].ID)
if err != nil {
t.Fatalf("GetFile error: %v", err)
}
} // else skip

req := CompletionRequest{MaxTokens: 5}
req.Prompt = "Lorem ipsum"
_, err = c.CreateCompletion(ctx, "ada", req)
Expand Down
10 changes: 10 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gogpt

type ErrorResponse struct {
Error *struct {
Code *int `json:"code,omitempty"`
Message string `json:"message"`
Param *string `json:"param,omitempty"`
Type string `json:"type"`
} `json:"error,omitempty"`
}
50 changes: 50 additions & 0 deletions files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package gogpt

import (
"context"
"fmt"
"net/http"
)

// File struct represents an OpenAPI file
type File struct {
Bytes int `json:"bytes"`
CreatedAt int `json:"created_at"`
ID string `json:"id"`
FileName string `json:"filename"`
Object string `json:"object"`
Owner string `json:"owner"`
Purpose string `json:"purpose"`
}

// FilesList is a list of files that belong to the user or organization
type FilesList struct {
Files []File `json:"data"`
}

// ListFiles Lists the currently available files,
// and provides basic information about each file such as the file name and purpose.
func (c *Client) ListFiles(ctx context.Context) (files FilesList, err error) {
req, err := http.NewRequest("GET", c.fullURL("/files"), nil)
if err != nil {
return
}

req = req.WithContext(ctx)
err = c.sendRequest(req, &files)
return
}

// GetFile Retrieves a file instance, providing basic information about the file
// such as the file name and purpose.
func (c *Client) GetFile(ctx context.Context, fileID string) (file File, err error) {
urlSuffix := fmt.Sprintf("/files/%s", fileID)
req, err := http.NewRequest("GET", c.fullURL(urlSuffix), nil)
if err != nil {
return
}

req = req.WithContext(ctx)
err = c.sendRequest(req, &file)
return
}