Using Jev in MATLAB
Recently, it seems that everyone is talking about the new AI model in town, Jev. Built by a company called TypeSafe AI, Jev is a completely different type of AI to the ones we have gotten used to in recent years. There's no chat bot for a start! It doesn't produce code and it doesn't explain its reasoning to you. So, what does it do?
TypeSafe AI say that Jev is their first "System One" model and go on to explain that "System One models make fast, structured decisions for software." I wondered what this might mean to a MATLAB user like me whose experience level of this technology is "Has watched a couple of YouTube introductory videos about Jev"
What types of questions can I ask Jev?
Jev understands natural language and you can ask it three types of question: Choice, Score and Noul.
- Choice: Answers "Which of these options"
- Score: Answers "Which level"
- Noul: Answers "Is this true?"
I defer to TypeSafe AI's explanation of these question primitives for more details since I only started getting my head around them myself this morning.
I decided to zoom in on the simplest example: The Noul, which returns the probability that the answer is "yes, this is true".
I asked Jev: Can this MATLAB loop be replaced by a vectorized expression with no loop?
Is the following MATLAB code vectorizable?
total = 0;
for k = 1:numel(x)
total = total + x(k)^2;
end
Let's ask Jev what it thinks.
Setting up the TypeSafe API key in MATLAB using the MATLAB Vault
I got an account on TypeSafe AI and requested an API key. This was a string that I put into my MATLAB vault with the line
setSecret("JevAPIKey")
A secure dialogue box popped up where I could paste my API key and now it is one of my MATLAB Vault secrets
listSecrets
apiKey = getSecret("JevAPIKey");
Creating the request to Jev
The API to Jev is a single JSON endpoint: You POST state, model and questions to https://api.typesafe.ai/v1/systemone. Let's set up the URL and model
url = "https://api.typesafe.ai/v1/systemone";
model = "jev-latest";
Now we define our question type, a noul, and give it some instructions
question.type = "noul";
question.instructions = "Can this MATLAB loop be replaced by a vectorized expression with no loop?";
Now we define the state which is the thing Jev is being asked about. In this case, our code snippet
code = join([
"total = 0;"
"for k = 1:numel(x)"
" total = total + x(k)^2;"
"end"], newline);
From everything we've written so far, we form our request to Jev as a MATLAB struct.
request.model = model;
request.state = code;
request.questions.is_vectorizable = question;
Jev expects requests to be in the form of JSON objects. By setting the MediaType to "application/json", webwrite runs our data through jsonencode so struct fields become JSON keys, strings become JSON strings and nested structs become nested JSON objects.
This way, we can think in MATLAB and let webwrite take care of the JSON.
opts = weboptions("MediaType", "application/json", "HeaderFields", {'Authorization', char("Bearer " + apiKey)});
response = webwrite(url, request, opts);
OK. So, what's the answer? Does Jev think the code is vectorizable?
response.answers.is_vectorizable.noul
The answer is a probability so Jev is 99% sure that we can rewrite this for-loop in vectorized form and, of course, we can.
total = sum(x.^2, "all");
However, Jev will not give us this code. It only told us that it is 99% sure that such code was possible. Jev is not a code-writer, it is a decision maker and TypeSafe AI claim that it is a very fast and cheap decision maker compared to other AI models.
Asking Jev about several MATLAB code snippets at once
I can ask Jev about several MATLAB code snippets at once. Let's create a structure containing a bunch of code snippets; starting with the sum of squares example that I used above.
snippets.sum_squares = join([
"total = 0;"
"for k = 1:numel(x)"
" total = total + x(k)^2;"
"end"], newline);
snippets.clip_negatives = join([
"for k = 1:numel(x)"
" if x(k) < 0"
" x(k) = 0;"
" end"
"end"], newline);
snippets.logistic_map = join([
"for k = 1:n-1"
" x(k+1) = r*x(k)*(1 - x(k));"
"end"], newline);
snippets.collatz_steps = join([
"steps = 0;"
"while n ~= 1"
" if mod(n, 2) == 0"
" n = n/2;"
" else"
" n = 3*n + 1;"
" end"
" steps = steps + 1;"
"end"], newline);
snippets.exp_smooth = join([
"y = zeros(size(x));"
"y(1) = (1-a)*x(1);"
"for k = 2:numel(x)"
" y(k) = a*y(k-1) + (1-a)*x(k);"
"end"], newline);
We now have five questions to ask so we form our list of questions in the request as follows.
request.state = snippets;
request.questions = struct();
for name = string(fieldnames(snippets))'
request.questions.(name) = struct("type", "noul", "instructions", ...
"Can the loop in `" + name + "` be replaced by a vectorized MATLAB expression with no loop?");
end
Let's look at one of these questions:
request.questions.clip_negatives
The backticks around clip_negatives have meaning here. They are TypeSafe's convention for pointing at a named field in the state (The snippets struct in this case), which is how each question knows which of the five snippets it's about.
Let's send the request to Jev and time how long it takes to respond for all 5 of these questions
t = tic;
response = webwrite(url, request, opts);
elapsed = toc(t)
Now I want to construct the results table by pulling each probability out by snippet name.
names = string(fieldnames(snippets));
p = zeros(size(names));
for k = 1:numel(names)
p(k) = response.answers.(names(k)).noul;
end
results = table(names, p, VariableNames=["snippet", "p_vectorizable"])
So for three of the examples, Jev is pretty sure that it's possible to vectorize the for loops. For the other two, it's reasonably sure that you cannot. I am inclined to agree with it. Let me know if you disagree.
One point of interest here is that when I asked Jev about the sum of squares example earlier, it gave me a probability of 0.99 that it could be vectorized and it was always 0.99 every time I asked. Here, it has changed to 0.98. The actual decision hasn't changed; it's pretty sure you can vectorise it but the exact probability is different now. I think that this is because the state is different in the two cases. In my first example, the state only contained one code snippet but the second contained all five and this difference shifts the probabilities; perhaps because it has more (or just different) context?
Finally I can ask the response about token usage, the exact model used and report the timing we made earlier
fprintf("\nModel: %s | %d input tokens | %d output tokens | %.0f ms round trip\n", ...
response.model, response.usage.input_tokens, response.usage.output_tokens, elapsed * 1000);
At the time of writing, TypeSafe AI say that output tokens are not billed and input tokens cost $0.042 per million so this request would have cost me 0.0027 cents. Put another way, I could make 370 requests like this one for 1 cent.
What can you imagine using Jev in MATLAB for?
This is a very simple example showing how easy it is to get started with this new AI model in MATLAB. I genuinely have no idea if the example I've used is a good one or not; I've only been using the technology for a couple of days! I just wanted to show you how to use it in MATLAB with a MATLAB-y example.
TypeSafe AI gives many examples of how they think the model could be useful. I'm wondering what you think. How do you think you might use Jev in MATLAB?


댓글
댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.