Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Open sidebar
- Use OpenAI API Jupyter Notebook
- Edward Ross
- How to use the ChatGPT API to directly import code produced by ChatGPT into a Jupyter Notebook
- To use the ChatGPT API to directly import code produced by ChatGPT into a Jupyter Notebook, you can use the openai python package.
- First, you will need to install the package by running !pip install openai in a Jupyter notebook cell.
- Next, you will need to set up an API key to access the API. You can do this by signing up for an API key on the OpenAI website. Once you have your API key, you can use it to authenticate with the API by running the following code in a Jupyter notebook cell:
- Copy code
- import openai_secret_manager
- assert "openai" in openai_secret_manager.get_services()
- secrets = openai_secret_manager.get_secrets("openai")
- print(secrets)
- Once you have authenticated with the API, you can use the openai package to generate code from a prompt. For example, to generate code for a prompt "Write a function that takes a list of integers and returns the sum of the integers":
- Copy code
- import openai
- prompt = "Write a function that takes a list of integers and returns the sum of the integers"
- completions = openai.Completion.create(
- engine="text-davinci-002",
- prompt=prompt,
- max_tokens=1024,
- n=1,
- stop=None,
- temperature=0.5,
- )
- message = completions.choices[0].text
- print(message.strip())
- This will print the generated code, which you can then copy and paste into a Jupyter notebook cell to run it.
- Note : Make sure you have the right permission to access the api key and the api is enabled for your account.
- Edward Ross
- I don't want to have to copy the code into the Jupyter Notebook - the program should automatically move the code into the Jupyter Notebook
- Yes, it is possible to automatically move the generated code into a Jupyter notebook cell without manually copying and pasting it.
- One way to do this is to use the ipython package, which provides an API for interacting with Jupyter notebooks.
- You can use the run_cell method to run a cell in the notebook. You can pass the generated code as a string to this method, and it will be executed in the notebook:
- Copy code
- from IPython.core.getipython import get_ipython
- ipy = get_ipython()
- ipy.run_cell(message)
- You can also use the set_next_input method to insert the generated code into the next empty cell in the notebook:
- Copy code
- ipy.set_next_input(message)
- This will insert the generated code as the input of the next cell, so that you can run it by simply executing the cell.
- Keep in mind that to use the ipython package, it must be installed in the same environment where your Jupyter notebook is running.
- Also, you should be careful when running the code from untrusted source, as it could potentially execute malicious code.
- Edward Ross
- Please integrate the two answers so that my ChatGPT requests go directly into the Jupyter Notebook
- To use the ChatGPT API to directly import code produced by ChatGPT into a Jupyter Notebook, you can use the openai and ipython packages.
- First, you will need to install the packages by running !pip install openai ipython in a Jupyter notebook cell.
- Next, you will need to set up an API key to access the API. You can do this by signing up for an API key on the OpenAI website. Once you have your API key, you can use it to authenticate with the API by running the following code in a Jupyter notebook cell:
- Copy code
- import openai_secret_manager
- assert "openai" in openai_secret_manager.get_services()
- secrets = openai_secret_manager.get_secrets("openai")
- print(secrets)
- Once you have authenticated with the API, you can use the openai package to generate code from a prompt and then use the ipython package to insert the generated code into the next empty cell in the Jupyter Notebook.
- Copy code
- import openai
- from IPython.core.getipython import get_ipython
- ipy = get_ipython()
- prompt = "Write a function that takes a list of integers and returns the sum of the integers"
- completions = openai.Completion.create(
- engine="text-davinci-002",
- prompt=prompt,
- max_tokens=1024,
- n=1,
- stop=None,
- temperature=0.5,
- )
- message = completions.choices[0].text
- ipy.set_next_input(message.strip())
- This will insert the generated code as the input of the next cell, so that you can run it by simply executing the cell.
- Please note that to use the ipython package, it must be installed in the same environment where your Jupyter notebook is running and you should be careful when running the code from untrusted source, as it could potentially execute malicious code.
- Edward Ross
- ChatGPT Jan 9 Version. Free Research Preview. Our goal is to make AI systems more natural and safe to interact with. Your feedback will help us improve.
- New chat
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement