Claude.ai -- Opus & Sonnet & Haiku
Claude.ai -- Opus & Sonnet & Haiku
In a blog-post on 4th May 2024 Anthropic introduced a new family of Large Language Models; the blog post shows a benchmark table, which suggests that the most advanced model, Opus, outperforms GPT-4 in all listed benchmarks. In this blog post, I will make a couple of functions (that were ironically produced with GPT-4) available to interact with Claude.
The most advanced model, which also is the most expensive one, is called OPUS. A medium model and less costly called Sonnet was released at the same time as OPUS. The cheapest, but apparently still high performing model called Haiku was made available some days after the initial release of the first two models.
API access to Anthropic’s models
API access to Anthropic’s models
Similar to OpenAI, Anthropic offers access to the models via a web-interface and also via an API. In fact, access to earlier models of Anthropic is built into Mathematica. You can open a Chat-Enabled notebook and will be able to choose Claude Instant 1 and Claude 2 to work with.
At the time of writing there is no option for Claude 3 within the interface.
In order to gain access to the API, you do not need a monthly subscription. If you make an account on Anthropic’s website, you get (at the time of writing) a $5 budget to play with the API. You can then top-up the account as usual. The page will also give you the API key. I will assume that you have added the respective key to your SystemCrediential like so:
SystemCredential["ANTHROPIC_API_KEY"]="YOUR KEY GOES HERE";
We will first implement (well, we’ll point to the documentation and have GPT-4 program this....) a function to directly talk to the Claude-3 Opus version of the models:
callClaude[myPrompt_,myAPIKey_]:=Module[{myRequest,jsonResponse,jsonBody},(*PreparetheJSONbodyoftherequest*)jsonBody=ExportString[{"model"->"claude-3-opus-20240229","max_tokens"->4096,"messages"->{{"role"->"user","content"->myPrompt}}},"JSON"];(*CreatetheHTTPrequest*)myRequest=HTTPRequest["https://api.anthropic.com/v1/messages",<|"Method"->"POST","Body"->jsonBody,"ContentType"->"application/json","Headers"->{"x-api-key"->myAPIKey,"anthropic-version"->"2023-06-01"}|>];(*ExecutetherequestandgettheJSONresponse*)jsonResponse=URLExecute[myRequest];(*Convertthelistofrulestoanassociationforeasieraccessbykey*)jsonResponse=Replace[jsonResponse,l_List:>Association[l],{0,Infinity}];(*Checkiftheresponsecontainstheexpectedcontentkey*)If[!KeyExistsQ[jsonResponse,"content"],(*Ifthe'content'keyismissing,return$Failed*)Return[$Failed]];(*Extractthe'content'andthenthe'text'content*)jsonResponse["content","text"]]
A typical call would look tike this:
In[]:=
callClaude["Take the total engery per time, i.e. power, produced by the sun in Watts. Then use the sun's volume to compute the power production per cubic metre in Watts per cubic metre. Then compare that to the power (engergy per time) produced in a heap of cow manure.",SystemCredential["ANTHROPIC_API_KEY"]]
Out[]=
To solve this problem, we need to find the total power produced by the sun, calculate the sun's volume, and then compare the power production per cubic metre of the sun to that of a heap of cow manure.Given:- The sun's power output (luminosity) is approximately 3.8 × 10^26 Watts.- The sun's radius is about 6.96 × 10^8 metres.Step 1: Calculate the sun's volume.Volume of a sphere = (4/3) × π × radius^3Sun's volume = (4/3) × π × (6.96 × 10^8)^3Sun's volume ≈ 1.41 × 10^27 cubic metresStep 2: Calculate the power production per cubic metre of the sun.Power per cubic metre = Total power / VolumeSun's power per cubic metre = (3.8 × 10^26) / (1.41 × 10^27)Sun's power per cubic metre ≈ 0.27 Watts per cubic metreStep 3: Compare the sun's power per cubic metre to that of a heap of cow manure.A typical value for the power production of a cubic metre of cow manure during decomposition is approximately 10 Watts per cubic metre.Therefore, a heap of cow manure produces about 37 times more power per cubic metre than the sun (10 Watts per cubic metre for cow manure compared to 0.27 Watts per cubic metre for the sun).
If you intend to use the other two variants of the model you can substitute the “claude-3-opus-20240229” by
“claude-3-sonnet-20240229” or “claude-3-haiku-20240307” respectively. We will play with these options later.
“claude-3-sonnet-20240229” or “claude-3-haiku-20240307” respectively. We will play with these options later.
Now that the “first contact” to Claude 3 has been made, I offer some additional options for our interactions. First, I want to introduce a similar function with includes the “system” (as opposed to “user”) input. The system input is useful to provide high priority instructions to the model.
callClaudeSys[myPrompt_,myAPIKey_,systemMessage_:None]:=Module[{myRequest,jsonResponse,jsonBody,basePayload},(*PreparethebaseJSONpayload*)basePayload={"model"->"claude-3-opus-20240229","max_tokens"->2048,"messages"->{{"role"->"user","content"->myPrompt}}};(*Addthesystemmessageifitisprovided*)If[systemMessage=!=None,AppendTo[basePayload,"system"->systemMessage]];(*ConvertthepayloadtoaJSONstring*)jsonBody=ExportString[basePayload,"JSON"];(*CreatetheHTTPrequest*)myRequest=HTTPRequest["https://api.anthropic.com/v1/messages",<|"Method"->"POST","Body"->jsonBody,"ContentType"->"application/json","Headers"->{"x-api-key"->myAPIKey,"anthropic-version"->"2023-06-01"}|>];(*ExecutetherequestandgettheJSONresponse*)jsonResponse=URLExecute[myRequest];(*Convertthelistofrulestoanassociationforeasieraccessbykey*)jsonResponse=Replace[jsonResponse,l_List:>Association[l],{0,Infinity}];(*Checkiftheresponsecontainstheexpected'content'key*)If[!KeyExistsQ[jsonResponse,"content"],(*Ifthe'content'keyismissing,return$Failed*)Return[$Failed]];(*Extractthe'content'andthenthe'text'content*)jsonResponse["content","text"]]
Technically, you can use this function instead of the previous one, as it works with no system input at all as well. Here is a typical use case:
In[]:=
callClaudeSys["List three features that make the Wolfram Language special!",SystemCredential["ANTHROPIC_API_KEY"],"Answer in Spanish."]
Out[]=
Aquí hay tres características que hacen especial al lenguaje de programación Wolfram:1. Tiene una enorme cantidad de conocimiento integrado, que abarca matemáticas, ciencia, finanzas, geografía y muchas otras áreas. Esto permite un rápido desarrollo de aplicaciones sofisticadas.2. Soporta programación simbólica y numérica, combinando el poder del álgebra computacional con cálculos numéricos de alta precisión. Puede manipular ecuaciones y expresiones simbólicamente.3. Tiene capacidades integradas de visualización y presentación gráfica de datos muy potentes. Puede generar fácilmente gráficos interactivos y visualizaciones usando simples comandos.En resumen, la amplia base de conocimientos, el manejo simbólico y las capacidades gráficas hacen a Wolfram Language una herramienta muy poderosa y versátil para computación técnica y desarrollo de software.
The system message has a high priority and can be used to adjust the behaviour of the LLM.
Image Analysis with Claude.ai
Image Analysis with Claude.ai
All three variants of the model come with the ability to analyse image. Here is piece of code to use this.
I will use a simple image of a peacock to demonstrate its abilities.
One of the important features is that the smallest model “Haiku” is very cost efficient. This is particularly relevant for the analysis of many images. I am using here a screenshot from a text I wrote.
Note that I change the model to the Haiku version:
Conversation with Claude
Conversation with Claude
Additionally to the functions presented so far, we might want to have access to the ability to “discuss” with Claude, i.e. make it aware of the previous prompts and responses so that we can get to better answers using train of thought prompting. Here is a function that can help us to achieve that:
The main idea is that we always submit the entire discussion so far, and the function only produces the latest answer and adds it to the total discussion, which we also have access to via the $conversationHistory variable. The third, optional argument of the function is to be set to True if we want and a discussion-thread and start a new one.
If you want to export the entire conversation you can export it like so:
Programming with Claude
Programming with Claude
When it comes to coding, I prefer train of thought prompting instead of single shot prompting. Let’s use the discussion version from the last section for this.
Conclusion
Conclusion
In this post I introduce some basic functionality to interact with the latest models by Anthropic. This ranges from the small Haiku model which offers a highly cost efficient tool, which allows for image analysis, up to Opus, a tool that excels at reasoning and produces human like text. I hope that some members of this community will find these tools useful. Let me finish by asking Claude why a Mathematica user should use it.
(I will add a Wolfram Language script which contains the functions shown in this post, and similar functionality I have developed for other API to access LLMs.)
(I will add a Wolfram Language script which contains the functions shown in this post, and similar functionality I have developed for other API to access LLMs.)
CITE THIS NOTEBOOK
CITE THIS NOTEBOOK
API access to Claude 3 and use cases: image processing, conversation and programming
by Marco Thiel
Wolfram Community, STAFF PICKS, March 17, 2024
https://community.wolfram.com/groups/-/m/t/3142280
by Marco Thiel
Wolfram Community, STAFF PICKS, March 17, 2024
https://community.wolfram.com/groups/-/m/t/3142280