I have been publish a couple of notes on the community recently, one where I introduced some simple functions to access features of GPT-4 vision and DALLE, as well as another one where I developed functions to access the Llama API via the API on this website. Both are easy to set up. OpenAI at the time of writing this, uses a scheme where they charge your credit card, but you can set limits. Llama-api uses a prepayment system, where you top up an account in 5$ increments. Especially, when it comes to programming and mathematics my (very subjective) experience shows that GPT-4 is more powerful. This means as a coding support tool or brainstorming tool I prefer GPT-4. As of today the internet is abuzz with speculation about an imminent release of the next version of GPT-4; this might change the applicability of the support that LLMs can provide. My last post is on Mistal.AI. This is a powerful model that is open - you can run it on your own hardware.
​
Today I want to illustrate how to use Gemini Pro, the API of which was recently made available by google for developers. There was a lot of hype around their announcement. The interaction with Gemini is very different from the other models I have discussed earlier. I have developed the function to interact with ChatGPT. One advantage of Gemini Pro is that it appears to me that at the time of writing, Gemini Pro is mostly free to use (unless you go above 60 requests per minute or so).

Setting up Google Cloud

Here’s a step-by-step guide on how to set up and use the Google Cloud SDK (`gcloud`) for making API requests to the Vertex AI Gemini API on macOS. It was generated using OpenAIs ChatGPT:
​
### Setting Up Google Cloud SDK (`gcloud`)
​
1. **Download the Google Cloud SDK:**
- Visit the official [Google Cloud SDK Documentation](https://cloud.google.com/sdk/docs/install) page.
- Download the appropriate version for macOS.
​
2. **Install the Google Cloud SDK:**
- Open a terminal window.
- Run the downloaded installation script with `./google-cloud-sdk/install.sh`.
- Follow the on-screen instructions to initialize the SDK.
​
3. **Initialize the Google Cloud SDK:**
- In the terminal, run `gcloud init` to authenticate and set up the SDK.
- Log in with your Google account when prompted.
- Set your default project when prompted.
​
4. **Set Up Application Default Credentials:**
- Run `gcloud auth application-default login`.
- A browser window will open asking for permission; grant it.
- These credentials will be saved and used by default for your API requests.
​
### Creating a Project and Obtaining Project ID
​
1. **Create a New Project:**
- Go to the [Google Cloud Console](https://console.cloud.google.com/).
- Click on the project drop-down at the top of the page and select “New Project”.
- Enter a project name and, optionally, edit the project ID.
- Click “Create”.
​
2. **Obtain Your Project ID:**
- Once created, you can find your project ID on the [Dashboard](https://console.cloud.google.com/home/dashboard) of the Google Cloud Console.
- The project ID is needed to make requests to Google Cloud APIs.
​
### Enabling APIs and Setting Permissions
​
1. **Enable the Vertex AI API:**
- Navigate to the [API Library](https://console.cloud.google.com/apis/library) in the Google Cloud Console.
- Search for “Vertex AI API” and select it.
- Click “Enable”.
​
2. **Set Permissions (if necessary):**
- In the Google Cloud Console, go to the “IAM & Admin” section.
- Make sure your user account has the necessary roles (like “Owner” or “Editor”) to make API requests.
​
### Using the Google Cloud SDK (`gcloud`) in Mathematica
​
1. **Create Functions to Obtain Tokens and Call the API:**
- Define a Mathematica function (`getGeminiToken`) to get an access token using `gcloud`.
- Define another Mathematica function (`callGemini`) to make the API request using the token.
​
2. **Test Your Setup:**
- Run your Mathematica functions to ensure they can successfully get a token and make API requests.
​
### General Tips
​
- Ensure you’re logged in to the correct Google account with sufficient permissions.
- If you’re part of an organization, you may need additional permissions granted by the organization’s admin.
- Keep your `gcloud` tools updated with `gcloud components update`.
- Regularly check your API usage and quota limits to avoid service interruptions.
​
Remember to replace any placeholders with your actual project ID and other relevant details when using these instructions.

Function

Once the google cloud SDK is installed and connects to your cloud (and they know your credit card details), you can use these two functions to interact with the LLM. The first function helps to generate an access token:
In[]:=
getGeminiToken[]:=Module[{gcloudPath,command},​​gcloudPath="/Users/thiel/google-cloud-sdk/bin/gcloud";​​​​(*Setthecommandtogettheaccesstoken*)​​command=gcloudPath<>" auth application-default print-access-token";​​​​(*UseRunProcesstoexecutethecommandwithzsh*)​​accessTokenResult=RunProcess[{"zsh","-c",command},"StandardOutput"];​​​​(*Extracttheaccesstoken*)​​accessToken=StringTrim[accessTokenResult];​​accessToken]
The second function performs the interaction. At the beginning I check whether the accessToken is a string, indicating that the connection will work.
In[]:=
accessTokenResult$24565346543=getGeminiToken[]
In[]:=
callGemini[query_String]:=Module[{headers,jsonBody,apiUrl,response,responseParts,fullresponse},​​​​If[!StringQ[accessTokenResult$24565346543],(*Ifnoaccesstokencreatenewone.*)accessTokenResult$24565346543=getGeminiToken[];];​​headers={"Authorization"->("Bearer "<>accessTokenResult$24565346543),"Content-Type"->"application/json"};​​(*ConstructtheJSONbodyforyourrequest*)jsonBody=ExportString[{"contents"->{"role"->"user","parts"->{{"text"->query}}}},"JSON"];​​(*ConstructtheAPIURL*)apiUrl="https://us-central1-aiplatform.googleapis.com/v1/projects/"<>"mathematica-thiel"<>"/locations/us-central1/publishers/google/models/gemini-pro:streamGenerateContent";​​(*MaketheAPIrequest*)response=URLExecute[HTTPRequest[apiUrl,<|"Method"->"POST","Headers"->headers,"Body"->jsonBody|>]];​​(*Assuming'response'containstheparsedAPIresponse*)​​responseParts=Flatten["text"/.("parts"/.("content"/.("candidates"/.#)))&/@response];​​​​(*Combinethepartsintoonestringtoformthecompleteresponse*)​​fullresponse=StringJoin[Riffle[responseParts,"\n"]];​​​​(*Outputtheresponse*)​​fullresponse]
I have given the accessToken the “unique” identifier accessTokenResult$24565346543; I will let it leak from inside the function definition so that it carries over to future function calls. You will have to put a project id from the Google Cloud console in the place of “mathematica-thiel”.

Example 1

Let’s run a couple of examples, starting with a simple physics question.
In[]:=
sunweightGemini=callGemini["What is the mass of the sun and how was it measured?"]
Out[]=
The mass of the Sun is approximately 1.989 × 10^30 kilograms, or about 330,000 times the mass of Earth. This value has been determined using a variety of methods, including:​1. **Kepler's Laws:** By observing the motion of planets around the Sun, Johannes Kepler formulated laws describing their orbits. These laws allowed scientists to calculate the mass of the Sun relative to the mass of Earth.​2. **Newton's Law of Universal Gravitation:** Isaac Newton's law of universal gravitation states that the force of gravity between two objects is proportional to the product of their masses and inversely proportional to the square of the distance between them. By measuring the gravitational force between the Sun and Earth, scientists can calculate the mass of the Sun.​3. **Doppler Shift:** The Doppler shift refers to the change in the frequency of light due to the relative motion of the light source and the observer. By measuring the Doppler shift of light from the Sun, scientists can determine its radial velocity towards or away from Earth. Combined with the Sun's distance, which can be measured using parallax techniques, this allows for the calculation of its mass.​4. **Solar Mass from Solar Luminosity:** The luminosity of a star, or the amount of energy it emits per second, is related to its mass. By measuring the Sun's luminosity and using theoretical models of stellar structure, scientists can estimate its mass.​5. **Relativistic Effects:** Einstein's theory of general relativity predicts that the gravity of a massive object can cause a curvature of spacetime. By observing the bending of light around the Sun during solar eclipses, scientists can measure this effect and use it to calculate the Sun's mass.​These methods, and others, have been used throughout history to refine our understanding of the Sun's mass. The currently accepted value is based on a combination of these techniques and is considered to be accurate to within 0.1%.
Let’s compare that to GPT-4
In[]:=
sunweightGPT=LLMSynthesize["What is the mass of the sun and how was it measured?",LLMEvaluator->LLMConfiguration["Model"->"gpt-4-1106-preview"]]
Out[]=
The mass of the Sun is approximately 1.989 × 10^30 kilograms (about 333,000 times the mass of the Earth). This mass has been determined through a combination of observations and calculations grounded in the laws of gravity as described by Isaac Newton's law of universal gravitation and later refined by Albert Einstein's theory of general relativity.​The mass of the Sun can be calculated using the orbital period and distance of the Earth from the Sun:​1. Observations provide the average distance from the Earth to the Sun, known as an astronomical unit (AU), which is about 149.6 million kilometers.​2. The orbital period of the Earth, which is one year, or approximately 365.25 days, is well-known.​Using Newton's law of universal gravitation, one can use the relationship between the gravitational force (F), the masses of the two objects (m1 and m2, where m1 is the mass of the Earth and m2 is the mass of the Sun), and the distance (r) between their centers:​F = G * (m1 * m2) / r^2​Here, G is the gravitational constant, which is approximately 6.674 × 10^-11 N(m/kg)^2.​From the laws of motion (also described by Newton), we know that the centripetal force required to keep the Earth in orbit (which is the same as the gravitational force provided by the Sun) is given by:​F = (m1 * v^2) / r​The velocity (v) can be determined from the distance the Earth travels in its orbit (the circumference of the orbit) divided by the time it takes (one year) to travel that distance.​Since both expressions for the force (F) are equal, the equations can be set equal to each other and the mass of the Earth (m1) cancels out:​G * (m1 * m2) / r^2 = (m1 * v^2) / r​This simplifies to:​m2 = (v^2 * r) / G​By plugging in the values for the Earth's orbital velocity and radius, as well as the gravitational constant, one can solve for m2, the mass of the Sun.​Additional techniques involve studying the orbits of other solar system objects and performing high-precision measurements of the dynamics of the solar system. Modern measurements also take into account the effects described by general relativity, which are particularly important for very precise calculations in the strong gravitational fields of massive bodies like the Sun.
Let’s ask GPT to compare the two answers:
This is an interesting result as GPT suggests that Gemini’s answer is in fact better than its own response.

Example 2

Let’s try to produce some Mathematica code with Gemini.
This is the function
which does not run.
Let’s try the same in GPT:
Again we can execute the code:
So in this case GPT is ahead.

Example 3

Believe it or not, but this is a very difficult question for an LLM:
So both models get this wrong.

Conclusion

I have presented a function to access Gemini Pro. At the time of writing the model appears to be free.
I will add the function to a list of functions for other API and attach them to this post. The file will also contain (updated) versions of the functions for Llama, some additional functions for OpenAI, such as speech generation and functionality for Mistral. I will post further pieces on these LLMs in due course.

CITE THIS NOTEBOOK

Google's Gemini - API access from the Wolfram Language (alternative to GPT-4?)​
by Marco Thiel​
Wolfram Community, STAFF PICKS, December 17, 2023
​https://community.wolfram.com/groups/-/m/t/3085582