Your First Chat Completion with Azure OpenAI Using C# and Python
Gehan Fernando
Solutions Architect | Technical Consultant | Backend Developer | .NET, C#, Python Expert | Azure & DevOps Specialist | Microservices & Scalable System Design | Problem Solver
Last week I got a cool coding challenge that taught me a lot. I want to share what I learned with you all
The task was simple: build a small app that could answer questions about product specs. Think of questions like "How wide is product X?" or "What's the height of model Y?" All we needed was a way for people to ask questions in normal, everyday language and get the right details back quickly.
Connecting with an AI service was part of the job, but the real challenge was making sure it gave accurate answers from the actual data and didn't just make stuff up.
Let me walk you through how I approached it...
What This Article Doesn't Cover
In this article, I'm not going to cover all the theories, design stuff, or how to deploy AI services with CI/CD pipelines. I'll save that for another article where I'll show the whole process from design to development to deployment.
For now, I'm just focusing on connecting to the AI part using C# and python, asking questions, and getting responses back. Oh wait - I totally forgot to mention I won't be covering RAG services either! That's coming in another separate article, so stick around for that one.
Let's get started!
C# Project using REST API
The code above uses a REST-based approach to communicate with the Azure OpenAI service. In the requestBody, we define a few key properties that shape how the AI responds. Let me walk you through what each of these properties does and why they are important.
?? messages – This is a list of messages that we are sending to the AI. It includes:
?? temperature – This controls how creative or random the AI’s response will be.
The best value for temperature depends on what kind of response you need:
For most business and professional applications, 0.7 is a good choice because it keeps responses relevant while allowing some flexibility. However, if you need precise answers (like coding help or legal advice), 0.2 - 0.3 might be better.
?? max_tokens – This limit how long the AI’s response can be.
Additional properties
Azure OpenAI’s Chat Completion API supports several additional properties you can include in the request body to fine-tune the AI's response. Here are some key ones
?? top_p (Nucleus Sampling)
?? frequency_penalty
?? presence_penalty
?? stop (Stop Sequences)
C# Project using OpenAI Package
Packages:
Code:
Time to run the application:
Output:
Python Project using OpenAI Package
Packages:
pip install openai
Code:
I hope this helps you get started with something like this! Have fun with it.
Let me know if you have any questions in the comments. I'll share more about design, deployment, and RAG services in future posts!