-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvectorio.go
More file actions
379 lines (345 loc) · 15.2 KB
/
vectorio.go
File metadata and controls
379 lines (345 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright (c) Meta Platforms, Inc. and affiliates.
// All rights reserved.
//
// This source code is licensed under the terms described in the LICENSE file in
// the root directory of this source tree.
//
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package llamastackclient
import (
"context"
"encoding/json"
"net/http"
"slices"
"github.com/llamastack/llama-stack-client-go/internal/apijson"
"github.com/llamastack/llama-stack-client-go/internal/requestconfig"
"github.com/llamastack/llama-stack-client-go/option"
"github.com/llamastack/llama-stack-client-go/packages/param"
"github.com/llamastack/llama-stack-client-go/packages/respjson"
)
// VectorIoService contains methods and other services that help with interacting
// with the llama-stack-client API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewVectorIoService] method instead.
type VectorIoService struct {
Options []option.RequestOption
}
// NewVectorIoService generates a new service that applies the given options to
// each request. These options are applied after the parent client's options (if
// there is one), and before any request-specific options.
func NewVectorIoService(opts ...option.RequestOption) (r VectorIoService) {
r = VectorIoService{}
r.Options = opts
return
}
// Insert chunks into a vector database.
func (r *VectorIoService) Insert(ctx context.Context, body VectorIoInsertParams, opts ...option.RequestOption) (err error) {
opts = slices.Concat(r.Options, opts)
opts = append([]option.RequestOption{option.WithHeader("Accept", "")}, opts...)
path := "v1/vector-io/insert"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, nil, opts...)
return
}
// Query chunks from a vector database.
func (r *VectorIoService) Query(ctx context.Context, body VectorIoQueryParams, opts ...option.RequestOption) (res *QueryChunksResponse, err error) {
opts = slices.Concat(r.Options, opts)
path := "v1/vector-io/query"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Response from querying chunks in a vector database.
type QueryChunksResponse struct {
// List of content chunks returned from the query
Chunks []QueryChunksResponseChunk `json:"chunks,required"`
// Relevance scores corresponding to each returned chunk
Scores []float64 `json:"scores,required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Chunks respjson.Field
Scores respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r QueryChunksResponse) RawJSON() string { return r.JSON.raw }
func (r *QueryChunksResponse) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// A chunk of content that can be inserted into a vector database.
type QueryChunksResponseChunk struct {
// Unique identifier for the chunk. Must be provided explicitly.
ChunkID string `json:"chunk_id,required"`
// The content of the chunk, which can be interleaved text, images, or other types.
Content InterleavedContentUnion `json:"content,required"`
// Metadata associated with the chunk that will be used in the model context during
// inference.
Metadata map[string]QueryChunksResponseChunkMetadataUnion `json:"metadata,required"`
// Metadata for the chunk that will NOT be used in the context during inference.
// The `chunk_metadata` is required backend functionality.
ChunkMetadata QueryChunksResponseChunkChunkMetadata `json:"chunk_metadata"`
// Optional embedding for the chunk. If not provided, it will be computed later.
Embedding []float64 `json:"embedding"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
ChunkID respjson.Field
Content respjson.Field
Metadata respjson.Field
ChunkMetadata respjson.Field
Embedding respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r QueryChunksResponseChunk) RawJSON() string { return r.JSON.raw }
func (r *QueryChunksResponseChunk) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// QueryChunksResponseChunkMetadataUnion contains all possible properties and
// values from [bool], [float64], [string], [[]any].
//
// Use the methods beginning with 'As' to cast the union to one of its variants.
//
// If the underlying value is not a json object, one of the following properties
// will be valid: OfBool OfFloat OfString OfAnyArray]
type QueryChunksResponseChunkMetadataUnion struct {
// This field will be present if the value is a [bool] instead of an object.
OfBool bool `json:",inline"`
// This field will be present if the value is a [float64] instead of an object.
OfFloat float64 `json:",inline"`
// This field will be present if the value is a [string] instead of an object.
OfString string `json:",inline"`
// This field will be present if the value is a [[]any] instead of an object.
OfAnyArray []any `json:",inline"`
JSON struct {
OfBool respjson.Field
OfFloat respjson.Field
OfString respjson.Field
OfAnyArray respjson.Field
raw string
} `json:"-"`
}
func (u QueryChunksResponseChunkMetadataUnion) AsBool() (v bool) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u QueryChunksResponseChunkMetadataUnion) AsFloat() (v float64) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u QueryChunksResponseChunkMetadataUnion) AsString() (v string) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
func (u QueryChunksResponseChunkMetadataUnion) AsAnyArray() (v []any) {
apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
return
}
// Returns the unmodified JSON received from the API
func (u QueryChunksResponseChunkMetadataUnion) RawJSON() string { return u.JSON.raw }
func (r *QueryChunksResponseChunkMetadataUnion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Metadata for the chunk that will NOT be used in the context during inference.
// The `chunk_metadata` is required backend functionality.
type QueryChunksResponseChunkChunkMetadata struct {
// The dimension of the embedding vector for the chunk.
ChunkEmbeddingDimension int64 `json:"chunk_embedding_dimension"`
// The embedding model used to create the chunk's embedding.
ChunkEmbeddingModel string `json:"chunk_embedding_model"`
// The ID of the chunk. If not set, it will be generated based on the document ID
// and content.
ChunkID string `json:"chunk_id"`
// The tokenizer used to create the chunk. Default is Tiktoken.
ChunkTokenizer string `json:"chunk_tokenizer"`
// The window of the chunk, which can be used to group related chunks together.
ChunkWindow string `json:"chunk_window"`
// The number of tokens in the content of the chunk.
ContentTokenCount int64 `json:"content_token_count"`
// An optional timestamp indicating when the chunk was created.
CreatedTimestamp int64 `json:"created_timestamp"`
// The ID of the document this chunk belongs to.
DocumentID string `json:"document_id"`
// The number of tokens in the metadata of the chunk.
MetadataTokenCount int64 `json:"metadata_token_count"`
// The source of the content, such as a URL, file path, or other identifier.
Source string `json:"source"`
// An optional timestamp indicating when the chunk was last updated.
UpdatedTimestamp int64 `json:"updated_timestamp"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
ChunkEmbeddingDimension respjson.Field
ChunkEmbeddingModel respjson.Field
ChunkID respjson.Field
ChunkTokenizer respjson.Field
ChunkWindow respjson.Field
ContentTokenCount respjson.Field
CreatedTimestamp respjson.Field
DocumentID respjson.Field
MetadataTokenCount respjson.Field
Source respjson.Field
UpdatedTimestamp respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r QueryChunksResponseChunkChunkMetadata) RawJSON() string { return r.JSON.raw }
func (r *QueryChunksResponseChunkChunkMetadata) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type VectorIoInsertParams struct {
// The chunks to insert. Each `Chunk` should contain content which can be
// interleaved text, images, or other types. `metadata`: `dict[str, Any]` and
// `embedding`: `List[float]` are optional. If `metadata` is provided, you
// configure how Llama Stack formats the chunk during generation. If `embedding` is
// not provided, it will be computed later.
Chunks []VectorIoInsertParamsChunk `json:"chunks,omitzero,required"`
// The identifier of the vector database to insert the chunks into.
VectorStoreID string `json:"vector_store_id,required"`
// The time to live of the chunks.
TtlSeconds param.Opt[int64] `json:"ttl_seconds,omitzero"`
paramObj
}
func (r VectorIoInsertParams) MarshalJSON() (data []byte, err error) {
type shadow VectorIoInsertParams
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *VectorIoInsertParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// A chunk of content that can be inserted into a vector database.
//
// The properties ChunkID, Content, Metadata are required.
type VectorIoInsertParamsChunk struct {
// Unique identifier for the chunk. Must be provided explicitly.
ChunkID string `json:"chunk_id,required"`
// The content of the chunk, which can be interleaved text, images, or other types.
Content InterleavedContentUnionParam `json:"content,omitzero,required"`
// Metadata associated with the chunk that will be used in the model context during
// inference.
Metadata map[string]VectorIoInsertParamsChunkMetadataUnion `json:"metadata,omitzero,required"`
// Metadata for the chunk that will NOT be used in the context during inference.
// The `chunk_metadata` is required backend functionality.
ChunkMetadata VectorIoInsertParamsChunkChunkMetadata `json:"chunk_metadata,omitzero"`
// Optional embedding for the chunk. If not provided, it will be computed later.
Embedding []float64 `json:"embedding,omitzero"`
paramObj
}
func (r VectorIoInsertParamsChunk) MarshalJSON() (data []byte, err error) {
type shadow VectorIoInsertParamsChunk
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *VectorIoInsertParamsChunk) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Only one field can be non-zero.
//
// Use [param.IsOmitted] to confirm if a field is set.
type VectorIoInsertParamsChunkMetadataUnion struct {
OfBool param.Opt[bool] `json:",omitzero,inline"`
OfFloat param.Opt[float64] `json:",omitzero,inline"`
OfString param.Opt[string] `json:",omitzero,inline"`
OfAnyArray []any `json:",omitzero,inline"`
paramUnion
}
func (u VectorIoInsertParamsChunkMetadataUnion) MarshalJSON() ([]byte, error) {
return param.MarshalUnion(u, u.OfBool, u.OfFloat, u.OfString, u.OfAnyArray)
}
func (u *VectorIoInsertParamsChunkMetadataUnion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, u)
}
func (u *VectorIoInsertParamsChunkMetadataUnion) asAny() any {
if !param.IsOmitted(u.OfBool) {
return &u.OfBool.Value
} else if !param.IsOmitted(u.OfFloat) {
return &u.OfFloat.Value
} else if !param.IsOmitted(u.OfString) {
return &u.OfString.Value
} else if !param.IsOmitted(u.OfAnyArray) {
return &u.OfAnyArray
}
return nil
}
// Metadata for the chunk that will NOT be used in the context during inference.
// The `chunk_metadata` is required backend functionality.
type VectorIoInsertParamsChunkChunkMetadata struct {
// The dimension of the embedding vector for the chunk.
ChunkEmbeddingDimension param.Opt[int64] `json:"chunk_embedding_dimension,omitzero"`
// The embedding model used to create the chunk's embedding.
ChunkEmbeddingModel param.Opt[string] `json:"chunk_embedding_model,omitzero"`
// The ID of the chunk. If not set, it will be generated based on the document ID
// and content.
ChunkID param.Opt[string] `json:"chunk_id,omitzero"`
// The tokenizer used to create the chunk. Default is Tiktoken.
ChunkTokenizer param.Opt[string] `json:"chunk_tokenizer,omitzero"`
// The window of the chunk, which can be used to group related chunks together.
ChunkWindow param.Opt[string] `json:"chunk_window,omitzero"`
// The number of tokens in the content of the chunk.
ContentTokenCount param.Opt[int64] `json:"content_token_count,omitzero"`
// An optional timestamp indicating when the chunk was created.
CreatedTimestamp param.Opt[int64] `json:"created_timestamp,omitzero"`
// The ID of the document this chunk belongs to.
DocumentID param.Opt[string] `json:"document_id,omitzero"`
// The number of tokens in the metadata of the chunk.
MetadataTokenCount param.Opt[int64] `json:"metadata_token_count,omitzero"`
// The source of the content, such as a URL, file path, or other identifier.
Source param.Opt[string] `json:"source,omitzero"`
// An optional timestamp indicating when the chunk was last updated.
UpdatedTimestamp param.Opt[int64] `json:"updated_timestamp,omitzero"`
paramObj
}
func (r VectorIoInsertParamsChunkChunkMetadata) MarshalJSON() (data []byte, err error) {
type shadow VectorIoInsertParamsChunkChunkMetadata
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *VectorIoInsertParamsChunkChunkMetadata) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type VectorIoQueryParams struct {
// The query to search for.
Query InterleavedContentUnionParam `json:"query,omitzero,required"`
// The identifier of the vector database to query.
VectorStoreID string `json:"vector_store_id,required"`
// The parameters of the query.
Params map[string]VectorIoQueryParamsParamUnion `json:"params,omitzero"`
paramObj
}
func (r VectorIoQueryParams) MarshalJSON() (data []byte, err error) {
type shadow VectorIoQueryParams
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *VectorIoQueryParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Only one field can be non-zero.
//
// Use [param.IsOmitted] to confirm if a field is set.
type VectorIoQueryParamsParamUnion struct {
OfBool param.Opt[bool] `json:",omitzero,inline"`
OfFloat param.Opt[float64] `json:",omitzero,inline"`
OfString param.Opt[string] `json:",omitzero,inline"`
OfAnyArray []any `json:",omitzero,inline"`
paramUnion
}
func (u VectorIoQueryParamsParamUnion) MarshalJSON() ([]byte, error) {
return param.MarshalUnion(u, u.OfBool, u.OfFloat, u.OfString, u.OfAnyArray)
}
func (u *VectorIoQueryParamsParamUnion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, u)
}
func (u *VectorIoQueryParamsParamUnion) asAny() any {
if !param.IsOmitted(u.OfBool) {
return &u.OfBool.Value
} else if !param.IsOmitted(u.OfFloat) {
return &u.OfFloat.Value
} else if !param.IsOmitted(u.OfString) {
return &u.OfString.Value
} else if !param.IsOmitted(u.OfAnyArray) {
return &u.OfAnyArray
}
return nil
}