Agentic Design Pattern: Spatial Reasoning

Written by

in


    """
Pattern 163: Spatial Reasoning

Description:
    The Spatial Reasoning pattern enables agents to understand and reason about spatial
    relationships, positions, orientations, and geometric properties. This pattern helps
    agents solve problems involving physical space, layouts, navigation, and spatial
    configurations.

Components:
    1. Coordinate System: Tracks positions and locations
    2. Relationship Analyzer: Identifies spatial relationships (above, below, left, right, etc.)
    3. Distance Calculator: Computes distances and proximity
    4. Orientation Tracker: Manages directions and rotations
    5. Spatial Query Engine: Answers spatial questions
    6. Visualization Helper: Generates spatial descriptions

Use Cases:
    - Navigation and pathfinding
    - Layout design and optimization
    - Object placement and arrangement
    - Map understanding and interpretation
    - Geometry problem solving
    - Spatial puzzle solving
    - Floor plan analysis

Benefits:
    - Enables spatial understanding
    - Supports geometric reasoning
    - Facilitates navigation tasks
    - Improves layout optimization
    - Better scene understanding

Trade-offs:
    - Complex coordinate management
    - Requires clear spatial representation
    - May need visualization capabilities
    - Challenging for abstract spaces
    - Limited by 2D/3D representation

LangChain Implementation:
    Uses LLM for spatial relationship interpretation with structured coordinate
    systems and distance calculations. Combines symbolic representation with
    natural language understanding for spatial queries.
"""

import os
import math
from typing import List, Dict, Any, Tuple, Optional
from dataclasses import dataclass
from enum import Enum
from dotenv import load_dotenv

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

load_dotenv()


class Direction(Enum):
    """Cardinal and ordinal directions"""
    NORTH = "north"
    SOUTH = "south"
    EAST = "east"
    WEST = "west"
    NORTHEAST = "northeast"
    NORTHWEST = "northwest"
    SOUTHEAST = "southeast"
    SOUTHWEST = "southwest"
    UP = "up"
    DOWN = "down"


class SpatialRelation(Enum):
    """Types of spatial relationships"""
    ABOVE = "above"
    BELOW = "below"
    LEFT_OF = "left of"
    RIGHT_OF = "right of"
    IN_FRONT_OF = "in front of"
    BEHIND = "behind"
    INSIDE = "inside"
    OUTSIDE = "outside"
    ADJACENT = "adjacent"
    NEAR = "near"
    FAR = "far"
    BETWEEN = "between"


@dataclass
class SpatialObject:
    """Represents an object in space"""
    name: str
    x: float
    y: float
    z: float = 0.0
    width: float = 1.0
    height: float = 1.0
    depth: float = 1.0
    orientation: float = 0.0  # degrees


@dataclass
class SpatialQuery:
    """A spatial reasoning query"""
    question: str
    objects: List[SpatialObject]
    context: str = ""


@dataclass
class SpatialAnalysis:
    """Result of spatial reasoning"""
    answer: str
    relationships: List[str]
    distances: Dict[str, float]
    reasoning: str
    visualization: str


