curl -X POST "https://api.tensorone.ai/v2/ai/code-generation" \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "prompt": "Create a function that implements binary search on a sorted array", "language": "python", "model": "codellama-34b", "taskType": "generation", "style": "clean", "includeComments": true, "includeTests": true }'
Copy
{ "generatedCode": "def binary_search(arr, target):\n \"\"\"\n Perform binary search on a sorted array.\n \n Args:\n arr (list): Sorted array to search in\n target: Element to search for\n \n Returns:\n int: Index of target if found, -1 otherwise\n \"\"\"\n left, right = 0, len(arr) - 1\n \n while left <= right:\n mid = (left + right) // 2\n \n if arr[mid] == target:\n return mid\n elif arr[mid] < target:\n left = mid + 1\n else:\n right = mid - 1\n \n return -1", "language": "python", "explanation": "This function implements binary search algorithm to find a target element in a sorted array. It uses the divide-and-conquer approach, repeatedly dividing the search space in half until the target is found or the search space is exhausted.", "tests": "import unittest\n\nclass TestBinarySearch(unittest.TestCase):\n def setUp(self):\n self.sorted_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]\n \n def test_found_element(self):\n self.assertEqual(binary_search(self.sorted_array, 7), 3)\n self.assertEqual(binary_search(self.sorted_array, 1), 0)\n self.assertEqual(binary_search(self.sorted_array, 19), 9)\n \n def test_not_found_element(self):\n self.assertEqual(binary_search(self.sorted_array, 4), -1)\n self.assertEqual(binary_search(self.sorted_array, 20), -1)\n \n def test_empty_array(self):\n self.assertEqual(binary_search([], 5), -1)\n\nif __name__ == '__main__':\n unittest.main()", "suggestions": [ "Consider adding type hints for better code documentation", "Add input validation to ensure the array is sorted", "Consider using bisect module for built-in binary search functionality" ], "dependencies": [], "complexity": { "timeComplexity": "O(log n)", "spaceComplexity": "O(1)", "cyclomaticComplexity": 4 }, "metadata": { "model": "codellama-34b", "tokensGenerated": 245, "generationTime": 2.3 }}
AI Services
Code Generation
Generate, complete, and refactor code using specialized AI models trained on programming languages
Copy
curl -X POST "https://api.tensorone.ai/v2/ai/code-generation" \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "prompt": "Create a function that implements binary search on a sorted array", "language": "python", "model": "codellama-34b", "taskType": "generation", "style": "clean", "includeComments": true, "includeTests": true }'
Copy
{ "generatedCode": "def binary_search(arr, target):\n \"\"\"\n Perform binary search on a sorted array.\n \n Args:\n arr (list): Sorted array to search in\n target: Element to search for\n \n Returns:\n int: Index of target if found, -1 otherwise\n \"\"\"\n left, right = 0, len(arr) - 1\n \n while left <= right:\n mid = (left + right) // 2\n \n if arr[mid] == target:\n return mid\n elif arr[mid] < target:\n left = mid + 1\n else:\n right = mid - 1\n \n return -1", "language": "python", "explanation": "This function implements binary search algorithm to find a target element in a sorted array. It uses the divide-and-conquer approach, repeatedly dividing the search space in half until the target is found or the search space is exhausted.", "tests": "import unittest\n\nclass TestBinarySearch(unittest.TestCase):\n def setUp(self):\n self.sorted_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]\n \n def test_found_element(self):\n self.assertEqual(binary_search(self.sorted_array, 7), 3)\n self.assertEqual(binary_search(self.sorted_array, 1), 0)\n self.assertEqual(binary_search(self.sorted_array, 19), 9)\n \n def test_not_found_element(self):\n self.assertEqual(binary_search(self.sorted_array, 4), -1)\n self.assertEqual(binary_search(self.sorted_array, 20), -1)\n \n def test_empty_array(self):\n self.assertEqual(binary_search([], 5), -1)\n\nif __name__ == '__main__':\n unittest.main()", "suggestions": [ "Consider adding type hints for better code documentation", "Add input validation to ensure the array is sorted", "Consider using bisect module for built-in binary search functionality" ], "dependencies": [], "complexity": { "timeComplexity": "O(log n)", "spaceComplexity": "O(1)", "cyclomaticComplexity": 4 }, "metadata": { "model": "codellama-34b", "tokensGenerated": 245, "generationTime": 2.3 }}
Generate high-quality code in multiple programming languages using AI models specifically trained on code datasets. Perfect for code completion, function generation, debugging, and automated refactoring.
def batch_generate_code(prompts, language="python"): results = [] for prompt in prompts: response = requests.post( "https://api.tensorone.ai/v2/ai/code-generation", headers={"Authorization": "Bearer YOUR_API_KEY"}, json={ "prompt": prompt, "language": language, "model": "codellama-34b", "style": "clean", "includeComments": True } ) results.append(response.json()) return results# Generate a complete data structureprompts = [ "Implement a Node class for a linked list", "Implement a LinkedList class with add, remove, and find methods", "Add a method to reverse the linked list", "Add a method to find the middle element of the linked list"]results = batch_generate_code(prompts)for i, result in enumerate(results): print(f"Function {i+1}:") print(result['generatedCode']) print()
Advanced Models: $0.02 per 1K tokens (70B+ parameter models)
Code Analysis: Additional $0.005 per analysis
Batch Processing: 15% discount for 100+ generations
Enterprise: Custom pricing for high-volume usage
Code generation typically completes in 2-10 seconds depending on complexity and model size. Longer or more complex requests may take up to 30 seconds.
For best results, provide specific requirements, expected input/output, and any relevant context from your existing codebase. The more context you provide, the better the generated code will integrate with your project.