Skip to content

atovk/ai_coding_assistant

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AI Coding Assistant

A simple, extensible AI programming tool built with design patterns for maintainability and scalability.

Features

  • 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

Quick Start

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)

Architecture

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               # 使用文档

Core Components

  • 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

Design Patterns Used

  1. Singleton: CodingAssistant ensures single instance
  2. Factory: AssistantFactory for object creation
  3. Strategy: AIProvider interface for different AI services
  4. Observer: Event handling for logging and metrics
  5. Chain of Responsibility: Handler selection for requests
  6. Registry: Dynamic handler registration

Extending the System

Adding New AI Provider

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

Adding New Task Handler

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

Requirements

  • Python 3.8+
  • Optional: openai package for OpenAI integration

Installation

# Clone the repository
git clone <repository-url>
cd ai_coding_assistant

# Install dependencies (optional)
pip install openai  # Only if using OpenAI provider

Example Usage

Run the example:

python -m ai_coding_assistant.example

This will demonstrate:

  • Code completion
  • Code explanation
  • Metrics collection
  • Multiple task processing

Chain of Thought Reasoning

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}")

Chain of Thought Features

  • 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.

Development

# 安装项目依赖
poetry install

# 激活虚拟环境
poetry shell

# 运行演示
poetry run demo-cot

# 运行测试
poetry run pytest

# 代码格式化
poetry run black .

# 代码检查
poetry run flake8

# 构建包
poetry build

# 发布包
poetry publish

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages