pub struct Blob {
pub id: Option<String>,
pub metadata: HashMap<String, Value>,
pub data: Option<BlobData>,
pub mimetype: Option<String>,
pub encoding: String,
pub path: Option<PathBuf>,
}Expand description
Raw data abstraction for document loading and file processing.
Represents raw bytes or text, either in-memory or by file reference. Used primarily by document loaders to decouple data loading from parsing.
Inspired by Mozilla’s Blob
§Examples
Initialize a blob from in-memory data:
use agent_chain_core::documents::Blob;
let blob = Blob::from_data("Hello, world!");
// Read the blob as a string
assert_eq!(blob.as_string().unwrap(), "Hello, world!");
// Read the blob as bytes
assert_eq!(blob.as_bytes().unwrap(), b"Hello, world!");Load from memory and specify MIME type and metadata:
use agent_chain_core::documents::Blob;
use std::collections::HashMap;
let blob = Blob::builder()
.data("Hello, world!")
.mime_type("text/plain")
.metadata(HashMap::from([("source".to_string(), serde_json::json!("https://example.com"))]))
.build()
.unwrap();Fields§
§id: Option<String>An optional identifier for the blob.
metadata: HashMap<String, Value>Arbitrary metadata associated with the blob.
data: Option<BlobData>Raw data associated with the blob (bytes or string).
mimetype: Option<String>MIME type, not to be confused with a file extension.
encoding: StringEncoding to use if decoding the bytes into a string.
Uses utf-8 as default encoding if decoding to string.
path: Option<PathBuf>Location where the original content was found.
Implementations§
Source§impl Blob
impl Blob
Sourcepub fn builder() -> BlobBuilder
pub fn builder() -> BlobBuilder
Create a new Blob builder.
Sourcepub fn from_bytes(data: Vec<u8>) -> Self
pub fn from_bytes(data: Vec<u8>) -> Self
Create a Blob from in-memory bytes.
Sourcepub fn from_path(
path: impl AsRef<Path>,
mime_type: Option<String>,
encoding: Option<String>,
metadata: Option<HashMap<String, Value>>,
) -> Self
pub fn from_path( path: impl AsRef<Path>, mime_type: Option<String>, encoding: Option<String>, metadata: Option<HashMap<String, Value>>, ) -> Self
Load the blob from a path.
The data is not loaded immediately - the blob treats the path as a reference to the underlying data.
§Arguments
path- Path to the filemime_type- Optional MIME type (will be guessed from extension if not provided)encoding- Encoding to use (defaults to “utf-8”)metadata- Optional metadata
Sourcepub fn source(&self) -> Option<String>
pub fn source(&self) -> Option<String>
The source location of the blob as string if known otherwise none.
If a path is associated with the Blob, it will default to the path location.
Unless explicitly set via a metadata field called 'source', in which
case that value will be used instead.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Blob
impl<'de> Deserialize<'de> for Blob
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl StructuralPartialEq for Blob
Auto Trait Implementations§
impl Freeze for Blob
impl RefUnwindSafe for Blob
impl Send for Blob
impl Sync for Blob
impl Unpin for Blob
impl UnwindSafe for Blob
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.