Skip to content

Chat Conversations

Tutorial 4: Chat Conversation Archive

Build a conversation similarity system with role-weighted embeddings.

Goal

Compare chat conversations where: - User messages are more important than assistant messages - Topic enables filtering - Similar conversation patterns are grouped

Configuration

schema:
  user_message:
    type: text
    required: true
  assistant_message:
    type: text
    required: true
  topic: text

embeddings:
  # User message embedding
  user_vec:
    field: user_message
    model: tfidf

  # Assistant message embedding
  assistant_vec:
    field: assistant_message
    model: tfidf

  # Combined conversation embedding with user bias
  chat_vec:
    combine:
      - ref: user_vec
        weight: 0.6
      - ref: assistant_vec
        weight: 0.4

similarities:
  # Conversation semantic similarity
  chat_sim:
    embedding: chat_vec

  # Topic exact match
  topic_sim:
    field: topic
    metric: exact

  # Combined similarity
  overall:
    combine:
      - ref: chat_sim
        weight: 0.8
      - ref: topic_sim
        weight: 0.2

network:
  edges:
    similarity: overall
    min: 0.35

Implementation

rag = (NetworkRAG.builder()
       .with_storage('chats.db')
       .from_config('config/conversations.yaml')
       .build())

conversations = [
    {
        'id': 'conv1',
        'user_message': 'How do I implement a binary search tree in Python?',
        'assistant_message': 'Here is a binary search tree implementation...',
        'topic': 'data-structures'
    },
    {
        'id': 'conv2',
        'user_message': 'What is the time complexity of binary search?',
        'assistant_message': 'Binary search has O(log n) time complexity...',
        'topic': 'algorithms'
    },
    # ... more conversations
]

for conv in conversations:
    rag.add(conv['id'], document=conv)

rag.build_network()

# Find similar conversations
results = rag.search('implementing tree structures').top(5)

for result in results:
    doc = result.metadata
    print(f"\nTopic: {doc['topic']}")
    print(f"User: {doc['user_message'][:80]}...")
    print(f"Score: {result.score:.3f}")