Package: langpy-notebook
LangCode - Enable Code-of-Thought Reasoning for LLMsMulti-round, interactive, smooth programming on Colab and Jupyter notebook.Inserting runnable cells with generated, context-aware code.Improved interpretability and problem solving ability withcode-of-thoughts reasoning, a new neural-symbolic augmentation technique.Explore and Control LLM'sCODE OF THOUGHTSHongyin Luo@ MIT[hyluo at mit dot edu]CODE of Thoughts: LLMs Following All Instructions by ProgrammingThe 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 mostlydeterministicresults, ensuring the mathematical correctness in reasoning and computation based on the given Python code.Code-of-Thoughtsis 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-Thoughtscan deal with BOTH natural language and programming tasks.Game of 24 ExampleInstruction: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 librariesimportitertoolsimportoperator# Step 2: Define necessary functions that generally solve this type of problemdeffind_expression(numbers,target):operations=[operator.add,operator.sub,operator.mul,operator.truediv]ops={operator.add:'+',operator.sub:'-',operator.mul:'*',operator.truediv:'/'}forpermutationinitertools.permutations(numbers):forops_comboinitertools.product(operations,repeat=len(numbers)-1):expression=f"({permutation[0]}{ops[ops_combo[0]]}{permutation[1]})"foriinrange(2,len(numbers)):expression=f"({expression}{ops[ops_combo[i-1]]}{permutation[i]})"try:ifeval(expression)==target:returnexpression.replace("/","//")exceptZeroDivisionError:continuereturnNone# Step 3: Define constant variables for the tasknumbers=[4,9,10,13]target=24# Step 4: Print an answer in natural language.expression=find_expression(numbers,target)ifexpression: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 exampleInstruction: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 problemdefcalculate_keystrokes(number):keystrokes=0foriinrange(1,number+1):keystrokes+=len(str(i))returnkeystrokes# Step 3: Define constant variables for the tasknumber=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 exampleInstruction:Build a multi-layer perceptron with two hiden states and dropout in Pytorch.Program generated by LLM# Step 1: Import necessary built-in librariesimporttorchimporttorch.nnasnnimporttorch.nn.functionalasF# Step 2: Define necessary classes that generally solve this type of problemclassMultiLayerPerceptron(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)defforward(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)returnout# Step 3: Define constant variables for the taskinput_size=784# 28x28 pixels for MNIST datasethidden_size1=500hidden_size2=100num_classes=10# 10 digits for MNIST datasetdropout_rate=0.5# Step 4: Instantiate the networknet=MultiLayerPerceptron(input_size,hidden_size1,hidden_size2,num_classes,dropout_rate)# Print out the network architectureprint(net)ResultMultiLayerPerceptron(
  (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 exampleInstruction 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 taskfirst_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 taskfirst_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.ContentsSupported EnvironmentsQuick Start with ColabInstallationDefine LangPy agentCode generationRun generated codeCode completionCSV file processingChat with Python by Code GenerationHirarchical Instructng by Code CompletionContactSupported Environments[Option 1]Colab- you are already good to go![Option 2]Jupyter notebook 6.XdeploymentCreate a conda enviroment and launch Jupyter notebookLang.py does not work in vscode for now.Quick Start with ColabInstallationInstall the LangCode package in the IPython notebook by runing the following command in a code cell.!pipinstalllangpy-notebookAs an early release, we first deal with thePythonprogramming language and release theLangPyclass. We also look forward toLangJulia,LangR,LangGo, etc.Define LangPy agentImport the library and define a Langpy agent. Thelangpy.auto.get_langpyfunction automatically identify if the backend is Jupyter 6 or Colab.fromlangpy.autoimportget_langpy# clp: an instance of ColabLangPyclp=get_langpy(api_key='[YOUR-API-KEY]',platform='gpt',model='gpt-4')The platforms we support aregpt(OpenAI) andpalm(Google), and you can select models on these platforms. For example,platform='palm',model='text-bison-001'uses Google'stext-bison-001.Generated code to answeranyquestionAdd the following code to a code cell, and run it to generate a program that answers your question. For example, theGame of 24task.# 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=24clp.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 inin a scratch cell (Colab), orthe next code cell (Jupyter notebook 6)Run the generated CodeRun the generated code, you'll get the answer innatural languageWe 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 samplingoneoutput! This is more efficient than tree-of-thoughts.Code CompletionTheLangPyagent 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 librariesimporttorchimporttorch.nnasnn# Step 2: Define necessary functions that generally solve this type of problemclassMLP(nn.Module):def__init__(self):super(MLP,self).__init__()self.layers=nn.Sequential(nn.Linear(10,5),nn.ReLU(),nn.Linear(5,2))defforward(self,x):x=self.layers(x)returnx# Step 3: Instantiate the defined classmlp=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 ProcessingAs 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 cellsclp.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 GenerationWe currently offer three parameters forLangPy.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 readhttp: 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 CompletionIn 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 librariesimporttorchimporttorch.nnasnn# Step 2: Define necessary functions that generally solve this type of problemclassMLP(nn.Module):def__init__(self):super(MLP,self).__init__()self.layers=nn.Sequential(nn.Linear(10,5),nn.ReLU(),nn.Linear(5,2))defforward(self,x):x=self.layers(x)returnx# Step 3: Instantiate the defined classmlp=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 librariesimporttorchimporttorch.nnasnn# 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 librariesimporttorchimporttorch.nnasnn# Step 2: Define a multi-layer perceptron that has three linear layers and uses tanh activation function.# Python program:classMultiLayerPerceptron(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()defforward(self,x):x=self.tanh(self.layer1(x))x=self.tanh(self.layer2(x))x=self.layer3(x)returnx# 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, changingimport torchtoimport tensorflow.keras as keras. Your are encourged to explore more possibilities!ContactIf there is any question, feel free to post an issue or contact Hongyin Luo athyluo [at] mit [dot] edu.