class SpatialReasoningAgent:
    """Agent that performs spatial reasoning tasks"""
    
    def __init__(self, model_name: str = "gpt-3.5-turbo"):
        """Initialize the spatial reasoning agent"""
        self.llm = ChatOpenAI(model=model_name, temperature=0)
        
        # Prompts for different spatial reasoning tasks
        self.relation_prompt = ChatPromptTemplate.from_messages([
            ("system", """You are a spatial reasoning expert. Analyze the positions 
            of objects and describe their spatial relationships clearly and accurately.
            Consider distances, directions, and relative positions."""),
            ("user", """Objects and their positions:
{object_descriptions}

Question: {question}

Provide a clear answer based on the spatial relationships.""")
        ])
        
        self.path_prompt = ChatPromptTemplate.from_messages([
            ("system", """You are a navigation expert. Given object positions, 
            determine optimal paths and directions."""),
            ("user", """Objects and their positions:
{object_descriptions}

Navigation task: {task}

Describe the path or navigation instructions.""")
        ])
        
        self.relation_chain = self.relation_prompt | self.llm | StrOutputParser()
        self.path_chain = self.path_prompt | self.llm | StrOutputParser()
    
    def analyze_spatial_query(self, query: SpatialQuery) -> SpatialAnalysis:
        """Analyze a spatial query and return comprehensive analysis"""
        # Calculate all pairwise distances
        distances = self._calculate_distances(query.objects)
        
        # Identify spatial relationships
        relationships = self._identify_relationships(query.objects)
        
        # Create object descriptions
        object_descriptions = self._create_object_descriptions(query.objects)
        
        # Get LLM reasoning
        reasoning = self.relation_chain.invoke({
            "object_descriptions": object_descriptions,
            "question": query.question
        })
        
        # Create visualization
        visualization = self._create_visualization(query.objects)
        
        return SpatialAnalysis(
            answer=reasoning,
            relationships=relationships,
            distances=distances,
            reasoning=reasoning,
            visualization=visualization
        )
    
    def find_path(self, start: SpatialObject, end: SpatialObject, 
                  obstacles: List[SpatialObject]) -> Dict[str, Any]:
        """Find path from start to end avoiding obstacles"""
        # Calculate direct distance
        direct_distance = self._distance(start, end)
        
        # Calculate direction
        direction = self._get_direction(start, end)
        
        # Check for obstacles in path
        obstacles_in_path = self._check_obstacles(start, end, obstacles)
        
        # Get navigation instructions
        object_descriptions = self._create_object_descriptions(
            [start, end] + obstacles
        )
        
        instructions = self.path_chain.invoke({
            "object_descriptions": object_descriptions,
            "task": f"Navigate from {start.name} to {end.name}"
        })
        
        return {
            "direct_distance": direct_distance,
            "direction": direction,
            "obstacles_in_path": obstacles_in_path,
            "instructions": instructions,
            "path_clear": len(obstacles_in_path) == 0
        }
    
    def find_nearest(self, reference: SpatialObject, 
                     candidates: List[SpatialObject]) -> Tuple[SpatialObject, float]:
        """Find the nearest object to reference from candidates"""
        nearest = None
        min_distance = float('inf')
        
        for candidate in candidates:
            distance = self._distance(reference, candidate)
            if distance  bool:
        """Check if a specific spatial relationship holds between two objects"""
        if relation == SpatialRelation.ABOVE:
            return obj1.z > obj2.z
        elif relation == SpatialRelation.BELOW:
            return obj1.z < obj2.z
        elif relation == SpatialRelation.LEFT_OF:
            return obj1.x  obj2.x
        elif relation == SpatialRelation.IN_FRONT_OF:
            return obj1.y > obj2.y
        elif relation == SpatialRelation.BEHIND:
            return obj1.y < obj2.y
        elif relation == SpatialRelation.NEAR:
            distance = self._distance(obj1, obj2)
            return distance  10.0  # Threshold for "far"
        elif relation == SpatialRelation.ADJACENT:
            distance = self._distance(obj1, obj2)
            return distance  List[SpatialObject]:
        """Get all objects within a spherical region"""
        result = []
        cx, cy, cz = center
        
        for obj in objects:
            distance = math.sqrt(
                (obj.x - cx)**2 + 
                (obj.y - cy)**2 + 
                (obj.z - cz)**2
            )
            if distance  float:
        """Calculate Euclidean distance between two objects"""
        return math.sqrt(
            (obj1.x - obj2.x)**2 + 
            (obj1.y - obj2.y)**2 + 
            (obj1.z - obj2.z)**2
        )
    
    def _calculate_distances(self, objects: List[SpatialObject]) -> Dict[str, float]:
        """Calculate all pairwise distances"""
        distances = {}
        for i, obj1 in enumerate(objects):
            for obj2 in objects[i+1:]:
                key = f"{obj1.name} to {obj2.name}"
                distances[key] = self._distance(obj1, obj2)
        return distances
    
    def _identify_relationships(self, objects: List[SpatialObject]) -> List[str]:
        """Identify spatial relationships between objects"""
        relationships = []
        
        for i, obj1 in enumerate(objects):
            for obj2 in objects[i+1:]:
                # Check various relationships
                if self.check_relationship(obj1, obj2, SpatialRelation.ABOVE):
                    relationships.append(f"{obj1.name} is above {obj2.name}")
                if self.check_relationship(obj1, obj2, SpatialRelation.BELOW):
                    relationships.append(f"{obj1.name} is below {obj2.name}")
                if self.check_relationship(obj1, obj2, SpatialRelation.LEFT_OF):
                    relationships.append(f"{obj1.name} is left of {obj2.name}")
                if self.check_relationship(obj1, obj2, SpatialRelation.RIGHT_OF):
                    relationships.append(f"{obj1.name} is right of {obj2.name}")
                if self.check_relationship(obj1, obj2, SpatialRelation.NEAR):
                    relationships.append(f"{obj1.name} is near {obj2.name}")
                if self.check_relationship(obj1, obj2, SpatialRelation.ADJACENT):
                    relationships.append(f"{obj1.name} is adjacent to {obj2.name}")
        
        return relationships
    
    def _get_direction(self, from_obj: SpatialObject, 
                       to_obj: SpatialObject) -> Direction:
        """Determine cardinal direction from one object to another"""
        dx = to_obj.x - from_obj.x
        dy = to_obj.y - from_obj.y
        
        angle = math.atan2(dy, dx) * 180 / math.pi
        
        # Normalize to 0-360
        if angle < 0:
            angle += 360
        
        # Determine direction
        if 22.5 <= angle < 67.5:
            return Direction.NORTHEAST
        elif 67.5 <= angle < 112.5:
            return Direction.NORTH
        elif 112.5 <= angle < 157.5:
            return Direction.NORTHWEST
        elif 157.5 <= angle < 202.5:
            return Direction.WEST
        elif 202.5 <= angle < 247.5:
            return Direction.SOUTHWEST
        elif 247.5 <= angle < 292.5:
            return Direction.SOUTH
        elif 292.5 <= angle  List[str]:
        """Check which obstacles are in the path between start and end"""
        obstacles_in_path = []
        
        for obstacle in obstacles:
            # Simple line intersection check
            if self._is_near_line(start, end, obstacle):
                obstacles_in_path.append(obstacle.name)
        
        return obstacles_in_path
    
    def _is_near_line(self, start: SpatialObject, end: SpatialObject,
                      point: SpatialObject, threshold: float = 1.0) -> bool:
        """Check if a point is near the line between start and end"""
        # Calculate perpendicular distance from point to line
        x1, y1 = start.x, start.y
        x2, y2 = end.x, end.y
        x0, y0 = point.x, point.y
        
        numerator = abs((y2-y1)*x0 - (x2-x1)*y0 + x2*y1 - y2*x1)
        denominator = math.sqrt((y2-y1)**2 + (x2-x1)**2)
        
        if denominator == 0:
            return False
        
        distance = numerator / denominator
        
        # Also check if point is between start and end
        between = (min(x1, x2) <= x0 <= max(x1, x2) and 
                  min(y1, y2) <= y0 <= max(y1, y2))
        
        return distance  str:
        """Create textual descriptions of object positions"""
        descriptions = []
        for obj in objects:
            desc = f"{obj.name}: position ({obj.x:.1f}, {obj.y:.1f}, {obj.z:.1f})"
            if obj.width != 1.0 or obj.height != 1.0:
                desc += f", size ({obj.width:.1f}x{obj.height:.1f}x{obj.depth:.1f})"
            descriptions.append(desc)
        return "\n".join(descriptions)
    
    def _create_visualization(self, objects: List[SpatialObject]) -> str:
        """Create ASCII visualization of object positions"""
        if not objects:
            return "No objects to visualize"
        
        # Find bounds
        min_x = min(obj.x for obj in objects) - 2
        max_x = max(obj.x for obj in objects) + 2
        min_y = min(obj.y for obj in objects) - 2
        max_y = max(obj.y for obj in objects) + 2
        
        # Create grid
        width = int(max_x - min_x) + 1
        height = int(max_y - min_y) + 1
        
        grid = [[' ' for _ in range(width)] for _ in range(height)]
        
        # Place objects
        for i, obj in enumerate(objects):
            x = int(obj.x - min_x)
            y = int(obj.y - min_y)
            if 0 <= x < width and 0 <= y < height:
                grid[height - 1 - y][x] = chr(65 + i)  # A, B, C, ...
        
        # Create visualization string
        vis = "\n" + "\n".join("".join(row) for row in grid)
        vis += "\n\nLegend: " + ", ".join(
            f"{chr(65+i)}={obj.name}" for i, obj in enumerate(objects)
        )
        
        return vis


