MCPcopy Index your code
hub / github.com/InfinitiBit/graphbit / extract_facts

Method extract_facts

core/src/memory/processor.rs:33–68  ·  view source on GitHub ↗

Extract discrete facts from a list of conversation messages. Returns a `Vec ` of facts parsed from the LLM's JSON response.

(&self, messages: &[LlmMessage])

Source from the content-addressed store, hash-verified

31 ///
32 /// Returns a `Vec<String>` of facts parsed from the LLM's JSON response.
33 pub async fn extract_facts(&self, messages: &[LlmMessage]) -> GraphBitResult<Vec<String>> {
34 if messages.is_empty() {
35 return Ok(Vec::new());
36 }
37
38 let conversation = messages
39 .iter()
40 .map(|m| format!("{}: {}", role_label(&m.role), &m.content))
41 .collect::<Vec<_>>()
42 .join("\n");
43
44 let system_prompt = concat!(
45 "You are a memory extraction assistant. Your task is to extract important facts, ",
46 "preferences, and information from the conversation that would be useful to remember ",
47 "for future interactions.\n\n",
48 "Rules:\n",
49 "- Extract only factual, specific information (not greetings or filler).\n",
50 "- Each fact should be a single, self-contained sentence.\n",
51 "- Do not duplicate facts.\n",
52 "- If no meaningful facts exist, return an empty array.\n\n",
53 "Return a JSON array of strings. Example: [\"User lives in Munich\", \"User prefers dark mode\"]",
54 );
55
56 let request = LlmRequest::with_messages(vec![
57 LlmMessage::system(system_prompt),
58 LlmMessage::user(format!("Extract facts from this conversation:\n\n{conversation}")),
59 ])
60 .with_max_tokens(self.max_tokens)
61 .with_temperature(self.temperature);
62
63 let response = self.llm_provider.complete(request).await.map_err(|e| {
64 GraphBitError::memory(format!("Fact extraction LLM call failed: {e}"))
65 })?;
66
67 parse_json_string_array(&response.content)
68 }
69
70 /// Given extracted facts and existing memories, ask the LLM to decide
71 /// whether each fact should be added, used to update an existing memory,

Callers 1

addMethod · 0.80

Calls 5

parse_json_string_arrayFunction · 0.85
is_emptyMethod · 0.45
with_temperatureMethod · 0.45
with_max_tokensMethod · 0.45
completeMethod · 0.45

Tested by

no test coverage detected