Chat in Python on IPython notebook.
Project description
LangCode - Enable Code-of-Thought Reasoning for LLMs
- Multi-round, interactive, smooth programming on Colab and Jupyter notebook.
- Inserting runnable cells with generated, context-aware code.
- Improved interpretability and problem solving ability with code-of-thoughts reasoning, a new neural-symbolic augmentation technique.
CODE of Thoughts: LLMs Following All Instructions by Programming
The auto-regressive large language models (LLMs) generate natural and programming languages by predicting the next tokens, sampling flexible and undeterministic texts.
The Python interpreter reads structured source code and output mostly deterministic results, ensuring the mathematical correctness in reasoning and computation based on the given Python code.
Code-of-Thoughts is a neural-symbolic environment with an LLM and a code interpreter.
- LLM plans the reasoning strategy by generating runnable programs.
- Code interpreter executes the generated code and output desired results.
Code-of-Thoughts can deal with BOTH natural language and programming tasks.
Game of 24 Example
- Instruction:
What is the correct mathematical expression consisting of the numbers {4, 9, 10, 13} in any order, along with the parentheses and arithmetic operations that results in 24? - Program generated by LLM
# Step 1: Import necessary built-in libraries
import itertools
import operator
# Step 2: Define necessary functions that generally solve this type of problem
def find_expression(numbers, target):
operations = [operator.add, operator.sub, operator.mul, operator.truediv]
ops = {operator.add: '+', operator.sub: '-', operator.mul: '*', operator.truediv: '/'}
for permutation in itertools.permutations(numbers):
for ops_combo in itertools.product(operations, repeat=len(numbers)-1):
expression = f"({permutation[0]} {ops[ops_combo[0]]} {permutation[1]})"
for i in range(2, len(numbers)):
expression = f"({expression} {ops[ops_combo[i-1]]} {permutation[i]})"
try:
if eval(expression) == target:
return expression.replace("/", "//")
except ZeroDivisionError:
continue
return None
# Step 3: Define constant variables for the task
numbers = [4, 9, 10, 13]
target = 24
# Step 4: Print an answer in natural language.
expression = find_expression(numbers, target)
if expression:
print(f"The correct mathematical expression that results in 24 is {expression}.")
else:
print("There is no correct mathematical expression that results in 24 using these numbers.")
- Result:
The correct mathematical expression that results in 24 is (((9 + 10) - 13) * 4).
Question answering example
- Instruction:
How many keystrokes are needed to type the numbers from 1 to 104892? - Program generated by LLM
# Step 1: Import necessary built-in libraries
# No need to import
# Step 2: Define necessary functions that generally solve this type of problem
def calculate_keystrokes(number):
keystrokes = 0
for i in range(1, number + 1):
keystrokes += len(str(i))
return keystrokes
# Step 3: Define constant variables for the task
number = 104892
# Step 4: Print an answer in natural language.
keystrokes = calculate_keystrokes(number)
print(f"To type the numbers from 1 to {number}, {keystrokes} keystrokes are needed.")
- Result:
To type the numbers from 1 to 104892, 518247 keystrokes are needed.
Pytorch programming example
- Instruction:
Build a multi-layer perceptron with two hiden states and dropout in Pytorch. - Program generated by LLM
# Step 1: Import necessary built-in libraries
import torch
import torch.nn as nn
import torch.nn.functional as F
# Step 2: Define necessary classes that generally solve this type of problem
class MultiLayerPerceptron(nn.Module):
def __init__(self, input_size, hidden_size1, hidden_size2, num_classes, dropout_rate):
super(MultiLayerPerceptron, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size1)
self.fc2 = nn.Linear(hidden_size1, hidden_size2)
self.fc3 = nn.Linear(hidden_size2, num_classes)
self.dropout = nn.Dropout(dropout_rate)
def forward(self, x):
out = F.relu(self.fc1(x))
out = self.dropout(out)
out = F.relu(self.fc2(out))
out = self.dropout(out)
out = self.fc3(out)
return out
# Step 3: Define constant variables for the task
input_size = 784 # 28x28 pixels for MNIST dataset
hidden_size1 = 500
hidden_size2 = 100
num_classes = 10 # 10 digits for MNIST dataset
dropout_rate = 0.5
# Step 4: Instantiate the network
net = MultiLayerPerceptron(input_size, hidden_size1, hidden_size2, num_classes, dropout_rate)
# Print out the network architecture
print(net)
- Result
MultiLayerPerceptron(
(fc1): Linear(in_features=784, out_features=500, bias=True)
(fc2): Linear(in_features=500, out_features=100, bias=True)
(fc3): Linear(in_features=100, out_features=10, bias=True)
(dropout): Dropout(p=0.5, inplace=False)
)
Multi-turn conversation example
- Instruction 1:
who is the first emperor of the Tang dynasty? - First program generated by LLM
# Step 1: Import necessary built-in libraries
# No need to import
# Step 2: Define necessary functions that generally solve this type of problem
# No need to define a function
# Step 3: Define constant variables for the task
first_emperor_tang_dynasty = "Emperor Gaozu"
# Step 4: Print an answer in natural language.
print(f"The first emperor of the Tang dynasty is {first_emperor_tang_dynasty}.")
- Result 1:
The first emperor of the Tang dynasty is Emperor Gaozu. - Instruction 2:
Tell me more about him. - Second program generated by LLM
# Step 1: Import necessary built-in libraries
# No need to import
# Step 2: Define necessary functions that generally solve this type of problem
# No need to define a function
# Step 3: Define constant variables for the task
first_emperor_tang_dynasty_info = {
"name": "Emperor Gaozu",
"born": "566",
"died": "635",
"reign": "618-626",
"full_name": "Li Yuan",
"era_name": "Wude",
"children": "27, including Emperor Taizong",
"notable_events": "founded the Tang dynasty, expanded the empire, implemented policies promoting economic prosperity and cultural innovation"
}
# Step 4: Print an answer in natural language.
print(f"Emperor Gaozu, born in the year {first_emperor_tang_dynasty_info['born']} and died in the year {first_emperor_tang_dynasty_info['died']}, was the first emperor of the Tang dynasty. His reign, from {first_emperor_tang_dynasty_info['reign']}, is known as the Wude era. His given name was Li Yuan. He had 27 children, including his successor, Emperor Taizong. During his reign, he not only founded the Tang dynasty but also expanded the empire and implemented policies that promoted economic prosperity and cultural innovation.")
- Result 2:
Emperor Gaozu, born in the year 566 and died in the year 635, was the first emperor of the Tang dynasty. His reign, from 618-626, is known as the Wude era. His given name was Li Yuan. He had 27 children, including his successor, Emperor Taizong. During his reign, he not only founded the Tang dynasty but also expanded the empire and implemented policies that promoted economic prosperity and cultural innovation.
Contents
- Supported Environments
- Quick Start with Colab
- Chat with Python by Code Generation
- Hirarchical Instructng by Code Completion
- Contact
Supported Environments
- [Option 1]
Colab- you are already good to go! - [Option 2]
Jupyter notebook 6.Xdeployment - Lang.py does not work in vscode for now.
Quick Start with Colab
Installation
Install the LangCode package in the IPython notebook by runing the following command in a code cell.
!pip install langpy-notebook
As an early release, we first deal with the Python programming language and release the LangPy class. We also look forward to LangJulia, LangR, LangGo, etc.
Define LangPy agent
Import the library and define a Langpy agent. The langpy.auto.get_langpy function automatically identify if the backend is Jupyter 6 or Colab.
from langpy.auto import get_langpy
# clp: an instance of ColabLangPy
clp = get_langpy(
api_key='[YOUR-API-KEY]',
platform='gpt',
model='gpt-4'
)
The platforms we support are gpt (OpenAI) and palm (Google), and you can select models on these platforms. For example, platform='palm',model='text-bison-001' uses Google's text-bison-001.
Generated code to answer any question
Add the following code to a code cell, and run it to generate a program that answers your question. For example, the Game of 24 task.
# mode: control history reading to enable multi-turn programming
# hist_mode = 'all' (default) -> read all cell history
# hist_mode = 'single' -> only read the current instruction
# hist_mode = INTERGE -> Read {INTEGER} history cells.
# hist_mode = 0 is equivalent to hist_mode = 'single'
numbers = '{4, 9, 10, 13}'
target = 24
clp.generate(
f'What is the correct mathematical expression consisting of the numbers {numbers} in any order, along with the parentheses and arithmetic operations that results in {target}?',
hist_mode = 'single'
)
The generated program would be placed in
- in a scratch cell (Colab), or
- the next code cell (Jupyter notebook 6)
Run the generated Code
Run the generated code, you'll get the answer in natural language
We did not use any Game of 24 example to prompt GPT-4, but it still gets the correct answer (4 - 10) * (9 - 13) with only sampling one output! This is more efficient than tree-of-thoughts.
Code Completion
The LangPy agent can also complete code that is not finished yet.
Run the code in the upper cell will complete the code in the lower cell, getting the following program
# Step 1: Import necessary built-in libraries
import torch
import torch.nn as nn
# Step 2: Define necessary functions that generally solve this type of problem
class MLP(nn.Module):
def __init__(self):
super(MLP, self).__init__()
self.layers = nn.Sequential(
nn.Linear(10, 5),
nn.ReLU(),
nn.Linear(5, 2)
)
def forward(self, x):
x = self.layers(x)
return x
# Step 3: Instantiate the defined class
mlp = MLP()
# Step 4: Print an answer in natural language.
print(f"A multi-layer perceptron (MLP) has been defined. It consists of an input layer, a hidden layer with 5 neurons activated by ReLU function, and an output layer with 2 neurons. The input layer has 10 neurons, representing the dimension of the input vectors.")
Note that both the code and comment in the generated cell can be edited for another completion, enabling more flexible and direct instructions.
CSV File Processing
As an early release, we provide basic CSV file processing ability. Running the following code,
clp.process_csv('example_data.csv')
a new code cell will be inserted:
# First three rows of the input file:
# placeName,placeDcid,xDate,xValue-DifferenceRelativeToBaseDate2006_Max_Temperature_RCP45,yDate,yValue-Percent_Person_WithCoronaryHeartDisease,xPopulation-Count_Person,yPopulation-Count_Person
# "Autauga County, AL",geoId/01001,2050-06,-1.15811100000001,2020,6.3,59095,56145
# "Baldwin County, AL",geoId/01003,2050-06,-1.073211,2020,5.9,239294,229287
# ...
file_name = 'example_data.csv'
input_file = open(file_name)
Running this code snipet, you can further analyze the data in the csv file using Lang.py. For example, asking Lang.py to generate code for visualization:
# hist_mode = 'all' allows Lang.py to read previous cells
clp.generate('Visualize the correlation of xData and yData of Alabama in the file.', hist_mode = 'all')
The generated code and execution results are shown below.
Chat with Python Through Code Generation
We currently offer three parameters for LangPy.generate(instruction, mode = 'all', http = 'optional')
instruction [str]: The input question / request in natural language.mode [str or int]: Wether or note read previous code and markdown cellsmode = 'all': Read all previous cellsmode = 'single': Just read the instruction in the current cellmode = [int]: The number of latest history cells to read
http: Wether allow the generated code to include http requests.http = 'optional': (Default) Lang.py decides itself about sending HTTP requests or not.http = 'forbid': Lang.py will try to avoid generated HTTP requesting code.http = 'force': Lang.py will try to generated code that sends HTTP requests to solve the given question.
Hirarchical Instruction Following Through Code Completion
In the previous example we have shown,
Lang.py implemented a Multi-layer perceptron (MLP) with Pytorch using the following code
# Step 1: Import necessary built-in libraries
import torch
import torch.nn as nn
# Step 2: Define necessary functions that generally solve this type of problem
class MLP(nn.Module):
def __init__(self):
super(MLP, self).__init__()
self.layers = nn.Sequential(
nn.Linear(10, 5),
nn.ReLU(),
nn.Linear(5, 2)
)
def forward(self, x):
x = self.layers(x)
return x
# Step 3: Instantiate the defined class
mlp = MLP()
# Step 4: Print an answer in natural language.
print(f"A multi-layer perceptron (MLP) has been defined. It consists of an input layer, a hidden layer with 5 neurons activated by ReLU function, and an output layer with 2 neurons. The input layer has 10 neurons, representing the dimension of the input vectors.")
Unlike ChatGPT that requires user to write a good instruction at once, Lang.py allows adding multiple instructions in different places for one task. Editting the code for completion can guide model to generate different code. For example,
# Step 1: Import necessary built-in libraries
import torch
import torch.nn as nn
# Step 2: Define a multi-layer perceptron that has three linear layers and uses tanh activation function.
The code completion result would be
# Step 1: Import necessary built-in libraries
import torch
import torch.nn as nn
# Step 2: Define a multi-layer perceptron that has three linear layers and uses tanh activation function.
# Python program:
class MultiLayerPerceptron(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(MultiLayerPerceptron, self).__init__()
self.layer1 = nn.Linear(input_size, hidden_size)
self.layer2 = nn.Linear(hidden_size, hidden_size)
self.layer3 = nn.Linear(hidden_size, output_size)
self.tanh = nn.Tanh()
def forward(self, x):
x = self.tanh(self.layer1(x))
x = self.tanh(self.layer2(x))
x = self.layer3(x)
return x
# Step 3: Print an answer in natural language.
print("A multi-layer perceptron is a type of artificial neural network. It has an input layer, one or more hidden layers, and an output layer. In each layer, the inputs are multiplied by weights, summed, and passed through an activation function. In this implementation, the activation function is the hyperbolic tangent function (tanh).")
Besides the comments, you can also control the direction of the generated code by modifying the imported libraries. For example, changing import torch to import tensorflow.keras as keras. Your are encourged to explore more possibilities!
Contact
If there is any question, feel free to post an issue or contact Hongyin Luo at hyluo [at] mit [dot] edu.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langpy-notebook-0.4.tar.gz.
File metadata
- Download URL: langpy-notebook-0.4.tar.gz
- Upload date:
- Size: 16.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e6dd5e8c5b0266548f622ec2eb298f12f749bdf2f5012bff02962c419de0da96
|
|
| MD5 |
d5d955c4d2ae734b979f31c3e0654845
|
|
| BLAKE2b-256 |
01763536ff118f184c1f7e527c54fcdcd29a2c2676d31179fa7de573c4ab29f2
|
File details
Details for the file langpy_notebook-0.4-py3-none-any.whl.
File metadata
- Download URL: langpy_notebook-0.4-py3-none-any.whl
- Upload date:
- Size: 13.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
155d08e5041bd8cc7748d427b445f2fce556a04d5cc1a879de2d4513bf6515f3
|
|
| MD5 |
7ebe5b3d7af54bfe3b36a69503f505cf
|
|
| BLAKE2b-256 |
f528e1336559ff4ee956ca49afa9d7c6705504ccfe4f0c32390cc5c0ef6a2098
|