Get Most Relevant Facts for an Arbitrary Query

In this recipe, we demonstrate how to retrieve the most relevant facts from the knowledge graph using an arbitrary search query.

First, we perform a search on the knowledge graph using a sample query:

1zep_client = AsyncZep(api_key=API_KEY)
2results = await client.graph.search(user_id="some user_id", query="Some search query", scope="edges")

Then, we get the edges from the search results and construct our fact list. We also include the temporal validity data to each fact string:

1# Build list of formatted facts
2relevant_edges = results.edges
3formatted_facts = []
4for edge in relevant_edges:
5 valid_at = edge.valid_at if edge.valid_at is not None else "date unknown"
6 invalid_at = edge.invalid_at if edge.invalid_at is not None else "present"
7 formatted_fact = f"{edge.fact} (Date range: {valid_at} - {invalid_at})"
8 formatted_facts.append(formatted_fact)
9
10# Print the results
11print("\nFound facts:")
12for fact in formatted_facts:
13 print(f"- {fact}")

We demonstrated how to retrieve the most relevant facts for an arbitrary query using the Zep client. Adjust the query and parameters as needed to tailor the search for your specific use case.