Advertisement
edaross

Python ChatGPT and Jupyter Notebooks

Jan 25th, 2023
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.79 KB | None | 0 0
  1. Open sidebar
  2. Use OpenAI API Jupyter Notebook
  3.  
  4.  
  5. Edward Ross
  6. How to use the ChatGPT API to directly import code produced by ChatGPT into a Jupyter Notebook
  7.  
  8. To use the ChatGPT API to directly import code produced by ChatGPT into a Jupyter Notebook, you can use the openai python package.
  9.  
  10. First, you will need to install the package by running !pip install openai in a Jupyter notebook cell.
  11.  
  12. 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:
  13.  
  14. Copy code
  15. import openai_secret_manager
  16.  
  17. assert "openai" in openai_secret_manager.get_services()
  18. secrets = openai_secret_manager.get_secrets("openai")
  19.  
  20. print(secrets)
  21. 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":
  22.  
  23. Copy code
  24. import openai
  25.  
  26. prompt = "Write a function that takes a list of integers and returns the sum of the integers"
  27.  
  28. completions = openai.Completion.create(
  29.     engine="text-davinci-002",
  30.     prompt=prompt,
  31.     max_tokens=1024,
  32.     n=1,
  33.     stop=None,
  34.     temperature=0.5,
  35. )
  36.  
  37. message = completions.choices[0].text
  38. print(message.strip())
  39. This will print the generated code, which you can then copy and paste into a Jupyter notebook cell to run it.
  40.  
  41. Note : Make sure you have the right permission to access the api key and the api is enabled for your account.
  42.  
  43.  
  44.  
  45.  
  46. Edward Ross
  47. 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
  48.  
  49. Yes, it is possible to automatically move the generated code into a Jupyter notebook cell without manually copying and pasting it.
  50.  
  51. One way to do this is to use the ipython package, which provides an API for interacting with Jupyter notebooks.
  52.  
  53. 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:
  54.  
  55. Copy code
  56. from IPython.core.getipython import get_ipython
  57. ipy = get_ipython()
  58.  
  59. ipy.run_cell(message)
  60. You can also use the set_next_input method to insert the generated code into the next empty cell in the notebook:
  61.  
  62. Copy code
  63. ipy.set_next_input(message)
  64. This will insert the generated code as the input of the next cell, so that you can run it by simply executing the cell.
  65.  
  66. Keep in mind that to use the ipython package, it must be installed in the same environment where your Jupyter notebook is running.
  67.  
  68. Also, you should be careful when running the code from untrusted source, as it could potentially execute malicious code.
  69.  
  70.  
  71.  
  72.  
  73. Edward Ross
  74. Please integrate the two answers so that my ChatGPT requests go directly into the Jupyter Notebook
  75.  
  76. To use the ChatGPT API to directly import code produced by ChatGPT into a Jupyter Notebook, you can use the openai and ipython packages.
  77.  
  78. First, you will need to install the packages by running !pip install openai ipython in a Jupyter notebook cell.
  79.  
  80. 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:
  81.  
  82. Copy code
  83. import openai_secret_manager
  84.  
  85. assert "openai" in openai_secret_manager.get_services()
  86. secrets = openai_secret_manager.get_secrets("openai")
  87.  
  88. print(secrets)
  89. 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.
  90.  
  91. Copy code
  92. import openai
  93. from IPython.core.getipython import get_ipython
  94. ipy = get_ipython()
  95.  
  96. prompt = "Write a function that takes a list of integers and returns the sum of the integers"
  97. completions = openai.Completion.create(
  98.    engine="text-davinci-002",
  99.    prompt=prompt,
  100.    max_tokens=1024,
  101.    n=1,
  102.    stop=None,
  103.    temperature=0.5,
  104. )
  105.  
  106. message = completions.choices[0].text
  107. ipy.set_next_input(message.strip())
  108. This will insert the generated code as the input of the next cell, so that you can run it by simply executing the cell.
  109.  
  110. 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.
  111.  
  112.  
  113.  
  114.  
  115. Edward Ross
  116.  
  117. 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.
  118. New chat
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement