Code Execution with Google Gemini Flash



Giant Language Fashions, the successors to the Transformers have largely labored inside the area of Pure Language Processing and Pure Language Understanding. From their introduction, they’ve been changing the standard rule-based chatbots. LLMs have a greater capability to grasp textual content and may create pure conversations, so they’re changing the traditional chatbots. However since their introduction, the LLMs are doing greater than what they’re able to. Like changing Pure Language to SQL Queries, in a position to browse the web to fetch the newest data. And now they’ve the flexibility even to execute code. On this article, we are going to take a look at the newly launched characteristic of Gemini, i.e. the Code Execution.

Studying Targets

  • Study Code Execution with LLMs.
  • Get launched to Gemini Flash 1.5.
  • Discover ways to get the API Key for Gemini.
  • Understanding how the LLMs fail in mathematical duties.
  • Leveraging LLMs with Code Execution for exact and correct solutions.

This text was printed as part of the Knowledge Science Blogathon.

Gemini – Google’s Giant Language Mode

Gemini fashions are a household of enormous language fashions launched by Google. It’s launched by Google to rival the favored closed-source massive language fashions just like the GPT4 from OpenAI and Claude from Anthropic. Gemini is a multimodal massive language mannequin that’s able to understanding textual content, pictures, audio, and even movies.

GPT4 was in a position to do the identical as Gemini does however one which it differentiates from Gemini is working the Code that it generates. And now not too long ago Google has up to date the Gemini mannequin making it run code. The code execution is feasible as a result of perform calling capabilities of Gemini, the Code Execution is similar to it and the code it generates, will run and get the outcomes to generate the ultimate output to the consumer.

The code that Gemini generates shall be run in an remoted sandboxed surroundings. Proper now, solely the numpy and the sympy libraries are current within the sandboxed surroundings and the generated code by no means can obtain and set up new Python libraries.

Getting Began with Code Execution

Earlier than we start coding, we have to get the free API key that Google offers to check the Gemini mannequin. The free API even helps the Code Execution. To get the free API, you possibly can click on on the hyperlink right here. Now, we are going to begin with putting in the library.

!pip set up -q -U google-generativeai

It is very important maintain the -U flag whereas putting in the google-generativeai library. It is because the Code Execution is a brand new characteristic and would require the newest model of google-generativeai library to work. Now we are going to authenticate ourselves.

import google.generativeai as genai



Right here we import the google.generativeai library and name the .configure() methodology. To this, we give the API Key that now we have obtained by signing to the Google AI Cloud. Now we are able to begin working with the Gemini Fashions.

mannequin = genai.GenerativeModel(model_name="gemini-1.5-flash")

response = mannequin.generate_content("How are you?")

print(response.textual content)
Code Execution with Google Gemini Flash


  • Right here we begin by creating an occasion of the GenerativeModel Class.
  • Whereas instantiating this object, we give the title of the mannequin that we’re working with, which right here is the gemini-1.5-flash, which is the newest mannequin from Google.
  • To check the mannequin, we name the .generate_content() methodology after which, give the question to it, and retailer the generated textual content within the response variable.
  • Lastly, we print the response. We are able to observe the response in pic above.

Not every little thing might be answered appropriately with the Giant Language Mannequin. To check this, allow us to attempt asking the Gemini Flash mannequin a easy query to show the primary 5 letters of the phrase Mississippi.

response = mannequin.generate_content("Trim this phrase to first 5 letters, Mississippi")

print(response.textual content)
Code response

Right here, working the code and seeing the output above, we see that Google’s Gemini mannequin, the newest LLM development from the Google staff has did not reply such a simple query. This isn’t solely with the Google Gemini fashions, however even the GPT4 from OpenAI and even Claude from Anthropic fail to reply it.

It is because they don’t have the flexibility to rely backward. That’s after producing the letter “i” the mannequin has no concept that it has outputted the second letter. It simply outputs a letter given the earlier letter, however has no thought concerning the size of the earlier letters.

One other Instance

Allow us to check out one other query that the massive language mannequin fails to reply.

response = mannequin.generate_content("What's the sum of first 100 fibonaocci numbers?")

