This repository contains a Python notebook that demonstrates how to use the OpenAI API, particularly the ChatGPT model, as a pair-programming partner. The notebook provides various templates to interact with ChatGPT for different programming tasks such as improving existing code, exploring multiple ways to rewrite code, simplifying code, writing test cases, making code more efficient, and debugging.
- You need an API key from OpenAI. Store it in a
.env
file in the format:
OPENAI_API_KEY=sk-...
openai
: For interacting with the OpenAI API.tiktoken
: To count how many tokens are in a text string without making an API call.dotenv
: To load environment variables from a.env
file.IPython
: For displaying Markdown and other rich output formats in Jupyter.
-
Clone the repository.
-
Install the required dependencies.
-
Add your OpenAI API key to the
.env
file. -
Run the code:
python pair_programmer/app.py
You will get an output like this:
Running on local URL: http://127.0.0.1:7861 To create a public link, set `share=True` in `launch()`.
-
Open the URL in your browser.
- Improve Existing Code: Provide a code snippet and get suggestions on how to improve it.
- Ask for Multiple Ways of Rewriting Your Code: Provide a code snippet and get multiple ways to achieve the same functionality.
- Recommend the Most Pythonic Way: Provide a code snippet and get the most Pythonic way to write it.
- Simplify Code: Provide a complex code snippet and get a simplified version of it.
- Write Test Cases: Provide a code snippet and get test cases for it.
- Make Code More Efficient: Provide a code snippet and get a more efficient version of it.
- Debug Code: Provide a buggy code snippet and get debugging help.
- Explain Code: Provide a code snippet and get a detailed explanation of how it works.
- Document Code: Provide a code snippet and get documentation for it.
- Write Docstrings: Provide a code snippet and get docstrings for the functions or classes.
- Optimize Code: Provide a code snippet and get an optimized version of it.
- Create Readme: Write a
Readme.md
file based on the pasted code.
The notebook uses the gpt-4-0613
model from OpenAI for completions. You can change the model by modifying the model
parameter in the get_completion
function.