A simple, extensible AI programming tool built with design patterns for maintainability and scalability.
- Multiple Task Types: Code completion, explanation, review, and bug fixing
- Chain of Thought Reasoning: Advanced step-by-step problem analysis
- Complex Reasoning: Multi-step logical analysis for complex coding problems
- Extensible Architecture: Easy to add new AI providers and task handlers
- Observer Pattern: Built-in logging and metrics collection
- Design Patterns: Uses Singleton, Factory, Strategy, Observer, and Chain of Responsibility patterns
from core.assistant import CodingAssistant
from core.interfaces import AIRequest, CodeContext, TaskType
from providers.openai_provider import MockOpenAIProvider
from handlers.code_handlers import CodeCompletionHandler
# Create assistant
provider = MockOpenAIProvider()
assistant = CodingAssistant()
assistant.set_ai_provider(provider)
# Register handler
assistant.register_handler(CodeCompletionHandler(provider))
# Create request
context = CodeContext(
file_path="example.py",
language="python",
current_line=5,
surrounding_code="def add(a, b):\n return",
cursor_position=50
)
request = AIRequest(
task_type=TaskType.CODE_COMPLETION,
context=context,
user_input="complete this function"
)
# Process request
response = assistant.process_request(request)
print(response.content)
ai_coding_assistant/
├── core/ # 核心组件
│ ├── interfaces.py # 抽象接口和数据结构
│ └── assistant.py # 主助手类(单例模式)
├── providers/ # AI服务提供商
│ └── openai_provider.py # OpenAI集成(含Mock版本)
├── handlers/ # 任务处理器
│ └── code_handlers.py # 代码相关任务处理器
├── observers/ # 观察者
│ └── base_observer.py # 日志、指标、控制台观察者
├── utils/ # 扩展工具
│ └── extensions.py # 自定义处理器和注册表
├── example.py # 使用示例
├── test_extensions.py # 扩展功能测试
└── README.md # 使用文档
- interfaces.py: Abstract base classes and data structures
- assistant.py: Main assistant class with Singleton pattern
- providers/: AI service provider implementations
- handlers/: Task-specific request handlers
- observers/: Event observers for logging and metrics
- Singleton: CodingAssistant ensures single instance
- Factory: AssistantFactory for object creation
- Strategy: AIProvider interface for different AI services
- Observer: Event handling for logging and metrics
- Chain of Responsibility: Handler selection for requests
- Registry: Dynamic handler registration
from ai_coding_assistant.core.interfaces import AIProvider, AIRequest, AIResponse
class CustomAIProvider(AIProvider):
def generate_response(self, request: AIRequest) -> AIResponse:
# Implement your AI logic here
pass
from ai_coding_assistant.core.interfaces import TaskHandler, TaskType
class CustomTaskHandler(TaskHandler):
def can_handle(self, task_type: TaskType) -> bool:
return task_type == TaskType.YOUR_CUSTOM_TYPE
def handle(self, request: AIRequest) -> AIResponse:
# Implement your task logic here
pass
- Python 3.8+
- Optional:
openai
package for OpenAI integration
# Clone the repository
git clone <repository-url>
cd ai_coding_assistant
# Install dependencies (optional)
pip install openai # Only if using OpenAI provider
Run the example:
python -m ai_coding_assistant.example
This will demonstrate:
- Code completion
- Code explanation
- Metrics collection
- Multiple task processing
The framework includes advanced Chain of Thought (CoT) reasoning capabilities for complex problem analysis:
from core.assistant import CodingAssistant
from core.interfaces import AIRequest, CodeContext, TaskType
from handlers.code_handlers import ChainOfThoughtHandler
from providers.openai_provider import MockOpenAIProvider
# Set up assistant with Chain of Thought
provider = MockOpenAIProvider()
assistant = CodingAssistant()
assistant.set_ai_provider(provider)
# Add Chain of Thought handler
cot_handler = ChainOfThoughtHandler(provider)
assistant.register_handler(cot_handler)
# Create context for complex problem
context = CodeContext(
file_path="algorithm.py",
language="python",
current_line=10,
surrounding_code="""
def fibonacci(n):
# TODO: Implement efficient fibonacci
pass
"""
)
# Request chain of thought analysis
request = AIRequest(
task_type=TaskType.CHAIN_OF_THOUGHT,
context=context,
user_input="Implement fibonacci with optimal performance"
)
# Process and view reasoning steps
response = assistant.process_request(request)
if response.reasoning_enabled and response.chain_of_thought:
for step in response.chain_of_thought.steps:
print(f"Step {step.step_number}: {step.description}")
print(f"Thought: {step.thought}")
print(f"Confidence: {step.confidence}")
- Step-by-step reasoning: Break down complex problems
- Confidence scoring: Each step includes confidence level
- Multiple reasoning types: Sequential, parallel, recursive
- Structured output: Organized reasoning process
For detailed Chain of Thought documentation, see CHAIN_OF_THOUGHT.md.
# 安装项目依赖
poetry install
# 激活虚拟环境
poetry shell
# 运行演示
poetry run demo-cot
# 运行测试
poetry run pytest
# 代码格式化
poetry run black .
# 代码检查
poetry run flake8
# 构建包
poetry build
# 发布包
poetry publish