print(response.textual content)
Code Execution with Google Gemini Flash

Right here, we ask the Gemini Flash mannequin to present us the sum of the primary 100 Fibonacci collection. Operating the code and seeing the output pic, we are able to say that the mannequin has did not reply our query. As an alternative of returning the sum, it has given us the steps to get the sum of the primary 100 Fibonacci collection. The mannequin failed as a result of massive language fashions are text-generation fashions. They haven’t any capability to carry out mathematical operations

So in each circumstances, the mannequin has failed. Now, what if the gemini mannequin has capability to execute Python code? The mannequin may attempt to write a code that might lead us to the reply we expect. Possibly for the primary query, the mannequin may carry out a string operation and run the code and for the second query, it may write a perform to calculate the sum.

Gemini – Code Execution

So now, allow us to attempt to ask the mannequin the identical two questions however this time, offering it entry to the Code Execution device.

model2 = genai.GenerativeModel(model_name="gemini-1.5-flash", 

response = model2.generate_content("Trim this phrase to first 5 letters, 
Mississippi. Use code execution device")

print(response.textual content)
Code Execution with Google Gemini Flash

Right here once more, we create an occasion of the category GenerativeModel and provides it the Gemini-1.5-flash mannequin title, however together with it, we even give it the instruments that the mannequin can work with. And right here we offer it with the code_execution device. Now, we ask the identical query to the mannequin. This time, we even inform it to work with the code_execution device.

Operating the code and seeing the output pic above, we are able to discover that, the Gemini Flash mannequin has written a Python code to do a string operation i.e. slicing right here, it has sliced the primary 5 letters of the phrase Mississippi and has lastly given us the answered that we wished. Now allow us to attempt the identical with the second query, the place we ask the LLM to present us the sum of the primary 100 Fibonacci numbers.

response = model2.generate_content("What's the sum of first 100 fibanocci numbers?")

print(response.textual content)

Right here, working the coding and seeing the output, we see that the Gemini Flash has generated a perform to calculate the Fibonacci quantity. Then known as the perform by giving it 100 for n worth after which lastly printed the output. With the code_execution device, the gemini llm was in a position to appropriately give us the reply to the query. This manner it might clear up mathematical issues by making a code out of it and working the code to get the reply.


The introduction of code execution in Google’s Gemini mannequin represents a big development within the capabilities of enormous language fashions. By integrating this characteristic, Gemini can not solely perceive and generate textual content but in addition execute code to unravel complicated issues. This improvement enhances its utility in a wide range of purposes, from pure language processing to performing particular computational duties. The flexibility to run code permits Gemini to beat a number of the inherent limitations of language fashions, significantly in dealing with exact calculations and procedural duties. 

Key Takeaways

  • Gemini can perceive and course of textual content, pictures, audio, and video, making it a real multimodal.
  • Giant Language Fashions usually fail to reply mathematical questions with precision, as a result of they can’t carry out calculations.
  • Code Execution permits an LLM to run code in a sandboxed surroundings.
  • Giant Language Fashions can run Python Code by performing a device name and giving the device the related Python code to execute.
  • Google’s free API permits customers to entry the Gemini Flash API that may Execute Code.

Ceaselessly Requested Questions

Q1. What’s Gemini?

A. Gemini is a household of enormous language fashions launched by Google, able to understanding textual content, pictures, audio, and movies.

Q2. Does Gemini have the performance to execute code?

A. Not too long ago, Google has introduced the characteristic of Code Execution for the Gemini Mannequin. It’s open to the general public by means of the free Google Gemini API Key.

Q3. What libraries can be found in Gemini’s sandboxed surroundings?

A. At present, solely the numpy and sympy libraries can be found in Gemini’s sandboxed surroundings.

This fall. How does code execution enhance Gemini’s capabilities?

A. With code execution, Gemini can generate and run Python code to carry out duties reminiscent of string operations and mathematical calculations precisely

Q5. How do you allow code execution for Gemini?

A. To allow code execution, create an occasion of the GenerativeModel class with the code_execution device and supply the suitable mannequin title.

The media proven on this article is just not owned by Analytics Vidhya and is used on the Creator’s discretion.