DEV Community

El Bruno for Microsoft Azure

Posted on • Originally published at elbruno.com on

💬 #ChatGPT Plugin in NET – GetAll and Add, 2 API endpoints in a single PlugIn

ChatGPT NET PlugIn Series

Hi!

Moving forward in my PetStore plugin, I decided that my plugin must support several actions:

  • List all pets
  • Add a new pet
  • Search for a pet
  • Delete a pet

GetAll and AddNew Operations

In my current implementation, I have 2 operations to handle GetAll and AddNew. Let’s look at the Swagger def:

swagger definition with the GetAll and AddPet operations

It’s also important to remark that the pet uses a specific model that is also part of the Swagger def.

These are the 2 operations. Notice how the AddPet operation uses a typed class to represent a Pet, including pet’s owner information.


// return the list of pets
app.MapGet("/GetAllPets", () =>
{
    Console.WriteLine("GET ALL PETS");
    var petsFile = File.ReadAllText("data/pets.json");
    return Results.Json(petsFile);
})
.WithName("GetAllPets")
.WithOpenApi(generatedOperation =>
{
    generatedOperation.Description = "Gets the list of pets available in El Bruno's Pet catalog.";
    return generatedOperation;
});

// add a new pet
app.MapPost("/AddPet", async (Root newPet) =>
{
    Console.WriteLine($"ADDPET / PET info: {newPet.name}");
    var petsFile = File.ReadAllText("data/pets.json");
    var pets = JsonSerializer.Deserialize<List<Root>>(petsFile);
    pets.Add(newPet);
    petsFile = JsonSerializer.Serialize(pets);
    File.WriteAllText("data/pets.json", petsFile);
    return Results.Ok();
})
.WithName("AddPet")
.WithOpenApi(generatedOperation =>
{
    generatedOperation.Description = "Add a new pet to the pet catalog available in El Bruno's Pet catalog. Requires pet and pet's owner information.";
    return generatedOperation;
});

Enter fullscreen mode Exit fullscreen mode

Testing the PlugIn

Ok, not that the plugin have the 2 operations, let’s register it into the plugin list. And let’s ask a cool question:

What info is needed to add a new pet?

The output is great. The plugin reads the openapi definition, and from there it suggests the needed info,

Time to test and register a new Pet. Let’s use this prompt.

OK, let’s add a new pet. His name is Net, is a calico breed, 17-year-old brown cat. He weighs 3 kilograms. His owner is Bruno Capuano, with the same owner information that you have in the database.

Using a single prompt, I added a new pet. And hey, I didn’t even need to describe all the owner information, I asked ChatGPT to reuse the information from Bruno.

And the expanded Json shows a view of the information posted to the API endpoint. This is especially useful to debug and learn more about how plugins call our functions.

This is super cool. I’ll continue updating the repo with the demo instructions, and a live session will be scheduled soon!

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno


Top comments (0)