rexis-graph 0.1.0

Rexis Graph - Graph-based agent orchestration with hybrid state management and memory integration
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
//! # RRAG Integration
//!
//! Integration layer between RGraph and RRAG for RAG-powered agent workflows.
//! This module provides nodes that can leverage RRAG's retrieval and generation capabilities.

use crate::core::{ExecutionContext, ExecutionResult, Node, NodeId};
use crate::state::{GraphState, StateValue};
use crate::{RGraphError, RGraphResult};
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Configuration for RAG retrieval nodes
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct RagRetrievalConfig {
    pub query_key: String,
    pub context_key: String,
    pub top_k: usize,
    pub similarity_threshold: Option<f32>,
    pub metadata_filters: Vec<(String, String)>,
}

impl Default for RagRetrievalConfig {
    fn default() -> Self {
        Self {
            query_key: "user_query".to_string(),
            context_key: "retrieval_context".to_string(),
            top_k: 5,
            similarity_threshold: Some(0.7),
            metadata_filters: Vec::new(),
        }
    }
}

/// Configuration for RAG generation nodes
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct RagGenerationConfig {
    pub query_key: String,
    pub context_key: String,
    pub response_key: String,
    pub system_prompt: Option<String>,
    pub max_tokens: Option<usize>,
    pub temperature: Option<f32>,
}

impl Default for RagGenerationConfig {
    fn default() -> Self {
        Self {
            query_key: "user_query".to_string(),
            context_key: "retrieval_context".to_string(),
            response_key: "rag_response".to_string(),
            system_prompt: Some(
                "You are a helpful AI assistant. Use the provided context to answer the user's question accurately and comprehensively.".to_string()
            ),
            max_tokens: Some(512),
            temperature: Some(0.7),
        }
    }
}

/// Configuration for context evaluation nodes
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ContextEvaluationConfig {
    pub context_key: String,
    pub query_key: String,
    pub relevance_key: String,
    pub min_relevance_score: f32,
}

impl Default for ContextEvaluationConfig {
    fn default() -> Self {
        Self {
            context_key: "retrieval_context".to_string(),
            query_key: "user_query".to_string(),
            relevance_key: "context_relevance".to_string(),
            min_relevance_score: 0.6,
        }
    }
}

/// A node that performs RAG retrieval (simplified mock implementation)
pub struct RagRetrievalNode {
    id: NodeId,
    name: String,
    config: RagRetrievalConfig,
}

impl RagRetrievalNode {
    pub fn new(id: impl Into<NodeId>, name: impl Into<String>, config: RagRetrievalConfig) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            config,
        }
    }
}

#[async_trait]
impl Node for RagRetrievalNode {
    async fn execute(
        &self,
        state: &mut GraphState,
        context: &ExecutionContext,
    ) -> RGraphResult<ExecutionResult> {
        // Get the query from state
        let query = state.get(&self.config.query_key)?;
        let query_text = query
            .as_string()
            .ok_or_else(|| RGraphError::node(self.id.as_str(), "Query must be a string"))?;

        // Simulate document retrieval (in real implementation, this would use RRAG)
        let mock_documents = vec![
            create_mock_document("Machine learning is a method of data analysis that automates analytical model building.", 0.85),
            create_mock_document("It is a branch of artificial intelligence based on the idea that systems can learn from data.", 0.78),
            create_mock_document("ML algorithms build a model based on training data to make predictions or decisions.", 0.72),
        ];

        // Filter based on similarity threshold if provided
        let filtered_docs: Vec<StateValue> =
            if let Some(threshold) = self.config.similarity_threshold {
                mock_documents
                    .into_iter()
                    .filter(|doc| {
                        if let Some(obj) = doc.as_object() {
                            if let Some(score_val) = obj.get("score") {
                                if let Some(score) = score_val.as_float() {
                                    return score >= threshold as f64;
                                }
                            }
                        }
                        false
                    })
                    .take(self.config.top_k)
                    .collect()
            } else {
                mock_documents.into_iter().take(self.config.top_k).collect()
            };

        // Store retrieval context
        state.set_with_context(
            context.current_node.as_str(),
            &self.config.context_key,
            StateValue::Array(filtered_docs.clone()),
        );

        // Store retrieval metadata
        state.set_with_context(
            context.current_node.as_str(),
            "retrieval_metadata",
            StateValue::from(serde_json::json!({
                "total_results": filtered_docs.len(),
                "retrieved_count": filtered_docs.len(),
                "query": query_text
            })),
        );

        Ok(ExecutionResult::Continue)
    }

    fn id(&self) -> &NodeId {
        &self.id
    }

    fn name(&self) -> &str {
        &self.name
    }

    fn input_keys(&self) -> Vec<&str> {
        vec![&self.config.query_key]
    }

    fn output_keys(&self) -> Vec<&str> {
        vec![&self.config.context_key, "retrieval_metadata"]
    }
}

/// A node that performs RAG generation (simplified mock implementation)
pub struct RagGenerationNode {
    id: NodeId,
    name: String,
    config: RagGenerationConfig,
}

impl RagGenerationNode {
    pub fn new(
        id: impl Into<NodeId>,
        name: impl Into<String>,
        config: RagGenerationConfig,
    ) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            config,
        }
    }
}

#[async_trait]
impl Node for RagGenerationNode {
    async fn execute(
        &self,
        state: &mut GraphState,
        context: &ExecutionContext,
    ) -> RGraphResult<ExecutionResult> {
        // Get query and context from state
        let query = state.get(&self.config.query_key)?;
        let query_text = query
            .as_string()
            .ok_or_else(|| RGraphError::node(self.id.as_str(), "Query must be a string"))?;

        let context_value = state.get(&self.config.context_key)?;
        let context_docs = if let Some(array) = context_value.as_array() {
            array
                .iter()
                .filter_map(|v| {
                    if let Some(obj) = v.as_object() {
                        if let Some(content) = obj.get("content") {
                            content.as_string()
                        } else {
                            None
                        }
                    } else {
                        None
                    }
                })
                .collect::<Vec<&str>>()
                .join("\n\n")
        } else {
            return Err(RGraphError::node(
                self.id.as_str(),
                "Context must be an array of documents",
            ));
        };

        // Simulate response generation (in real implementation, this would use RRAG's generation engine)
        let response = format!(
            "Based on the provided context, here's what I can tell you about {}: {}",
            query_text,
            if context_docs.is_empty() {
                "I don't have specific information available, but I can provide a general response."
            } else {
                "The context provides relevant information that I can use to answer your question comprehensively."
            }
        );

        // Calculate token estimate before moving response
        let token_estimate = response.len() / 4;

        // Store the generated response
        state.set_with_context(
            context.current_node.as_str(),
            &self.config.response_key,
            StateValue::String(response),
        );

        // Store generation metadata
        state.set_with_context(
            context.current_node.as_str(),
            "generation_metadata",
            StateValue::from(serde_json::json!({
                "tokens_used": token_estimate,
                "model": "mock-model",
                "finish_reason": "complete"
            })),
        );

        Ok(ExecutionResult::Continue)
    }

    fn id(&self) -> &NodeId {
        &self.id
    }

    fn name(&self) -> &str {
        &self.name
    }

    fn input_keys(&self) -> Vec<&str> {
        vec![&self.config.query_key, &self.config.context_key]
    }

    fn output_keys(&self) -> Vec<&str> {
        vec![&self.config.response_key, "generation_metadata"]
    }
}

/// A node that evaluates context relevance
pub struct ContextEvaluationNode {
    id: NodeId,
    name: String,
    config: ContextEvaluationConfig,
}

impl ContextEvaluationNode {
    pub fn new(
        id: impl Into<NodeId>,
        name: impl Into<String>,
        config: ContextEvaluationConfig,
    ) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            config,
        }
    }
}

