{"id":22015364,"url":"https://github.com/luohongyin/langcode","last_synced_at":"2026-03-15T01:45:07.984Z","repository":{"id":195899975,"uuid":"693871673","full_name":"luohongyin/LangCode","owner":"luohongyin","description":"LangCode - Improving alignment and reasoning of large language models (LLMs) with natural language embedded program (NLEP).","archived":false,"fork":false,"pushed_at":"2023-09-22T16:54:47.000Z","size":2300,"stargazers_count":42,"open_issues_count":0,"forks_count":7,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-03-31T04:41:13.530Z","etag":null,"topics":["colab","gpt4","jupyter-notebook","llm","nlep","reasoning"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/luohongyin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-19T22:08:57.000Z","updated_at":"2025-01-09T23:04:20.000Z","dependencies_parsed_at":"2023-09-20T03:29:42.772Z","dependency_job_id":"20ce1dd4-adff-4ed2-913f-45f37f9eaaf5","html_url":"https://github.com/luohongyin/LangCode","commit_stats":null,"previous_names":["luohongyin/langcode"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luohongyin%2FLangCode","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luohongyin%2FLangCode/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luohongyin%2FLangCode/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luohongyin%2FLangCode/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/luohongyin","download_url":"https://codeload.github.com/luohongyin/LangCode/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252793564,"owners_count":21805054,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["colab","gpt4","jupyter-notebook","llm","nlep","reasoning"],"created_at":"2024-11-30T04:21:30.987Z","updated_at":"2026-03-15T01:45:02.955Z","avatar_url":"https://github.com/luohongyin.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LangCode - Enable NLEP Reasoning for LLMs\n- Implementation and iPython toolkit for the paper [Natural Language Embedded Programs for Hybrid Language Symbolic Reasoning](https://arxiv.org/pdf/2309.10814.pdf).\n- Improved interpretability and problem solving ability with **natural language embedded programs (NLEP)**, a hybrid language-symbolic reasoning technique.\n- Multi-round, interactive, smooth programming on Colab and Jupyter notebook.\n\n\u003cdiv align=\"center\"\u003e\n\n\u003cimg src=\"images/demo.png\" width=\"280px\"\u003e\n\nRespresenting the Language of Thought\n\n[Hongyin Luo](https://luohongyin.github.io/) @ MIT *[hyluo at mit dot edu]*\n\n\u003c/div\u003e\n\n## NLEP: Natural Language Embedded Programming\nThe auto-regressive large language models (LLMs) generate natural and programming languages by scoring the next tokens, sampling flexible but undeterministic texts.\n\nThe 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.\n\n`NLEP` is an end-to-end, hybrid language-symbolic framework with an LLM and a code interpreter.\n- LLM plans for the reasoning by generating step-by-step programs.\n- Code interpreter executes the generated program and prints desired responses.\n- No further edition needed for program standard outputs.\n\n`NLEP` can deal with natural language, math, symbolic, and programming tasks.\n\u003cdetails\u003e\n\u003csummary\u003eGame of 24 Example\u003c/summary\u003e\n\n- 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?`\n- Program generated by LLM\n```python\n# Step 1: Import necessary built-in libraries\nimport itertools\nimport operator\n\n# Step 2: Define necessary functions that generally solve this type of problem\ndef find_expression(numbers, target):\n    operations = [operator.add, operator.sub, operator.mul, operator.truediv]\n    ops = {operator.add: '+', operator.sub: '-', operator.mul: '*', operator.truediv: '/'}\n\n    for permutation in itertools.permutations(numbers):\n        for ops_combo in itertools.product(operations, repeat=len(numbers)-1):\n            expression = f\"({permutation[0]} {ops[ops_combo[0]]} {permutation[1]})\"\n            for i in range(2, len(numbers)):\n                expression = f\"({expression} {ops[ops_combo[i-1]]} {permutation[i]})\"\n            try:\n                if eval(expression) == target:\n                    return expression.replace(\"/\", \"//\")\n            except ZeroDivisionError:\n                continue\n    return None\n\n# Step 3: Define constant variables for the task\nnumbers = [4, 9, 10, 13]\ntarget = 24\n\n# Step 4: Print an answer in natural language.\nexpression = find_expression(numbers, target)\nif expression:\n    print(f\"The correct mathematical expression that results in 24 is {expression}.\")\nelse:\n    print(\"There is no correct mathematical expression that results in 24 using these numbers.\")\n```\n\n- Result: `The correct mathematical expression that results in 24 is (((9 + 10) - 13) * 4).`\n\n\u003c/details\u003e\n\u003cdetails\u003e\n\u003csummary\u003eQuestion answering example\u003c/summary\u003e\n\n- Instruction: `How many keystrokes are needed to type the numbers from 1 to 104892?`\n- Program generated by LLM\n```python\n# Step 1: Import necessary built-in libraries\n# No need to import\n\n# Step 2: Define necessary functions that generally solve this type of problem\ndef calculate_keystrokes(number):\n    keystrokes = 0\n    for i in range(1, number + 1):\n        keystrokes += len(str(i))\n    return keystrokes\n\n# Step 3: Define constant variables for the task\nnumber = 104892\n\n# Step 4: Print an answer in natural language.\nkeystrokes = calculate_keystrokes(number)\nprint(f\"To type the numbers from 1 to {number}, {keystrokes} keystrokes are needed.\")\n```\n- Result: `To type the numbers from 1 to 104892, 518247 keystrokes are needed.`\n\u003c/details\u003e\n\u003cdetails\u003e\n\u003csummary\u003ePytorch programming example\u003c/summary\u003e\n\n- Instruction: `Build a multi-layer perceptron with two hiden states and dropout in Pytorch.`\n- Program generated by LLM\n```python\n# Step 1: Import necessary built-in libraries\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\n# Step 2: Define necessary classes that generally solve this type of problem\nclass MultiLayerPerceptron(nn.Module):\n    def __init__(self, input_size, hidden_size1, hidden_size2, num_classes, dropout_rate):\n        super(MultiLayerPerceptron, self).__init__()\n        self.fc1 = nn.Linear(input_size, hidden_size1)\n        self.fc2 = nn.Linear(hidden_size1, hidden_size2)\n        self.fc3 = nn.Linear(hidden_size2, num_classes)\n        self.dropout = nn.Dropout(dropout_rate)\n\n    def forward(self, x):\n        out = F.relu(self.fc1(x))\n        out = self.dropout(out)\n        out = F.relu(self.fc2(out))\n        out = self.dropout(out)\n        out = self.fc3(out)\n        return out\n\n# Step 3: Define constant variables for the task\ninput_size = 784 # 28x28 pixels for MNIST dataset\nhidden_size1 = 500\nhidden_size2 = 100\nnum_classes = 10 # 10 digits for MNIST dataset\ndropout_rate = 0.5\n\n# Step 4: Instantiate the network\nnet = MultiLayerPerceptron(input_size, hidden_size1, hidden_size2, num_classes, dropout_rate)\n\n# Print out the network architecture\nprint(net)\n```\n- Result\n```\nMultiLayerPerceptron(\n  (fc1): Linear(in_features=784, out_features=500, bias=True)\n  (fc2): Linear(in_features=500, out_features=100, bias=True)\n  (fc3): Linear(in_features=100, out_features=10, bias=True)\n  (dropout): Dropout(p=0.5, inplace=False)\n)\n```\n\n\u003c/details\u003e\n\u003cdetails\u003e\n\u003csummary\u003eMulti-turn conversation example\u003c/summary\u003e\n\n- Instruction 1: `who is the first emperor of the Tang dynasty?`\n- First program generated by LLM\n```python\n# Step 1: Import necessary built-in libraries\n# No need to import\n\n# Step 2: Define necessary functions that generally solve this type of problem\n# No need to define a function\n\n# Step 3: Define constant variables for the task\nfirst_emperor_tang_dynasty = \"Emperor Gaozu\"\n\n# Step 4: Print an answer in natural language.\nprint(f\"The first emperor of the Tang dynasty is {first_emperor_tang_dynasty}.\")\n```\n- Result 1: `The first emperor of the Tang dynasty is Emperor Gaozu.`\n- Instruction 2: `Tell me more about him.`\n- Second program generated by LLM\n```python\n# Step 1: Import necessary built-in libraries\n# No need to import\n\n# Step 2: Define necessary functions that generally solve this type of problem\n# No need to define a function\n\n# Step 3: Define constant variables for the task\nfirst_emperor_tang_dynasty_info = {\n    \"name\": \"Emperor Gaozu\",\n    \"born\": \"566\",\n    \"died\": \"635\",\n    \"reign\": \"618-626\",\n    \"full_name\": \"Li Yuan\",\n    \"era_name\": \"Wude\",\n    \"children\": \"27, including Emperor Taizong\",\n    \"notable_events\": \"founded the Tang dynasty, expanded the empire, implemented policies promoting economic prosperity and cultural innovation\"\n}\n\n# Step 4: Print an answer in natural language.\nprint(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.\")\n```\n- 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.`\n\n\u003c/details\u003e\n\n## Contents\n- [Supported Environments](#supported-environments)\n- [Config API Endpoint](#config-api-endpoint)\n- [Quick Start with Colab](#quick-start-with-colab)\n    - [Installation](#installation)\n    - [Define LangPy agent](#define-langpy-agent)\n    - [Code generation](#generated-code-to-answer-any-question)\n    - [Run generated code](#run-the-generated-code)\n    - [Code completion](#code-completion)\n    - [CSV file processing](#csv-file-processing)\n- [Chat with Python by Code Generation](#chat-with-python-through-code-generation)\n- [Hirarchical Instructng by Code Completion](#hirarchical-instruction-following-through-code-completion)\n- [Contact](#contact)\n\n## Supported Environments\n- [Option 1] `Colab` - Recommended! you are already good to go!\n    - [Example Colab notebook](https://colab.research.google.com/drive/132vl1t3MJlq8ekzMNycGejvBTcwoDcDM?usp=sharing) might be helpfu!\n- [Option 2] `Jupyter notebook 6.X` deployment\n    - [Create a conda enviroment and launch Jupyter notebook](documents/conda_jupyter.md)\n- LangCode does not work in vscode for now.\n\n## Customizing NLEP Generation API\nBy default, we provide an API server that generates NLEPs without saving users' API keys. We will continue improving the prompt strategy and the quality of generated NELPs. However, we provide an option for customized servers.\n\nPlease refer to [LangCode/server/](https://github.com/luohongyin/LangCode/tree/main/server) for the server setup. We provide complete code and minimal prompt to generate high-quality NLEP for different tasks. Use the following command to switch to the preferred server.\n```python\nclp.config_api_endpoint()\n# Then input your server url in the following format:\n# http://{HOSTNAME}:{PORT}/items/0\n```\n\nOur server timeouts at 30s since the GPT-4 API is not efficient enough, but the custom server we provide does not involve this limitation.\n\n## Quick Start with Colab\n\n### Installation\nInstall the LangCode package in the IPython notebook by runing the following command in a code cell.\n```bash\n!pip install langpy-notebook\n```\nAs 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.\n\n### Define LangPy agent\nImport the library and define a Langpy agent. The `langpy.auto.get_langpy` function automatically identify if the backend is Jupyter 6 or Colab.\n```python\nfrom langpy.auto import get_langpy\n\n# clp: an instance of ColabLangPy\nclp = get_langpy(\n    api_key='YOUR-API-KEY',\n    platform='gpt',\n    model='gpt-4'\n)\n```\nThe 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`.\n\n### Generated code to answer *any* question\nAdd 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.\n\n```python\n# mode: control history reading to enable multi-turn programming\n\n# hist_mode = 'all' (default)        -\u003e read all cell history\n# hist_mode = 'single'               -\u003e only read the current instruction\n# hist_mode = INTERGER               -\u003e Read {INTEGER} history cells.\n# hist_mode = 0 is equivalent to hist_mode = 'single'\n\nnumbers = '{4, 9, 10, 13}'\ntarget = 24\nclp.generate(\n    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}?',\n    hist_mode = 'single'\n)\n```\nThe generated program would be placed in\n- in a scratch cell (Colab), or\n- the next code cell (Jupyter notebook 6)\n\n### Run the generated Code\nRun the generated code, you'll get the answer in **natural language**\n\n\u003cimg src=\"images/game24.png\"\u003e\n\nWe 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.\n\n### Code Completion\nThe `LangPy` agent can also complete code that is not finished yet.\n\u003cimg src=\"images/cpl_example.png\"\u003e\nRun the code in the upper cell will complete the code in the lower cell, getting the following program\n```python\n# Step 1: Import necessary built-in libraries\nimport torch\nimport torch.nn as nn\n\n# Step 2: Define necessary functions that generally solve this type of problem\nclass MLP(nn.Module):\n    def __init__(self):\n        super(MLP, self).__init__()\n        self.layers = nn.Sequential(\n            nn.Linear(10, 5),\n            nn.ReLU(),\n            nn.Linear(5, 2)\n        )\n\n    def forward(self, x):\n        x = self.layers(x)\n        return x\n\n# Step 3: Instantiate the defined class\nmlp = MLP()\n\n# Step 4: Print an answer in natural language.\nprint(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.\")\n```\nNote that both the code and comment in the generated cell can be edited for another completion, enabling more flexible and direct instructions.\n\n### CSV File Processing\nAs an early release, we provide basic CSV file processing ability. Running the following code,\n```python\nclp.preview_data(file_name = 'example_data.csv', data_type = 'csv)\n```\na new code cell will be inserted:\n```python\n# First three rows of the input file:\n# placeName,placeDcid,xDate,xValue-DifferenceRelativeToBaseDate2006_Max_Temperature_RCP45,yDate,yValue-Percent_Person_WithCoronaryHeartDisease,xPopulation-Count_Person,yPopulation-Count_Person\n# \"Autauga County, AL\",geoId/01001,2050-06,-1.15811100000001,2020,6.3,59095,56145\n# \"Baldwin County, AL\",geoId/01003,2050-06,-1.073211,2020,5.9,239294,229287\n# ...\nfile_name = 'example_data.csv'\ninput_file = open(file_name)\n```\nRunning this code snipet, you can further analyze the data in the csv file using LangCode. For example, asking LangCode to generate code for visualization:\n```python\n# hist_mode = 'all' allows LangCode to read previous cells\n\nclp.generate('Visualize the correlation of xData and yData of Alabama in the file.', hist_mode = 'all')\n```\nThe generated code and execution results are shown below.\n\u003cimg src=\"images/csv_example.png\"\u003e\n\n## Chat with Python Through Code Generation\nWe currently offer three parameters for `LangPy.generate(instruction, mode = 'all', http = 'optional')`\n- `instruction [str]`: The input question / request in natural language. \n- `mode [str or int]`: Wether or note read previous code and markdown cells\n    - `mode = 'all'`: Read all previous cells\n    - `mode = 'single'`: Just read the instruction in the current cell\n    - `mode = [int]`: The number of latest history cells to read\n- `http`: Wether allow the generated code to include http requests.\n    - `http = 'optional'`: (Default) LangCode decides itself about sending HTTP requests or not.\n    - `http = 'forbid'`: LangCode will try to avoid generated HTTP requesting code.\n    - `http = 'force'`: LangCode will try to generated code that sends HTTP requests to solve the given question.\n\n## Hirarchical Instruction Following Through Code Completion\nIn the previous example we have shown,\n\u003cimg src=\"images/cpl_example.png\"\u003e\nLangCode implemented a Multi-layer perceptron (MLP) with Pytorch using the following code\n```python\n# Step 1: Import necessary built-in libraries\nimport torch\nimport torch.nn as nn\n\n# Step 2: Define necessary functions that generally solve this type of problem\nclass MLP(nn.Module):\n    def __init__(self):\n        super(MLP, self).__init__()\n        self.layers = nn.Sequential(\n            nn.Linear(10, 5),\n            nn.ReLU(),\n            nn.Linear(5, 2)\n        )\n\n    def forward(self, x):\n        x = self.layers(x)\n        return x\n\n# Step 3: Instantiate the defined class\nmlp = MLP()\n\n# Step 4: Print an answer in natural language.\nprint(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.\")\n```\n\nUnlike ChatGPT that requires user to write a good instruction at once, LangCode allows adding multiple instructions in different places for one task. Editting the code for completion can guide model to generate different code. For example,\n\n```python\n# Step 1: Import necessary built-in libraries\nimport torch\nimport torch.nn as nn\n\n# Step 2: Define a multi-layer perceptron that has three linear layers and uses tanh activation function.\n```\nThe code completion result would be\n```python\n# Step 1: Import necessary built-in libraries\nimport torch\nimport torch.nn as nn\n\n# Step 2: Define a multi-layer perceptron that has three linear layers and uses tanh activation function.\n# Python program:\nclass MultiLayerPerceptron(nn.Module):\n    def __init__(self, input_size, hidden_size, output_size):\n        super(MultiLayerPerceptron, self).__init__()\n        self.layer1 = nn.Linear(input_size, hidden_size)\n        self.layer2 = nn.Linear(hidden_size, hidden_size)\n        self.layer3 = nn.Linear(hidden_size, output_size)\n        self.tanh = nn.Tanh()\n\n    def forward(self, x):\n        x = self.tanh(self.layer1(x))\n        x = self.tanh(self.layer2(x))\n        x = self.layer3(x)\n        return x\n\n# Step 3: Print an answer in natural language.\nprint(\"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).\")\n```\n\nBesides 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!\n\n## Contact\n\nIf there is any question, feel free to post an issue or contact Hongyin Luo at *hyluo [at] mit [dot] edu*.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluohongyin%2Flangcode","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluohongyin%2Flangcode","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluohongyin%2Flangcode/lists"}