AI Code Playground

Want to build your own software with code generation abilities? CodeChain is a library for generating code with LLMs. Here's how to get started quickly in Python:


Install CodeChain:

pip install codechain openai python-dotenv      

Add your OpenAI API key to an environment variable:



Create a Python script to run the generation:

from codechain.generation import ModifyCodeChain
from langchain.chat_models import ChatOpenAI
from dotenv import load_dotenv

# Load API key

# Setup the generator
generator = ModifyCodeChain.from_instruction(
    "Return a complete and correct version of the given code.",
    ChatOpenAI(model="gpt-3.5-turbo", temperature=0.2)

# Generate
result ="""
def fibonacci(n):
# Generate the n-th fibonacci number.


CodeChain is composable with LangChain, thus has support for all Langchain chat models including OpenAI, Anthropic, Azure, Palm, and Llama.