-
Notifications
You must be signed in to change notification settings - Fork 680
Expand file tree
/
Copy pathdocument.go
More file actions
291 lines (261 loc) · 8.47 KB
/
document.go
File metadata and controls
291 lines (261 loc) · 8.47 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
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package ai
import (
"encoding/json"
"fmt"
)
// A Document is a piece of data that can be embedded, indexed, or retrieved.
// It includes metadata. It can contain multiple parts.
type Document struct {
// The data that is part of this document.
Content []*Part `json:"content,omitempty"`
// The metadata for this document.
Metadata map[string]any `json:"metadata,omitempty"`
}
// A Part is one part of a [Document]. This may be plain text or it
// may be a URL (possibly a "data:" URL with embedded data).
type Part struct {
Kind PartKind `json:"kind,omitempty"`
ContentType string `json:"contentType,omitempty"` // valid for kind==blob
Text string `json:"text,omitempty"` // valid for kind∈{text,blob}
ToolRequest *ToolRequest `json:"toolRequest,omitempty"` // valid for kind==partToolRequest
ToolResponse *ToolResponse `json:"toolResponse,omitempty"` // valid for kind==partToolResponse
Custom map[string]any `json:"custom,omitempty"` // valid for plugin-specific custom parts
Metadata map[string]any `json:"metadata,omitempty"` // valid for all kinds
}
type PartKind int8
const (
PartText PartKind = iota
PartMedia
PartData
PartToolRequest
PartToolResponse
PartCustom
PartReasoning
)
// NewTextPart returns a Part containing text.
func NewTextPart(text string) *Part {
return &Part{Kind: PartText, ContentType: "plain/text", Text: text}
}
// NewJSONPart returns a Part containing JSON.
func NewJSONPart(text string) *Part {
return &Part{Kind: PartText, ContentType: "application/json", Text: text}
}
// NewMediaPart returns a Part containing structured data described
// by the given mimeType.
func NewMediaPart(mimeType, contents string) *Part {
return &Part{Kind: PartMedia, ContentType: mimeType, Text: contents}
}
// NewDataPart returns a Part containing raw string data.
func NewDataPart(contents string) *Part {
return &Part{Kind: PartData, Text: contents}
}
// NewToolRequestPart returns a Part containing a request from
// the model to the client to run a Tool.
// (Only genkit plugins should need to use this function.)
func NewToolRequestPart(r *ToolRequest) *Part {
return &Part{Kind: PartToolRequest, ToolRequest: r}
}
// NewToolResponsePart returns a Part containing the results
// of applying a Tool that the model requested.
func NewToolResponsePart(r *ToolResponse) *Part {
return &Part{Kind: PartToolResponse, ToolResponse: r}
}
// NewResponseForToolRequest returns a Part containing the results
// of executing the tool request part.
func NewResponseForToolRequest(p *Part, output any) *Part {
if !p.IsToolRequest() {
return nil
}
return &Part{Kind: PartToolResponse, ToolResponse: &ToolResponse{
Name: p.ToolRequest.Name,
Ref: p.ToolRequest.Ref,
Output: output,
}}
}
// NewCustomPart returns a Part containing custom plugin-specific data.
func NewCustomPart(customData map[string]any) *Part {
return &Part{Kind: PartCustom, Custom: customData}
}
// NewReasoningPart returns a Part containing reasoning text
func NewReasoningPart(text string, metadata map[string]any) *Part {
return &Part{
Kind: PartReasoning,
ContentType: "plain/text",
Text: text,
Metadata: metadata,
}
}
// IsText reports whether the [Part] contains plain text.
func (p *Part) IsText() bool {
return p.Kind == PartText
}
// IsMedia reports whether the [Part] contains structured media data.
func (p *Part) IsMedia() bool {
return p.Kind == PartMedia
}
// IsData reports whether the [Part] contains unstructured data.
func (p *Part) IsData() bool {
return p.Kind == PartData
}
// IsToolRequest reports whether the [Part] contains a request to run a tool.
func (p *Part) IsToolRequest() bool {
return p.Kind == PartToolRequest
}
// IsToolResponse reports whether the [Part] contains the result of running a tool.
func (p *Part) IsToolResponse() bool {
return p.Kind == PartToolResponse
}
// IsCustom reports whether the [Part] contains custom plugin-specific data.
func (p *Part) IsCustom() bool {
return p.Kind == PartCustom
}
// IsReasoning reports whether the [Part] contains a reasoning text
func (p *Part) IsReasoning() bool {
return p.Kind == PartReasoning
}
// MarshalJSON is called by the JSON marshaler to write out a Part.
func (p *Part) MarshalJSON() ([]byte, error) {
// This is not handled by the schema generator because
// Part is defined in TypeScript as a union.
switch p.Kind {
case PartText:
v := textPart{
Text: p.Text,
Metadata: p.Metadata,
}
return json.Marshal(v)
case PartMedia:
v := mediaPart{
Media: &Media{
ContentType: p.ContentType,
Url: p.Text,
},
Metadata: p.Metadata,
}
return json.Marshal(v)
case PartData:
v := dataPart{
Data: p.Text,
Metadata: p.Metadata,
}
return json.Marshal(v)
case PartToolRequest:
v := toolRequestPart{
ToolRequest: p.ToolRequest,
Metadata: p.Metadata,
}
return json.Marshal(v)
case PartToolResponse:
v := toolResponsePart{
ToolResponse: p.ToolResponse,
Metadata: p.Metadata,
}
return json.Marshal(v)
case PartCustom:
v := customPart{
Custom: p.Custom,
Metadata: p.Metadata,
}
return json.Marshal(v)
case PartReasoning:
v := reasoningPart{
Reasoning: p.Text,
Metadata: p.Metadata,
}
return json.Marshal(v)
default:
return nil, fmt.Errorf("invalid part kind %v", p.Kind)
}
}
type partSchema struct {
Text string `json:"text,omitempty" yaml:"text,omitempty"`
Media *Media `json:"media,omitempty" yaml:"media,omitempty"`
Data string `json:"data,omitempty" yaml:"data,omitempty"`
ToolRequest *ToolRequest `json:"toolRequest,omitempty" yaml:"toolRequest,omitempty"`
ToolResponse *ToolResponse `json:"toolResponse,omitempty" yaml:"toolResponse,omitempty"`
Custom map[string]any `json:"custom,omitempty" yaml:"custom,omitempty"`
Metadata map[string]any `json:"metadata,omitempty" yaml:"metadata,omitempty"`
Reasoning string `json:"reasoning,omitempty" yaml:"reasoning,omitempty"`
}
// unmarshalPartFromSchema updates Part p based on the schema s.
func (p *Part) unmarshalPartFromSchema(s partSchema) {
switch {
case s.Media != nil:
p.Kind = PartMedia
p.Text = s.Media.Url
p.ContentType = s.Media.ContentType
case s.ToolRequest != nil:
p.Kind = PartToolRequest
p.ToolRequest = s.ToolRequest
case s.ToolResponse != nil:
p.Kind = PartToolResponse
p.ToolResponse = s.ToolResponse
case s.Custom != nil:
p.Kind = PartCustom
p.Custom = s.Custom
default:
p.Kind = PartText
p.Text = s.Text
p.ContentType = ""
if s.Data != "" {
// Note: if part is completely empty, we use text by default.
p.Kind = PartData
p.Text = s.Data
}
}
p.Metadata = s.Metadata
}
// UnmarshalJSON is called by the JSON unmarshaler to read a Part.
func (p *Part) UnmarshalJSON(b []byte) error {
var s partSchema
if err := json.Unmarshal(b, &s); err != nil {
return err
}
p.unmarshalPartFromSchema(s)
return nil
}
// UnmarshalYAML implements goccy/go-yaml library's InterfaceUnmarshaler interface.
func (p *Part) UnmarshalYAML(unmarshal func(any) error) error {
var s partSchema
if err := unmarshal(&s); err != nil {
return err
}
p.unmarshalPartFromSchema(s)
return nil
}
// JSONSchemaAlias tells the JSON schema reflection code to use a different
// type for the schema for this type. This is needed because the JSON
// marshaling of Part uses a schema that matches the TypeScript code,
// rather than the natural JSON marshaling. This matters because the
// current JSON validation code works by marshaling the JSON.
func (Part) JSONSchemaAlias() any {
return partSchema{}
}
// DocumentFromText returns a [Document] containing a single plain text part.
// This takes ownership of the metadata map.
func DocumentFromText(text string, metadata map[string]any) *Document {
return &Document{
Content: []*Part{
{
Kind: PartText,
Text: text,
},
},
Metadata: metadata,
}
}