def demonstrate_spatial_reasoning():
    """Demonstrate spatial reasoning capabilities"""
    print("=" * 80)
    print("SPATIAL REASONING PATTERN DEMONSTRATION")
    print("=" * 80)
    
    agent = SpatialReasoningAgent()
    
    # Example 1: Room layout analysis
    print("\n" + "=" * 80)
    print("Example 1: Room Layout Analysis")
    print("=" * 80)
    
    room_objects = [
        SpatialObject("Table", 5, 5, 0, 2, 1, 3),
        SpatialObject("Chair1", 4, 5, 0, 1, 1, 1),
        SpatialObject("Chair2", 6, 5, 0, 1, 1, 1),
        SpatialObject("Lamp", 5, 5, 2, 0.5, 0.5, 1),
        SpatialObject("Door", 0, 5, 0, 1, 2, 0.1),
        SpatialObject("Window", 10, 5, 1, 2, 1, 0.1)
    ]
    
    query = SpatialQuery(
        question="What is the spatial arrangement of furniture in the room?",
        objects=room_objects,
        context="Living room layout"
    )
    
    analysis = agent.analyze_spatial_query(query)
    
    print(f"\nQuery: {query.question}")
    print(f"\nAnswer: {analysis.answer}")
    print(f"\nKey Relationships:")
    for rel in analysis.relationships[:5]:
        print(f"  - {rel}")
    print(f"\nVisualization:{analysis.visualization}")
    
    # Example 2: Navigation pathfinding
    print("\n" + "=" * 80)
    print("Example 2: Navigation Pathfinding")
    print("=" * 80)
    
    start = SpatialObject("Start", 0, 0, 0)
    end = SpatialObject("Goal", 10, 10, 0)
    obstacles = [
        SpatialObject("Obstacle1", 5, 5, 0, 2, 2, 1),
        SpatialObject("Obstacle2", 7, 3, 0, 1, 1, 1)
    ]
    
    path_result = agent.find_path(start, end, obstacles)
    
    print(f"\nNavigation from {start.name} to {end.name}")
    print(f"Direct distance: {path_result['direct_distance']:.2f} units")
    print(f"Direction: {path_result['direction'].value}")
    print(f"Path clear: {path_result['path_clear']}")
    if path_result['obstacles_in_path']:
        print(f"Obstacles in path: {', '.join(path_result['obstacles_in_path'])}")
    print(f"\nInstructions:\n{path_result['instructions']}")
    
    # Example 3: Nearest neighbor search
    print("\n" + "=" * 80)
    print("Example 3: Nearest Neighbor Search")
    print("=" * 80)
    
    reference = SpatialObject("Home", 5, 5, 0)
    stores = [
        SpatialObject("Grocery Store", 3, 4, 0),
        SpatialObject("Pharmacy", 8, 6, 0),
        SpatialObject("Coffee Shop", 5, 7, 0),
        SpatialObject("Bookstore", 10, 10, 0)
    ]
    
    nearest, distance = agent.find_nearest(reference, stores)
    
    print(f"\nFinding nearest store to {reference.name}")
    print(f"Nearest: {nearest.name}")
    print(f"Distance: {distance:.2f} units")
    
    print("\nAll distances:")
    for store in stores:
        dist = agent._distance(reference, store)
        direction = agent._get_direction(reference, store)
        print(f"  {store.name}: {dist:.2f} units {direction.value}")
    
    # Example 4: Spatial relationship checking
    print("\n" + "=" * 80)
    print("Example 4: Spatial Relationship Checking")
    print("=" * 80)
    
    ceiling_light = SpatialObject("Ceiling Light", 5, 5, 8)
    floor_lamp = SpatialObject("Floor Lamp", 5, 5, 0)
    couch = SpatialObject("Couch", 3, 5, 0)
    
    relations_to_check = [
        (ceiling_light, floor_lamp, SpatialRelation.ABOVE),
        (floor_lamp, ceiling_light, SpatialRelation.BELOW),
        (couch, floor_lamp, SpatialRelation.NEAR),
        (couch, floor_lamp, SpatialRelation.ADJACENT)
    ]
    
    print("\nChecking spatial relationships:")
    for obj1, obj2, relation in relations_to_check:
        holds = agent.check_relationship(obj1, obj2, relation)
        print(f"  {obj1.name} is {relation.value} {obj2.name}: {holds}")
    
    # Example 5: Regional search
    print("\n" + "=" * 80)
    print("Example 5: Regional Search")
    print("=" * 80)
    
    all_objects = [
        SpatialObject("Object1", 2, 2, 0),
        SpatialObject("Object2", 5, 5, 0),
        SpatialObject("Object3", 8, 8, 0),
        SpatialObject("Object4", 3, 7, 0),
        SpatialObject("Object5", 10, 2, 0)
    ]
    
    search_center = (5, 5, 0)
    search_radius = 4
    
    objects_in_region = agent.get_objects_in_region(
        all_objects, search_center, search_radius
    )
    
    print(f"\nSearching for objects within {search_radius} units of {search_center}")
    print(f"Found {len(objects_in_region)} objects:")
    for obj in objects_in_region:
        dist = math.sqrt(
            (obj.x - search_center[0])**2 + 
            (obj.y - search_center[1])**2 + 
            (obj.z - search_center[2])**2
        )
        print(f"  {obj.name} at ({obj.x}, {obj.y}, {obj.z}) - {dist:.2f} units away")
    
    # Example 6: Complex spatial query
    print("\n" + "=" * 80)
    print("Example 6: Complex Spatial Query")
    print("=" * 80)
    
    city_objects = [
        SpatialObject("City Hall", 5, 5, 0, 3, 3, 10),
        SpatialObject("Park", 8, 8, 0, 5, 5, 0),
        SpatialObject("School", 2, 8, 0, 2, 2, 5),
        SpatialObject("Hospital", 9, 2, 0, 3, 3, 8),
        SpatialObject("Library", 1, 2, 0, 2, 2, 4)
    ]
    
    complex_query = SpatialQuery(
        question="Which public facilities are located in the northern part of the city and which are in the south?",
        objects=city_objects,
        context="City planning analysis"
    )
    
    complex_analysis = agent.analyze_spatial_query(complex_query)
    
    print(f"\nQuery: {complex_query.question}")
    print(f"\nAnalysis:\n{complex_analysis.answer}")
    
    # Summary
    print("\n" + "=" * 80)
    print("SUMMARY")
    print("=" * 80)
    print("""
The Spatial Reasoning pattern enables agents to:
✓ Understand spatial relationships and positions
✓ Calculate distances and directions
✓ Find optimal paths and navigation routes
✓ Identify nearest neighbors
✓ Search within spatial regions
✓ Analyze complex spatial arrangements
✓ Combine geometric calculations with semantic understanding

This pattern is valuable for:
- Navigation and routing systems
- Layout and design optimization
- Geographic information systems
- Robotics and autonomous vehicles
- Game AI and virtual environments
- Spatial data analysis
- Map-based applications
    """)


