A few weeks in the past OpenAI released GPTs. A GPT is mainly a customized ChatGPT chatbot hosted by OpenAI. Its most vital characteristic is the flexibility to increase your GPTs with an API. Magic‘s most vital characteristic is the flexibility to simply create an API.
Magic and GPTs therefor turns into an ideal match, permitting you to create your personal customized GPT, one thing already illustrated in an article we printed a few days in the past demonstrating create a Low-Code CRUD-based GPT in some few seconds.
CRUD after all is kick ass, and essential when coping with your database – Nonetheless, every so often you want some customized performance, permitting you to increase your GPT with a unique piece of code, solely created from scratch utilizing C#. This text demonstrates accomplish this.
The code
First you want some C# code. Create a brand new folder named “cs-demo” inside your “modules” folder, and create a file in it referred to as “slot.cs”. Put the next code in your file.
utilizing System;
utilizing magic.node;
utilizing magic.node.extensions;
utilizing magic.alerts.contracts;
[Slot(Name = "get-employee-details")]
public class Foo : ISlot
{
public void Sign(ISignaler signaler, Node enter)
{
change (enter.GetEx<string>().ToLower())
{
case "thomas":
enter.Add(new Node("title", "cto"));
enter.Add(new Node("cellphone", "90909090"));
enter.Add(new Node("e-mail", "thomas@ainiro.io"));
break;
case "aria":
enter.Add(new Node("title", "coo"));
enter.Add(new Node("cellphone", "91919191"));
enter.Add(new Node("e-mail", "aria@ainiro.io"));
break;
case "tage":
enter.Add(new Node("title", "ceo"));
enter.Add(new Node("cellphone", "92929292"));
enter.Add(new Node("e-mail", "tage@ainiro.io"));
break;
}
}
}
If you’re carried out your Hyper IDE ought to resemble the next minus the “get-employee-details.get.hl” file which we are going to come again to additional down on this article.
Rationalization of C# code
The above code declares a slot referred to as [get-employee-details]. It takes a single parameter being some identify, and returns information for Tage, Thomas, or Aria – Relying upon the worth of your argument. As soon as compiled, the above code may be invoked from Hyperlambda utilizing the next in your Hyperlambda Playground.
get-employee-details:Thomas
Considered one of Hyperlambda’s essential characteristic is the flexibility to go in and return graph objects out of your slots. The above C# code assumes the caller handed in a reputation as the worth of the invocation, being the only real parameter to your C# code, for then to return 3 new nodes as kids of your invocation. After executing the above code in your Hyperlambda Playground you’ll find yourself with the next consequence.
get-employee-details:Thomas
title:cto
cellphone:90909090
e-mail:thomas@ainiro.io
Discover, the GetEx<string>()
invocation in our C# code permits us to guage lambda expressions in our code.
Creating our API endpoint
We have to compile the code earlier than we are able to use it although, and wrap it into an HTTP endpoint. Considered one of Magic’s options is having the ability to nearly use C# as a scripting language – Implying we are able to compile the code on the fly, and execute it nearly the identical means we would usually use a scripting language similar to JavaScript or Python.
This characteristic permits us to easily wrap the compilation course of inside our Hyperlambda endpoint, compile the code on the fly, execute it, and return the results of our execution. Create a brand new file referred to as “get-employee-details.get.hl” inside your “cs-demo” folder and put the next content material in your file.
.arguments
identify:string
.description:Returns the title, cellphone and e-mail for the required worker
// Masses file, compiles it, and hundreds the ensuing meeting.
io.file.load:/modules/cs-demo/slot.cs
system.compile
references
.:netstandard
.:System.Runtime
.:System.ComponentModel
.:System.Non-public.CoreLib
.:magic.node
.:magic.node.extensions
.:magic.alerts.contracts
code:x:@io.file.load
assembly-name:workers.dll
system.plugin.load:x:@system.compile
// Executes our C# slot.
get-employee-details:x:@.arguments/*/identify
// Returns the results of our C# code.
return:x:@get-employee-details/*
Within the video under we’re making use of some clever caching to keep away from recompiling the code on each single request in the direction of our endpoint, however to maintain the code simply understood, I eliminated these elements within the above code. Nonetheless, with the above code we’re now essentially carried out, and we are able to already join our GPT with our API endpoint. Use the Supervisor/Endpoints part in Magic to execute your endpoint similar to illustrated under.
Be sure you filter on “worker” to seek out the endpoint, after which add “thomas” as your identify parameter.
Rationalization of Hyperlambda code
The above Hyperlambda code hundreds your C# file, then it compiles the code producing a uncooked byte[]
that we dynamically load as an meeting, injecting the meeting into our AppDomain. This suggests you’ll be able to change your C# code with out manually triggering any recompilation, and the brand new code will probably be dynamically compiled “on the fly” and replicate your adjustments instantly.
But once more, with Magic and Hyperlambda you’ll be able to deal with C# nearly as if it was a scripting language.
In an actual world utility it would be best to keep away from recompiling the C# code on each single invocation, because it’s an costly course of. I’m strolling you thru some tips associated to this within the video additional down on the web page – However to maintain the code clear and simply understood, I averted this within the above instance.
Join your endpoint to a GPT
At this level all we have to do is to attach our API endpoint to a customized GPT. Create a new GPT and be sure you filter your endpoints on “openapi” in your Magic’s endpoints part. This could provide you with one endpoint resembling the next.
When you will have carried out the above you’ll be able to both copy the ensuing JSON or copy the URL to your endpoint with the copy button in your endpoints part. At this level all we have to do is to present this OpenAPI specification to OpenAI as an “Motion” for our GPT, and we’re carried out.
Testing your GPT
At this level you are essentially carried out, and you may already begin testing your GPT. You’ll be able to ask it questions similar to:
- What’s Tage’s cellphone quantity?
- Thomas is an worker, search for his info utilizing my get worker motion.
- What’s Aria’s e-mail handle?
- And many others …
Under is an instance of consuming our GPT.
As you’ll be able to see above our GPT is now accurately passing in “Tage” to our API endpoint, which once more returns Tage’s e-mail handle, and makes use of the e-mail handle in its conversations with you. At this level you’ll be able to create as an illustration a “Ship e-mail” endpoint the place ChatGPT helps you compose an e-mail, and sends an e-mail to Tage – However that is an train for one more day. For now you’ll be able to play with the customized GPT I created under.
Watch me stroll via your complete course of within the following video.