How AI models extract entities, topics, actions, and relationships from conversations
TypeAgent uses AI models to extract structured knowledge from unstructured conversation text. This process transforms raw messages into queryable entities, actions, topics, and relationships stored in the semantic reference system.
# "Alice discussed the project with Bob"Action( verbs=["discuss"], verb_tense="past", subject_entity_name="Alice", object_entity_name="project", indirect_object_entity_name="Bob")# "The team completed the sprint"Action( verbs=["complete"], verb_tense="past", subject_entity_name="team", object_entity_name="sprint")
@dataclassclass KnowledgeResponse: """Detailed and comprehensive knowledge response.""" entities: list[ConcreteEntity] actions: list[Action] # subject/object names must match entity names inverse_actions: list[Action] # Reverse forms: (A give B) → (B receive A) topics: list[str] # Detailed, descriptive topics and keywords
from typeagent.knowpro.convknowledge import KnowledgeExtractorfrom typeagent.aitools.model_adapters import create_chat_model# Create extractor with AI modelextractor = KnowledgeExtractor( model=create_chat_model(), max_chars_per_chunk=2048, merge_action_knowledge=False)# The extractor creates a TypeChat translator internally# that converts text to KnowledgeResponse objects
import typechatmessage = """Alice discussed the new API design with Bob yesterday. The team decided to use GraphQL instead of REST for better flexibility.Bob suggested implementing rate limiting to prevent abuse."""# Extract knowledgeresult = await extractor.extract(message)if isinstance(result, typechat.Success): knowledge: KnowledgeResponse = result.value # Extracted entities for entity in knowledge.entities: print(f"Entity: {entity.name} ({entity.type})") # Output: # Entity: Alice (['person']) # Entity: Bob (['person']) # Entity: API (['technology']) # Extracted actions for action in knowledge.actions: print(f"Action: {action.subject_entity_name} {action.verbs} {action.object_entity_name}") # Output: # Action: Alice ['discuss'] API_design # Action: team ['decide'] GraphQL # Action: Bob ['suggest'] rate_limiting # Extracted topics for topic in knowledge.topics: print(f"Topic: {topic}") # Output: # Topic: API design # Topic: GraphQL vs REST # Topic: rate limitingelse: print(f"Extraction failed: {result.message}")
from typeagent.knowpro.knowledge import extract_knowledge_from_text_batchtext_batch = [ "Alice reviewed the pull request.", "Bob deployed the new feature to staging.", "The team celebrated the successful launch."]knowledge_results = await extract_knowledge_from_text_batch( extractor, text_batch, max_concurrent=3 # Process 3 at a time)for i, result in enumerate(knowledge_results): if isinstance(result, typechat.Success): print(f"Message {i}: {len(result.value.entities)} entities") else: print(f"Message {i}: extraction failed")
@dataclassclass SemanticRef: semantic_ref_ordinal: int # Unique ID range: TextRange # Location in messages knowledge: Knowledge # Entity, Action, or Topic@dataclassclass TextRange: start: TextLocation end: TextLocation | None@dataclassclass TextLocation: message_ordinal: int # Which message chunk_ordinal: int # Which chunk in message
TypeAgent supports incremental knowledge extraction as new messages arrive:
from typeagent.knowpro.conversation_base import ConversationBase# Add messages with automatic knowledge extraction and indexingresult = await conversation.add_messages_with_indexing( messages=[msg1, msg2, msg3], source_ids=["email_123", "email_124", "email_125"])print(f"Added {result.messages_added} messages")print(f"Created {result.semrefs_added} semantic references")
The incremental process:
Metadata extraction: Extract basic knowledge from message metadata
LLM extraction: Extract entities, actions, topics using AI (if enabled)
Update SemanticRefIndex: Add new terms and references
Update PropertyIndex: Add structured properties
Update secondary indexes: Timestamps, embeddings, related terms
All index updates happen within a transaction (for SQLite) or as a sequence (for memory storage). If any step fails, SQLite storage rolls back all changes.