|
| 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 | +} |
0 commit comments