Sample Code using the new OpenAI library for .NET

El Bruno - Jun 7 - - Dev Community

Hi!

The new OpenAI SDK for NET was officially announced, so today, let’s review the annoucement and show some sample code on how to use it.

The current blog scenarios are

  • Sample Chat demo
  • Sample Audio to Text demo
  • Sample Image analisis demo

Here is a sample output for the chat demo! And yes, with a funny system message and questions about France!

sample output for the chat scenario

Code Repository: GPT-4o Labs

OpenAI SDK for .NET

Microsoft Build 2024 has unveiled new AI investments for .NET developers, including the first beta release of the official OpenAI .NET library, version 2.0.0-beta.1. This library facilitates smooth integration with OpenAI and Azure OpenAI, complementing existing libraries for Python and TypeScript/JavaScript.

Developed on GitHub, the .NET library will stay current with OpenAI’s latest features, with ongoing work to refine it based on community feedback. The release acknowledges Roger Pincombe’s pioneering work on the initial OpenAI .NET package and encourages continued innovation from community library developers. Participation and collaboration within the community are highly encouraged as the project progresses.

_ Note: Part of the content of this post was generated by Microsoft Copilot, an AI assistant._

Scenario 1: Chat

Time to share the “Hello World” of using GPT models. The following sample is a console application that interacts with the OpenAI API to generate chat responses.

  • It retrieves an API key from user secrets and specifies the model to be used for the chat.
  • It initializes a ChatClient with the model and API key.
  • The system message and user question are defined and added to a list of chat messages.
  • The chat is then completed using the ChatClient and the response from the chat is retrieved.
  • Finally, the system prompt, user question, and chat response are displayed in the console.

This is the sample output for this program:

System Prompt: You are a useful assitant that replies using a funny style.

User Question: What is the capital of France?

Response: Well, let me put on my fancy beret and sip some imaginary café au lait while I tell ya – the capital of France is the one and only Paris! Yes, indeed, the city of love, croissants, and an Eiffel Tower that’s basically the world’s largest toothpick! So, if you’re planning to hunt for baguettes or have a romantic escapade, Paris is where the magic happens! ??????

Scenario 2: Audio

The SDK also allow us to work with Whisper and audio files.

The following code is a C# console application that uses the OpenAI API to transcribe audio files.

  • It sets up the configuration to retrieve the OpenAI API key and specifies the model to be used for transcription.
  • It creates an AudioClient instance with the model and API key.
  • The audio file to be transcribed is specified, and transcription options are set, including the response format and granularities for timestamps.
  • The audio file is then transcribed, and the transcription text is printed to the console.
  • Additionally, the start and end times for each word and segment in the transcription are also printed to the console.

This is a sample output for this program:


Transcription:
Estás escuchando No Tiene Nombre, un podcast sobre tecnología que es bastante probable que sea escrito por una inteligencia artificial, por ejemplo, con chat GPT. El host, autor, editor y encargado de los efectos visuales del podcast es Bruno Capuano. Puedes contactarlo en Twitter en arroba elbruno o en las redes sociales también buscando por elbruno. Mi nombre es Elena, de Italia YOS invito al episodio de hoy. Subtítulos por la comunidad de Amara.org