if __name__ == "__main__":
    demonstrate_spatial_reasoning()

    

Agentic Design Pattern: Spatial Reasoning

Pattern 163: Spatial Reasoning – Line-by-Line Explanation

Overview

This document provides a detailed line-by-line explanation of the Spatial Reasoning pattern implementation.


Pattern Description

Pattern 163: Spatial Reasoning

Description: The Spatial Reasoning pattern enables agents to understand and reason about spatial relationships, positions, orientations, and geometric properties. This pattern helps agents solve problems involving physical space, layouts, navigation, and spatial configurations.

Components: 1. Coordinate System: Tracks positions and locations 2. Relationship Analyzer: Identifies spatial relationships (above, below, left, right, etc.) 3. Distance Calculator: Computes distances and proximity 4. Orientation Tracker: Manages directions and rotations 5. Spatial Query Engine: Answers spatial questions 6. Visualization Helper: Generates spatial descriptions

Use Cases: – Navigation and pathfinding – Layout design and optimization – Object placement and arrangement – Map understanding and interpretation – Geometry problem solving – Spatial puzzle solving – Floor plan analysis

Benefits: – Enables spatial understanding – Supports geometric reasoning – Facilitates navigation tasks – Improves layout optimization – Better scene understanding

Trade-offs: – Complex coordinate management – Requires clear spatial representation – May need visualization capabilities – Challenging for abstract spaces – Limited by 2D/3D representation

