CodeChain

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:

Setup

Install CodeChain:

pip install codechain openai python-dotenv      

Add your OpenAI API key to an environment variable:

echo "OPENAI_API_KEY=sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" > .env       

Usage

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
load_dotenv()

# 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 = generator.run("""
def fibonacci(n):
# Generate the n-th fibonacci number.
""")

print(result)

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