-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
816 lines (708 loc) · 28.6 KB
/
Copy pathmain.rs
File metadata and controls
816 lines (708 loc) · 28.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
mod llama_cpp_backend;
mod llm;
mod skill_discovery;
use agent_core::{
agent::{apply_tool_result, process_model_output, AgentDecision, AgentState, Role},
guardrail::{GuardrailChain, GuardrailContext, GuardrailResult, PlausibilityGuard},
skill::{
parse_skill_output, validate_extraction_output, ExtractionInput, ExtractionTarget,
SkillError, SkillRequest, SkillResult_,
},
tool::{ToolRequest, ToolResult},
};
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use llama_cpp_backend::LlamaCppBackend;
use llm::{LLMBackend, LLMInput};
use serde_json::json;
use skill_discovery::{build_available_skills_prompt, discover_skills};
use std::io::{self, Write};
use std::path::PathBuf;
use std::process::Command;
const BASE_SYSTEM_PROMPT: &str = r#"You are a helpful AI agent with access to tools and skills.
Available tools:
- shell: Execute shell commands
Available skills:
- extract: Extract structured information from text (email, url, date, entity, name)
To invoke a tool, respond with JSON:
{"tool": "shell", "command": "your command here"}
To invoke a skill, respond with JSON:
{"skill": "extract", "text": "the text to extract from", "target": "email"}
Supported extraction targets: email, url, date, entity, name
IMPORTANT:
- Only output JSON when you want to invoke a tool or skill
- For final answers, respond in plain text (no JSON)
- Be concise and helpful
Example tool invocation:
{"tool": "shell", "command": "ls -la"}
Example skill invocation:
{"skill": "extract", "text": "Contact us at hello@agent.rs", "target": "email"}
Example final answer:
The directory contains 5 files including README.md and src/."#;
const TOOL_RESPONSE_SCHEMA: &str = r#"When responding after tool usage:
- First provide an OBSERVATIONS section containing factual information derived directly from tool output.
- Then provide a FINAL ANSWER section that directly answers the user request.
Both sections are required."#;
const DEFAULT_MODEL_PATH: &str = "./granite-4.0-micro-Q8_0.gguf";
fn build_system_prompt(available_skills_prompt: &str) -> String {
let mut prompt = String::new();
prompt.push_str(BASE_SYSTEM_PROMPT);
if !available_skills_prompt.trim().is_empty() {
prompt.push_str("\n\n");
prompt.push_str(available_skills_prompt);
}
prompt
}
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Cli {
/// Use `extract` to call the skill directly, or omit for full agent loop
#[command(subcommand)]
command: Option<CliCommand>,
/// Path to the GGUF model file (agent mode)
#[arg(short, long)]
model: Option<PathBuf>,
/// The user query to process (agent mode)
#[arg(short, long)]
query: Option<String>,
/// Maximum number of agent loop iterations
#[arg(short = 'i', long, default_value = "5")]
max_iterations: usize,
/// Number of tokens to generate per iteration
#[arg(short = 'n', long, default_value = "256")]
max_tokens: usize,
}
#[derive(Subcommand, Debug)]
enum CliCommand {
/// Invoke the extraction skill directly (bypasses agent loop)
Extract {
/// Text to extract from
#[arg(long)]
text: String,
/// Target to extract (email, url, date, entity)
#[arg(short, long, value_parser = parse_target)]
target: ExtractionTarget,
/// Path to the GGUF model file
#[arg(short, long)]
model: Option<PathBuf>,
/// Number of tokens to generate
#[arg(short = 'n', long, default_value = "256")]
max_tokens: usize,
},
/// Invoke a specific skill explicitly (extensible for future skills)
Skill {
#[command(subcommand)]
command: SkillCommand,
},
}
#[derive(Subcommand, Debug)]
enum SkillCommand {
/// Extract structured data from text
Extract {
/// Text to extract from
#[arg(long)]
text: String,
/// Target to extract (email, url, date, entity)
#[arg(short, long, value_parser = parse_target)]
target: ExtractionTarget,
/// Path to the GGUF model file
#[arg(short, long)]
model: Option<PathBuf>,
/// Number of tokens to generate
#[arg(short = 'n', long, default_value = "256")]
max_tokens: usize,
},
}
#[derive(Debug)]
struct AgentArgs {
model: PathBuf,
query: String,
max_iterations: usize,
max_tokens: usize,
}
fn parse_target(value: &str) -> Result<ExtractionTarget, String> {
ExtractionTarget::from_str(value).ok_or_else(|| {
format!(
"Invalid target '{}'. Expected one of: email, url, date, entity",
value
)
})
}
fn main() -> Result<()> {
let cli = Cli::parse();
match &cli.command {
Some(CliCommand::Extract {
text,
target,
model,
max_tokens,
}) => {
let model_path = model
.clone()
.unwrap_or_else(|| PathBuf::from(DEFAULT_MODEL_PATH));
run_extract_mode(text, *target, model_path, *max_tokens)
}
Some(CliCommand::Skill { command }) => match command {
SkillCommand::Extract {
text,
target,
model,
max_tokens,
} => {
let model_path = model
.clone()
.unwrap_or_else(|| PathBuf::from(DEFAULT_MODEL_PATH));
run_extract_mode(text, *target, model_path, *max_tokens)
}
},
None => {
let model = cli
.model
.clone()
.unwrap_or_else(|| PathBuf::from(DEFAULT_MODEL_PATH));
let query = cli
.query
.clone()
.ok_or_else(|| anyhow::anyhow!("Missing required --query argument"))?;
let args = AgentArgs {
model,
query,
max_iterations: cli.max_iterations,
max_tokens: cli.max_tokens,
};
let discovered_skills = discover_skills(&[PathBuf::from("skills")]);
let available_skills_prompt = build_available_skills_prompt(&discovered_skills);
let system_prompt = build_system_prompt(&available_skills_prompt);
run_agent(args, system_prompt)
}
}
}
fn run_agent(args: AgentArgs, system_prompt: String) -> Result<()> {
println!("=== agent.rs ===");
println!("Query: {}\n", args.query);
// Initialize LLM backend (llama.cpp in this case)
let mut llm_backend =
LlamaCppBackend::new(&args.model).context("Failed to initialize LLM backend")?;
// Initialize semantic guardrail chain
let guardrail_chain = GuardrailChain::new().add(Box::new(PlausibilityGuard::new()));
// Initialize agent state
let mut state = AgentState::new(&args.query);
let mut iteration = 0;
let mut current_pos: i32 = 0; // Track KV cache position
let mut tool_used = false; // Track if any tool has been invoked
let mut first_generation = true; // Track first decode (Metal shader compilation)
// Agent loop
while iteration < args.max_iterations {
iteration += 1;
// Lifecycle callback: before_llm_call
let prompt = before_llm_call(&state, tool_used, false, &system_prompt);
// Call LLM backend
let llm_output = llm_backend.infer(LLMInput {
prompt,
max_tokens: args.max_tokens,
current_pos,
first_generation,
})?;
current_pos += llm_output.tokens_processed;
first_generation = false;
// Process the output
match process_model_output(&mut state, llm_output.text) {
AgentDecision::InvokeSkill(skill_request) => {
// Execute skill
let result = execute_skill(
&skill_request,
&mut llm_backend,
args.max_tokens,
&mut current_pos,
)?;
if result.success {
// Apply result to state
state.add_message(Role::Tool, format!("Skill output:\n{}", result.to_json()));
println!("\n✓ Skill result: {}", result.to_json());
} else {
// Skill failed - add error to state
let error_msg = result.error.as_deref().unwrap_or("unknown error");
state.add_message(Role::Tool, format!("Skill failed: {}", error_msg));
eprintln!("\n✗ Skill error: {}", error_msg);
}
}
AgentDecision::InvokeTool(tool_request) => {
// Execute tool
let result = execute_tool(&tool_request)?;
// Validate tool output with semantic guardrails
let guard_ctx = GuardrailContext {
state: &state,
tool_request: &tool_request,
tool_result: &result,
};
match guardrail_chain.validate(&guard_ctx) {
GuardrailResult::Accept => {
// Apply result to state
apply_tool_result(&mut state, &result);
// Lifecycle callback: after_tool_execution
after_tool_execution(&mut state, &result);
tool_used = true;
}
GuardrailResult::Reject { reason } => {
// Guardrail rejected output - treat as inconclusive
eprintln!("\n⚠️ Guardrail rejected tool output:");
eprintln!(" {}", reason);
eprintln!("\n Attempting corrective retry...\n");
// Corrective retry with stricter instructions
let corrective_prompt =
before_llm_call(&state, tool_used, true, &system_prompt);
let retry_output = llm_backend.infer(LLMInput {
prompt: corrective_prompt,
max_tokens: args.max_tokens,
current_pos,
first_generation: false,
})?;
current_pos += retry_output.tokens_processed;
// Process retry output
match process_model_output(&mut state, retry_output.text) {
AgentDecision::InvokeSkill(skill_request) => {
// Execute skill on retry
let result = execute_skill(
&skill_request,
&mut llm_backend,
args.max_tokens,
&mut current_pos,
)?;
if result.success {
state.add_message(
Role::Tool,
format!("Skill output:\n{}", result.to_json()),
);
} else {
let error_msg =
result.error.as_deref().unwrap_or("unknown error");
state.add_message(
Role::Tool,
format!("Skill failed: {}", error_msg),
);
}
}
AgentDecision::InvokeTool(retry_request) => {
// Execute retry
let retry_result = execute_tool(&retry_request)?;
// Validate retry output
let retry_guard_ctx = GuardrailContext {
state: &state,
tool_request: &retry_request,
tool_result: &retry_result,
};
match guardrail_chain.validate(&retry_guard_ctx) {
GuardrailResult::Accept => {
// Success - apply result
apply_tool_result(&mut state, &retry_result);
after_tool_execution(&mut state, &retry_result);
tool_used = true;
}
GuardrailResult::Reject {
reason: retry_reason,
} => {
report_guardrail_failure(&reason, &retry_reason);
}
}
}
AgentDecision::Done(answer) => {
println!("\n{}", answer);
return Ok(());
}
AgentDecision::Inconclusive(retry_output) => {
report_inconclusive_after_guardrail_failure(&reason, &retry_output);
}
}
}
}
}
AgentDecision::Done(answer) => {
println!("\n{}", answer);
return Ok(());
}
AgentDecision::Inconclusive(output) => {
// Model failed to produce a tool call or complete the task
eprintln!("\n⚠️ Model produced inconclusive output:");
eprintln!(" \"{}\"", output.lines().next().unwrap_or(&output));
eprintln!("\n Attempting corrective retry with stricter instructions...\n");
// Corrective retry: re-prompt with explicit tool requirement
let corrective_prompt = before_llm_call(&state, tool_used, true, &system_prompt);
let retry_output = llm_backend.infer(LLMInput {
prompt: corrective_prompt,
max_tokens: args.max_tokens,
current_pos,
first_generation: false,
})?;
current_pos += retry_output.tokens_processed;
// Process retry output
match process_model_output(&mut state, retry_output.text) {
AgentDecision::InvokeSkill(skill_request) => {
// Success - execute skill
let result = execute_skill(
&skill_request,
&mut llm_backend,
args.max_tokens,
&mut current_pos,
)?;
if result.success {
state.add_message(
Role::Tool,
format!("Skill output:\n{}", result.to_json()),
);
} else {
let error_msg = result.error.as_deref().unwrap_or("unknown error");
state.add_message(Role::Tool, format!("Skill failed: {}", error_msg));
}
}
AgentDecision::InvokeTool(tool_request) => {
// Success - execute tool
let result = execute_tool(&tool_request)?;
apply_tool_result(&mut state, &result);
after_tool_execution(&mut state, &result);
tool_used = true;
}
AgentDecision::Done(answer) => {
println!("\n{}", answer);
return Ok(());
}
AgentDecision::Inconclusive(retry_output) => {
// Still inconclusive after retry - fail loudly
eprintln!(
"\n❌ ERROR: Model failed to produce a valid response after retry.\n"
);
eprintln!(
"Original output: \"{}\"",
output.lines().next().unwrap_or(&output)
);
eprintln!(
"Retry output: \"{}\"",
retry_output.lines().next().unwrap_or(&retry_output)
);
eprintln!(
"\nThe model did not invoke a tool/skill or provide a complete answer."
);
eprintln!("This is common with small models (3-4B parameters).");
eprintln!("\nSuggestions:");
eprintln!(" - Use a larger model (7B+ parameters)");
eprintln!(" - Use a model specifically tuned for tool use");
eprintln!(" - Simplify the query");
std::process::exit(1);
}
}
}
}
}
eprintln!("\n⚠️ Warning: Agent reached maximum iterations without completing.");
std::process::exit(1)
}
fn run_extract_mode(
text: &str,
target: ExtractionTarget,
model: PathBuf,
max_tokens: usize,
) -> Result<()> {
println!("=== agent.rs | extract ===");
println!("Model: {}", model.display());
println!("Target: {}", target.as_str());
println!("Text: \"{}\"\n", truncate_string(text, 80));
let mut llm_backend =
LlamaCppBackend::new(&model).context("Failed to initialize LLM backend")?;
let mut current_pos: i32 = 0;
let request = SkillRequest::new(
"extract",
json!({
"text": text,
"target": target.as_str()
}),
);
let result =
execute_extraction_skill(&request, &mut llm_backend, max_tokens, &mut current_pos)?;
if result.success {
println!("{}", result.to_json());
Ok(())
} else {
let msg = result
.error
.clone()
.unwrap_or_else(|| "unknown error".to_string());
Err(anyhow::anyhow!(msg))
}
}
/// Lifecycle callback: before_llm_call
/// Constructs the prompt and injects response schema if tools have been used
/// If `corrective` is true, adds stricter instructions for tool invocation
fn before_llm_call(
state: &AgentState,
tool_used: bool,
corrective: bool,
system_prompt: &str,
) -> String {
let mut prompt = String::new();
// Add system prompt
prompt.push_str(system_prompt);
prompt.push_str("\n\n");
// Add conversation history
for msg in &state.history {
match msg.role {
Role::User => {
prompt.push_str("User: ");
prompt.push_str(&msg.content);
prompt.push_str("\n\n");
}
Role::Assistant => {
prompt.push_str("Assistant: ");
prompt.push_str(&msg.content);
prompt.push_str("\n\n");
}
Role::Tool => {
prompt.push_str(&msg.content);
prompt.push_str("\n\n");
}
}
}
// Inject response schema if at least one tool has been used
if tool_used {
prompt.push_str(TOOL_RESPONSE_SCHEMA);
prompt.push_str("\n\n");
}
// Add corrective instruction if this is a retry
// This prompt addresses common LLM failures: reasoning instead of action,
// and generating commands that produce unusable outputs (headers, summaries).
if corrective {
prompt.push_str("CRITICAL: You MUST call a tool to complete this task.\n");
prompt.push_str("Respond ONLY with valid JSON in the exact format shown above.\n");
prompt.push_str(
"Do NOT explain what you will do. Do NOT use plain text. Output JSON only.\n\n",
);
prompt.push_str("IMPORTANT: The tool command must directly produce the final answer.\n");
prompt.push_str("Avoid commands that output headers, summaries, or non-answer lines.\n");
prompt.push_str(
"The tool output should be the actual data requested, not metadata about it.\n\n",
);
// NOTE: Semantic guardrails validate tool outputs at runtime.
// TODO: Future enhancement - Tool-defined postconditions
//
// Tools should optionally declare explicit semantic contracts (postconditions)
// that replace heuristic guardrails. This aligns with agent.cpp's callback
// extensibility and any-guardrail's pluggable validation model.
}
prompt.push_str("Assistant: ");
prompt
}
/// Lifecycle callback: after_tool_execution
/// Logs tool execution details and validates result
fn after_tool_execution(_state: &mut AgentState, tool_result: &ToolResult) {
// Silent - tool result is already in state history
// Future: add logging, metrics, validation here
let _ = tool_result; // Suppress unused warning
}
/// Report guardrail failure to user with structured output
///
/// Event: AgentFailedAfterGuardrails
/// Triggered when the agent fails after guardrails reject both initial and retry attempts.
fn report_guardrail_failure(initial_reason: &str, retry_reason: &str) -> ! {
let message = format!(
r#"
❌ TASK FAILED: Agent could not produce valid output
What happened:
• The agent attempted to complete your task
• Tool commands were executed successfully
• However, the tool outputs were semantically invalid
• A corrective retry was attempted
• The retry also produced invalid output
Validation failures:
Initial attempt: {}
Retry attempt: {}
Why this happened:
This model lacks sufficient tool-reasoning capability for this task.
The system refused to return incorrect results (this is by design).
What you can do:
• Use a larger model (7B+ parameters recommended)
• Use a model specifically fine-tuned for tool use
• Simplify the query to reduce reasoning complexity
• Verify the task is achievable with available tools
Note: A correct system that fails honestly is better than one that
returns plausible-looking but incorrect results.
"#,
initial_reason, retry_reason
);
eprintln!("{}", message);
std::process::exit(1);
}
/// Report model failure to produce tool call after guardrail rejection
fn report_inconclusive_after_guardrail_failure(guardrail_reason: &str, model_output: &str) -> ! {
let message = format!(
r#"
❌ TASK FAILED: Model could not recover from validation failure
What happened:
• A tool was executed but its output was rejected by validation
• Guardrail rejection: {}
• A corrective retry was attempted
• The model failed to produce a valid tool call
• Model output: "{}"
Why this happened:
The model cannot adjust its approach in response to validation feedback.
This indicates insufficient tool-reasoning capability.
What you can do:
• Use a larger model (7B+ parameters recommended)
• Use a model specifically fine-tuned for tool use
• Simplify the query
"#,
guardrail_reason,
model_output.lines().next().unwrap_or(model_output)
);
eprintln!("{}", message);
std::process::exit(1);
}
/// Execute a tool request
fn execute_tool(request: &ToolRequest) -> Result<ToolResult> {
match request.tool.as_str() {
"shell" => execute_shell_tool(request),
_ => Ok(ToolResult::failure(format!(
"Unknown tool: {}",
request.tool
))),
}
}
/// Execute the shell tool with human approval
fn execute_shell_tool(request: &ToolRequest) -> Result<ToolResult> {
// Extract command from params
let command = request
.params
.get("command")
.and_then(|v| v.as_str())
.ok_or_else(|| anyhow::anyhow!("Missing 'command' parameter"))?;
println!("\n→ shell: {}", command);
print!(" Execute? (y/n): ");
io::stdout().flush()?;
let mut input = String::new();
io::stdin().read_line(&mut input)?;
if !input.trim().eq_ignore_ascii_case("y") {
println!(" ✗ Rejected\n");
return Ok(ToolResult::failure("Command rejected by user"));
}
let output = Command::new("sh").arg("-c").arg(command).output()?;
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
if output.status.success() {
let result = stdout.to_string();
// Always show output section, even if empty
if !result.is_empty() {
println!("\n{}", result);
} else {
println!(" (no output)\n");
}
// Send to model (empty output is valid)
Ok(ToolResult::success(result))
} else {
let error = if !stderr.is_empty() {
stderr.to_string()
} else {
format!("Command exited with status {}", output.status)
};
println!(" ✗ {}\n", error);
Ok(ToolResult::failure(error))
}
}
/// Execute a skill request
///
/// Skills are contract-based operations with built-in guardrails.
/// The host executes the skill by:
/// 1. Validating input
/// 2. Calling LLM with extraction prompt
/// 3. Validating output against schema and anti-hallucination rules
fn execute_skill(
request: &SkillRequest,
llm_backend: &mut LlamaCppBackend,
max_tokens: usize,
current_pos: &mut i32,
) -> Result<SkillResult_> {
match request.skill.as_str() {
"extract" => execute_extraction_skill(request, llm_backend, max_tokens, current_pos),
_ => Ok(SkillResult_::failure(SkillError::UnknownSkill(
request.skill.clone(),
))),
}
}
/// Execute the extraction skill
fn execute_extraction_skill(
request: &SkillRequest,
llm_backend: &mut LlamaCppBackend,
max_tokens: usize,
current_pos: &mut i32,
) -> Result<SkillResult_> {
// Parse and validate input
let input = match request.parse_extraction_input() {
Ok(input) => input,
Err(e) => return Ok(SkillResult_::failure(e)),
};
let target = match input.validate() {
Ok(target) => target,
Err(e) => return Ok(SkillResult_::failure(e)),
};
println!("\n→ skill: extract (target: {})", target.as_str());
println!(" Text: \"{}\"", truncate_string(&input.text, 50));
// Build extraction prompt
let extraction_prompt = build_extraction_prompt(&input, target);
// Call LLM
let llm_output = llm_backend.infer(LLMInput {
prompt: extraction_prompt,
max_tokens,
current_pos: *current_pos,
first_generation: false,
})?;
*current_pos += llm_output.tokens_processed;
// Parse LLM output
let output = match parse_skill_output(&llm_output.text, target) {
Ok(output) => output,
Err(e) => {
eprintln!(" ✗ {}", e);
return Ok(SkillResult_::failure(e));
}
};
// Validate output (anti-hallucination)
if let Err(e) = validate_extraction_output(&input, &output, target) {
eprintln!(" ✗ {}", e);
return Ok(SkillResult_::failure(e));
}
// Success
Ok(SkillResult_::success(output.result))
}
/// Build prompt for extraction skill
fn build_extraction_prompt(input: &ExtractionInput, target: ExtractionTarget) -> String {
let target_desc = match target {
ExtractionTarget::Email => "email addresses",
ExtractionTarget::Url => "URLs",
ExtractionTarget::Date => "dates (in ISO format YYYY-MM-DD)",
ExtractionTarget::Entity => "named entities (people, organizations, locations)",
ExtractionTarget::Name => "person names (first name, last name, full names)",
};
let output_format = match target {
ExtractionTarget::Entity => {
r#"{"entity": {"people": [...], "organizations": [...], "locations": [...]}}"#
}
_ => &format!(r#"{{"{}": [...]}}"#, target.as_str()),
};
format!(
r#"Extract {target_desc} from the following text.
IMPORTANT:
- Output ONLY valid JSON
- Only include values that ACTUALLY APPEAR in the text
- Do NOT invent or hallucinate values
- If no matches found, return an empty array
Text: "{text}"
Output format: {output_format}
JSON output:"#,
target_desc = target_desc,
text = input.text,
output_format = output_format
)
}
/// Truncate string for display
fn truncate_string(s: &str, max_len: usize) -> String {
if s.len() <= max_len {
s.to_string()
} else {
format!("{}...", &s[..max_len])
}
}