feat(context): Phase 5 - Model Adapters (Claude, OpenAI) #83

Closed
opened 2026-01-04 00:52:20 +00:00 by cardosofelipe · 0 comments

Overview

Implement model-specific context formatting adapters for optimal LLM interaction.

Parent Issue

  • #61: Context Management Engine

Implementation Tasks

1. Create adapters/base.py

  • Create ModelAdapter abstract base class
  • Define abstract format() method
  • Implement _group_by_type() helper
class ModelAdapter(ABC):
    @abstractmethod
    def format(self, contexts: list[BaseContext]) -> str:
        """Format contexts for the specific model."""
        ...

    def _group_by_type(
        self,
        contexts: list[BaseContext]
    ) -> dict[ContextType, list[BaseContext]]:
        """Group contexts by their type."""
        ...

2. Create adapters/claude.py

  • Create ClaudeAdapter class
  • Use XML tags for structure (<documents>, <document>, <task>)
  • Format system context appropriately
  • Order: System → Knowledge → Task → Conversation
class ClaudeAdapter(ModelAdapter):
    """Claude-specific formatting with XML tags."""

    def format(self, contexts: list[BaseContext]) -> str:
        parts = []
        by_type = self._group_by_type(contexts)

        if ContextType.KNOWLEDGE in by_type:
            parts.append("<documents>")
            for ctx in by_type[ContextType.KNOWLEDGE]:
                parts.append(f'<document source="{ctx.source}">{ctx.content}</document>')
            parts.append("</documents>")
        ...

3. Create adapters/openai.py

  • Create OpenAIAdapter class
  • Use markdown formatting
  • Use headers for structure (##, ###)
class OpenAIAdapter(ModelAdapter):
    """OpenAI-specific formatting with markdown."""

    def format(self, contexts: list[BaseContext]) -> str:
        parts = []
        by_type = self._group_by_type(contexts)

        if ContextType.KNOWLEDGE in by_type:
            parts.append("## Reference Documents\n")
            for ctx in by_type[ContextType.KNOWLEDGE]:
                parts.append(f"### Source: {ctx.source}\n{ctx.content}\n")
        ...

4. Create adapters/default.py

  • Create DefaultAdapter for unknown models
  • Use simple text formatting
  • Add source attribution

5. Update adapters/__init__.py

  • Export all adapters
  • Create get_adapter() factory function

Files to Create

backend/app/services/context/adapters/
├── __init__.py
├── base.py
├── claude.py
├── openai.py
└── default.py

Acceptance Criteria

  • Claude adapter uses XML tags correctly
  • OpenAI adapter uses markdown correctly
  • Default adapter works for unknown models
  • Context ordering is consistent per model
  • Unit tests for each adapter
  • Output is well-formatted and readable

Dependencies

  • #79 (Phase 1 - Foundation)

Labels

phase-2, context, backend

## Overview Implement model-specific context formatting adapters for optimal LLM interaction. ## Parent Issue - #61: Context Management Engine --- ## Implementation Tasks ### 1. Create `adapters/base.py` - [ ] Create `ModelAdapter` abstract base class - [ ] Define abstract `format()` method - [ ] Implement `_group_by_type()` helper ```python class ModelAdapter(ABC): @abstractmethod def format(self, contexts: list[BaseContext]) -> str: """Format contexts for the specific model.""" ... def _group_by_type( self, contexts: list[BaseContext] ) -> dict[ContextType, list[BaseContext]]: """Group contexts by their type.""" ... ``` ### 2. Create `adapters/claude.py` - [ ] Create `ClaudeAdapter` class - [ ] Use XML tags for structure (`<documents>`, `<document>`, `<task>`) - [ ] Format system context appropriately - [ ] Order: System → Knowledge → Task → Conversation ```python class ClaudeAdapter(ModelAdapter): """Claude-specific formatting with XML tags.""" def format(self, contexts: list[BaseContext]) -> str: parts = [] by_type = self._group_by_type(contexts) if ContextType.KNOWLEDGE in by_type: parts.append("<documents>") for ctx in by_type[ContextType.KNOWLEDGE]: parts.append(f'<document source="{ctx.source}">{ctx.content}</document>') parts.append("</documents>") ... ``` ### 3. Create `adapters/openai.py` - [ ] Create `OpenAIAdapter` class - [ ] Use markdown formatting - [ ] Use headers for structure (`##`, `###`) ```python class OpenAIAdapter(ModelAdapter): """OpenAI-specific formatting with markdown.""" def format(self, contexts: list[BaseContext]) -> str: parts = [] by_type = self._group_by_type(contexts) if ContextType.KNOWLEDGE in by_type: parts.append("## Reference Documents\n") for ctx in by_type[ContextType.KNOWLEDGE]: parts.append(f"### Source: {ctx.source}\n{ctx.content}\n") ... ``` ### 4. Create `adapters/default.py` - [ ] Create `DefaultAdapter` for unknown models - [ ] Use simple text formatting - [ ] Add source attribution ### 5. Update `adapters/__init__.py` - [ ] Export all adapters - [ ] Create `get_adapter()` factory function --- ## Files to Create ``` backend/app/services/context/adapters/ ├── __init__.py ├── base.py ├── claude.py ├── openai.py └── default.py ``` --- ## Acceptance Criteria - [ ] Claude adapter uses XML tags correctly - [ ] OpenAI adapter uses markdown correctly - [ ] Default adapter works for unknown models - [ ] Context ordering is consistent per model - [ ] Unit tests for each adapter - [ ] Output is well-formatted and readable --- ## Dependencies - #79 (Phase 1 - Foundation) ## Labels `phase-2`, `context`, `backend`
Sign in to join this conversation.