llm_chain::parsing

Function find_yaml

Source
pub fn find_yaml<T: DeserializeOwned>(
    text: &str,
) -> Result<Vec<T>, ExtractionError>
Expand description

Attempts to find a YAML object in a string and deserialize it into the specified type.

§Arguments

  • text - A string slice containing the document which may contain YAML content.

§Returns

  • Ok(Vec<T>) - If the YAML content is successfully extracted and deserialized into the specified type.
  • Err(ExtractionError) - If an error occurs during extraction or deserialization.

§Type Parameters

  • T: DeserializeOwned - The type into which the extracted YAML content should be deserialized.

§Examples

It handles the obvious case where it is just YAML.

#[derive(serde::Deserialize)]
struct Dummy {
   hello: String
}
use llm_chain::parsing::find_yaml;
let data = "
hello: world
";
let data: Vec<Dummy> = find_yaml(data).unwrap();
assert_eq!(data[0].hello, "world");

It handles the case where it is in a code block.

use llm_chain::parsing::find_yaml;
// NOTE: we are escaping the backticks because this is a doc test.
let data = "
\u{60}``yaml
hello: world
\u{60}``
";
find_yaml::<serde_yaml::Value>(data).unwrap();