Skip to content

About

Tutorial of C# OpenAI SDK

Resources

Stars

1 star

Watchers

0 watching

Forks

 
 

Latest commit

 

History

18 Commits

Folders and files

Repository files navigation

Zero To Hero for C# Programmers - OpenAI Library Tutorial

This tutorial provides step-by-step instructions for using the Azure OpenAI client Library in a C# application.

Prerequisites

  • Deploy the OpenAI Service on Azure.

Configuration

  1. Copy appsettings.json.example to appsettings.json.
  2. Fill in the required settings in appsettings.json.

Step 1: Simple Conversation

Learn the fundamentals of the ChatCompletionAPI. The LLM is stateless, so you always need to send a request with all contexts.

  1. Switch to the step1 branch.

  2. Start the project.

  3. Interact with the LLM. Your input becomes the User Prompt. Modify the System Prompt as needed.

  4. Debug the system and inspect the payload of the Request and Response in the ConversationExecutor.

  5. Update the System Prompt. For example: When I ask for help to write something, you will reply with a document that contains at least one joke or playful comment in every paragraph.

  6. Read about Prompt Engineering and experiment with different patterns.

Step 2: History

In Step 1, the history feature was not implemented. The LLM doesn't remember the history, so you need to provide it with the history information.

  1. Switch to the step2 branch. Refer to the commit.
  2. Implement the history feature and observe its functionality.
  3. Debug the ConversationExecutor to see how it behaves.
  4. Interact with the chat and verify if it retains the history.

Step 3: Function Calling

Learn how to call external functions from the LLM. This is useful for real-world scenarios where you need to call external systems or logic, such as Azure AI Search, Cosmos DB, or Kusto Cluster. This is known as Function Calling.

  1. Switch to the step3 branch. Refer to the commit.
  2. Implement the function calling feature. Modify the definition/implementation as needed.
  3. Provide your name to the LLM and ask, "Could you tell me which OpenAI Service should I use?".
  4. Modify the System Prompt or functionDescription and observe the behavior differences. Stabilizing the system response is key to AI software development.
  5. Add new function calls to your bot.

Step 4: Structured Output

For more deterministic results, consider using structured output. This is useful for obtaining specific types of results.

  1. Switch to the step4 branch. Refer to the commit.
  2. Implement the structured output feature. Modify the definition/implementation as needed.

Note: Check the supported models for Structured Output.

Step 5: Managing Tokens

Learn how to count the number of tokens consumed in advance. Compare the estimated token count with the actual token count, as LLM usage is charged by tokens.

  1. Switch to the step5 branch. Refer to the commit. Read the code and run debug and learn how token management works in ConversationExecutor. Understand how token is managed before/after the ChatCompletion API call.

For managing token, we use Microsoft.DeepDev.TokenizerLib but should migrate to Microsoft.ML.Tokenizers.

Conclusion

By completing this tutorial, you will understand 80% of the techniques used in AI coding for developing a C# application. Enjoy your learning journey!

Reference

About

Tutorial of C# OpenAI SDK

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages