Adapter traits are the extension seam where your domain logic plugs into the RAG pipeline. MagicAF ships default implementations for rapid prototyping.

Module: magicaf_core::adapters


EvidenceFormatter

#[async_trait]
pub trait EvidenceFormatter: Send + Sync {
    async fn format_evidence(&self, results: &[SearchResult]) -> Result<String>;
}

Converts raw vector search results into a text block for the LLM prompt.

ParameterTypeDescription
results&[SearchResult]Search results from the vector store

Returns: A formatted string ready for inclusion in the prompt.

DefaultEvidenceFormatter

Pass-through formatter that pretty-prints each result’s JSON payload:

{}""csEoovnuitrdecenent"c":e:"1"...(..s."c"o,re:0.9500)
use magicaf_core::adapters::DefaultEvidenceFormatter;

let formatter = DefaultEvidenceFormatter;

PromptBuilder

#[async_trait]
pub trait PromptBuilder: Send + Sync {
    async fn build_prompt(&self, query: &str, evidence: &str) -> Result<String>;
}

Assembles the final prompt from the user query and formatted evidence.

ParameterTypeDescription
query&strThe user’s original question
evidence&strFormatted evidence from the EvidenceFormatter

DefaultPromptBuilder

Wraps evidence in <context> XML tags and appends the query:

[<[Qoce/upocetniositdntoeetinxneoatcxnl>et:]>s[yqsuteermy]instruction]
use magicaf_core::adapters::DefaultPromptBuilder;

// Without system instruction
let builder = DefaultPromptBuilder::new();

// With system instruction
let builder = DefaultPromptBuilder::new()
    .with_system("You are a helpful technical assistant.");

Fields:

FieldTypeDescription
system_instructionOption<String>Optional text prepended before the context block

ResultParser<T>

#[async_trait]
pub trait ResultParser<T>: Send + Sync {
    async fn parse_result(&self, raw_output: &str) -> Result<T>;
}

Parses the LLM’s raw text output into a strongly-typed result.

ParameterTypeDescription
raw_output&strRaw text from the LLM response

Type parameter T: Your domain result type.

RawResultParser

Returns the raw LLM output as a String — zero processing.

use magicaf_core::adapters::RawResultParser;

let parser = RawResultParser;
// ResultParser<String>

JsonResultParser<T>

Deserializes the LLM output as JSON into any type implementing DeserializeOwned:

use magicaf_core::adapters::JsonResultParser;

#[derive(serde::Deserialize)]
struct MyResult {
    answer: String,
    confidence: f32,
}

let parser = JsonResultParser::<MyResult>::new();
// ResultParser<MyResult>

Errors: Returns MagicError::SerializationError if JSON parsing fails.


Prelude Re-exports

All adapter types are available through the prelude:

use magicaf_core::prelude::*;

// Gives you:
// - EvidenceFormatter (trait)
// - PromptBuilder (trait)
// - ResultParser (trait)
// - DefaultEvidenceFormatter
// - DefaultPromptBuilder
// - RawResultParser
// - JsonResultParser