-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdocument.rs
More file actions
209 lines (183 loc) · 7.26 KB
/
Copy pathdocument.rs
File metadata and controls
209 lines (183 loc) · 7.26 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
use aws_sdk_bedrockruntime::types as aws_bedrock;
use rig::{
completion::CompletionError,
message::{Document, DocumentSourceKind},
};
pub(crate) use super::media_types::RigDocumentMediaType;
use base64::{Engine, prelude::BASE64_STANDARD};
use uuid::Uuid;
#[derive(Clone)]
pub struct RigDocument(pub Document);
impl TryFrom<RigDocument> for aws_bedrock::DocumentBlock {
type Error = CompletionError;
fn try_from(
RigDocument(Document {
data, media_type, ..
}): RigDocument,
) -> Result<Self, Self::Error> {
let document_media_type = media_type.map(|doc| RigDocumentMediaType(doc).try_into());
let document_media_type = match document_media_type {
Some(Ok(document_format)) => Ok(Some(document_format)),
Some(Err(err)) => Err(err),
None => Ok(None),
}?;
let document_source = match data {
DocumentSourceKind::Base64(blob) => {
let bytes = BASE64_STANDARD
.decode(blob)
.map_err(|e| CompletionError::RequestError(e.into()))?;
aws_bedrock::DocumentSource::Bytes(aws_smithy_types::Blob::new(bytes))
}
// NOTE: until [aws-sdk-bedrockruntime DocumentSource bug #1365](https://github.com/awslabs/aws-sdk-rust/issues/1365)
// is resolved we will use this as a workaround
// DocumentSourceKind::String(str) => aws_bedrock::DocumentSource::Text(str),
DocumentSourceKind::String(str) => {
aws_bedrock::DocumentSource::Bytes(aws_smithy_types::Blob::new(str.as_bytes()))
}
doc => {
return Err(CompletionError::RequestError(
format!("Unsupported document kind: {doc}").into(),
));
}
};
let random_string = Uuid::new_v4().simple().to_string();
let document_name = format!("document-{random_string}");
let result = aws_bedrock::DocumentBlock::builder()
.source(document_source)
.name(document_name)
.set_format(document_media_type)
.build()
.map_err(|e| CompletionError::ProviderError(e.to_string()))?;
Ok(result)
}
}
impl TryFrom<aws_bedrock::DocumentBlock> for RigDocument {
type Error = CompletionError;
fn try_from(value: aws_bedrock::DocumentBlock) -> Result<Self, Self::Error> {
let media_type: RigDocumentMediaType = value.format.try_into()?;
let media_type = media_type.0;
let data = match value.source {
Some(aws_bedrock::DocumentSource::Bytes(blob)) => {
let encoded_data = BASE64_STANDARD.encode(blob.into_inner());
Ok(DocumentSourceKind::Base64(encoded_data))
}
Some(aws_bedrock::DocumentSource::Text(str)) => Ok(DocumentSourceKind::String(str)),
doc => Err(CompletionError::ProviderError(format!(
"Unsupported document type: {doc:?}"
))),
}?;
Ok(RigDocument(Document {
data,
media_type: Some(media_type),
additional_params: None,
}))
}
}
#[cfg(test)]
mod tests {
use aws_sdk_bedrockruntime::types as aws_bedrock;
use base64::{Engine, prelude::BASE64_STANDARD};
use rig::{
completion::CompletionError,
message::{Document, DocumentMediaType, DocumentSourceKind},
};
use super::RigDocument;
#[test]
fn test_document_to_aws_document() {
let rig_document = RigDocument(Document {
data: DocumentSourceKind::Base64("data".into()),
media_type: Some(DocumentMediaType::PDF),
additional_params: None,
});
let aws_document: Result<aws_bedrock::DocumentBlock, _> = rig_document.clone().try_into();
assert!(aws_document.is_ok());
let aws_document = aws_document.unwrap();
assert_eq!(aws_document.format, aws_bedrock::DocumentFormat::Pdf);
let document_data = rig_document
.0
.data
.try_into_inner()
.unwrap()
.as_bytes()
.to_vec();
let document_data = BASE64_STANDARD.decode(document_data).unwrap();
let aws_document_bytes = aws_document
.source()
.unwrap()
.as_bytes()
.unwrap()
.as_ref()
.to_owned();
let doc_name = aws_document.name;
assert!(doc_name.starts_with("document-"));
assert_eq!(aws_document_bytes, document_data)
}
#[test]
fn test_base64_document_to_aws_document() {
let rig_document = RigDocument(Document {
data: DocumentSourceKind::Base64("data".into()),
media_type: Some(DocumentMediaType::PDF),
additional_params: None,
});
let aws_document: aws_bedrock::DocumentBlock = rig_document.clone().try_into().unwrap();
let document_data = BASE64_STANDARD
.decode(rig_document.0.data.try_into_inner().unwrap())
.unwrap();
let aws_document_bytes = aws_document
.source()
.unwrap()
.as_bytes()
.unwrap()
.as_ref()
.to_owned();
assert_eq!(aws_document_bytes, document_data)
}
#[test]
fn test_unsupported_document_to_aws_document() {
let rig_document = RigDocument(Document {
data: DocumentSourceKind::Base64("data".into()),
media_type: Some(DocumentMediaType::Javascript),
additional_params: None,
});
let aws_document: Result<aws_bedrock::DocumentBlock, _> = rig_document.clone().try_into();
assert_eq!(
aws_document.err().unwrap().to_string(),
CompletionError::ProviderError(
"Unsupported media type application/x-javascript".into()
)
.to_string()
)
}
#[test]
fn test_aws_document_to_rig_document() {
let data = aws_smithy_types::Blob::new("document_data");
let document_source = aws_bedrock::DocumentSource::Bytes(data);
let aws_document = aws_bedrock::DocumentBlock::builder()
.format(aws_bedrock::DocumentFormat::Pdf)
.name("Document")
.source(document_source)
.build()
.unwrap();
let rig_document: Result<RigDocument, _> = aws_document.clone().try_into();
assert!(rig_document.is_ok());
let rig_document = rig_document.unwrap().0;
assert_eq!(rig_document.media_type.unwrap(), DocumentMediaType::PDF)
}
#[test]
fn test_unsupported_aws_document_to_rig_document() {
let data = aws_smithy_types::Blob::new("document_data");
let document_source = aws_bedrock::DocumentSource::Bytes(data);
let aws_document = aws_bedrock::DocumentBlock::builder()
.format(aws_bedrock::DocumentFormat::Xlsx)
.name("Document")
.source(document_source)
.build()
.unwrap();
let rig_document: Result<RigDocument, _> = aws_document.clone().try_into();
assert!(rig_document.is_err());
assert_eq!(
rig_document.err().unwrap().to_string(),
CompletionError::ProviderError("Unsupported media type xlsx".into()).to_string()
)
}
}