Skip to content

Commit 1e488b2

Browse files
author
BitsAdmin
committed
Merge branch 'feat/ark/moderation_hit_type' into 'integration_2025-01-09_669760003330'
feat: [development task] ark-runtime-manual-Golang (965061) See merge request iaasng/volcengine-go-sdk!447
2 parents e2ab935 + c481548 commit 1e488b2

File tree

6 files changed

+312
-5
lines changed

6 files changed

+312
-5
lines changed

service/arkruntime/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,3 +493,7 @@ func (c *Client) getRetryAfter(v model.Response) int64 {
493493
}
494494
return retryAfterInterval
495495
}
496+
497+
func (c *Client) isAPIKeyAuthentication() bool {
498+
return c.config.apiKey != ""
499+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package arkruntime
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
"strconv"
9+
10+
"github.com/volcengine/volcengine-go-sdk/service/arkruntime/model"
11+
)
12+
13+
const contentGenerationTaskPath = "/contents/generations/tasks"
14+
15+
func (c *Client) CreateContentGenerationTask(
16+
ctx context.Context,
17+
request model.CreateContentGenerationTaskRequest,
18+
setters ...requestOption,
19+
) (response model.CreateContentGenerationTaskResponse, err error) {
20+
if !c.isAPIKeyAuthentication() {
21+
return response, model.ErrAKSKNotSupported
22+
}
23+
24+
requestOptions := append(setters, withBody(request))
25+
err = c.Do(ctx, http.MethodPost, c.fullURL(contentGenerationTaskPath), resourceTypeEndpoint, request.Model, &response, requestOptions...)
26+
return
27+
}
28+
29+
func (c *Client) GetContentGenerationTask(
30+
ctx context.Context,
31+
request model.GetContentGenerationTaskRequest,
32+
setters ...requestOption,
33+
) (response model.GetContentGenerationTaskResponse, err error) {
34+
if !c.isAPIKeyAuthentication() {
35+
return response, model.ErrAKSKNotSupported
36+
}
37+
38+
url := fmt.Sprintf("%s/%s", c.fullURL(contentGenerationTaskPath), request.ID)
39+
40+
err = c.Do(ctx, http.MethodGet, url, resourceTypeEndpoint, "", &response, setters...)
41+
return
42+
}
43+
44+
func (c *Client) DeleteContentGenerationTask(
45+
ctx context.Context,
46+
request model.DeleteContentGenerationTaskRequest,
47+
setters ...requestOption,
48+
) (err error) {
49+
if !c.isAPIKeyAuthentication() {
50+
return model.ErrAKSKNotSupported
51+
}
52+
53+
url := fmt.Sprintf("%s/%s", c.fullURL(contentGenerationTaskPath), request.ID)
54+
55+
err = c.Do(ctx, http.MethodDelete, url, resourceTypeEndpoint, "", nil, setters...)
56+
return err
57+
}
58+
59+
func (c *Client) ListContentGenerationTasks(
60+
ctx context.Context,
61+
request model.ListContentGenerationTasksRequest,
62+
setters ...requestOption,
63+
) (response model.ListContentGenerationTasksResponse, err error) {
64+
if !c.isAPIKeyAuthentication() {
65+
return response, model.ErrAKSKNotSupported
66+
}
67+
68+
values := url.Values{}
69+
if pageNum := request.PageNum; pageNum != nil && *pageNum > 0 {
70+
values.Add("page_num", strconv.Itoa(*pageNum))
71+
}
72+
if pageSize := request.PageSize; pageSize != nil && *pageSize > 0 {
73+
values.Add("page_size", strconv.Itoa(*pageSize))
74+
}
75+
76+
if filter := request.Filter; filter != nil {
77+
if status := filter.Status; status != nil && *status != "" {
78+
values.Add("filter.status", *status)
79+
}
80+
if model := filter.Model; model != nil && *model != "" {
81+
values.Add("filter.model", *model)
82+
}
83+
for _, taskID := range filter.TaskIDs {
84+
values.Add("filter.task_ids", *taskID)
85+
}
86+
}
87+
88+
endpoint := fmt.Sprintf("%s?%s", c.fullURL(contentGenerationTaskPath), values.Encode())
89+
90+
err = c.Do(ctx, http.MethodGet, endpoint, resourceTypeEndpoint, "", &response, setters...)
91+
if err != nil {
92+
return response, err
93+
}
94+
95+
return response, nil
96+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/volcengine/volcengine-go-sdk/volcengine"
7+
"os"
8+
9+
"github.com/volcengine/volcengine-go-sdk/service/arkruntime"
10+
"github.com/volcengine/volcengine-go-sdk/service/arkruntime/model"
11+
)
12+
13+
/**
14+
* Authentication
15+
* 1.If you authorize your endpoint using an API key, you can set your api key to environment variable "ARK_API_KEY"
16+
* client := arkruntime.NewClientWithApiKey(os.Getenv("ARK_API_KEY"))
17+
* Note: If you use an API key, this API key will not be refreshed.
18+
* To prevent the API from expiring and failing after some time, choose an API key with no expiration date.
19+
*/
20+
func main() {
21+
client := arkruntime.NewClientWithApiKey(os.Getenv("ARK_API_KEY"))
22+
ctx := context.Background()
23+
modelEp := "YOUR_ENDPOINT_ID"
24+
25+
fmt.Println("----- create content generation task -----")
26+
createReq := model.CreateContentGenerationTaskRequest{
27+
Model: modelEp, // Replace with your endpoint ID
28+
Content: []*model.CreateContentGenerationContentItem{
29+
{
30+
Type: model.ContentGenerationContentItemTypeText,
31+
Text: volcengine.String("龙与地下城女骑士背景是起伏的平原,目光从镜头转向平原 --ratio 1:1"),
32+
},
33+
{
34+
Type: model.ContentGenerationContentItemTypeImage,
35+
ImageURL: &model.ImageURL{
36+
URL: "${YOUR URL HERE}", // Replace with URL
37+
},
38+
},
39+
},
40+
}
41+
42+
createResponse, err := client.CreateContentGenerationTask(ctx, createReq)
43+
if err != nil {
44+
fmt.Printf("create content generation error: %v\n", err)
45+
return
46+
}
47+
fmt.Printf("Task Created with ID: %s\n", createResponse.ID)
48+
49+
fmt.Println("----- get content generation task -----")
50+
taskID := createResponse.ID
51+
52+
getRequest := model.GetContentGenerationTaskRequest{ID: taskID}
53+
54+
getResponse, err := client.GetContentGenerationTask(ctx, getRequest)
55+
if err != nil {
56+
fmt.Printf("get content generation task error: %v\n", err)
57+
return
58+
}
59+
60+
fmt.Printf("Task ID: %s\n", getResponse.ID)
61+
fmt.Printf("Model: %s\n", getResponse.Model)
62+
fmt.Printf("Status: %s\n", getResponse.Status)
63+
fmt.Printf("Failure Reason: %v\n", getResponse.FailureReason)
64+
fmt.Printf("Video URL: %s\n", getResponse.Content.VideoURL)
65+
fmt.Printf("Completion Tokens: %d\n", getResponse.Usage.CompletionTokens)
66+
fmt.Printf("Created At: %d\n", getResponse.CreatedAt)
67+
fmt.Printf("Updated At: %d\n", getResponse.UpdatedAt)
68+
69+
fmt.Println("----- list content generation task -----")
70+
71+
listRequest := model.ListContentGenerationTasksRequest{
72+
PageNum: volcengine.Int(1),
73+
PageSize: volcengine.Int(10),
74+
Filter: &model.ListContentGenerationTasksFilter{
75+
Status: volcengine.String(model.StatusSucceeded),
76+
//TaskIDs: volcengine.StringSlice([]string{"cgt-example-1", "cgt-example-2"}),
77+
//Model: volcengine.String(modelEp),
78+
},
79+
}
80+
81+
listResponse, err := client.ListContentGenerationTasks(ctx, listRequest)
82+
if err != nil {
83+
fmt.Printf("failed to list content generation tasks: %v\n", err)
84+
}
85+
86+
fmt.Printf("ListContentGenerationTasks returned %v results\n", listResponse.Total)
87+
88+
fmt.Println("----- delete content generation task -----")
89+
90+
deleteRequest := model.DeleteContentGenerationTaskRequest{ID: taskID}
91+
92+
err = client.DeleteContentGenerationTask(ctx, deleteRequest)
93+
if err != nil {
94+
fmt.Printf("delete content generation task error: %v\n", err)
95+
} else {
96+
fmt.Println("successfully deleted task id: ", taskID)
97+
}
98+
99+
}

