Expand description
§llmweb.rs
Powering the Web with Rust & LLMs
llmweb is a Rust library designed to seamlessly integrate Large Language Models (LLMs)
with web content. It allows you to fetch a webpage, extract its content, and then
use an LLM to get structured data from it based on a provided schema.
§Features
- 🚀 Seamless integration with major LLM APIs.
- ✨ Automatic structured data extraction from web content.
- 🔧 Schema-first approach for precise data formatting using
serde_json::Value. - ⚡ Async-first design for high performance.
§Example
Here’s a quick example of how to use llmweb to extract stories from Hacker News:
use llmweb::{LlmWeb, error::LlmWebError};
use serde::{Deserialize, Serialize};
use serde_json::json;
#[derive(Debug, Serialize, Deserialize)]
struct Story {
title: String,
points: f32,
by: Option<String>,
comments_url: Option<String>,
}
#[tokio::main]
async fn main() -> Result<(), LlmWebError> {
// 1. Define the schema for the data you want to extract.
let schema_json = json!({
"type": "array",
"items": {
"type": "object",
"properties": {
"by": { "type": "string" },
"comments_url": { "type": "string" },
"points": { "type": "number" },
"title": { "type": "string" }
},
"required": ["by", "comments_url", "points", "title"]
}
});
// 2. Create an LlmWeb instance with the desired model.
// Make sure you have the GEMINI_API_KEY environment variable set.
let llmweb = LlmWeb::new("gemini-1.5-flash");
// 3. Call completion with the URL and schema.
let structured_value: Vec<Story> = llmweb
.completion("https://news.ycombinator.com", schema_json)
.await?;
// 4. Print the result.
println!("{:#?}", structured_value);
Ok(())
}Modules§
Macros§
Structs§
- LlmWeb
- The main struct for interacting with web pages and LLMs.
Enums§
- LlmWeb
Format - Represents the desired output format.