Extract discrete facts from a list of conversation messages. Returns a `Vec ` of facts parsed from the LLM's JSON response.
(&self, messages: &[LlmMessage])
| 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, |
no test coverage detected