1. How to realize
This project is a GitHub open source project and is very easy to use. Just download it, install the dependencies, and run the corresponding Python script.
python3 python2cppconverter.py
Take the following Python code as an example
def add_something(x, y): print("casually adding some stuff together") z = x + y return z if __name__ == "__main__": print('Okay, lets go') print(add_something(5, 2))
The conversion to C++ code is as follows.
// C++ Code generated from Python Code: #include <iostream> using namespace std; int add_something(int x, int y) { cout << "casually adding some stuff together" << endl; int z = x + y; return z; } int main() { cout << "Okay, lets go" << endl; cout << add_something(5, 2) << endl; return 0; }
python2cppconverter.py actually calls OpenAI's API to do the code conversion. The core code is as follows.
openai.Completion.create(engine='code-davinci-002', prompt=input_prompt, temperature=temperature, max_tokens=num_tokens, stream=STREAM, stop='===================\n', top_p=1.0, frequency_penalty=0.0, presence_penalty=0.0)
is essentially a function call, where the engage parameter is a code transformation model.
Before introducing code-davinci-002, let's understand the GPT-3 model.
GPT-3 is a model released by OpenAI in May 2020. This model contains 175 billion parameters, which is two orders of magnitude more than GPT-2 and a dramatic improvement over GPT-2.
GPT-3 achieves robust performance on many NLP datasets, including translation, question-and-answer, and completion tasks, as well as some tasks that require on-the-fly reasoning or domain adaptation, such as deciphering words or performing arithmetic operations.
The code-davinci-002 model is a descendant of GPT-3, and its training data contains natural language and billions of lines of public code from GitHub. As a result, it understands and generates code and is fluent in more than a dozen programming languages, with the most expertise in Python.
Therefore, the code-davinci-002 model not only enables interconversion between programming languages, but also interconversion with natural languages, finding bugs in the code, writing documentation based on the code, and so on.
For example, the following example generates a textual description of the code function

Code to Natural Language
Take a look at the following example to generate docstring for Python code

docstring
For other examples, you can see the official website of OpenAI.