service/arkruntime/model/chat_completion.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,13 @@ func (r FinishReason) MarshalJSON() ([]byte, error) {
298298
return []byte(`"` + string(r) + `"`), nil // best effort to not break future API changes
299299
}
300300

301+
type ChatCompletionResponseChoicesElemModerationHitType string
302+
303+
const (
304+
ChatCompletionResponseChoicesElemModerationHitTypeViolence ChatCompletionResponseChoicesElemModerationHitType = "violence"
305+
ChatCompletionResponseChoicesElemModerationHitTypeSevereViolation ChatCompletionResponseChoicesElemModerationHitType = "severe_violation"
306+
)
307+
301308
type ChatCompletionChoice struct {
302309
Index int `json:"index"`
303310
Message ChatCompletionMessage `json:"message"`
@@ -309,7 +316,12 @@ type ChatCompletionChoice struct {
309316
// content_filter: Omitted content due to a flag from our content filters
310317
// null: API response still in progress or incomplete
311318
FinishReason FinishReason `json:"finish_reason"`
312-
LogProbs *LogProbs `json:"logprobs,omitempty"`
319+
// ModerationHitType
320+
// The type of content moderation strategy hit.
321+
// Only after selecting a moderation strategy for the endpoint that supports returning moderation hit types,
322+
// API will return the corresponding values.
323+
ModerationHitType *ChatCompletionResponseChoicesElemModerationHitType `json:"moderation_hit_type,omitempty" yaml:"moderation_hit_type,omitempty" mapstructure:"moderation_hit_type,omitempty"`
324+
LogProbs *LogProbs `json:"logprobs,omitempty"`
313325
}
314326

315327
// ChatCompletionResponse represents a response structure for chat completion API.
@@ -332,10 +344,11 @@ type ChatCompletionStreamChoiceDelta struct {
332344
}
333345

334346
type ChatCompletionStreamChoice struct {
335-
Index int `json:"index"`
336-
Delta ChatCompletionStreamChoiceDelta `json:"delta"`
337-
LogProbs *LogProbs `json:"logprobs,omitempty"`
338-
FinishReason FinishReason `json:"finish_reason"`
347+
Index int `json:"index"`
348+
Delta ChatCompletionStreamChoiceDelta `json:"delta"`
349+
LogProbs *LogProbs `json:"logprobs,omitempty"`
350+
FinishReason FinishReason `json:"finish_reason"`
351+
ModerationHitType *ChatCompletionResponseChoicesElemModerationHitType `json:"moderation_hit_type,omitempty" yaml:"moderation_hit_type,omitempty" mapstructure:"moderation_hit_type,omitempty"`
339352
}
340353

341354
type ChatCompletionStreamResponse struct {
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package model
2+
3+
type ContentGenerationContentItemType string
4+
5+
const (
6+
ContentGenerationContentItemTypeText ContentGenerationContentItemType = "text"
7+
ContentGenerationContentItemTypeImage ContentGenerationContentItemType = "image_url"
8+
)
9+
10+
const (
11+
StatusSucceeded = "succeeded"
12+
StatusCancelled = "cancelled"
13+
StatusFailed = "failed"
14+
StatusRunning = "running"
15+
StatusQueued = "queued"
16+
)
17+
18+
type CreateContentGenerationTaskRequest struct {
19+
Model string `json:"model"`
20+
Content []*CreateContentGenerationContentItem `json:"content"`
21+
}
22+
23+
type CreateContentGenerationTaskResponse struct {
24+
ID string `json:"id"`
25+
26+
HttpHeader
27+
}
28+
29+
type GetContentGenerationTaskRequest struct {
30+
ID string `json:"id"`
31+
}
32+
33+
type GetContentGenerationTaskResponse struct {
34+
ID string `json:"id"`
35+
Model string `json:"model"`
36+
Status string `json:"status"`
37+
FailureReason *string `json:"failure_reason,omitempty"`
38+
Content Content `json:"content"`
39+
Usage Usage `json:"usage"`
40+
CreatedAt int64 `json:"created_at"`
41+
UpdatedAt int64 `json:"updated_at"`
42+
43+
HttpHeader
44+
}
45+
46+
type ListContentGenerationTasksRequest struct {
47+
PageNum *int `json:"page_num,omitempty"`
48+
PageSize *int `json:"page_size,omitempty"`
49+
Filter *ListContentGenerationTasksFilter `json:"filter,omitempty"`
50+
}
51+
52+
type DeleteContentGenerationTaskRequest struct {
53+
ID string `json:"id"`
54+
}
55+
56+
type ListContentGenerationTasksFilter struct {
57+
Status *string `json:"status,omitempty"`
58+
TaskIDs []*string `json:"task_ids,omitempty"`
59+
Model *string `json:"model,omitempty"`
60+
}
61+
62+
type CreateContentGenerationContentItem struct {
63+
Type ContentGenerationContentItemType `json:"type"`
64+
Text *string `json:"text,omitempty"`
65+
ImageURL *ImageURL `json:"image_url,omitempty"`
66+
}
67+
68+
type ImageURL struct {
69+
URL string `json:"url"`
70+
}
71+
type Content struct {
72+
VideoURL string `json:"video_url"`
73+
}
74+
75+
type ContentGenerationUsage struct {
76+
CompletionTokens int `json:"completion_tokens"`
77+
}
78+
79+
type ListContentGenerationTasksResponse struct {
80+
Total int64 `json:"total"`
81+
Items []ListContentGenerationTaskItem `json:"items"`
82+
HttpHeader
83+
}
84+
85+
type ListContentGenerationTaskItem struct {
86+
ID string `json:"id"`
87+
Model string `json:"model"`
88+
Status string `json:"status"`
89+
FailureReason *string `json:"failure_reason,omitempty"`
90+
Content Content `json:"content"`
91+
Usage Usage `json:"usage"`
92+
CreatedAt int64 `json:"created_at"`
93+
UpdatedAt int64 `json:"updated_at"`
94+
}

service/arkruntime/model/error.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,5 @@ var (
5454
ErrContentFieldsMisused = errors.New("can't use both Content and MultiContent properties simultaneously")
5555
ErrBodyWithoutEndpoint = errors.New("can't fetch endpoint sts token without endpoint")
5656
ErrBodyWithoutBot = errors.New("can't fetch bot sts token without bot id")
57+
ErrAKSKNotSupported = errors.New("ak&sk authentication is currently not supported for this method, please use api key instead")
5758
)

0 commit comments

Comments
 (0)