#[async_trait]
impl Node for ContextEvaluationNode {
    async fn execute(
        &self,
        state: &mut GraphState,
        context: &ExecutionContext,
    ) -> RGraphResult<ExecutionResult> {
        // Get context and query from state
        let context_value = state.get(&self.config.context_key)?;
        let query_value = state.get(&self.config.query_key)?;

        let query_text = query_value
            .as_string()
            .ok_or_else(|| RGraphError::node(self.id.as_str(), "Query must be a string"))?;

        let context_docs = if let Some(array) = context_value.as_array() {
            array
        } else {
            return Err(RGraphError::node(
                self.id.as_str(),
                "Context must be an array of documents",
            ));
        };

        // Evaluate relevance for each document
        let mut relevant_docs = Vec::new();
        let mut total_score = 0.0;
        let mut evaluated_count = 0;

        for doc in context_docs {
            if let Some(obj) = doc.as_object() {
                if let Some(content_val) = obj.get("content") {
                    if let Some(content) = content_val.as_string() {
                        // Simple relevance scoring based on keyword overlap
                        let relevance_score = self.calculate_relevance_score(query_text, content);

                        if relevance_score >= self.config.min_relevance_score {
                            let mut relevant_doc_map = obj.clone();
                            relevant_doc_map.insert(
                                "relevance_score".to_string(),
                                StateValue::Float(relevance_score as f64),
                            );
                            let relevant_doc = StateValue::Object(relevant_doc_map);
                            relevant_docs.push(relevant_doc);
                        }

                        total_score += relevance_score;
                        evaluated_count += 1;
                    }
                }
            }
        }

        let average_relevance = if evaluated_count > 0 {
            total_score / evaluated_count as f32
        } else {
            0.0
        };

        // Store filtered relevant context
        state.set_with_context(
            context.current_node.as_str(),
            "filtered_context",
            StateValue::Array(relevant_docs.clone()),
        );

        // Store relevance metrics
        state.set_with_context(
            context.current_node.as_str(),
            &self.config.relevance_key,
            StateValue::from(serde_json::json!({
                "average_score": average_relevance,
                "relevant_docs_count": relevant_docs.len(),
                "total_docs_evaluated": evaluated_count,
                "min_threshold": self.config.min_relevance_score
            })),
        );

        Ok(ExecutionResult::Continue)
    }

    fn id(&self) -> &NodeId {
        &self.id
    }

    fn name(&self) -> &str {
        &self.name
    }

    fn input_keys(&self) -> Vec<&str> {
        vec![&self.config.context_key, &self.config.query_key]
    }

    fn output_keys(&self) -> Vec<&str> {
        vec!["filtered_context", &self.config.relevance_key]
    }
}

impl ContextEvaluationNode {
    /// Simple relevance scoring based on keyword overlap
    fn calculate_relevance_score(&self, query: &str, content: &str) -> f32 {
        let query_words: std::collections::HashSet<String> = query
            .to_lowercase()
            .split_whitespace()
            .map(|w| w.trim_matches(|c: char| !c.is_alphanumeric()))
            .filter(|w| !w.is_empty())
            .map(|w| w.to_string())
            .collect();

        let content_words: std::collections::HashSet<String> = content
            .to_lowercase()
            .split_whitespace()
            .map(|w| w.trim_matches(|c: char| !c.is_alphanumeric()))
            .filter(|w| !w.is_empty())
            .map(|w| w.to_string())
            .collect();

        if query_words.is_empty() || content_words.is_empty() {
            return 0.0;
        }

        let intersection_count = query_words.intersection(&content_words).count();
        let union_count = query_words.union(&content_words).count();

        if union_count == 0 {
            0.0
        } else {
            intersection_count as f32 / union_count as f32
        }
    }
}

/// Builder for RAG-powered workflows
pub struct RagWorkflowBuilder;

impl RagWorkflowBuilder {
    pub fn new() -> Self {
        Self
    }

    /// Create a RAG retrieval node
    pub fn build_retrieval_node(
        &self,
        id: impl Into<NodeId>,
        name: impl Into<String>,
        config: RagRetrievalConfig,
    ) -> RGraphResult<Arc<RagRetrievalNode>> {
        Ok(Arc::new(RagRetrievalNode::new(id, name, config)))
    }

    /// Create a RAG generation node
    pub fn build_generation_node(
        &self,
        id: impl Into<NodeId>,
        name: impl Into<String>,
        config: RagGenerationConfig,
    ) -> RGraphResult<Arc<RagGenerationNode>> {
        Ok(Arc::new(RagGenerationNode::new(id, name, config)))
    }

    /// Create a context evaluation node
    pub fn build_evaluation_node(
        &self,
        id: impl Into<NodeId>,
        name: impl Into<String>,
        config: ContextEvaluationConfig,
    ) -> RGraphResult<Arc<ContextEvaluationNode>> {
        Ok(Arc::new(ContextEvaluationNode::new(id, name, config)))
    }
}

impl Default for RagWorkflowBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Helper function to create mock documents for demonstration
fn create_mock_document(content: &str, score: f64) -> StateValue {
    let mut doc = HashMap::new();
    doc.insert(
        "content".to_string(),
        StateValue::String(content.to_string()),
    );
    doc.insert("score".to_string(), StateValue::Float(score));
    doc.insert("metadata".to_string(), StateValue::Object(HashMap::new()));
    StateValue::Object(doc)
}