LangChain Implementation: Uses LLM for spatial relationship interpretation with structured coordinate systems and distance calculations. Combines symbolic representation with natural language understanding for spatial queries.


File Statistics

  • Total Lines: 605
  • Classes: 6
  • Functions: 1
  • Constants: 0

Module Documentation

"""
Pattern 163: Spatial Reasoning

Description:
    The Spatial Reasoning pattern enables agents to understand and reason about spatial
    relationships, positions, orientations, and geometric properties. This pattern helps
    agents solve problems involving physical space, layouts, navigation, and spatial
    configurations.

Components:
    1. Coordinate System: Tracks positions and locations
    2. Relationship Analyzer: Identifies spatial relationships (above, below, left, right, etc.)
    3. Distance Calculator: Computes distances and proximity
    4. Orientation Tracker: Manages directions and rotations
    5. Spatial Query Engine: Answers spatial questions
    6. Visualization Helper: Generates spatial descriptions

Use Cases:
    - Navigation and pathfinding
    - Layout design and optimization
    - Object placement and arrangement
    - Map understanding and interpretation
    - Geometry problem solving
    - Spatial puzzle solving
    - Floor plan analysis

Benefits:
    - Enables spatial understanding
    - Supports geometric reasoning
    - Facilitates navigation tasks
    - Improves layout optimization
    - Better scene understanding

Trade-offs:
    - Complex coordinate management
    - Requires clear spatial representation
    - May need visualization capabilities
    - Challenging for abstract spaces
    - Limited by 2D/3D representation

LangChain Implementation:
    Uses LLM for spatial relationship interpretation with structured coordinate
    systems and distance calculations. Combines symbolic representation with
    natural language understanding for spatial queries.
"""

