How TypeAgent’s structured RAG approach differs from traditional RAG systems
TypeAgent implements a structured RAG (Retrieval-Augmented Generation) system that combines semantic similarity search with structured knowledge queries. This hybrid approach provides both the precision of structured queries and the recall of semantic search.
from typeagent.knowpro import searchlangimport typechat# Create query translatormodel = create_chat_model()query_translator = utils.create_translator( model, search_query_schema.SearchQuery)# Translate natural languagequery_text = "What did John discuss about the project last week?"result = await searchlang.search_query_from_language( conversation, query_translator, query_text)if isinstance(result, typechat.Success): search_query = result.value # search_query.search_expressions - list of SearchExpr # Each SearchExpr contains filters for entities, actions, time ranges
Results from multiple indexes are merged and ranked:
from typeagent.knowpro.search import run_search_query# Execute search across all relevant indexesquery_results = await run_search_query( conversation, search_query_expr, search_options)# Results contain:# - ConversationSearchResult objects# - Each with scored semantic references# - Ranked by relevance and combined scores
When structured search finds no results, the system can fall back to pure semantic similarity using the MessageTextIndex. This ensures queries always return relevant results, even if the structured approach fails.
The final stage synthesizes answers using retrieved knowledge:
from typeagent.knowpro import answers# Configure context optionsanswer_options = answers.AnswerContextOptions( entities_top_k=50, # Include top 50 entities topics_top_k=50, # Include top 50 topics messages_top_k=None # Include all matched messages)# Generate answer from search results_, combined_answer = await answers.generate_answers( answer_translator, search_results, conversation, question, options=answer_options)match combined_answer.type: case "Answered": return combined_answer.answer case "NoAnswer": return combined_answer.why_no_answer
The answer generator receives:
Entities: Structured entity objects with types and facets
Actions: Subject-verb-object triples with relationships
Topics: Relevant topics and keywords
Messages: Original message text for context
This rich context enables the LLM to generate accurate, grounded answers with proper citations.