interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / crates / spark-harvester / src / extractor.rs

121 lines · 3847 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use regex::Regex;
17use serde::{Deserialize, Serialize};
18
19#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
20pub struct ExtractedPair {
21 pub prompt: String,
22 pub reasoning: Option<String>,
23 pub completion: String,
24 pub provider: String,
25 pub model: String,
26 pub token_count: usize,
27 pub quality_score: f32,
28}
29
30pub struct ReasoningExtractor {
31 think_re: Regex,
32 thought_re: Regex,
33}
34
35impl Default for ReasoningExtractor {
36 fn default() -> Self {
37 Self::new()
38 }
39}
40
41impl ReasoningExtractor {
42 pub fn new() -> Self {
43 Self {
44 think_re: Regex::new(r"(?s)<think>(.*?)</think>").unwrap(),
45 thought_re: Regex::new(r"(?s)<thought>(.*?)</thought>").unwrap(),
46 }
47 }
48
49 pub fn extract(
50 &self,
51 prompt: &str,
52 raw_response: &str,
53 provider: &str,
54 model: &str,
55 ) -> ExtractedPair {
56 let mut reasoning = None;
57 let mut completion = raw_response.to_string();
58
59 if let Some(caps) = self.think_re.captures(raw_response) {
60 if let Some(m) = caps.get(1) {
61 reasoning = Some(m.as_str().trim().to_string());
62 completion = self.think_re.replace(raw_response, "").trim().to_string();
63 }
64 } else if let Some(caps) = self.thought_re.captures(raw_response) {
65 if let Some(m) = caps.get(1) {
66 reasoning = Some(m.as_str().trim().to_string());
67 completion = self.thought_re.replace(raw_response, "").trim().to_string();
68 }
69 }
70
71 let token_count = (prompt.len() + raw_response.len()) / 4;
72 let quality_score = Self::score_quality(prompt, reasoning.as_deref(), &completion);
73
74 ExtractedPair {
75 prompt: prompt.to_string(),
76 reasoning,
77 completion,
78 provider: provider.to_string(),
79 model: model.to_string(),
80 token_count,
81 quality_score,
82 }
83 }
84
85 fn score_quality(_prompt: &str, reasoning: Option<&str>, completion: &str) -> f32 {
86 let mut score = 0.5f32;
87 if let Some(r) = reasoning {
88 if !r.is_empty() {
89 score += 0.2;
90 }
91 if r.len() > 50 {
92 score += 0.1;
93 }
94 if r.contains("step") || r.contains("verify") || r.contains("O(") {
95 score += 0.1;
96 }
97 }
98 if completion.contains("```") {
99 score += 0.1;
100 }
101 score.min(1.0)
102 }
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108
109 #[test]
110 fn test_extract_think_tags() {
111 let extractor = ReasoningExtractor::new();
112 let prompt = "Implement fibonacci in Rust";
113 let raw = "<think>Let us analyze recursion vs iteration. Iteration is O(N) time and O(1) space.</think>```rust\nfn fib(n: u32) -> u64 { 0 }\n```";
114 let pair = extractor.extract(prompt, raw, "anthropic", "claude-3-7-sonnet");
115
116 assert!(pair.reasoning.is_some());
117 assert!(pair.reasoning.unwrap().contains("analyze recursion"));
118 assert!(pair.completion.starts_with("```rust"));
119 assert!(pair.quality_score >= 0.8);
120 }
121}