Purpose: This module implements the Spatial Reasoning pattern, providing a structured approach to agent design and implementation.


Import Statements

Standard Library Imports

import dataclasses.dataclass
import enum.Enum
import langchain_core.output_parsers.StrOutputParser
import langchain_core.prompts.ChatPromptTemplate
import os
import typing.Any
import typing.Dict
import typing.List
import typing.Optional
import typing.Tuple

Purpose: Core Python functionality: – dataclasses.dataclass: Simplified class definitions with @dataclass – enum.Enum: Enumeration support – langchain_core.output_parsers.StrOutputParser: Regular expression operations – langchain_core.prompts.ChatPromptTemplate: Regular expression operations – os: Environment variable access and system operations – typing.Any: Type hints for better code documentation (List, Dict, Any, Optional) – typing.Dict: Type hints for better code documentation (List, Dict, Any, Optional) – typing.List: Type hints for better code documentation (List, Dict, Any, Optional) – typing.Optional: Type hints for better code documentation (List, Dict, Any, Optional) – typing.Tuple: Type hints for better code documentation (List, Dict, Any, Optional)

LangChain Imports

from langchain_openai.ChatOpenAI

Purpose: LangChain framework components: – LLM Interface: Wrapper for OpenAI models – Chains: Composable building blocks for complex workflows

Other Imports

from dotenv.load_dotenv
from math

Purpose: Additional dependencies for specialized functionality.


Classes

Class: Direction

Lines 61-72

Inherits from: Enum

Purpose:

Cardinal and ordinal directions

Class: SpatialRelation

Lines 75-88

Inherits from: Enum

Purpose:

Types of spatial relationships

Class: SpatialObject

Lines 92-101

Purpose:

Represents an object in space

Class: SpatialQuery

Lines 105-109

Purpose:

A spatial reasoning query

Class: SpatialAnalysis

Lines 113-119

Purpose:

Result of spatial reasoning

Class: SpatialReasoningAgent

