-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package zhipu | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-resty/resty/v2" | ||
) | ||
|
||
type ImageGenerationService struct { | ||
client *Client | ||
|
||
model string | ||
prompt string | ||
userID string | ||
} | ||
|
||
type ImageGenerationResponse struct { | ||
Created int64 `json:"created"` | ||
Data []ImageURL `json:"data"` | ||
} | ||
|
||
func (c *Client) ImageGenerationService(model string) *ImageGenerationService { | ||
return &ImageGenerationService{ | ||
client: c, | ||
model: model, | ||
} | ||
} | ||
|
||
// SetModel sets the model parameter | ||
func (s *ImageGenerationService) SetModel(model string) *ImageGenerationService { | ||
s.model = model | ||
return s | ||
} | ||
|
||
// SetPrompt sets the prompt parameter | ||
func (s *ImageGenerationService) SetPrompt(prompt string) *ImageGenerationService { | ||
s.prompt = prompt | ||
return s | ||
} | ||
|
||
// SetUserID sets the userID parameter | ||
func (s *ImageGenerationService) SetUserID(userID string) *ImageGenerationService { | ||
s.userID = userID | ||
return s | ||
} | ||
|
||
func (s *ImageGenerationService) Do(ctx context.Context) (res ImageGenerationResponse, err error) { | ||
var ( | ||
resp *resty.Response | ||
apiError APIError | ||
) | ||
|
||
body := M{ | ||
"model": s.model, | ||
"prompt": s.prompt, | ||
} | ||
|
||
if s.userID != "" { | ||
body["user_id"] = s.userID | ||
} | ||
|
||
if resp, err = s.client.R(ctx).SetBody(body).SetResult(&res).SetError(&apiError).Post("images/generations"); err != nil { | ||
return | ||
} | ||
|
||
if resp.IsError() { | ||
err = apiError | ||
return | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package zhipu | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestImageGenerationService(t *testing.T) { | ||
client, err := NewClient() | ||
require.NoError(t, err) | ||
|
||
s := client.ImageGenerationService("cogview-3") | ||
s.SetPrompt("一只可爱的小猫") | ||
|
||
res, err := s.Do(context.Background()) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, res.Data) | ||
t.Log(res.Data[0].URL) | ||
} |