Generate responses with the ChatCompletions API
Unit 5 of 7 · Module 3: Develop a generative AI chat app with Microsoft Foundry
Before you start
Short explainers for ideas this lesson builds on. Watch these first if they're new to you.
Read this lesson
An established API
The ChatCompletions API is used across a huge range of generative AI models and platforms. The Responses API is recommended for new projects, but you'll still meet ChatCompletions when you maintain existing code, or need cross-platform compatibility. It wraps your prompt in a collection of message objects, in JSON. Each message has a role: system, for instructions, and user, for the prompt. Call chat dot completions dot create with your model deployment name and the messages, then read the reply from the first choice's message content.
You keep the script
Unlike the Responses API, ChatCompletions doesn't track state for you. To keep context, your code tracks the conversation. Start a list with the system message. Append the user's message, get a completion, then append the assistant's reply to the same list. For the next question, append it and send the whole list again. In a real app, that's a loop, and the entire conversation history is submitted on every turn. It's not as fully featured as the Responses API, but it's well established across the ecosystem, so it's worth knowing well.
Facts from Microsoft's documentation: Work with chat completion models; Get started with Microsoft Foundry SDKs and endpoints (CC BY 4.0), rewritten in Loop's own words and examples. Narration, animation, and quiz by Loop. The coding lab is based on code samples in Microsoft's Azure AI documentation (MicrosoftDocs/azure-ai-docs, MIT License; notice). Microsoft, Azure, and Microsoft Foundry are trademarks of Microsoft. Loop is independent and not affiliated with or endorsed by Microsoft.