Lines 122-416

Purpose:

Agent that performs spatial reasoning tasks

Methods:

__init__(self, model_name)

Lines 125-154

Purpose:

Initialize the spatial reasoning agent

Parameters: – model_name: Language model instance

analyze_spatial_query(self, query)

Lines 156-182

Purpose:

Analyze a spatial query and return comprehensive analysis

Parameters: – query: Input query or question

Returns: SpatialAnalysis

find_path(self, start, end, obstacles)

Lines 184-212

Purpose:

Find path from start to end avoiding obstacles

Parameters: – start: Start – end: End – obstacles: Obstacles

Returns: <ast.Subscript object at 0x000001A06D17A650>

find_nearest(self, reference, candidates)

Lines 214-226

Purpose:

Find the nearest object to reference from candidates

Parameters: – reference: Reference – candidates: Candidates

Returns: <ast.Subscript object at 0x000001A06D60A550>

check_relationship(self, obj1, obj2, relation)

Lines 228-253

Purpose:

Check if a specific spatial relationship holds between two objects

Parameters: – obj1: Obj1 – obj2: Obj2 – relation: Relation

Returns: bool

get_objects_in_region(self, objects, center, radius)

Lines 255-271

Purpose:

Get all objects within a spherical region

Parameters: – objects: Objects – center: Center – radius: Radius

Returns: <ast.Subscript object at 0x000001A06D74A710>

_distance(self, obj1, obj2)

Lines 273-279

Purpose:

Calculate Euclidean distance between two objects

Parameters: – obj1: Obj1 – obj2: Obj2

Returns: float

_calculate_distances(self, objects)

Lines 281-288

Purpose:

Calculate all pairwise distances

Parameters: – objects: Objects

Returns: <ast.Subscript object at 0x000001A06D669750>

_identify_relationships(self, objects)

Lines 290-310

Purpose:

Identify spatial relationships between objects

Parameters: – objects: Objects

Returns: <ast.Subscript object at 0x000001A06D63A550>

_get_direction(self, from_obj, to_obj)

Lines 312-340

Purpose:

Determine cardinal direction from one object to another

Parameters: – from_obj: From obj – to_obj: To obj

Returns: Direction

_check_obstacles(self, start, end, obstacles)

Lines 342-352

Purpose:

Check which obstacles are in the path between start and end

Parameters: – start: Start – end: End – obstacles: Obstacles

Returns: <ast.Subscript object at 0x000001A06D7EFDD0>

_is_near_line(self, start, end, point, threshold)

Lines 354-374

Purpose:

Check if a point is near the line between start and end

Parameters: – start: Start – end: End – point: Point – threshold: Threshold

Returns: bool

_create_object_descriptions(self, objects)

Lines 376-384

Purpose:

Create textual descriptions of object positions

Parameters: – objects: Objects

Returns: str

_create_visualization(self, objects)

Lines 386-416

Purpose:

Create ASCII visualization of object positions

Parameters: – objects: Objects

Returns: str


Functions

demonstrate_spatial_reasoning()

Lines 419-600

Purpose:

Demonstrate spatial reasoning capabilities

Key Concepts Summary

Pattern Overview

The Spatial Reasoning pattern provides a structured approach to building AI agents with specific capabilities and behaviors. This implementation leverages LangChain’s composable components to create a robust and maintainable solution.

Core Components

  1. Agent Class: Main implementation of the pattern logic
  2. Prompt Engineering: Carefully crafted prompts to guide LLM behavior
  3. Tool Integration: Optional tools for extended functionality
  4. Memory Management: State handling across interactions
  5. Output Parsing: Structured response processing

When to Use

✅ Good for: – Scenarios requiring this specific pattern’s strengths – Applications needing structured agent behavior – Systems where explainability is important – Production environments requiring reliability

❌ Not ideal for: – Simple single-prompt tasks – Scenarios requiring different pattern characteristics – Resource-constrained environments (if computationally intensive)


Best Practices Demonstrated

1. Environment Configuration

  • ✅ Use .env files for API keys
  • ✅ Never hardcode credentials
  • ✅ Validate environment variables on startup

2. Error Handling

  • ✅ Graceful degradation on failures
  • ✅ Informative error messages
  • ✅ Logging for debugging

