pub struct LlmWeb { /* private fields */ }Expand description
The main struct for interacting with web pages and LLMs.
It holds the client for the LLM and provides methods to perform completions on web content.
Implementations§
Source§impl LlmWeb
impl LlmWeb
Sourcepub fn new(name: &str) -> Self
pub fn new(name: &str) -> Self
Creates a new LlmWeb instance.
§Arguments
name- The name of the LLM model to use (e.g., “gemini-1.5-flash”).
Examples found in repository?
More examples
7async fn main() {
8 let schema_json = json!({
9 "type": "object",
10 "properties": {
11 "tweet_text": {
12 "type": "string",
13 },
14 },
15 "required": ["tweet_text"]
16 });
17
18 let llmweb = LlmWeb::new("gemini-2.0-flash");
19 let structed_value: Value = llmweb
20 .exec(
21 "https://x.com/ztgx5/status/1942242787317133452",
22 schema_json,
23 )
24 .await
25 .unwrap();
26 println!("{:#?}", structed_value);
27}13async fn main() {
14 // Load the schema from an external file as a string.
15 let schema_str = include_str!("../schemas/hn_schema.json");
16
17 let llmweb = LlmWeb::new("gemini-2.0-flash");
18 eprintln!("Fetching from Hacker News and extracting stories...");
19
20 // Use the convenience method `exec_from_schema_str` which handles
21 // parsing the schema string internally.
22 let structed_value: Vec<Story> = llmweb
23 .exec_from_schema_str("https://news.ycombinator.com", schema_str)
24 .await
25 .unwrap();
26 println!("{:#?}", structed_value);
27}Sourcepub async fn exec<R>(&self, url: &str, scheme: Value) -> Result<R>where
R: DeserializeOwned + Debug,
pub async fn exec<R>(&self, url: &str, scheme: Value) -> Result<R>where
R: DeserializeOwned + Debug,
Fetches content from a URL, sends it to an LLM for processing based on a schema, and returns the structured data.
This function performs the following steps:
- Launches a headless browser.
- Navigates to the specified URL.
- Extracts the HTML content of the page.
- Sends the content and a JSON schema to the configured LLM.
- Parses the LLM’s JSON response into the specified Rust type
R.
§Arguments
url- The URL of the web page to process.scheme- A serializable object representing the JSON schema for data extraction. This is typically aserde_json::Value.
§Errors
This function can return an LlmWebError if any of the steps fail, such as
browser errors, network issues, LLM API errors, or JSON deserialization errors.
Examples found in repository?
7async fn main() {
8 let schema_json = json!({
9 "type": "object",
10 "properties": {
11 "tweet_text": {
12 "type": "string",
13 },
14 },
15 "required": ["tweet_text"]
16 });
17
18 let llmweb = LlmWeb::new("gemini-2.0-flash");
19 let structed_value: Value = llmweb
20 .exec(
21 "https://x.com/ztgx5/status/1942242787317133452",
22 schema_json,
23 )
24 .await
25 .unwrap();
26 println!("{:#?}", structed_value);
27}Sourcepub async fn exec_from_schema_str<R>(
&self,
url: &str,
schema_str: &str,
) -> Result<R>where
R: DeserializeOwned + Debug,
pub async fn exec_from_schema_str<R>(
&self,
url: &str,
schema_str: &str,
) -> Result<R>where
R: DeserializeOwned + Debug,
A convenience method that accepts a schema as a string slice.
This method is useful when loading a schema from a file. It parses the
string into a serde_json::Value and then calls the main completion method.
§Arguments
url- The URL of the web page to process.schema_str- A string slice containing the JSON schema.
§Errors
Returns an error if the schema_str is not valid JSON, or if any of the
underlying operations in completion fail.
Examples found in repository?
More examples
13async fn main() {
14 // Load the schema from an external file as a string.
15 let schema_str = include_str!("../schemas/hn_schema.json");
16
17 let llmweb = LlmWeb::new("gemini-2.0-flash");
18 eprintln!("Fetching from Hacker News and extracting stories...");
19
20 // Use the convenience method `exec_from_schema_str` which handles
21 // parsing the schema string internally.
22 let structed_value: Vec<Story> = llmweb
23 .exec_from_schema_str("https://news.ycombinator.com", schema_str)
24 .await
25 .unwrap();
26 println!("{:#?}", structed_value);
27}Sourcepub async fn stream<R>(&self, url: &str, scheme: Value) -> Result<R>where
R: DeserializeOwned + Debug,
pub async fn stream<R>(&self, url: &str, scheme: Value) -> Result<R>where
R: DeserializeOwned + Debug,
Fetches content from a URL, sends it to an LLM for processing based on a schema, and returns the structured data.
This function performs the following steps:
- Launches a headless browser.
- Navigates to the specified URL.
- Extracts the HTML content of the page.
- Sends the content and a JSON schema to the configured LLM.
- Parses the LLM’s JSON response into the specified Rust type
R.
This method is intended for streaming responses.
§Arguments
url- The URL of the web page to process.scheme- A serializable object representing the JSON schema for data extraction. This is typically aserde_json::Value.
§Errors
This function can return an LlmWebError if any of the steps fail, such as
browser errors, network issues, LLM API errors, or JSON deserialization errors.
Auto Trait Implementations§
impl Freeze for LlmWeb
impl !RefUnwindSafe for LlmWeb
impl Send for LlmWeb
impl Sync for LlmWeb
impl Unpin for LlmWeb
impl !UnwindSafe for LlmWeb
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> 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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more