Hi everyone, if you want to place an API call to Claude from Anthropic, here is a snippet that works - it took me several hours and help from support to figure out how to re-format the JSON in Text Blaze, so I hope this will help anyone looking for the same thing:
-
You need to paste your own working API key, otherwise it will provide an error. (Details on how to obtain one: Getting access to Claude )
-
It needs some processing time, wait for the rotating arrows on the gray bar to stop. The example should return a 4 line poem in about 4 to 10 seconds once you pasted a valid API key
API Key: {formtext: name=APIKey; default=Enter YOUR API key here} {note}Please ensure the API Key is accurately entered. {endnote}
Temperature: {formtext: name=Temperature; default=0.7}
Max Tokens: {formtext: name=MaxTokens; default=4000}
Model: {formmenu: claude-3-opus-20240229; default=claude-3-sonnet-20240229; claude-3-haiku-20240307; name=Model} (Haiku = fastest, Opus = better details, Sonnet = in between)
{note} {formparagraph: name=Prompt; cols=40; rows=10; default=Please create a 4 line poem about ice cream}{endnote}
{formtoggle: name=Generate text; default=no; trim=left}{myModel=Model}
{myMessages=[[ "role": "user", "content": {=Prompt} ]]}
{urlload: https://api.anthropic.com/v1/messages; method=POST; headers=x-api-key:{=APIKey}, anthropic-version:2023-06-01, content-type:application/json; body={=toJSON([ "model": myModel, "max_tokens": MaxTokens, "temperature": Temperature, "messages": myMessages ], "{ model: string, max_tokens: number, temperature: number, messages: {role: string, content: string }[" & "] }")}; done=(contents, status)->["loading": no, "contents": fromJSON(contents), "status": status] }
{endformtoggle}{if: `generate text`=="no"}{error: Check "Generate text" to create the text; block=yes}{endif}
{if: `generate text`=="yes"}{note}PLEASE BE PATIENT. This might take a while:
There should be two gray arrows moving on the right side of the gray "URL-Call-Bar", indicating the AI is still working.
It could take up to 4:59 minutes (299 seconds) for complex tasks & long outputs with Opus (about 60 seconds with Haiku),
easy tasks with medium outputs should be finished in 3 to 15 seconds with Haiku (e. g. 9 seconds for a summary of a 12000 word text). {endnote} {endif}
Request Status:
{=status}
URL Contents:
{=contents["content"][1]["text"]}
-
You can select the model and temperature, enter/modify the prompt and when you check the "generate" checkbox, it'll start calling the API (I used the "prompt" field + "generate" checkbox just for testing and call it with the clipboard content instead, with a fixed prompt in front)
-
API documentation: Create a Message
-
The maximum Output length is 4096 tokens (about 3000 words depending on the language), context window is 200000 tokes (about 150k words) and the maximum I tested was to summarize a 12000 (12k) word text which model Haiku completes in about 5-8 seconds.
-
Here is a list of models. Haiku is the fastest, it returns 400 lines of ice cream poem (560 words, 3300 characters) in 12 seconds. Opus is the most intelligent one, but it needs 65 seconds for a 200 line poem. At 500 lines, they've reached their output limit (but the input can be much longer, I processed srt-files with 3000 lines in 7 seconds for a summary).
Paste the desired model into "myModell":
Model Latest API model name
Claude 3 Opus claude-3-opus-20240229
Claude 3 Sonnet claude-3-sonnet-20240229
Claude 3 Haiku claude-3-haiku-20240307
From: https://docs.anthropic.com/claude/docs/models-overview
- When Posting any such snippet or any modification of this, be carefull:
a.) At the end of the toJSON is a pair of square brackets that the forum software will "delete" when you paste it, preventing it from working. It took me 2 hours and the help of other users here to fix this, very hard to debug. Here's the solution for you and thanks @scott for your help:
If you take a snippet that works perfectly inside textblaze and post it here, you have to replace the square brackets with ] " & " [ or the snippet looses the 2 square brackets at the end of the toJSON and won't work. Details see screenshots and discussion below.
b.) The same for the ´ character in the if-statement, you have to replace it with \´
And one last thing: It seems like TextBlaze will strip any double spaces from any output if there is any HTML/Bold text in your snippet. This will destroy any structured list with sub-items AI models return as Markdown. The solution is here — remove ANY formatting from the snippet. But since this will make the snippet a lot harder to read IMHO, I left it like it is and linked to the solution here: What to do if TextBlaze removes double spaces needed for formatting. - simply use the 3-step-solution "when you don't need HTML code".
Enjoy!