3. Prompt Engineering

  • ✅ Clear, specific instructions
  • ✅ Structured output formats
  • ✅ Few-shot examples when beneficial

4. Code Organization

  • ✅ Separation of concerns (class-based design)
  • ✅ Type hints for clarity
  • ✅ Docstrings for documentation

5. Performance Optimization

  • ✅ Appropriate temperature settings
  • ✅ Token budget management
  • ✅ Caching where applicable

LangChain Components Used

  • ChatOpenAI: OpenAI model interface
  • Chains: Sequential component composition

Why LangChain? – Composable abstractions for LLM applications – Rich ecosystem of integrations – Production-ready patterns – Active community and development


Execution Flow

1. Initialize agent with configuration
   ↓
2. Load environment and dependencies
   ↓
3. Prepare prompt with input data
   ↓
4. Invoke LLM through LangChain
   ↓
5. Parse and validate response
   ↓
6. Return formatted result

Key Decision Points: – Model selection (temperature, model type) – Prompt structure (few-shot vs zero-shot) – Error handling strategy – Output format validation


Performance Considerations

Computational Cost

  • LLM Calls: Primary cost factor (based on tokens)
  • Latency: Depends on model and input size
  • Memory: Minimal for most patterns

Optimization Strategies

  1. Prompt Optimization: Minimize tokens while maintaining clarity
  2. Caching: Cache repeated queries/responses
  3. Model Selection: Use smaller models when appropriate
  4. Batch Processing: Group similar requests
  5. Streaming: For long responses, stream tokens

Cost Estimates (Approximate)

  • GPT-3.5-turbo: ~$0.002 per request
  • GPT-4: ~$0.03 per request
  • GPT-4-turbo: ~$0.01 per request

Actual costs vary based on input/output token counts


Common Pitfalls to Avoid

1. Prompt Design Issues

❌ Vague or ambiguous instructions
✅ Clear, specific, testable prompts

2. Error Handling

❌ Ignoring API errors or rate limits
✅ Implement retry logic and graceful degradation

3. Token Management

❌ Exceeding model token limits
✅ Truncate inputs, chunk processing

4. Security

❌ Exposing API keys in code
✅ Use environment variables

5. Testing

❌ Only testing happy paths
✅ Test edge cases, failures, malformed inputs

6. Cost Management

❌ Unmonitored API usage
✅ Implement budgets, alerts, and monitoring


Further Improvements

Production Enhancements

  1. Monitoring: Add logging, metrics, tracing
  2. Validation: Input/output schema validation
  3. Rate Limiting: Implement request throttling
  4. Caching: Redis or in-memory cache for responses
  5. Async Support: Non-blocking operations for scalability

Feature Extensions

  1. Multi-Model Support: Switch between providers
  2. Fallback Models: Graceful degradation strategy
  3. Custom Tools: Domain-specific function calling
  4. Advanced Memory: Vector stores for long-term memory
  5. Evaluation: Automated testing and benchmarking

Code Quality

  1. Testing: Unit tests, integration tests
  2. Documentation: API docs, usage examples
  3. Type Safety: Stricter type hints
  4. Configuration: Externalized config files

Related Patterns

  • Pattern 162
  • Pattern 164

Pattern Families: – Reasoning Patterns (001-011) – Multi-Agent Patterns (012-019) – Tool Use Patterns (020-025) – Memory Patterns (026-031) – And many more…


Summary

The Spatial Reasoning pattern provides a powerful approach to building AI agents with specific capabilities. This implementation demonstrates:

✅ Key Strengths: – Clean, maintainable code structure – LangChain integration for composability – Type hints and documentation – Production-ready patterns

🎯 Best Use Cases: – Applications requiring this pattern’s characteristics – Systems needing structured agent behavior – Production environments requiring reliability

📚 Learning Resources: – LangChain Documentation: https://python.langchain.com/ – Pattern Documentation: See related patterns – OpenAI API Reference: https://platform.openai.com/docs/


File: 163_spatial_reasoning.py
Total Lines: 605
Pattern Category: AI Agent Design Pattern #163


Generated from actual implementation code
For educational and reference purposes

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *