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.
​
There are, however, many alternative API servers being made available all over the internet. One of which is Mixtral.Ai or Mistal. AI. The models are fully open models and you can, in principle, run them locally on your hardware. I have chosen to use them via their apis. If you sign up, you will get on a waiting list, but in my experience you will gain access soon. The website then generates an API key for you. Similar to Open.AI’s model you type in your credit card details, but can limit your monthly spent.
​
Advantages of the family of Mistral models are that they are very cheep to run. My very unscientific testing suggests that in many aspects this model does fairly well, and in terms of coding support it is preferable to LLama. In terms of summarising text or answering standard language questions Mistral performs very well. Note that there have been reports that Mistral is very little moderated and can answer questions that are blocked in other LLMs.

API function

In order to access the Mistral ai (and assuming you have made an API key on their website, you can use the following function to work with the LLM:
In[]:=
callMistralAI[myPrompt_,mistralAPIKey_]:=Module[{myRequest,jsonResponse,jsonBody},(*PreparetheJSONbodyoftherequest*)jsonBody=ExportString[{"model"->"mistral-medium","messages"->{<|"role"->"user","content"->myPrompt|>}},"JSON"];​​(*CreatetheHTTPrequest*)myRequest=HTTPRequest["https://api.mistral.ai/v1/chat/completions",<|"Method"->"POST","Body"->jsonBody,"ContentType"->"application/json","Headers"->{"Authorization"->"Bearer "<>mistralAPIKey}|>];​​(*ExecutetherequestandgettheJSONresponse*)jsonResponse=URLExecute[myRequest];​​(*Convertthelistofrulestoanassociationforeasieraccessbykey*)jsonResponse=Replace[jsonResponse,l_List:>Association[l],{0,Infinity}];​​(*Checkiftheresponsecontainstheexpected'choices'key*)If[!KeyExistsQ[jsonResponse,"choices"],(*Ifthe'choices'keyismissing,return$Failed*)Return[$Failed]];​​(*Extractthe'choices'content*)jsonResponse["choices","message","content"]]
I have autogenerated this function with ChatGPT. In order to run this on your system (I am running it mostly on OSX), you should first add your API key to the system credentials like this:
SystemCredential["MISTRAL_API_KEY"]="here goes your API key"
Before running the function (or in the paclet that I am using), you can then execute:
In[]:=
myMistralKey=SystemCredential["MISTRAL_API_KEY"];
which will allow you to run the callMistralAI function. Let’s look at some examples.

Examples of Mistral replies

Let’s have a look at some output we get from Mistral; as you can see from the function we are using the “mistral-medium” model. You could also use the “mistral-small” or “mistral-tiny” model.

Example 1

Let’s first ask a little question about the physics processes in the sun.
In[]:=
callMistralAI["Which process generates the engery radiated by the sun?",myMistralKey]
Out[]=
The process that generates the energy radiated by the sun is nuclear fusion, specifically the proton-proton chain reaction and the CNO cycle (carbon-nitrogen-oxygen cycle). In these processes, hydrogen nuclei combine to form helium, releasing a large amount of energy in the form of light and heat. This energy then travels through the layers of the sun and is eventually emitted as solar radiation.
Let’s try to get more details:
sunmistral=callMistralAI["Which process generates the engery radiated by the sun? Answer in 4 paragraphs. Answer at a level of a postgraduate physics student.",myMistralKey]
Out[]=
The energy radiated by the Sun is generated through a process known as nuclear fusion, which occurs in the Sun's core. In nuclear fusion, hydrogen atoms are combined at extremely high temperatures and pressures to form helium, releasing a large amount of energy in the form of photons and kinetic energy of the particles.\n\nThe Sun's core is made up primarily of hydrogen, which exists in a plasma state due to the high temperatures. The temperature in the core is approximately 15 million degrees Celsius, which is hot enough to overcome the electrostatic repulsion between the positively charged protons in the hydrogen nuclei. When two protons come close enough together, they can fuse to form a deuterium nucleus, consisting of one proton and one neutron. This process also releases a positron and a neutrino.\n\nThe deuterium nucleus can then fuse with another proton to form helium-3, which consists of two protons and one neutron. Alternatively, two deuterium nuclei can fuse to form helium-4, which consists of two protons and two neutrons, as well as a high-energy photon. This process is known as the proton-proton chain reaction and is the dominant energy production process in the Sun.\n\nThe energy released by nuclear fusion in the Sun's core takes a long time to reach the surface and be radiated into space. This is because the energy must travel through the Sun's outer layers, which are much cooler and denser than the core. The energy is transferred through a process known as convection, where hot material rises to the surface and cooler material sinks back down. Additionally, some of the energy is absorbed and re-emitted by atoms in the Sun's outer layers, further delaying its journey to the surface. The total time it takes for energy generated in the Sun's core to reach the surface is estimated to be around 100,000 years. Once the energy reaches the surface, it is radiated into space as light and heat, making the Sun a powerful source of energy for the planets in our solar system.
Let’s compare that to GPT-4-turbo’s answer
sungpt=LLMSynthesize["Which process generates the engery radiated by the sun? Answer in 4 paragraphs. Answer at a level of a postgraduate physics student.",LLMEvaluator->LLMConfiguration["Model"->"gpt-4-1106-preview"]]
Out[]=
The primary source of energy that the Sun radiates into space is nuclear fusion occurring in its core. The core's extreme temperature (approximately 15 million Kelvin) and pressure (estimated to be about 250 billion atmospheres) create the necessary conditions for fusion to take place. At these conditions, hydrogen nuclei (protons) have sufficient kinetic energy to overcome their electrostatic repulsion, allowing them to come close enough for the strong nuclear force to bind them together.\n\nThe most prevalent fusion process within the Sun is known as the proton-proton (pp) chain. This process begins with two protons fusing together to form a deuterium nucleus (one proton and one neutron), releasing a positron and a neutrino as byproducts of this reaction. The positron quickly annihilates with an electron, converting the mass of the two particles into two gamma-ray photons. The deuterium nucleus then typically fuses with another proton to form helium-3 (two protons and one neutron), releasing a gamma-ray photon in the process. When two helium-3 nuclei interact, they form a helium-4 nucleus (two protons and two neutrons) and release two excess protons back into the plasma.\n\nThe energy released throughout these fusion processes is primarily in the form of kinetic energy of the particles and high-energy gamma-ray photons. The photons generated in the core are gradually absorbed and re-emitted by particles in the Sun's radiative zone, creating a "photon random walk" that can take a significant amount of time—on the order of tens of thousands to hundreds of thousands of years—to reach the convective zone of the Sun. This outward flow of energy by radiative transfer results in a decreasing temperature gradient toward the Sun's surface. \n\nOnce the photons reach the convective zone, where the temperature is lower (around 2 million Kelvin), the density of the plasma is insufficient to maintain radiative transfer, and energy is instead transported by convection. Hot plasma rises while cooler plasma descends, akin to the bubbling of boiling water. The visible surface of the Sun, known as the photosphere, radiates energy as sunlight. While the dominant wavelength of this radiation is in the visible range—owing to the photosphere's temperature of approximately 5800 Kelvin—the Sun emits across the entire electromagnetic spectrum. Importantly, the total energy output from the Sun, known as the solar luminosity, is about 3.86 x 10^26 watts, and it remains relatively stable due to the equilibrium between gravitational pressure inward and radiation pressure outward in the star.
If you do not want to compare the two “by hand”, you can ask GPT of course
In[]:=
LLMFunction["Compare the following two texts `1` and `2` which are answers to the question: 'Which process generates the engery radiated by the sun? Answer in 4 paragraphs. Answer at a level of a postgraduate physics student.' Discuss which answer is better and mark the replies on a scale from 0 to 20."][sunmistral,sungpt]
Out[]=
Both answers are comprehensive and provide a detailed explanation of the process of nuclear fusion in the Sun's core, which generates the energy radiated by the Sun. They both discuss the proton-proton chain reaction, the conditions required for fusion to occur, and the journey of the energy from the core to the surface of the Sun. ​The first answer provides a clear and concise explanation of the process, with a good balance of detail and readability. It also includes an estimate of the time it takes for energy to travel from the core to the surface, which adds an interesting fact that is not included in the second answer. However, it does not mention the annihilation of the positron with an electron, which is a key part of the process. I would rate this answer 17 out of 20.​The second answer is more detailed and technical, providing a more in-depth explanation of the process. It includes the annihilation of the positron with an electron, which is a key part of the process. It also discusses the "photon random walk" and the solar luminosity, which are not mentioned in the first answer. However, it does not provide an estimate of the time it takes for energy to travel from the core to the surface. I would rate this answer 19 out of 20. ​In conclusion, while both answers are excellent, the second answer is slightly better due to its greater level of detail and inclusion of additional key points.
So GPT is convinced that it is doing better than Mistral.

Example 2

Let’s try to produce some Mathematica code with Mistral.
In[]:=
callMistralAI["Write a short piece of Wolfram Language code that displays a Sin function and allows to interactively change its frequency, amplitude and initial phase.",myMistralKey]
Out[]=
Here's a short piece of Wolfram Language code that creates a dynamic interface for interactively changing the frequency, amplitude, and initial phase of the Sin function:​```wolframManipulate[ Plot[a*Sin[2*Pi*f*x + phi], {x, 0, 4}, PlotRange -> {-3, 3}], {{a, 1, "Amplitude"}, 0, 3, 0.1, Appearance -> "Labeled"}, {{f, 1, "Frequency"}, 0, 5, 0.1, Appearance -> "Labeled"}, {{phi, 0, "Phase"}, -Pi, Pi, 0.1, Appearance -> "Labeled"}, ControlPlacement -> Top]```​In this code snippet, `Manipulate` is used to create an interactive interface with three controls (sliders) for adjusting the amplitude, frequency, and initial phase of the Sin function. The `Plot` function is employed to draw the Sin function with user-defined parameters, and `{{variable, initial_value, label}, min_value, max_value, step_size, Appearance -> "Labeled"}` is the format for defining each control.​Copy and paste this code into a Wolfram Language environment, such as Mathematica or Wolfram Cloud, and run it to see the interactive Sin function plot.
Here’s what that does:
In[]:=
Manipulate[​​Plot[a*Sin[2*Pi*f*x+phi],{x,0,4},PlotRange->{-3,3}],​​{{a,1,"Amplitude"},0,3,0.1,Appearance->"Labeled"},​​{{f,1,"Frequency"},0,5,0.1,Appearance->"Labeled"},​​{{phi,0,"Phase"},-Pi,Pi,0.1,Appearance->"Labeled"},​​ControlPlacement->Top​​]
Let’s try the same in GPT:
Again we can execute the code:
It seems that both LLMs give answer of a similar quality.

Example 3

Believe it or not, but this is a very difficult question for an LLM:
If we assume that Confidence: 100% is not part of the answer Mistral gets this one right, which is rare in LLMs. I have not observed it with other models before. (For full disclosure, occasionally, also Mistral gets it wrong.)

Conclusion

I have presented a function to access Mistral AI. It is a very promising model, cheap and powerful. It can be run own your own hardware.
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 Google’s Gemini. I will post further pieces on the speech generation and on Gemini in due course.

CITE THIS NOTEBOOK

Mistral AI via the API in the Wolfram Language (competing with GPT-4)​
by Marco Thiel​
Wolfram Community, STAFF PICKS, December 17, 2023
​https://community.wolfram.com/groups/-/m/t/3085573