Skip to main content

StructuredOutputParser

Struct StructuredOutputParser 

Source
pub struct StructuredOutputParser { /* private fields */ }
Expand description

结构化输出解析器

将 LLM 输出的键值对格式(每行一个 key: value)解析为 HashMap。 适用于 LLM 以非 JSON 格式输出结构化信息的场景。

§格式

输入格式应为每行一个 key: value,例如:

姓名: 张三
年龄: 28
城市: 北京

§示例

use langchainrust::output_parsers::StructuredOutputParser;

let parser = StructuredOutputParser::new();
let result = parser.parse("姓名: 张三\n年龄: 28").await?;
assert_eq!(result.get("姓名").unwrap(), "张三");

Implementations§

Source§

impl StructuredOutputParser

Source

pub fn new() -> Self

Source

pub fn with_separator(separator: char) -> Self

使用自定义分隔符创建解析器

Trait Implementations§

Source§

impl BaseOutputParser<HashMap<String, String>> for StructuredOutputParser

Source§

fn parse<'life0, 'life1, 'async_trait>( &'life0 self, text: &'life1 str, ) -> Pin<Box<dyn Future<Output = OutputParserResult<HashMap<String, String>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

将原始 LLM 输出文本解析为目标类型
Source§

fn get_format_instructions(&self) -> String

获取格式指令(用于提示 LLM 按指定格式输出)
Source§

fn parse_with_retry<'life0, 'life1, 'async_trait>( &'life0 self, text: &'life1 str, max_retries: usize, ) -> Pin<Box<dyn Future<Output = OutputParserResult<Output>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

带重试的解析(默认实现:真正重试 max_retries 次) Read more
Source§

impl Default for StructuredOutputParser

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Runnable<String, HashMap<String, String>> for StructuredOutputParser

Source§

type Error = OutputParserError

Error type.
Source§

fn invoke<'life0, 'async_trait>( &'life0 self, input: String, _config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<HashMap<String, String>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Transforms single input to output. Read more
Source§

fn stream<'life0, 'async_trait>( &'life0 self, input: String, _config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<HashMap<String, String>, Self::Error>> + Send>>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Streaming output - for real-time responses (LLM, etc). Read more
Source§

fn batch<'life0, 'async_trait>( &'life0 self, inputs: Vec<Input>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Vec<Output>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Batch processing - transforms multiple inputs to outputs. Read more
Source§

fn transform<'life0, 'async_trait>( &'life0 self, input: Pin<Box<dyn Stream<Item = Result<Input, Self::Error>> + Send>>, config: Option<RunnableConfig>, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Output, Self::Error>> + Send>>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Stream-to-stream transformation - the core of LCEL streaming. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more