This tutorial provides step-by-step instructions for using the Azure OpenAI client Library in a C# application.
- Deploy the OpenAI Service on Azure.
- Copy
appsettings.json.exampletoappsettings.json. - Fill in the required settings in
appsettings.json.
Learn the fundamentals of the ChatCompletionAPI. The LLM is stateless, so you always need to send a request with all contexts.
-
Switch to the step1 branch.
-
Start the project.
-
Interact with the LLM. Your input becomes the User Prompt. Modify the System Prompt as needed.
-
Debug the system and inspect the payload of the
RequestandResponsein the ConversationExecutor. -
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. -
Read about Prompt Engineering and experiment with different patterns.
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.
- Switch to the step2 branch. Refer to the commit.
- Implement the history feature and observe its functionality.
- Debug the ConversationExecutor to see how it behaves.
- Interact with the chat and verify if it retains the history.
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.
- Switch to the step3 branch. Refer to the commit.
- Implement the function calling feature. Modify the definition/implementation as needed.
- Provide your name to the LLM and ask, "Could you tell me which OpenAI Service should I use?".
- Modify the
System Promptor functionDescription and observe the behavior differences. Stabilizing the system response is key to AI software development. - Add new function calls to your bot.
For more deterministic results, consider using structured output. This is useful for obtaining specific types of results.
- Switch to the step4 branch. Refer to the commit.
- Implement the structured output feature. Modify the definition/implementation as needed.
Note: Check the supported models for Structured Output.
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.
- 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.
By completing this tutorial, you will understand 80% of the techniques used in AI coding for developing a C# application. Enjoy your learning journey!
- OpenAI client library for .NET - Microsoft Azure SDK
- openai-dotnet - OpenAI SDK
- examples/chat OpenAI SDK samples