GenAI: Generative AI Tooling for IPython
Overview
GenAI is an innovative tool designed to enhance the coding experience in IPython. This tool leverages the power of OpenAI's Large Language Models to offer intelligent suggestions and insights while users write code, run SQL queries, work with DataFrames, and handle exceptions. GenAI is versatile and can be seamlessly integrated across many Jupyter environments, including IPython, JupyterLab, Jupyter Notebook, and Noteable.
Getting Started
To begin using GenAI in your workflow, you can easily install it using pip:
%pip install genai
After installation, load the extension within your chosen notebook platform, ensuring first that the OPENAI_API_KEY
environment variable is set:
%load_ext genai
Key Features
-
Magic Command
%%assist
:- The
%%assist
command helps generate code from natural language instructions, making it easier for users to convert their thoughts into executable code.
- The
-
Custom Exception Suggestions:
- GenAI can provide suggestions to address exceptions. For example, if there’s an error in a pandas DataFrame operation, GenAI can guide users with suggestions tailored to fixing the issue.
Consider the example where a user tries to use sort_values()
on a DataFrame without specifying the required by
argument. GenAI would suggest supplying a column name for sorting, as shown in the snippet:
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Carol', 'David', 'Eva'],
'Age': [32, 24, 28, 35, 29],
'Salary': [60000, 40000, 35000, 80000, 45000]
})
# Correct usage with 'by' argument:
df_sorted = df.sort_values(by='Age')
print(df_sorted)
Practical Examples
Example Usage
GenAI can assist in understanding complex SQL queries. For instance, users querying data from a database can input:
In [1]: %load_ext genai
In [2]: %%assist
...: Can you explain this query to me so I can be sure we're doing the right things?
...:
...: ```sql
...: SELECT
...: COUNT(*) AS num_downloads,
...: DATE_TRUNC(DATE(timestamp), DAY) AS day
...: FROM `bigquery-public-data.pypi.file_downloads`
...: WHERE
...: file.project = 'genai'
...: AND DATE(timestamp)
...: BETWEEN DATE_TRUNC(DATE_SUB(CURRENT_DATE(), INTERVAL 6 MONTH), MONTH)
...: AND CURRENT_DATE()
...: GROUP BY day
...: ORDER BY day DESC
...: ```
GenAI aids in breaking down each component of the query, facilitating better understanding by the user. This functionality significantly enhances productivity and coding proficiency.
Conclusion
GenAI marks a step forward in making coding more accessible and intuitive. By combining the computational prowess of AI with an easy-to-use interface in Jupyter environments, it empowers coders to work more efficiently and tackle errors with confidence. Whether a seasoned developer or a beginner, GenAI can enhance your coding toolkit, transforming the way you interact with programming challenges.