Introduction
Generative AI has been on the forefront of latest developments in synthetic intelligence. It has change into part of each main sector, from tech and healthcare to finance and leisure, and continues remodeling our work. It has enabled us to create high-quality content material and carry out complicated duties in minutes.
Now, think about a world the place you should use easy textual content prompts to harness the facility of generative AI, permitting you to write down high-quality code or analyze complicated information immediately from a Jupyter Pocket book. Welcome to Jupyter AI, which seamlessly integrates cutting-edge generative AI fashions into your notebooks, permitting you to carry out all these complicated duties effortlessly whereas growing productiveness and effectivity.
Studying Aims
By the top of this text, you should have a transparent understanding of
- The variations between conventional Jupyter notebooks and Jupyter AI
- How you can successfully use Jupyter AI to carry out complicated duties and enhance productiveness
- Utilizing textual content prompts to generate code, visualize information, and automate handbook duties in Jupyter AI
- Knowledge and privateness issues when utilizing Jupyter AI
- Limitations and downsides of utilizing Jupyter AI
This text was revealed as part of the Knowledge Science Blogathon.
What’s Jupyter AI?
In contrast to conventional Jupyter notebooks, which require the consumer to carry out all duties manually, Jupyter AI can simply automate tedious and repetitive duties. It permits customers to write down high-quality code and analyze information extra successfully than ever through the use of easy textual content prompts. It has entry to a number of massive language mannequin suppliers, together with Open AI, Google, Anthropic, and Cohere. The interface is straightforward, user-friendly, and accessible immediately from a Jupyter Pocket book.
On this article, I’ll stroll you thru your complete means of utilizing Jupyter AI to change into a extra productive information scientist and enhance your effectivity. Jupyter AI can be utilized in two alternative ways. The primary technique is to work together with an AI chatbot by means of JupyterLab, and the second is to run the `jupyter_ai_magics` command in a Jupyter pocket book. We might be taking a look at each of those choices on this article. So, let’s get began straight away.
Generate API Keys
To make use of Jupyter AI with a particular mannequin supplier, we first want to offer the API keys in order that the mannequin supplier can serve our requests. There are alternatives for open-source fashions that received’t require an API key. Nevertheless, you have to set up all of the configuration recordsdata in your system to run them, which can require further cupboard space. Moreover, on this case, the inference could be made in your CPU, which might be a lot slower and take a very long time to generate responses to even a single immediate. Except you’re coping with extremely confidential information, I like to recommend utilizing cloud suppliers as a result of they’re beginner-friendly and deal with all of the complicated stuff.
I might be utilizing TogetherAI and Google Gemini for this tutorial. Collectively, AI supplies seamless integration with a number of main LLM fashions and supplies quick inference. Additionally, signing in with a brand new account will provide you with $25 in free credit. These are sufficient to run 110 million tokens on the Llama-2 13B mannequin. In perspective, 1 million tokens are roughly equal to 700,000 phrases. Compared, the huge Lord of the Rings trilogy has a mixed phrase rely of roughly 500,000 solely. This implies you would want greater than 150 of those books to make use of up the entire tokens. The free credit might be greater than adequate for any use case.
Should you use a special mannequin supplier and have already got an API key, be happy to skip this part.
TogetherAI API key
To generate a TogetherAI API key, observe the steps under:
- Create an account on the collectively.ai platform
- Check in to your account
- Go to collectively.ai to see your API keys
Google API key
You could create an API key to make use of the Google Gemini mannequin. The steps are:
- Go to Google Dev
- Choose the “Get API key in Google AI Studio” choice
- Check in together with your Google account
- In Google AI Studio, click on on Get API key and generate your API key
Cohere API key
To fine-tune the mannequin to our native information, we might additionally must have entry to an embedding mannequin. I might be utilizing Cohere’s textual content embeddings for this. Observe the steps under to generate a Cohere API key:
- Go to Cohere API
- Create your Cohere account
- Go to Trial keys and create your API key
Set up essential dependencies
Jupyter AI is appropriate with any system that helps Python variations 3.8 to three.11, together with Home windows, macOS, and Linux machines. Additionally, you will want a conda distribution to put in the required packages. Should you don’t have already got a conda distribution put in in your pc, you have to first set up conda from right here. I choose Anaconda, however the different two are additionally viable choices.
Create digital setting
The following step is to create a digital setting for our challenge. Earlier than beginning any challenge, you need to create a digital setting to keep away from piling up packages within the default Python setting and potential conflicts with different packages. Copy the code under into your shell to create an remoted setting with Python 3.11.
$ conda create -n jupyter-ai-env python=3.11
This may create a brand new conda setting referred to as `jupyter-ai-env` and set up Python model 3.11 to this setting. Subsequent, activate this setting utilizing the command
$ conda activate jupyter-ai-env
Set up JupyterLab and Jupyter AI
Subsequent, set up JupyterLab and Jupyter AI with the `conda set up` command
$ conda set up -c conda-forge jupyter-ai
This may set up JupyterLab and JupyterAI with all the opposite essential dependencies to our surroundings.
To make use of among the mannequin suppliers, equivalent to Open AI, Google, Anthropic, and NVIDIA, you have to first set up their required langchain dependencies. We’d additionally want to put in two further packages: `pypdf` for pdf assist and `cohere` for the embedding mannequin. To put in these, write
$ pip set up langchain-google-genai
$ pip set up langchain-openai
$ pip set up langchain-anthropic
$ pip set up langchain_nvidia_ai_endpoints
$ pip set up pypdf cohere
$ jupyter lab
You don’t want to put in all of them. Merely choose those you require based mostly in your wants and availability of the API key. Then begin an occasion of JupyterLab utilizing `jupyter lab`.
Jupyter AI in JupyterLab
On startup, the JupyterLab interface would seem like this:
Chat Interface
On the left aspect is Jupyternaut, the chatbot with which we’ll work together. Along with the essential chat performance, it gives quite a lot of different options. It could additionally study our native information after which present tailor-made responses to our prompts. As we’ll see within the later sections of this tutorial, it will probably even generate an entire Jupyter pocket book from only a single textual content immediate. You may choose the fashions by clicking on the settings icon on the prime proper of the Jupyternaut interface.
Language Mannequin vs Embedding Mannequin
There are two varieties of fashions right here: language mannequin and embedding mannequin. Let’s perceive the distinction between the 2. The language mannequin is the one which powers the chat UI, which we’ll use to talk and generate responses to prompts. The embedding mannequin, alternatively, generates vector embeddings of our native information recordsdata and shops them in a vector database. This permits the language mannequin to retrieve related data when requested particular questions in regards to the information. Utilizing Retrieval-Augmented Era (RAG), the mannequin can extract related data from the vector database and mix it with its present data to reply questions on a particular subject in an in depth and correct method.
Jupyter AI helps a variety of mannequin suppliers and their fashions. You may see the record of all of the mannequin suppliers within the dropdown.
Choose your most well-liked mannequin from the dropdown, enter your API keys into the suitable packing containers and save the modifications.
Easy Job
Let’s chat with our AI assistant and check its data with a easy query.
It just about nailed it. Together with the definitions, it accurately supplies the instance of picture classification for supervised studying and clustering for buyer segmentation process, that fall beneath the unsupervised studying class.
Code Era
Now, allow us to see the way it performs on a coding downside.
The code above seems to be environment friendly and logically right. Allow us to ask some follow-up inquiries to see if it is aware of what it’s discussing.
It certainly is aware of its ideas properly. To check it additional, we are able to add a pocket book to the right-side panel and have it optimize our code for us.
Code Optimization
To do that, you can spotlight a bit of your pocket book and embrace it together with your immediate. Choose the embrace choice choice together with your immediate to make the code seen to the chatbot. Now you can ask questions relating to the chosen code, as depicted within the picture under
Jupyternaut may even change your choice with its personal response by selecting the change choice choice. Allow us to inform it to print a extra optimized code model, together with feedback explaining it.
Jupyternaut sends the code to your chosen language mannequin after which replaces the choice with the mannequin’s response. It optimizes the code accurately through the use of a set somewhat than an inventory after which explaining it with correct feedback, as proven above.
Be taught from native information
Thus far, so good, however allow us to take it one step additional. Allow us to now ask just a few questions on our native information. To make use of this function, we should add some paperwork, ideally in textual content format, equivalent to .pdf or .txt recordsdata, to the present listing. Create a brand new folder named docs, and add your information recordsdata to this folder. After that, use the /study docs command as depicted under:
I fed it a analysis paper on sleep paralysis. Now, use /ask to ask any particular questions in regards to the information. You’d discover a major distinction between the AI’s responses earlier than and after studying from the paperwork. Right here’s an instance of me asking it about sleep paralysis
Earlier than studying the specifics of the doc, the chatbot offered a imprecise and generic response that conveyed no helpful data. Nevertheless, after studying the embeddings, it offered a a lot better response. That is the facility of retrieval-augmented era (RAG). It permits the language mannequin to cater to the very specifics of the info, offering extremely correct outcomes. The one factor to notice right here is that the accuracy and correctness of the responses rely completely on the standard of the info we’re feeding into the mannequin. As famously mentioned in information science, “Rubbish in, rubbish out.”
You can too delete the knowledge it discovered with the /study -d command, which is able to trigger it to overlook every thing it has discovered in regards to the native information.
Generate notebooks from scratch
To exhibit the total potential of JupyterAI, we’ll now permit it to create an entire pocket book from scratch. As that is such a posh process, it can require a extremely developed and nuanced mannequin like GPT-4 or Gemini Professional. These fashions use their langchain libraries to cope with complicated situations like these. I’m selecting Gemini Professional for this process. To generate a Jupyter Pocket book from a textual content immediate, begin the immediate with the /generate command. Let’s check out an instance of this
It created a pocket book demonstrating a classification use case from scratch in only one minute. You may examine the time stamps for your self to confirm this. That is what the generated pocket book seems to be like.
I used to be amazed to see this stage of element within the generated pocket book, and after testing completely different fashions on the identical process, I wasn’t anticipating this from Gemini. Nothing even got here near this. The pocket book generated by Gemini is solely excellent. It additionally adopted the entire directions I offered within the immediate. This actually unleashes the final word energy of LLMs. Knowledge scientists, beware!!
Export chat historical past
JupyterLab supplies yet one more helpful function. You can too save your chat information utilizing the /export command. This command exports your complete chat historical past to a Markdown file and saves it within the present listing. This makes JupyterAI an especially versatile software.
Jupyter AI in Jupyter notebooks
The chat interface is actually exceptional, however there may be extra to JupyterAI. Should you can’t set up JupyterLab or it doesn’t work correctly in your system, there may be yet one more different for utilizing JupyterAI. It may also be utilized in notebooks through JupyterAI magics with the `%%ai` command. This implies you’ll be able to make the most of JupyterAI’s options with out relying solely on JupyterLab. This works with any IPython interface, equivalent to Google Colab, VSCode, or your native Jupyter set up.
Allow Jupyter AI magics
If you have already got `jupyter_ai` put in, the magics package deal `jupyter_ai_magics` might be put in robotically. In any other case, use the next command to put in it:
pip set up jupyter_ai_magics
To load JupyterAI to your IPython interface, run the command under and the magics extension might be loaded to your pocket book.
%load_ext jupyter_ai_magics
To try the completely different mannequin suppliers, kind `%ai record`, or you’ll be able to record solely the fashions from a particular supplier utilizing %ai record <provider-id>.
You’ll now see a protracted record of all of the completely different mannequin suppliers and their fashions.
Once more, I might be utilizing the TogetherAI fashions and Gemini Professional. However earlier than going additional, we have to present our API key once more and retailer it in an setting variable. To do that, write
%env TOGETHER_API_KEY={YOUR_API_KEY}
%env GOOGLE_API_KEY={YOUR_API_KEY}
In case you are utilizing a special mannequin supplier, merely change the mannequin supplier title above, and also you’ll be good to go.
The mannequin’s full title accommodates the mannequin supplier, adopted by the mannequin title. We are able to use an alias as an alternative of writing the total title each time earlier than calling a cell. To set an alias for our mannequin title, use the code under:
%ai register raven togetherai:Nexusflow/NexusRaven-V2-13B
%ai register llama-guard togetherai:Meta-Llama/Llama-Guard-7b
%ai register hermes togetherai:Austism/chronos-hermes-13b
%ai register mytho-max togetherai:Gryphe/MythoMax-L2-13b
%ai register llama2 togetherai:NousResearch/Nous-Hermes-Llama2-13b
%ai register gemini gemini:gemini-pro
Now you can use these aliases as some other mannequin title with the `%%ai` magic command. To allow Jupyter AI for a particular cell and ship textual content prompts to our mannequin, we first must invoke the `%%ai` magic command with the mannequin title after which present the immediate under it
%%ai llama2
{Write your immediate right here}
Jupyter AI assumes {that a} mannequin will output markdown by default, so the output of a `%%ai` command might be in markdown format. This will typically trigger issues, inflicting some fashions to output nothing. You may change this by including the `-f` or `–format` flag to your magic command. Different legitimate codecs embrace code, math, html, textual content, photos, and json.
Textual content Era
Subsequently, setting the flag to textual content is at all times higher if you’d like a textual content output. An instance of that is proven under:
Mathematical Equations
We are able to additionally use it to write down mathematical equations, altering the format to math.
HTML Tables
It could additionally generate handsome HTML tables when the format modifications to HTML.
Language Translation
Utilizing curly braces, we are able to additionally embrace variables and different Python expressions within the immediate. Let’s perceive it utilizing an instance of translating textual content from English to Hindi
Just like f-strings, the `{lang}` and `{title}` placeholders are interpolated because the values assigned to the variables, respectively. It didn’t spell my title accurately, however I’ll let it get away with that.
Error Correction
It’s good at writing and optimizing code. Allow us to see how properly it does at correcting errors in code.
Jupyter AI has a particular “Err” technique that captures errors from completely different cell executions. This technique can then be utilized in one other cell to ask questions in regards to the error. Within the instance above, it accurately detects the error and rewrites the corrected code.
Producing a report
Let’s now give it a relatively extra sophisticated process to check its caliber once more. Right here is an instance the place I instructed it to generate a report on COVID-19 and its influence on the world.
As proven within the picture above, the report is well-structured, with distinct sections for introduction, international well being influence, financial influence, and social influence. It additionally elaborated on ongoing challenges and the way nations worldwide are addressing them.
Textual content Summarization
The interpolation performance might be prolonged additional by combining the enter/output of a particular cell with our immediate. Right here’s an instance the place I requested it to create a short abstract of the COVID-19 report.
It lists out the abstract of the report in crisp bullet factors. Additionally, interpolation permits the mannequin to learn the report immediately from Jupyter, saving us from the ache of copying and pasting the textual content every time.
Knowledge Visualization
Now, let’s put it to a ultimate check. For this, I uploaded the Titanic CSV file and instructed it to write down the code for univariate evaluation on the Titanic dataset.
Wow! Not unhealthy in any respect. There’s not even a single error within the code. Each time the AI generates code, it’s labeled as AI-generated, as proven within the picture above. The code it offered labored and resulted in some beautiful plots.
It additionally used subplots within the implementation, as specified within the immediate. It’s wonderful how properly it adapts to the specifics of the immediate.
Limitations and Challenges
Thus far, we have now regarded on the constructive features of Jupyter AI, however like the rest, it has limitations, too. Let’s have a look at these limitations one after the other.
Biased Response
As a result of LLMs are skilled on huge quantities of textual content information from all around the web, they generally produce biased responses to questions. Let’s have a look at an instance of this:
Firstly, it didn’t try to argue that the query was biased earlier than answering it. Second, it didn’t even take into account the chance that its factors could possibly be incorrect. That is simply typical biased habits.
Hallucinations
When the mannequin merely invents one thing nonexistent or makes stuff up, it’s mentioned to be hallucinating. Hallucinations are one of the vital distinguished issues with LLMs, significantly hampering their reliability.
It doesn’t ask for clarifications and completes the sentence in accordance with its choice. That’s why it’s at all times really helpful to fact-check each piece of knowledge an LLM generates somewhat than blindly trusting every thing it says.
Factual Inconsistency
When requested about an individual who has been to Mars, this was the response:
That is yet one more instance of the AI confidently stating improper info.
Jupyter AI poses another challenges as properly. These are:
- It’s tough to pick out a single dependable mannequin for every process as a result of fashions that carry out properly on one process could carry out poorly on others.
- If the query is just not structured, it might misread the immediate, leading to a suboptimal or hallucinated response.
Further Info
Other than these, listed here are some further factors to bear in mind when utilizing Jupyter AI:
- Jupyter AI sends information to third-party mannequin suppliers. Evaluation the supplier’s privateness coverage and pricing choices to know information utilization and fee obligations higher.
- Together with further context in messages can enhance token rely and prices. Subsequently, it’s suggested to examine value insurance policies earlier than making massive requests.
- AI-generated code could comprise errors, so it’s at all times greatest to rigorously evaluation all generated code earlier than operating it.
- Evaluation the supplier’s insurance policies for third-party embedding fashions earlier than sending any confidential or delicate information.
Conclusion
On this article, we regarded on the unimaginable energy of Jupyter AI and the way it can help in numerous duties, releasing us from tedious and repetitive duties and permitting us to give attention to the extra artistic features of our jobs. This brings us to the top of this text. That is only a glimpse of what Jupyter AI and LLMs, typically, are able to. They’ve limitless potential but to be unfolded.
I hope you loved this text. As at all times, thanks for studying, and I look ahead to seeing you at one other AI tutorial.
Key Takeaways
- Jupyter AI supplies chat help by means of a conversational assistant. This assistant can assist summarize textual content, write good-quality code, and supply extra particular data by studying about native information.
- We solved extremely complicated duties by writing easy textual content prompts, equivalent to creating a whole pocket book from scratch.
- Then, we examined the right way to rework our Jupyter notebooks into generative AI playgrounds utilizing the `%%ai` magic command.
- We used completely different fashions for numerous duties, equivalent to code optimization, information visualization, and producing a well-structured report.
- Lastly, we examined among the language fashions’ limitations, together with their skill to sometimes generate inconsistent and biased responses and hallucinations.
The media proven on this article will not be owned by Analytics Vidhya and is used on the Creator’s discretion.
Ceaselessly Requested Questions
A. Jupyter AI might be accessed in two important methods. The primary method is to make use of the chatbot as a conversational assistant in JupyterLab. The second technique is to make use of the %%ai magic command in an IPython kernel equivalent to Google Colab, Visible Studio Code or your native Jupyter set up.
A. Jupyter AI helps a variety of mannequin suppliers and fashions, together with Open AI, Cohere, Hugging Face, Anthropic, and Gemini. Go to the official documentation to see the whole record of supported mannequin suppliers.
A. Jupyter AI solely contacts an LLM whenever you particularly request it to. It doesn’t learn your information or transmit it to fashions with out your specific consent.
A. Jupyter AI can be utilized for a variety of duties, starting from answering easy inquiries to producing code, creating complicated information visualizations, summarizing paperwork, composing artistic content material like tales or articles, translating textual content between languages, and plenty of extra.
A. The selection between cloud and domestically hosted fashions boils all the way down to the trade-off between privateness and quicker inference. In different phrases, in case you have delicate or extremely confidential information and wish to guarantee most privateness, you need to use native fashions. If information privateness is just not a serious concern for you and also you need fast inference, you need to go for cloud mannequin suppliers.