Skip to Content
API ReferenceAPI Reference

API Reference

Technical documentation for CodaiPro’s backend API, model integration, and programmatic access.

For Developers: This section is intended for advanced users who want to integrate with or extend CodaiPro’s functionality.


🌐 Backend API Overview

CodaiPro runs a local FastAPI server on localhost:8000 that handles all AI inference requests.

Base URL

http://localhost:8000

Local Only: The API is bound to localhost and is not accessible from external networks. This is by design for security and privacy.


πŸ“‘ API Endpoints

Health Check

Check if the backend server is running.

Endpoint: GET /health

Request:

curl http://localhost:8000/health

Response:

{ "status": "healthy", "version": "2.1.0", "model_loaded": true, "uptime_seconds": 1234 }

Generate Response

Generate AI response for a given prompt.

Endpoint: POST /api/generate

Request Body:

{ "prompt": "Write a Python function to reverse a string", "temperature": 0.5, "max_tokens": 1000, "system_instruction": "You are a helpful coding assistant" }

Parameters:

ParameterTypeRequiredDefaultDescription
promptstringβœ… Yes-The user’s question or request
temperaturefloat❌ No0.5Creativity level (0.1-1.0)
max_tokensinteger❌ No1000Maximum response length
system_instructionstring❌ No-Custom system prompt

Response:

{ "response": "def reverse_string(s):\n return s[::-1]\n\n# Example usage\nprint(reverse_string('hello')) # Output: olleh", "tokens_used": 45, "generation_time_ms": 2340, "model": "codai-v2.1" }

Code Analysis

Analyze code for bugs, optimization opportunities, and improvements.

Endpoint: POST /api/analyze

Request Body:

{ "code": "def factorial(n):\n if n == 0:\n return 1\n return n * factorial(n-1)", "language": "python", "analysis_type": ["bugs", "optimization", "style"] }

Response:

{ "bugs": [ { "line": 2, "severity": "medium", "issue": "No handling for negative numbers", "suggestion": "Add validation: if n < 0: raise ValueError" } ], "optimizations": [ { "type": "performance", "suggestion": "Consider iterative approach to avoid stack overflow for large n" } ], "style_issues": [] }

Model Information

Get information about the loaded AI model.

Endpoint: GET /api/model/info

Response:

{ "model_name": "CodaiPro v2.1", "model_type": "GGUF", "model_size_mb": 120, "context_length": 4096, "languages_supported": ["python", "javascript", "java", "c++", "..."], "capabilities": ["code_generation", "debugging", "explanation", "optimization"] }

πŸ”§ Configuration API

Get Current Settings

Endpoint: GET /api/config

Response:

{ "temperature": 0.5, "max_tokens": 1000, "system_instruction": "", "model_path": "models/codai-model.gguf" }

Update Settings

Endpoint: POST /api/config

Request Body:

{ "temperature": 0.7, "max_tokens": 1500 }

Response:

{ "success": true, "updated_settings": { "temperature": 0.7, "max_tokens": 1500 } }

🐍 Python SDK Example

Interact with CodaiPro programmatically using Python:

import requests import json class CodaiProClient: def __init__(self, base_url="http://localhost:8000"): self.base_url = base_url def generate(self, prompt, temperature=0.5, max_tokens=1000): """Generate AI response""" endpoint = f"{self.base_url}/api/generate" payload = { "prompt": prompt, "temperature": temperature, "max_tokens": max_tokens } response = requests.post(endpoint, json=payload) return response.json() def analyze_code(self, code, language="python"): """Analyze code for issues""" endpoint = f"{self.base_url}/api/analyze" payload = { "code": code, "language": language, "analysis_type": ["bugs", "optimization", "style"] } response = requests.post(endpoint, json=payload) return response.json() def health_check(self): """Check if server is running""" endpoint = f"{self.base_url}/health" response = requests.get(endpoint) return response.json() # Usage example client = CodaiProClient() # Check health print(client.health_check()) # Generate code result = client.generate( prompt="Write a function to check if a string is a palindrome", temperature=0.5 ) print(result['response']) # Analyze code analysis = client.analyze_code( code="def add(a, b): return a + b", language="python" ) print(analysis)

πŸ“¦ Model Integration

Loading Custom Models

CodaiPro supports GGUF format models. To use a custom model:

  1. Place your .gguf model file in the models/ directory
  2. Update the configuration:
# In configuration file MODEL_PATH = "models/your-custom-model.gguf"
  1. Restart CodaiPro

Compatibility: Ensure your custom model is compatible with llama.cpp and supports code generation tasks.


Model Requirements

RequirementSpecification
FormatGGUF (GPT-Generated Unified Format)
Size50MB - 500MB recommended
Context Length2048 tokens minimum, 4096+ recommended
QuantizationQ4_K_M or higher for best quality

πŸ”’ Security Considerations

API Security

  • βœ… Localhost Only: API binds to 127.0.0.1, not accessible externally
  • βœ… No Authentication Required: Safe because it’s local-only
  • βœ… CORS Disabled: Prevents unauthorized web access
  • βœ… No Logging: Prompts and responses are not logged

Best Practices

  1. Don’t Expose Port 8000 to the internet
  2. Verify SSL when making requests (use HTTPS in production)
  3. Sanitize Input when building applications on top of the API
  4. Rate Limiting if exposing to multiple users

πŸš€ Advanced Integration Examples

Integration with VS Code Extension

// VS Code extension example import * as vscode from 'vscode'; import axios from 'axios'; export async function getCodeSuggestion(code: string): Promise<string> { try { const response = await axios.post('http://localhost:8000/api/generate', { prompt: `Improve this code:\n${code}`, temperature: 0.5, max_tokens: 1000 }); return response.data.response; } catch (error) { vscode.window.showErrorMessage('CodaiPro not running'); return ''; } }

Integration with CLI Tool

#!/bin/bash # codai-cli.sh - Command line wrapper for CodaiPro API CODAI_URL="http://localhost:8000" function codai_generate() { local prompt="$1" curl -s -X POST "$CODAI_URL/api/generate" \ -H "Content-Type: application/json" \ -d "{\"prompt\": \"$prompt\", \"temperature\": 0.5}" \ | jq -r '.response' } # Usage: ./codai-cli.sh "Write a hello world in Python" codai_generate "$@"

πŸ“Š Performance Metrics

Typical Response Times

Query TypeTokensAvg TimeMax Time
Simple Question50-1002-3s5s
Function Generation200-5005-8s12s
Class/Module500-100010-15s25s
Complex Program1000-200020-30s45s

Times vary based on hardware. Measured on Intel i5 8th gen, 8GB RAM, SSD.


πŸ› Error Handling

Common Error Codes

Status CodeErrorCauseSolution
200Success--
400Bad RequestInvalid parametersCheck request format
500Server ErrorModel loading failedRestart CodaiPro
503Service UnavailableServer not startedWait for startup
ECONNREFUSEDConnection RefusedServer not runningStart CodaiPro

Error Response Format

{ "error": { "code": "INVALID_PARAMETER", "message": "Temperature must be between 0.1 and 1.0", "details": { "parameter": "temperature", "received": 1.5, "expected": "0.1 - 1.0" } } }

πŸ“š Additional Resources


Ready to integrate? The CodaiPro API makes it easy to build custom tools, extensions, and workflows on top of the AI engine.

πŸ’‘ Pro Tip: Use the Python SDK example as a starting point for your integrations. It handles error checking and provides a clean interface to the API.

Last updated on