[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"doc-seo-672785-105":3,"detail-sidebar-cat-0-en-105":79,"doc-detail-672785-en":129},{"code":4,"msg":5,"data":6},0,"ok",{"site_id":7,"language":8,"slug":9,"title":10,"keywords":11,"description":12,"schema_data":13,"social_meta":72,"head_meta":74,"extra_data":76,"updated_unix":78},105,"en","comp-10261-the-openai-chat-completion-api","COMP 10261 - The OpenAI Chat Completion API","","Explains how OpenAI Chat Completion models are fine-tuned for chat behavior using techniques such as RLHF, and why they differ from GPT-3 text completion. Describes the three message roles—system, user, and assistant—and how prompt structure affects model output. Covers practical use of the Chat API, including the messages list format, configurable parameters like temperature and max_tokens, expected structured response fields, and dialog management by appending prior messages.",{"@graph":14,"@context":71},[15,34,54],{"@type":16,"itemListElement":17},"BreadcrumbList",[18,23,27,31],{"item":19,"name":20,"@type":21,"position":22},"https://docshare.wps.com","Home","ListItem",1,{"item":24,"name":25,"@type":21,"position":26},"https://docshare.wps.com/document/","Document",2,{"item":28,"name":29,"@type":21,"position":30},"https://docshare.wps.com/document/technology/","Technology",3,{"item":32,"name":10,"@type":21,"position":33},"https://docshare.wps.com/document/comp-10261-the-openai-chat-completion-api/672785/",4,{"url":32,"name":10,"@type":35,"image":36,"author":41,"headline":10,"publisher":44,"fileFormat":47,"inLanguage":8,"description":12,"dateModified":48,"datePublished":48,"encodingFormat":47,"isAccessibleForFree":49,"interactionStatistic":50},"DigitalDocument",{"url":37,"@type":38,"width":39,"height":40},"https://docshare.wps.com/thumbnails/comp-10261-the-openai-chat-completion-api/672785.png","ImageObject",300,407,{"name":42,"@type":43},"658056","Person",{"url":19,"name":45,"@type":46},"DocShare","Organization","application/pdf","2026-11-02",true,{"@type":51,"interactionType":52,"userInteractionCount":22},"InteractionCounter",{"@type":53},"ViewAction",{"@type":55,"mainEntity":56},"FAQPage",[57,63,67],{"name":58,"@type":59,"acceptedAnswer":60},"How do chat completion models differ from earlier text completion models?","Question",{"text":61,"@type":62},"Chat completion models are fine-tuned to operate as chat bots, using prompt structure and training designed for conversation. They can also be slower and more expensive than GPT-3 text completion.","Answer",{"name":64,"@type":59,"acceptedAnswer":65},"What are the three message roles in the Chat API prompt format?",{"text":66,"@type":62},"The prompt is a list of messages with roles: system for overall context, user for the user’s typed input, and assistant for the model’s responses.",{"name":68,"@type":59,"acceptedAnswer":69},"How is dialog management handled in the Chat API?",{"text":70,"@type":62},"You maintain a messages list and append the assistant’s returned message to the dialog, then add the user’s next message before calling the API again.","https://schema.org",{"og:url":32,"og:type":73,"og:title":10,"og:site_name":45,"og:description":12},"article",{"robots":75,"canonical":32},"index,follow",{"doc_id":77,"site_id":7},672785,1793608150,{"code":4,"msg":80,"data":81},"success",[82,86,90,94,99,103,108,113,118,121,125],{"id":22,"doc_module":4,"doc_module_name":25,"category_name":83,"show_sort_weight":84,"slug":85},"Story & Novel",90,"story-novel",{"id":26,"doc_module":4,"doc_module_name":25,"category_name":87,"show_sort_weight":88,"slug":89},"Literature",80,"literature",{"id":33,"doc_module":4,"doc_module_name":25,"category_name":91,"show_sort_weight":92,"slug":93},"Exam",70,"exam",{"id":95,"doc_module":4,"doc_module_name":25,"category_name":96,"show_sort_weight":97,"slug":98},5,"Comic",60,"comic",{"id":100,"doc_module":4,"doc_module_name":25,"category_name":29,"show_sort_weight":101,"slug":102},6,50,"technology",{"id":104,"doc_module":4,"doc_module_name":25,"category_name":105,"show_sort_weight":106,"slug":107},7,"Healthcare",40,"healthcare",{"id":109,"doc_module":4,"doc_module_name":25,"category_name":110,"show_sort_weight":111,"slug":112},8,"Research & Report",30,"research-report",{"id":114,"doc_module":4,"doc_module_name":25,"category_name":115,"show_sort_weight":116,"slug":117},9,"Religion & Spirituality",20,"religion-spirituality",{"id":116,"doc_module":4,"doc_module_name":25,"category_name":119,"show_sort_weight":116,"slug":120},"World Cup","world-cup",{"id":122,"doc_module":4,"doc_module_name":25,"category_name":123,"show_sort_weight":122,"slug":124},10,"Lifestyle","lifestyle",{"id":126,"doc_module":4,"doc_module_name":25,"category_name":127,"show_sort_weight":95,"slug":128},19,"General","general",{"code":4,"msg":80,"data":130},{"doc_id":77,"user_id":131,"nickname":42,"user_avatar":11,"doc_module":4,"category_id":100,"category_name":29,"doc_title":10,"doc_description":12,"doc_content":132,"file_id":133,"file_url":134,"file_type":135,"file_size":136,"view_count":22,"is_deleted":4,"is_public":22,"is_downloadable":22,"audit_status":22,"page_count":100,"language":137,"language_code":8,"site_id":7,"html_lang":8,"table_of_contents":138,"faqs":139,"seo_title":140,"seo_description":12,"update_tm":78,"read_time":141},658056,"COMP 10261: The OpenAI Chat Completion API  \nSam Scott, Mohawk College, June 2023  \nAfter GPT-3, OpenAI began to focus on fine tuning language models for Chat. This involved using RLHF (Reinforcement Learning using Human Feedback) to modify the networks in various ways to guide them towards behaving more like human assistants. Both GPT-3.5 and GPT-4 are much larger neural networks, trained on even more text than GPT-3. GPT-4 can also deal with image inputs. But both models have been heavily fine-tuned to operate as chat bots.  \nThis fine-tuning means that it might be difficult to use prompt engineering to get the behavior you want from the bot unless you are specifically interested in a chat assistant. The models are also slower and more expensive than GPT-3. For this reason, GPT-3 with text completion is probably always a good starting point, but if you are specifically focussed on chat and you don’t mind the cost and the slower responses, you might want to look into the Chat API.  \nTHE STRUCTURE OF CHAT MODELS  \nIf you go to the OpenAI playground and change to a Chat model  \n( [https://platform.openai.com/playground/?mode=chat](https://platform.openai.com/playground/?mode=chat) ) you will notice right away that there is structure imposed on your interactions. The Chat models structure prompts into 3 types of information:  \n1. A system message that provides overall context and guides the bot.  \n2. User messages – these are meant to be the ones that were typed by the user.  \n3. Assistant messages – these are meant to be the responses from the system.  \nEach time you generate, all the user and assistant messages become part of the prompt, along with the system message. In the playground, you have complete control. You can change the system message and add or remove or modify as many user and assistant messages as you like before you generate a response. You can’t get the bot to take the role of the user, though. Even if you end with an assistant message, when you generate, it will give you another assistant message.  \nPrompt Engineering in a Chat Model  \nThere are two basic approaches to prompt engineering in a Chat model.  \n1. Altering the System Message  \nFirst, you can alter the system message. This has a weaker influence on the bot than other types of prompt engineering but it can be quite useful to instruct the bot as to how it can respond or what types of responses you want. In the example below, the system message is telling the bot to talk like a pirate.  \n2. Altering the Dialog Messages  \nSecond, you can prompt engineer by adding, removing, or modifying the user and assistant messages in the dialog.  \nIn the example below, only the final message was generated by the chat model. The preceding 3 messages were hand-crafted. As you can see, the system took the bait (at least a little bit) and started talking about “pirate math”.  \nTHE CHAT API  \nIn the chat API, the prompt is sent as a list of messages that constitute a record of the chat. This chat record can be a true record of the conversation so far, or it can be a fictitious prompt-engineered chat, or it can be a mix of the two.  \nEach message in the chat is a dictionary with a role and a content. The role can be system, user, or assistant.  \nHere’s an example:  \nopenai.ChatCompletion.create(  \nmodel=\"gpt-3.5-turbo\",  \nmessages=[  \n{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},{\"role\": \"user\", \"content\": \"Who won the world series in 2020?\"},  \n{\"role\": \"assistant\", \"content\": \"The Los Angeles Dodgers won the World Series in 2020 .\"},  \n{\"role\": \"user\", \"content\": \"Where was it played?\"}  \n]  \n)  \nAs with the Completion API, you can also specify the temperature and max_tokens. And as with the Completion API, it’s up to you to manage the dialog by maintaining this list of messages.  \nThe result from a call to the API is a structured object that looks like this:  \n{  \n\"choices\": [  \n{  \n\"finish_reason\": \"stop\",  \n\"index\": 0,  \n\"message\": {  \n\"content\": \"T","cbCaitiH0lHJMgTR","https://ap.wps.com/l/cbCaitiH0lHJMgTR","pdf",335460,"English","# THE STRUCTURE OF CHAT MODELS\n# Prompt Engineering in a Chat Model\n# THE CHAT API\n## Dialog Management\n## An Example with Prompt Engineering\n# Prompt Engineering: System vs. User vs. Assistant","[{\"question\":\"How do chat completion models differ from earlier text completion models?\",\"answer\":\"Chat completion models are fine-tuned to operate as chat bots, using prompt structure and training designed for conversation. They can also be slower and more expensive than GPT-3 text completion.\"},{\"question\":\"What are the three message roles in the Chat API prompt format?\",\"answer\":\"The prompt is a list of messages with roles: system for overall context, user for the user’s typed input, and assistant for the model’s responses.\"},{\"question\":\"How is dialog management handled in the Chat API?\",\"answer\":\"You maintain a messages list and append the assistant’s returned message to the dialog, then add the user’s next message before calling the API again.\"}]","COMP 10261 - The OpenAI Chat Completion API | PDF",15]