Words:
            Estás : 700 - 1420
       escuchando : 1420 - 2060
               No : 2060 - 2420
            Tiene : 2420 - 2580
           Nombre : 2580 - 3000
               un : 3340 - 3500
          podcast : 3500 - 3780
            sobre : 3780 - 4100
       tecnología : 4100 - 4720
              que : 4720 - 4960
               es : 4960 - 5120
         bastante : 5120 - 5560
         probable : 5560 - 6020
              que : 6020 - 6260
              sea : 6260 - 6480
          escrito : 6480 - 6820
              por : 6820 - 7100
              una : 7100 - 7460
     inteligencia : 7460 - 7820
       artificial : 7820 - 8380
              por : 8820 - 9020
          ejemplo : 9020 - 9300
              con : 9680 - 9880
             chat : 9880 - 10060
              GPT : 10060 - 11340
               El : 11360 - 11760
             host : 11760 - 12100
            autor : 12660 - 12660
           editor : 13140 - 13140
                y : 13140 - 13340
        encargado : 13340 - 13740
               de : 13740 - 13920
              los : 13920 - 14300
          efectos : 14300 - 14420
         visuales : 14420 - 14860
              del : 14860 - 15020
          podcast : 15020 - 15400
               es : 15400 - 15760
            Bruno : 15760 - 15920
          Capuano : 15920 - 16400
           Puedes : 17640 - 17700
      contactarlo : 17700 - 18320
               en : 18320 - 18500
          Twitter : 18500 - 18740
               en : 18740 - 18920
           arroba : 18920 - 19160
          elbruno : 19160 - 19540
                o : 19540 - 19960
               en : 19960 - 20080
              las : 20080 - 20360
            redes : 20360 - 20360
         sociales : 20360 - 20800
          también : 20800 - 21180
         buscando : 21180 - 21560
              por : 21560 - 21960
          elbruno : 21960 - 22340
               Mi : 23480 - 23560
           nombre : 23560 - 23840
               es : 23840 - 24280
            Elena : 24280 - 24440
               de : 24780 - 24940
           Italia : 24940 - 25360
              YOS : 25360 - 26180
           invito : 26180 - 26660
               al : 26660 - 26880
         episodio : 26880 - 27340
               de : 27340 - 27720
              hoy : 27720 - 27720
       Subtítulos : 30000 - 31540
              por : 31540 - 31540
               la : 31540 - 31540
        comunidad : 31540 - 31540
               de : 31540 - 31540
            Amara : 31540 - 31540
              org : 31540 - 31540

Segments:
      Estás escuchando No Tiene Nombre, un podcast sobre tecnología que es bastante probable : 700 - 6480
                 que sea escrito por una inteligencia artificial, por ejemplo, con chat GPT. : 6480 - 12660
    El host, autor, editor y encargado de los efectos visuales del podcast es Bruno Capuano. : 13140 - 18500
   Puedes contactarlo en Twitter en arroba elbruno o en las redes sociales también buscando por elbruno. : 18500 - 26180
                                Mi nombre es Elena, de Italia YOS invito al episodio de hoy. : 26180 - 27720
                                                    Subtítulos por la comunidad de Amara.org : 30000 - 31540

Enter fullscreen mode Exit fullscreen mode

Scenario 3: Using Vision with GPT4o.

And the final scenario will be on how to use vision capabilities with this new SDK. The following program project is a C# console application that uses the OpenAI API to interact with an AI assistant.

  • It starts by reading the OpenAI API key from the user secrets and initializing the OpenAI client with this key.
  • It uploads an image file named foggyday.png from the imgs directory to the OpenAI server for the purpose of vision tasks.
  • After the image is uploaded, it creates an AI assistant with a specific instruction to reply in a funny style.
  • It starts a new thread with the assistant, sending an initial message asking the assistant to describe the uploaded image.
  • The program then enters a loop, listening for updates from the assistant.
  • When a new message is received from the assistant, it is printed to the console.
  • The program continues to listen for updates until it is manually stopped.

This is a sample output for this program, and below is the image used to test:


--- Run started! ---
Well, well, well! What do we have here? Is that paradise? A spa? Or just someone's backyard on a dreamy, misty morning? There's a pristine blue pool just begging for a cannonball, surrounded by neat paving stones that scream, "We mean business!" Looming over the scene is a terrific tree that looks like it's auditioning for the role of "Majestic Sentinel."

In the distance, the fog is putting up a great show, hiding whatever secrets lie beyond this yard-maybe a herd of unicorns or a neighborhood of lawn gnomes planning their next move. The whole scene feels like it's on the set of the next great mystery movie... or maybe just an ad for pool cleaning services. Either way, sign me up for a dip!
D:\sk\gpt4ol-sk-csharp\src\OAINETSDK_lab03\bin\Debug\net8.0\OAINETSDK_lab03.exe (process 3668) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

Enter fullscreen mode Exit fullscreen mode

picture of a backyard in a foggy day

Conclusion

The new OpenAI SDK for .NET is a great one. I’ll keep testing and sharing samples, for each new scenario supported by the SDK!

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.


. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player