Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for Software 20.04.
Download
238 views
ubuntu2004-dev
Kernel: Python 3 (system-wide)

OpenAI API in CoCalc

  1. Sign up to OpenAI and get your API key.

  2. Open CoCalc's Project Settings and scroll to the "Custom environment variables" section. Add your key there like:

    { "OPENAI_API_KEY": "sk-1942.......................AWEf" }
  3. Restart your project to make that environment variable be set.

Load some packages, openapi is already installed …

import os from IPython.display import Markdown import openai # already installed import pkg_resources pkg_resources.get_distribution('openai').version
'0.27.2'

Load your API key from that environment variable

openai.api_key = os.getenv("OPENAI_API_KEY")

Quick test, completing a prompt

prompt = """\ Tell me 5 cool things I can do with the programming language Python: """ response = openai.Completion.create(model="text-davinci-003", prompt=prompt, temperature=.7, max_tokens=100) print(response["choices"][0]["text"])
1. Create Artificial Intelligence and Machine Learning applications. 2. Develop Games, Desktop Applications and Web Applications. 3. Manage and Analyze Big Data. 4. Integrate with other Languages and Platforms. 5. Develop Automation Scripts for Data Processing and Repetitive Tasks.

Chatting with GPT-4

As of today, you need to have access to this model…

answer = openai.ChatCompletion.create( model="gpt-4", messages=[{ "role": "system", "content": "Formulate your answer to a student at a university, who knows a bit of Python." }, { "role": "user", "content": "Show me how to plot this sequence of numbers in Python and extrapolate it a little bit using statsmodels: 2, 3, 15, 14, 9.2, 11, 9, 12.1, 15, 16" }]) answer["choices"]
[<OpenAIObject at 0x7f563017ca90> JSON: { "finish_reason": "stop", "index": 0, "message": { "content": "To plot the given sequence of numbers and extrapolate it a little bit using statsmodels, you first need to install matplotlib, numpy, and statsmodels packages if you don't have them already. You can do that using the command:\n\n```console\npip install matplotlib numpy statsmodels\n```\n\nOnce you have these installed, you can use the following Python script to plot and extrapolate the sequence:\n\n```python\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom statsmodels.tsa.ar_model import AutoReg\n\n# Given sequence\nsequence = [2, 3, 15, 14, 9.2, 11, 9, 12.1, 15, 16]\n\n# Generating the x-axis (time index)\ntime_index = np.arange(len(sequence))\n\n# Create AutoReg model for the sequence\nmodel = AutoReg(sequence, lags=1, trend='c')\n\n# Fit the data to the model\nfit = model.fit()\n\n# Number of points to extrapolate\nextrapolate_points = 5\n\n# Generate extrapolated x-axis\nextrapolated_time_index = np.arange(len(sequence) + extrapolate_points)\n\n# Extrapolate based on the fitted model\nextrapolated_sequence = fit.predict(start=0, end=len(extrapolated_time_index) - 1)\n\n# Plot actual sequence and extrapolated sequence\nplt.plot(time_index, sequence, 'bo', label='Actual sequence')\nplt.plot(extrapolated_time_index, extrapolated_sequence, 'r-', label='Extrapolated sequence')\nplt.xlabel('Time Index')\nplt.ylabel('Values')\nplt.legend()\nplt.show()\n```\n\nThis code first imports the necessary libraries, creates an AutoReg model, fits the sequence to the model, extrapolates the sequence for the given number of points, and finally, plots both the actual and the extrapolated sequences.", "role": "assistant" } }]

Neatly display the answer using IPython's Markdown object

Markdown(answer["choices"][0]['message']["content"])

To plot the given sequence of numbers and extrapolate it a little bit using statsmodels, you first need to install matplotlib, numpy, and statsmodels packages if you don't have them already. You can do that using the command:

pip install matplotlib numpy statsmodels

Once you have these installed, you can use the following Python script to plot and extrapolate the sequence:

import numpy as np import matplotlib.pyplot as plt from statsmodels.tsa.ar_model import AutoReg # Given sequence sequence = [2, 3, 15, 14, 9.2, 11, 9, 12.1, 15, 16] # Generating the x-axis (time index) time_index = np.arange(len(sequence)) # Create AutoReg model for the sequence model = AutoReg(sequence, lags=1, trend='c') # Fit the data to the model fit = model.fit() # Number of points to extrapolate extrapolate_points = 5 # Generate extrapolated x-axis extrapolated_time_index = np.arange(len(sequence) + extrapolate_points) # Extrapolate based on the fitted model extrapolated_sequence = fit.predict(start=0, end=len(extrapolated_time_index) - 1) # Plot actual sequence and extrapolated sequence plt.plot(time_index, sequence, 'bo', label='Actual sequence') plt.plot(extrapolated_time_index, extrapolated_sequence, 'r-', label='Extrapolated sequence') plt.xlabel('Time Index') plt.ylabel('Values') plt.legend() plt.show()

This code first imports the necessary libraries, creates an AutoReg model, fits the sequence to the model, extrapolates the sequence for the given number of points, and finally, plots both the actual and the extrapolated sequences.

… and now copy/paste/adapt the provided code snippet

import numpy as np import matplotlib.pyplot as plt from statsmodels.tsa.ar_model import AutoReg # Given sequence sequence = [2, 3, 15, 14, 9.2, 11, 9, 12.1, 15, 16] # Generating the x-axis (time index) time_index = np.arange(len(sequence)) # Create AutoReg model for the sequence model = AutoReg(sequence, lags=1, trend='c') # Fit the data to the model fit = model.fit() # Number of points to extrapolate extrapolate_points = 5 # Generate extrapolated x-axis extrapolated_time_index = np.arange(len(sequence) + extrapolate_points) # Extrapolate based on the fitted model extrapolated_sequence = fit.predict(start=0, end=len(extrapolated_time_index) - 1) # Plot actual sequence and extrapolated sequence plt.plot(time_index, sequence, 'bo', label='Actual sequence') plt.plot(extrapolated_time_index, extrapolated_sequence, 'r-', label='Extrapolated sequence') plt.xlabel('Time Index') plt.ylabel('Values') plt.legend() plt.show()
Image in a Jupyter notebook