Skip to content

Commit 38922cc

Browse files
PromptObject API support (#2015)
1 parent 0d0f897 commit 38922cc

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

api-prompt-object.go

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* MinIO Go Library for Amazon S3 Compatible Cloud Storage
3+
* Copyright 2015-2024 MinIO, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package minio
19+
20+
import (
21+
"bytes"
22+
"context"
23+
"io"
24+
"net/http"
25+
26+
"github.com/goccy/go-json"
27+
"github.com/minio/minio-go/v7/pkg/s3utils"
28+
)
29+
30+
// PromptObject performs language model inference with the prompt and referenced object as context.
31+
// Inference is performed using a Lambda handler that can process the prompt and object.
32+
// Currently, this functionality is limited to certain MinIO servers.
33+
func (c *Client) PromptObject(ctx context.Context, bucketName, objectName, prompt string, opts PromptObjectOptions) (io.ReadCloser, error) {
34+
// Input validation.
35+
if err := s3utils.CheckValidBucketName(bucketName); err != nil {
36+
return nil, ErrorResponse{
37+
StatusCode: http.StatusBadRequest,
38+
Code: "InvalidBucketName",
39+
Message: err.Error(),
40+
}
41+
}
42+
if err := s3utils.CheckValidObjectName(objectName); err != nil {
43+
return nil, ErrorResponse{
44+
StatusCode: http.StatusBadRequest,
45+
Code: "XMinioInvalidObjectName",
46+
Message: err.Error(),
47+
}
48+
}
49+
50+
opts.AddLambdaArnToReqParams(opts.LambdaArn)
51+
opts.SetHeader("Content-Type", "application/json")
52+
opts.AddPromptArg("prompt", prompt)
53+
promptReqBytes, err := json.Marshal(opts.PromptArgs)
54+
if err != nil {
55+
return nil, err
56+
}
57+
58+
// Execute POST on bucket/object.
59+
resp, err := c.executeMethod(ctx, http.MethodPost, requestMetadata{
60+
bucketName: bucketName,
61+
objectName: objectName,
62+
queryValues: opts.toQueryValues(),
63+
customHeader: opts.Header(),
64+
contentSHA256Hex: sum256Hex(promptReqBytes),
65+
contentBody: bytes.NewReader(promptReqBytes),
66+
contentLength: int64(len(promptReqBytes)),
67+
})
68+
if err != nil {
69+
return nil, err
70+
}
71+
if resp != nil {
72+
if resp.StatusCode != http.StatusOK {
73+
return nil, httpRespToErrorResponse(resp, bucketName, objectName)
74+
}
75+
}
76+
77+
return resp.Body, nil
78+
}

api-prompt-options.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* MinIO Go Library for Amazon S3 Compatible Cloud Storage
3+
* Copyright 2015-2024 MinIO, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package minio
19+
20+
import (
21+
"net/http"
22+
"net/url"
23+
)
24+
25+
// PromptObjectOptions provides options to PromptObject call.
26+
// LambdaArn is the ARN of the Prompt Lambda to be invoked.
27+
// PromptArgs is a map of key-value pairs to be passed to the inference action on the Prompt Lambda.
28+
// "prompt" is a reserved key and should not be used as a key in PromptArgs.
29+
type PromptObjectOptions struct {
30+
LambdaArn string
31+
PromptArgs map[string]any
32+
headers map[string]string
33+
reqParams url.Values
34+
}
35+
36+
// Header returns the http.Header representation of the POST options.
37+
func (o PromptObjectOptions) Header() http.Header {
38+
headers := make(http.Header, len(o.headers))
39+
for k, v := range o.headers {
40+
headers.Set(k, v)
41+
}
42+
return headers
43+
}
44+
45+
// AddPromptArg Add a key value pair to the prompt arguments where the key is a string and
46+
// the value is a JSON serializable.
47+
func (o *PromptObjectOptions) AddPromptArg(key string, value any) {
48+
if o.PromptArgs == nil {
49+
o.PromptArgs = make(map[string]any)
50+
}
51+
o.PromptArgs[key] = value
52+
}
53+
54+
// AddLambdaArnToReqParams adds the lambdaArn to the request query string parameters.
55+
func (o *PromptObjectOptions) AddLambdaArnToReqParams(lambdaArn string) {
56+
if o.reqParams == nil {
57+
o.reqParams = make(url.Values)
58+
}
59+
o.reqParams.Add("lambdaArn", lambdaArn)
60+
}
61+
62+
// SetHeader adds a key value pair to the options. The
63+
// key-value pair will be part of the HTTP POST request
64+
// headers.
65+
func (o *PromptObjectOptions) SetHeader(key, value string) {
66+
if o.headers == nil {
67+
o.headers = make(map[string]string)
68+
}
69+
o.headers[http.CanonicalHeaderKey(key)] = value
70+
}
71+
72+
// toQueryValues - Convert the reqParams in Options to query string parameters.
73+
func (o *PromptObjectOptions) toQueryValues() url.Values {
74+
urlValues := make(url.Values)
75+
if o.reqParams != nil {
76+
for key, values := range o.reqParams {
77+
for _, value := range values {
78+
urlValues.Add(key, value)
79+
}
80+
}
81+
}
82+
83+
return urlValues
84+
}

0 commit comments

Comments
 (0)