From the uDemy course on LLM engineering.
https://www.udemy.com/course/llm-engineering-master-ai-and-large-language-models
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
5.1 KiB
5.1 KiB
Development Guide
This guide provides instructions for extending the CodeXchange AI application with new languages and AI models.
Before diving into development, it's recommended to review the Architecture Diagram to understand the component relationships and application flow.
Adding New Programming Languages
- Update Language Configuration (
config.py
):
SUPPORTED_LANGUAGES = [..., "NewLanguage"]
LANGUAGE_MAPPING = {..., "NewLanguage": "language_highlight_name"}
- Add Language Detection (
core/language_detection.py
):
class LanguageDetector:
@staticmethod
def detect_new_language(code: str) -> bool:
patterns = [r'pattern1', r'pattern2', r'pattern3']
return any(re.search(pattern, code) for pattern in patterns)
- Add Execution Support (
core/code_execution.py
):
def execute_new_language(self, code: str) -> tuple[str, Optional[bytes]]:
with tempfile.NamedTemporaryFile(suffix='.ext', mode='w', delete=False) as f:
f.write(code)
file_path = f.name
try:
result = subprocess.run(
["compiler/interpreter", file_path],
capture_output=True,
text=True,
check=True
)
return result.stdout, None
except Exception as e:
return f"Error: {str(e)}", None
finally:
os.unlink(file_path)
- Update the Dockerfile:
# Add necessary dependencies for the new language
RUN apt-get update && apt-get install -y --no-install-recommends \
new-language-package \
&& rm -rf /var/lib/apt/lists/*
# Verify the installation
RUN echo "New Language: $(new-language --version 2>/dev/null || echo 'NOT VERIFIED')"
- Update the UI Components in
app.py
:
def _initialize_components(self):
# Add the new language to dropdown options
self.source_language = gr.Dropdown(
choices=["Python", "JavaScript", ..., "NewLanguage"],
...
)
self.target_language = gr.Dropdown(
choices=["Python", "JavaScript", ..., "NewLanguage"],
...
)
- Add Language-Specific Instructions in
template.j2
:
{% if target_language == "NewLanguage" %}
# NewLanguage-specific conversion instructions
- Follow NewLanguage best practices
- Use idiomatic NewLanguage patterns
- Handle NewLanguage-specific edge cases
{% endif %}
Adding New AI Models
- Update Model Configuration (
config.py
):
NEW_MODEL = "model-name-version"
MODELS = [..., "NewModel"]
- Add Model Integration (
models/ai_streaming.py
):
def stream_new_model(self, prompt: str) -> Generator[str, None, None]:
try:
response = self.new_model_client.generate(
prompt=prompt,
stream=True
)
reply = ""
for chunk in response:
fragment = chunk.text
reply += fragment
yield reply
except Exception as e:
logger.error(f"New Model API error: {str(e)}", exc_info=True)
yield f"Error with New Model API: {str(e)}"
- Add API Client Initialization:
def __init__(self, api_keys: dict):
# Initialize existing clients
...
# Initialize new model client
if "NEW_MODEL_API_KEY" in api_keys:
self.new_model_client = NewModelClient(
api_key=api_keys["NEW_MODEL_API_KEY"]
)
- Update the Model Selection Logic:
def stream_completion(self, model: str, prompt: str) -> Generator[str, None, None]:
if model == "NewModel":
yield from self.stream_new_model(prompt)
else:
# Existing model handling
...
Testing
-
Add test cases for new components:
- Unit tests for language detection
- Integration tests for code execution
- End-to-end tests for UI components
-
Test language detection with sample code:
- Positive examples (valid code in the target language)
- Negative examples (code from other languages)
- Edge cases (minimal valid code snippets)
-
Test code execution with various examples:
- Simple "Hello World" programs
- Programs with external dependencies
- Programs with different runtime characteristics
- Error handling cases
-
Test model streaming with different prompts:
- Short prompts
- Long prompts
- Edge cases (empty prompts, very complex code)
- Error handling
-
Verify error handling and edge cases:
- API rate limiting
- Network failures
- Invalid inputs
- Resource constraints
Logging
The application uses a structured logging system:
- JSON formatted logs with timestamps
- Stored in
logs
directory - Separate console and file logging
- Detailed execution metrics
To add logging for new components:
import logging
logger = logging.getLogger(__name__)
def new_function():
try:
logger.info("Starting operation", extra={"component": "new_component"})
# Function logic
logger.info("Operation completed", extra={"component": "new_component", "metrics": {...}})
except Exception as e:
logger.error(f"Error: {str(e)}", exc_info=True, extra={"component": "new_component"})