ResponseUtils

Struct ResponseUtils 

Source
pub struct ResponseUtils;
Expand description

Response post-processing utilities

Implementations§

Source§

impl ResponseUtils

Source

pub fn extract_code_blocks(response: &str) -> Vec<CodeBlock>

Extract code blocks from markdown-formatted response

Recognizes both ``` and ` code fence formats.

§Examples
use oxify_connect_llm::ResponseUtils;

let response = "```rust\nfn main() {}\n```";
let blocks = ResponseUtils::extract_code_blocks(response);
assert_eq!(blocks.len(), 1);
assert_eq!(blocks[0].language, Some("rust".to_string()));
Source

pub fn extract_code_by_language( response: &str, language: &str, ) -> Option<String>

Extract first code block of a specific language

§Examples
use oxify_connect_llm::ResponseUtils;

let response = "```python\nprint('hello')\n```\n```rust\nprintln!(\"hi\")\n```";
let python_code = ResponseUtils::extract_code_by_language(response, "python");
assert_eq!(python_code, Some("print('hello')".to_string()));
Source

pub fn extract_all_code(response: &str) -> String

Extract all code regardless of language

Returns concatenated code from all blocks.

§Examples
use oxify_connect_llm::ResponseUtils;

let response = "```\ncode1\n```\n```\ncode2\n```";
let code = ResponseUtils::extract_all_code(response);
assert!(code.contains("code1"));
assert!(code.contains("code2"));
Source

pub fn parse_json(response: &str) -> Result<Value, Error>

Try to parse response as JSON

Attempts to extract JSON from the response, handling cases where the LLM wraps JSON in markdown code blocks.

§Examples
use oxify_connect_llm::ResponseUtils;

let response = r#"```json
{"name": "Alice", "age": 30}
```"#;
let json = ResponseUtils::parse_json(response);
assert!(json.is_ok());
Source

pub fn strip_markdown(response: &str) -> String

Remove markdown formatting from response

Strips common markdown elements like headers, bold, italic, etc.

§Examples
use oxify_connect_llm::ResponseUtils;

let response = "# Title\n**Bold** and *italic*";
let plain = ResponseUtils::strip_markdown(response);
assert!(!plain.contains('*'));
assert!(!plain.contains('#'));
Source

pub fn extract_numbered_list(response: &str) -> Vec<String>

Extract numbered list items from response

§Examples
use oxify_connect_llm::ResponseUtils;

let response = "1. First\n2. Second\n3. Third";
let items = ResponseUtils::extract_numbered_list(response);
assert_eq!(items, vec!["First", "Second", "Third"]);
Source

pub fn extract_bullet_list(response: &str) -> Vec<String>

Extract bullet list items from response

§Examples
use oxify_connect_llm::ResponseUtils;

let response = "- First\n* Second\n- Third";
let items = ResponseUtils::extract_bullet_list(response);
assert_eq!(items.len(), 3);
Source

pub fn truncate(response: &str, max_length: usize) -> String

Truncate response to a maximum length

Tries to truncate at sentence boundary if possible.

§Examples
use oxify_connect_llm::ResponseUtils;

let response = "First sentence. Second sentence. Third sentence.";
let truncated = ResponseUtils::truncate(response, 20);
assert!(truncated.len() <= 23); // 20 + "..."
Source

pub fn extract_urls(response: &str) -> Vec<String>

Extract URLs from response

§Examples
use oxify_connect_llm::ResponseUtils;

let response = "Check out https://example.com and http://test.org";
let urls = ResponseUtils::extract_urls(response);
assert_eq!(urls.len(), 2);
Source

pub fn count_sentences(response: &str) -> usize

Count sentences in response

§Examples
use oxify_connect_llm::ResponseUtils;

let response = "First sentence. Second sentence! Third sentence?";
assert_eq!(ResponseUtils::count_sentences(response), 3);
Source

pub fn count_words(response: &str) -> usize

Count words in response

§Examples
use oxify_connect_llm::ResponseUtils;

let response = "This is a test response";
assert_eq!(ResponseUtils::count_words(response), 5);
Source

pub fn normalize_whitespace(response: &str) -> String

Remove extra whitespace from response

§Examples
use oxify_connect_llm::ResponseUtils;

let response = "Too   many    spaces";
let normalized = ResponseUtils::normalize_whitespace(response);
assert_eq!(normalized, "Too many spaces");

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: 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: 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