yep
This commit is contained in:
parent
6eb3097f3d
commit
9981647c5e
12 changed files with 1384 additions and 59 deletions
|
|
@ -1,8 +1,8 @@
|
|||
use std::process::Command;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use anyhow::{Context, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::process::Command;
|
||||
|
||||
pub fn classify(input: &str, current_tag_tree: String) -> Result<String> {
|
||||
pub fn classify(tag_tree: &str, content: String) -> Result<String> {
|
||||
let prompt = format!("You are a resource classifier. Given a hierarchical tag tree and a resource, classify it into 1-3 most specific applicable tags.
|
||||
|
||||
# RULES:
|
||||
|
|
@ -12,10 +12,10 @@ pub fn classify(input: &str, current_tag_tree: String) -> Result<String> {
|
|||
- Output JSON only
|
||||
|
||||
# CURRENT TAG TREE:
|
||||
{current_tag_tree}
|
||||
{tag_tree}
|
||||
|
||||
# RESOURCE INFORMATION:
|
||||
{input}
|
||||
{content}
|
||||
|
||||
# OUTPUT FORMAT:
|
||||
{{
|
||||
|
|
@ -35,19 +35,56 @@ pub fn classify(input: &str, current_tag_tree: String) -> Result<String> {
|
|||
.arg("e")
|
||||
.arg(prompt)
|
||||
.output()
|
||||
.with_context(|| "Failed to execute tweet scraping command")?;
|
||||
.with_context(|| "Failed to execute classification command")?;
|
||||
println!("Output: {:?}", out);
|
||||
Ok(String::from_utf8_lossy(&out.stdout).to_string())
|
||||
}
|
||||
|
||||
pub fn classify_with_retry(
|
||||
tag_tree: &str,
|
||||
content: String,
|
||||
max_attempts: u32,
|
||||
) -> Result<ClassificationResult> {
|
||||
for attempt in 1..=max_attempts {
|
||||
match classify(tag_tree, content.clone()) {
|
||||
Ok(json) => match ClassificationResult::from_json(&json) {
|
||||
Ok(result) => return Ok(result),
|
||||
Err(e) => {
|
||||
eprintln!(
|
||||
"Attempt {}/{}: Failed to parse: {}",
|
||||
attempt, max_attempts, e
|
||||
);
|
||||
eprintln!("Raw response: {}", json);
|
||||
if attempt == max_attempts {
|
||||
return Err(e.into());
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!(
|
||||
"Attempt {}/{}: LLM call failed: {}",
|
||||
attempt, max_attempts, e
|
||||
);
|
||||
if attempt == max_attempts {
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
// Yeah
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct ClassificationResult {
|
||||
#[serde(default)]
|
||||
pub tags: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub confidence: Vec<f32>,
|
||||
#[serde(default)]
|
||||
pub new_tags: Vec<NewTagSuggestion>,
|
||||
#[serde(default)]
|
||||
pub reasoning: String,
|
||||
}
|
||||
|
||||
|
|
@ -66,7 +103,8 @@ impl ClassificationResult {
|
|||
|
||||
/// Get the most confident tag (if any exist)
|
||||
pub fn primary_tag(&self) -> Option<(&str, f32)> {
|
||||
self.tags.iter()
|
||||
self.tags
|
||||
.iter()
|
||||
.zip(self.confidence.iter())
|
||||
.max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
|
||||
.map(|(tag, conf)| (tag.as_str(), *conf))
|
||||
|
|
@ -79,7 +117,8 @@ impl ClassificationResult {
|
|||
|
||||
/// Get tags above confidence threshold
|
||||
pub fn confident_tags(&self, threshold: f32) -> Vec<&str> {
|
||||
self.tags.iter()
|
||||
self.tags
|
||||
.iter()
|
||||
.zip(self.confidence.iter())
|
||||
.filter(|&(_, &conf)| conf >= threshold)
|
||||
.map(|(tag, _)| tag.as_str())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue