Added a files endpoint, corrected a documents bug in answers requests, added error messages to API error handling (#6)
Co-authored-by: eyelevelai <33876565+eyelevelai@users.noreply.github.com>
This commit is contained in:
@@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type AnswerRequest struct {
|
type AnswerRequest struct {
|
||||||
Documents [][]string `json:"documents"`
|
Documents []string `json:"documents"`
|
||||||
Question string `json:"question"`
|
Question string `json:"question"`
|
||||||
SearchModel string `json:"search_model"`
|
SearchModel string `json:"search_model"`
|
||||||
Model string `json:"model"`
|
Model string `json:"model"`
|
||||||
|
|||||||
7
api.go
7
api.go
@@ -59,7 +59,12 @@ func (c *Client) sendRequest(req *http.Request, v interface{}) error {
|
|||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusBadRequest {
|
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 {
|
if err = json.NewDecoder(res.Body).Decode(&v); err != nil {
|
||||||
|
|||||||
12
api_test.go
12
api_test.go
@@ -24,6 +24,18 @@ func TestAPI(t *testing.T) {
|
|||||||
t.Fatalf("GetEngine error: %v", err)
|
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 := CompletionRequest{MaxTokens: 5}
|
||||||
req.Prompt = "Lorem ipsum"
|
req.Prompt = "Lorem ipsum"
|
||||||
_, err = c.CreateCompletion(ctx, "ada", req)
|
_, err = c.CreateCompletion(ctx, "ada", req)
|
||||||
|
|||||||
10
error.go
Normal file
10
error.go
Normal file
@@ -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
files.go
Normal file
50
files.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user