Generative AI
Build your Own ChatGPT Code Interpreter in Python
The future of programming with natural language
Imagine a future where our personal computers can understand us with just natural language, where you no longer have to write code to build software, automate tasks, or customize our device.
That future could arrive sooner than expected.
In this article, we’ll explore how to use OpenAI’s GPT-3.5 to interpret natural language questions to generate and run Python code on your machine.
You can find the code on this Deepnote notebook and this Github Repo
Note: the code was mostly referenced from Hacker’s Guide to Large Language Models by Jeremy Howard, creator of fast.ai
GPT call
Let’s first write a function to call GPT.
Nothing fancy here, we add **kwargs
so we can pass other parameters, like function calling which we will see later.
Here we see a complete output of the gpt model.
We use a useful function from fastcore.utils
, nested_idx
to get only the response content.
Function calling
OpenAI released function calling a while ago, it tells OpenAI about tools you have provided it to use.
Here we define a sum function.
However, OpenAI requires you to pass a schema, instead of the function itself. This is where Pydantic is used to create the schema of a function.
This function creates a JSON structure for any function you pass to it, including the description, parameters, type, etc.
Note that the docstring here matters, it’s tells GPT what the function is about in the description field.
Let’s provide the sum function to GPT as a tool.
Notice in the output, specifically the function_call
field, it returns something saying please call this function, with these arguments.
So let’s write code to call this function.
Parsing the code
I won’t go into the details here, but it’s essentially parsing python code using the ast package.
We also define a python function, which checks whether the code is safe to run. We wouldn’t want any malicious code messing up our machine.
Here we test it on 1+1
and it ran it as a Python code, returning 2.
We have what we need to call the function now.
Calling the function
We write a function that grabs the response from the function call, while checking if it exists, and whether it’s the python
function that’s being called.
We also include some error handling.
We have all the code needed to build our code interpreter!
Code interpreter
In our main function, we call functions if there was a function call present, if not, we return the response.
Let’s give it a test run!
Examples
Here we set verbose to be True, so you see the code generated as well.
Math
string operations
Datetime
File creation
Web scraping
Data analysis
The biggest question of all
Hope you enjoyed the examples!
Further improvements
Currently, it’s a one time run.
The ChatGPT, GPT4 Advanced Data analysis feature can store past messages as context, you could improve the code to make it iterative, turning it into your own data analyst.
The error handling can be refined, the y|n
checks aren’t called all of the time, so might be some bugs in there.
Feel free to leave a comment or any thoughts you have about this!
That’s all I have for you. Thank you for reading!
If you have any suggestions or thoughts, feel free to comment below!
Want more?
Be sure to check out bitgrit.ai and follow the bitgrit Data Science Publication to keep updated!
Follow us on the links below 👇 for data science content!
Discord | Website | Twitter | LinkedIn | Instagram | Facebook | YouTube
Follow me on LinkedIn and Twitter for awesome Data Science resources.
Like my writing? Join Medium with my referral link for the price of ☕.
You’ll be supporting me directly 🤗