1 Star 0 Fork 0

糍粑/funk-open-ai

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
files.go 2.38 KB
一键复制 编辑 原始数据 按行查看 历史
sashabaranov 提交于 2023-04-08 20:13 . Add files api tests (#238)
package openai
import (
"bytes"
"context"
"fmt"
"net/http"
"os"
)
type FileRequest struct {
FileName string `json:"file"`
FilePath string `json:"-"`
Purpose string `json:"purpose"`
}
// File struct represents an OpenAPI file.
type File struct {
Bytes int `json:"bytes"`
CreatedAt int64 `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"`
}
// CreateFile uploads a jsonl file to GPT3
// FilePath must be a local file path.
func (c *Client) CreateFile(ctx context.Context, request FileRequest) (file File, err error) {
var b bytes.Buffer
builder := c.createFormBuilder(&b)
err = builder.writeField("purpose", request.Purpose)
if err != nil {
return
}
fileData, err := os.Open(request.FilePath)
if err != nil {
return
}
err = builder.createFormFile("file", fileData)
if err != nil {
return
}
err = builder.close()
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL("/files"), &b)
if err != nil {
return
}
req.Header.Set("Content-Type", builder.formDataContentType())
err = c.sendRequest(req, &file)
return
}
// DeleteFile deletes an existing file.
func (c *Client) DeleteFile(ctx context.Context, fileID string) (err error) {
req, err := c.requestBuilder.build(ctx, http.MethodDelete, c.fullURL("/files/"+fileID), nil)
if err != nil {
return
}
err = c.sendRequest(req, nil)
return
}
// 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 := c.requestBuilder.build(ctx, http.MethodGet, c.fullURL("/files"), nil)
if err != nil {
return
}
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 := c.requestBuilder.build(ctx, http.MethodGet, c.fullURL(urlSuffix), nil)
if err != nil {
return
}
err = c.sendRequest(req, &file)
return
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/pc_859107393/funk-open-ai.git
git@gitee.com:pc_859107393/funk-open-ai.git
pc_859107393
funk-open-ai
funk-open-ai
master

搜索帮助