Skip to content

Commit

Permalink
Merge pull request #19 from hanyuancheung/feature/support-image-gener…
Browse files Browse the repository at this point in the history
…ation

feat: support image generation
  • Loading branch information
hanyuancheung authored Mar 28, 2023
2 parents 29c86e6 + 59603f4 commit 33c7bb6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ make chatgpt-example
- [x] Completion API (是主要的 gpt-3 API)
- [x] 对 Completion API 的流式支持
- [x] 文档搜索 API
- [x] 图片生成 API
- [x] 替换默认 url、用户代理、超时和其他选项

## 贡献者
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Check out the go docs for more detailed documentation on the types and methods p
- [x] Completion API (this is the main gpt-3 API)
- [x] Streaming support for the Completion API
- [x] Document Search API
- [x] Image generation API
- [x] Overriding default url, user-agent, timeout, and other options

## Contributor
Expand Down
17 changes: 17 additions & 0 deletions gpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,23 @@ func (c *client) Embeddings(ctx context.Context, request EmbeddingsRequest) (*Em
return &output, nil
}

// Image creates an image
func (c *client) Image(ctx context.Context, request ImageRequest) (*ImageResponse, error) {
req, err := c.newRequest(ctx, "POST", "/images/generations", request)
if err != nil {
return nil, err
}
rsp, err := c.performRequest(req)
if err != nil {
return nil, err
}
output := ImageResponse{}
if err := getResponseObject(rsp, &output); err != nil {
return nil, err
}
return &output, nil
}

func (c *client) performRequest(req *http.Request) (*http.Response, error) {
rsp, err := c.httpClient.Do(req)
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,24 @@ type SearchResponse struct {
Data []SearchData `json:"data"`
Object string `json:"object"`
}

// ImageRequest represents the request structure for the image API.
type ImageRequest struct {
Prompt string `json:"prompt,omitempty"`
N int `json:"n,omitempty"`
Size string `json:"size,omitempty"`
ResponseFormat string `json:"response_format,omitempty"`
User string `json:"user,omitempty"`
}

// ImageResponse represents a response structure for image API.
type ImageResponse struct {
Created int64 `json:"created,omitempty"`
Data []ImageResponseDataInner `json:"data,omitempty"`
}

// ImageResponseDataInner represents a response data structure for image API.
type ImageResponseDataInner struct {
URL string `json:"url,omitempty"`
B64JSON string `json:"b64_json,omitempty"`
}

0 comments on commit 33c7bb6

Please sign in to comment.