LLMs are very capable of generating and processing code. Here are a couple of patterns to consider when using them for code generation.

Generating code

A key challenge with generating code is that models have a tendency to add a lot of helping text, while you might only want the code. A technique that helps is to structure the output to separate instructions and code.

from opperai import AsyncOpper
from pydantic import BaseModel
import asyncio

opper = AsyncOpper()

class CodeGenerationOutput(BaseModel):
    explanation: str
    code: str

async def main():
    result, _ = await opper.call(
        input="Generate a Python function to calculate the factorial of a number.",
        output_type=CodeGenerationOutput
    )

    print(f"Explanation:\n{result.explanation}\n")
    print(f"Generated Code:\n{result.code}")

# Run the async function
asyncio.run(main())

# Example output:
# Explanation:
# This function calculates the factorial of a given number using recursion.
# The factorial of a number n is the product of all positive integers from 1 to n.
# For example, factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120.
#
# Generated Code:
# def factorial(n):
#     if n == 0 or n == 1:
#         return 1
#     else:
#         return n * factorial(n - 1)