> For clean Markdown of any page, append .md to the page URL. > For a complete documentation index, see https://docs-beta.getzep.com/v4/how-to-get-most-relevant-facts-for-an-arbitrary-query/llms.txt. > For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs-beta.getzep.com/_mcp/server. # 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. ## Search the graph First, we perform an [edge search](/searching-the-graph) on the knowledge graph using a sample query. The search takes the `graph_uuid` of the graph. For a user graph, use the `graph_uuid` that `user.create` returns, and store it next to your own user ID: ```python from zep_cloud.client import Zep zep_client = Zep(api_key=API_KEY) results = zep_client.graph.search_edges(zep_graph_uuid, query="Some search query") ``` ```typescript import { ZepClient } from "@getzep/zep-cloud"; const client = new ZepClient({ apiKey: process.env.ZEP_API_KEY || "" }); const results = await client.graph.searchEdges(zepGraphUuid, { body: { query: "Some search query" }, }); ``` ```go package main import ( "context" "fmt" "log" "os" zep "github.com/getzep/zep-go/v4" zepclient "github.com/getzep/zep-go/v4/client" "github.com/getzep/zep-go/v4/option" ) func main() { ctx := context.Background() client := zepclient.NewClient( option.WithAPIKey(os.Getenv("ZEP_API_KEY")), ) // zepGraphUUID is the graph UUID that you stored for this user. zepGraphUUID := os.Getenv("ZEP_GRAPH_UUID") results, err := client.Graph.SearchEdges(ctx, zepGraphUUID, &zep.GraphSearchEdgesRequest{ Body: &zep.SearchRequest{ Query: "Some search query", }, }) if err != nil { log.Fatalf("Error: %v", err) } ``` ## Build the fact list Then, we get the edges from the first page of search results and construct our fact list. We also include the temporal validity data to each fact string: ```python # Build list of formatted facts relevant_edges = results.items or [] formatted_facts = [] for edge in relevant_edges: valid_at = edge.valid_at if edge.valid_at is not None else "date unknown" invalid_at = edge.invalid_at if edge.invalid_at is not None else "present" formatted_fact = f"{edge.fact} (Date range: {valid_at} - {invalid_at})" formatted_facts.append(formatted_fact) # Print the results print("\nFound facts:") for fact in formatted_facts: print(f"- {fact}") ``` ```typescript // Build list of formatted facts const relevantEdges = results.data; const formattedFacts: string[] = []; for (const edge of relevantEdges) { const validAt = edge.validAt ?? "date unknown"; const invalidAt = edge.invalidAt ?? "present"; const formattedFact = `${edge.fact} (Date range: ${validAt} - ${invalidAt})`; formattedFacts.push(formattedFact); } // Print the results console.log("\nFound facts:"); for (const fact of formattedFacts) { console.log(`- ${fact}`); } ``` ```go // Build list of formatted facts relevantEdges := results.Results var formattedFacts []string for _, edge := range relevantEdges { fact := "" if edge.Fact != nil { fact = *edge.Fact } validAt := "date unknown" if edge.ValidAt != nil { validAt = *edge.ValidAt } invalidAt := "present" if edge.InvalidAt != nil { invalidAt = *edge.InvalidAt } formattedFact := fmt.Sprintf("%s (Date range: %s - %s)", fact, validAt, invalidAt) formattedFacts = append(formattedFacts, formattedFact) } // Print the results fmt.Println("\nFound facts:") for _, fact := range formattedFacts { fmt.Printf("- %s\n", fact) } } ``` ## Summary 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. > Search a Context Graph for facts that are relevant to a query