Your First Chat Completion with Azure OpenAI Using C# and Python

Your First Chat Completion with Azure OpenAI Using C# and Python

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:

  • A system message: This is like setting the mood for the AI. It tells the AI how it should behave when responding. For example, if we set it to "You are a helpful assistant," the AI will act accordingly.
  • A user message: This is the actual question or query that the user wants the AI to answer.

?? temperature – This controls how creative or random the AI’s response will be.

  • If we set it to 0, the AI will be very predictable and give the safest answer.
  • If we set it to 1, the AI will be more creative and may give varied responses.
  • Here, it's set to 0.7, which means the AI will be somewhat creative but still stick to relevant answers.

The best value for temperature depends on what kind of response you need:

  • 0.0 – 0.3 → Best for tasks that require accuracy and consistency, like factual answers, calculations, or structured responses.
  • 0.4 – 0.7 → A good balance between creativity and reliability. Useful for natural conversations, brainstorming, and general Q&A.
  • 0.8 – 1.0 → Encourages more creative and diverse responses. Best for storytelling, idea generation, or when you want unique and unexpected answers.

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.

  • A token is like a piece of a word. For example, "Hello" is one token, but "Understanding" might be two tokens.
  • Here, it's set to 100, meaning the AI’s response won't be too long.

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)

  • Controls diversity by limiting responses to a subset of the most likely tokens.
  • Works as an alternative to temperature, so it's recommended to adjust one, not both.

?? frequency_penalty

  • Controls how much the AI discourages repeating words.

?? presence_penalty

  • Encourages the AI to introduce new ideas instead of sticking to known facts.

?? stop (Stop Sequences)

  • Defines custom stop words or phrases that tell the AI when to stop generating text.


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!


要查看或添加评论,请登录

Gehan Fernando的更多文章

  • Are You Really Safe?

    Are You Really Safe?

    In a world where every CPU cycle counts, especially in high-performance or low-level scenarios, C# developers sometimes…

  • Machine Learning for Babies: A Simple Salary Prediction Project

    Machine Learning for Babies: A Simple Salary Prediction Project

    Introduction Hello everyone! I hope you’re doing great. Today, I want to take you on an exciting journey into Machine…

  • GPU for Daily Development: A Practical Guide

    GPU for Daily Development: A Practical Guide

    What is Processing Imagine you’re making a cup of coffee. You take water, coffee beans, and milk (your input), mix and…

  • Transforming your Python GUI App into an Executable Installer

    Transforming your Python GUI App into an Executable Installer

    This tutorial demonstrates how to convert a GUI app created with Python into a standalone executable file and then…

    1 条评论
  • Introduction to gRPC

    Introduction to gRPC

    What is gRPC gRPC is an open-source, high-performance remote procedure call (RPC) framework initially developed by…

  • API Security Best Practices

    API Security Best Practices

    What is API An API, or Application Programming Interface, is a set of rules and protocols that allows different…

  • Understanding and Detecting Memory Leaks in Python

    Understanding and Detecting Memory Leaks in Python

    Introduction Memory leaks can have a significant impact on the performance and reliability of software applications. In…

    3 条评论
  • Middleware for Azure Functions

    Middleware for Azure Functions

    Prior to perusing this article, I recommend reviewing my earlier publication titled "Serverless Programming" However…

    4 条评论
  • Serverless Programming

    Serverless Programming

    What is Serverless programming? Serverless programming is a model of cloud computing where the cloud service provider…

    1 条评论
  • Software Development Methodologies

    Software Development Methodologies

    What is Software development methodology? A software development methodology is a framework or a set of guidelines that…

    1 条评论