{"id":1191,"date":"2023-02-10T15:10:15","date_gmt":"2023-02-10T23:10:15","guid":{"rendered":"https:\/\/www.kenwalger.com\/blog\/?p=1191"},"modified":"2023-04-03T08:47:04","modified_gmt":"2023-04-03T15:47:04","slug":"%ef%bf%bcget-cooking-with-chatgpt","status":"publish","type":"post","link":"https:\/\/www.kenwalger.com\/blog\/python\/%ef%bf%bcget-cooking-with-chatgpt\/","title":{"rendered":"\ufffcGet Cooking with ChatGPT"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>ChatGPT from <a href=\"https:\/\/openai.com\/\">OpenAI<\/a> has been getting a lot of publicity recently. If you haven\u2019t heard of it, ChatGPT is an acronym for Chat Generative Pre-trained Transformer. What does that mean? It\u2019s a chatbot that\u2019s been tuned through reinforcement and supervised learning. There has been much discussion about the accuracy and usability of ChatGPT.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ChatGPT Models<\/h2>\n\n\n\n<p>ChatGPT currently offers three different models for response generation. They have different capabilities and come with different pricing. The models are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>GPT-3, for understanding and generating natural language.<\/li><li>Codex, for understanding and generating software code.<\/li><li>Content filter, for determining whether text may be sensitive or unsafe<\/li><\/ul>\n\n\n\n<p>Being in the software industry, the Codex model is intriguing, but I have yet to see examples of auto-generated code that does exactly what I\u2019m trying to accomplish. Therefore, having previous experience in the culinary industry I thought it would be interesting to try ChatGPT in that arena using the GPT-3 model. Let\u2019s see what sort of recipes ChatGPT can generate and how true to the classic recipes they are.<\/p>\n\n\n\n<p>There are four different model options available when using the overall GPT-3 model: <code>text-ada-001<\/code>, <code>text-babbage-001<\/code>, <code>text-curie-001<\/code>, and <code>text-davinci-003<\/code>. The models go along a speed\/cost scale of fast &amp; inexpensive with <code>text-ada-001 <\/code>to the most capable but slower &amp; more expensive <code>text-davinci-003<\/code>. While fine-tuning these models is an option, it goes beyond the scope of this post. With that in mind, let\u2019s look at what these different models can generate for us \u201cout of the box.\u201d<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Recipe<\/h2>\n\n\n\n<p>In classical French cuisine, there are five mother sauces on which other sauces are based. To test out ChatGPT\u2019s recipe generation, let\u2019s look at one of these classic sauces: B\u00e9chamel. B\u00e9chamel is a simple sauce, from a classic ingredient standpoint, in that it contains butter, all-purpose flour, milk, salt, and pepper.<\/p>\n\n\n\n<p>Let\u2019s write a brief <a href=\"https:\/\/www.python.org\/\">Python<\/a> application that we\u2019ll use to access the OpenAI API and see what responses we get from ChatGPT.<\/p>\n\n\n\n<p>First, we\u2019ll need to get an account with OpenAI and an API Key. As a best practice to keep your API Key safe, using an API Vault, such as <a rel=\"noreferrer noopener\" href=\"https:\/\/pangea.cloud\/docs\/vault\/\" data-type=\"URL\" data-id=\"https:\/\/pangea.cloud\/docs\/vault\/\" target=\"_blank\">Pangea&#8217;s Vault<\/a> is a great idea. At a bare minimum, assign it to an environment variable so you can access in your application but not need to save the key in your code file.<\/p>\n\n\n\n<p>Second, we\u2019ll need to install OpenAI using a package management tool such as pip or pipenv. I prefer pipenv, so running the following command will get that installed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pipenv install openai<\/code><\/pre>\n\n\n\n<p>Now, in a Python file, <code>chatgpt-python.py<\/code>, we\u2019ll make a \u201cHow do I make bechamel sauce?\u201d request to OpenAI using the four different GPT-3 models:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import openai\nimport os\n\nopenai.api_key = os.environ&#91;'OPENAPI_APIKEY']\n\n# Choose the model engine\nmodel_engines = &#91;\"text-ada-001\", \"text-babbage-001\", \"text-curie-001\", \"text-davinci-003\"]\n\n# The question passed into the API\nprompt = \"How do I make bechamel sauce?\"\n\nfor model_engine in model_engines:\n\n     # Response generation\n     generator = openai.Completion.create(\n          engine=model_engine,  # (REQUIRED) - the model to use\n          prompt=prompt,  # (OPTIONAL) - the prompt that's passed to the API\n          max_tokens=1024, # (OPTIONAL) - the max # of tokens to generate\n          n=1,  # (OPTIONAL) - how many completions to generate\n          stop=None,  # (OPTIONAL) - stop word string or array.\n          temperature=0.5.  # (OPTIONAL) - the coherency of the generated response. Number between 0 and 2\n)\n\n     generated_response = generator.choices&#91;0].text\n     print(\"Response for \" + model_engine + \": \")\n     print(generated_response)<\/code><\/pre>\n\n\n\n<p>In the response generator, we have a few options, and we&#8217;re defining the model engine, passing in the prompt, defining the number of tokens used from the API (default is 16), and how many completions are generated for each prompt (default is 1), any stop sequences we want to define to stop further token generation, and the temperature of the response.<\/p>\n\n\n\n<p>What is the temperature? It defines the randomness (coherency) of the generated response. Lower numbers here make the responses more focused and higher numbers generate more random output. We&#8217;ll explore this in a bit. For now, here are the responses I got from the above script. Remember that we&#8217;re looking for an answer to the question of &#8220;How do I make b\u00e9chamel sauce?&#8221;<\/p>\n\n\n\n<p><code>text-ada-001<\/code> (.50 second response)<\/p>\n\n\n\n<p><em>There is no one definitive way to make bechamel sauce. However, most likely you will need to use grated cheese, milk, and butt<\/em>er.<\/p>\n\n\n\n<p>Hmmm, that doesn&#8217;t really seem helpful and while there are sauces based on a bechamel that include cheese the base sauce doesn&#8217;t.<\/p>\n\n\n\n<p><code>text-babbage-001<\/code> (0.61 second response)<\/p>\n\n\n\n<p><em>Place a pot of water on the stove to boil. Add a cup of sugar and cook until the sugar has dissolved. Add a cup of milk and cook until the milk has boiled off. Pour the sauce into a saucepan and stir in the bechamel sauce ingredients. Cook over low heat until thickened.<\/em><\/p>\n\n\n\n<p>Again, not spectacularly useful, for classic b\u00e9chamel with sugar being mentioned. Also, what is a &#8220;pot of water&#8221; exactly? 1 cup, 1 quart, 5 gallons? I&#8217;m not sure what &#8220;until the milk has boiled off&#8221; means in this context either. And then, a very cyclical instruction for <em>how to make bechamel sauce<\/em>: &#8220;stir in the bechamel sauce ingredients.&#8221;<\/p>\n\n\n\n<p><code>text-curie-001<\/code> (.84 second response)<\/p>\n\n\n\n<p><em>Bechamel sauce is a simple sauce made from milk and flour. Heat milk in a saucepan over medium heat until it starts to simmer. Whisk in flour until a smooth paste forms. Cook, stirring constantly, until the sauce thickens. Serve hot.<\/em><\/p>\n\n\n\n<p>Getting closer here, but still not quite there. Using straight flour to thicken milk doesn&#8217;t create a great flavor. However, even without measurements, you could generate <em>something<\/em> here, right? Even with 100 gallons of milk being heated to a simmer you know that you must add in enough flour until a &#8220;smooth paste forms.&#8221; Still, far from a classical b\u00e9chamel sauce.<\/p>\n\n\n\n<p><code>text-davinci-003<\/code> (7.3 second response)<\/p>\n\n\n\n<p><em>Bechamel sauce is a classic French sauce made from a roux of butter and flour, and a liquid, usually milk. Here is a basic recipe:<\/em><\/p>\n\n\n\n<p><em>Ingredients:<\/em><\/p>\n\n\n\n<p><em>-3 tablespoons butter<br>-3 tablespoons all-purpose flour<br>-2 cups milk<br>-Salt and freshly ground pepper<br>-Pinch of ground nutmeg<\/em><\/p>\n\n\n\n<p><em>Instructions:<\/em><\/p>\n\n\n\n<p><em>1. Melt the butter in a saucepan over medium heat.<br>2. Add the flour and whisk until the mixture is smooth and bubbling.<br>3. Gradually whisk in the milk, whisking constantly to avoid lumps.<br>4. Continue to whisk until the sauce thickens, about 5 minutes.<br>5. Season with salt, pepper, and nutmeg.<br>6. Serve warm.<\/em><\/p>\n\n\n\n<p>This is a recipe that one could work with. I&#8217;d have a few nit-picks with the instructions, but overall, it makes sense. Personally, I wouldn&#8217;t add the nutmeg and it isn&#8217;t explicitly classical, but I&#8217;ve seen it done.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Temperature Adjustment<\/h2>\n\n\n\n<p>As mentioned earlier, the <em>temperature<\/em> parameter adjusts the &#8220;coherency&#8221; of the generated output. You can set that parameter to a number between 0 and 2. Recall that the above script ran with a <em>temperature<\/em> parameter of 0.5. The output was, while not always helpful, at least coherent.<\/p>\n\n\n\n<p>The output from the <code>text-davinci-003<\/code> model was useful. Let&#8217;s see what happens if we alter the <em>temperature<\/em> parameter value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">temperature=1.75<\/h3>\n\n\n\n<p><em>Bechamel is one of the staples in employing tha classic &#8220;mother tastes&#8221; that understands fundamental techniques, which are included bases like Bechamel felt se as Milk, simple sugars \/mutable faster blend together all seeds again serve holes along veggies traditional<\/em><\/p>\n\n\n\n<p>That&#8217;s just the introduction, the rest of the &#8220;recipe&#8221; isn&#8217;t much better\u2026<\/p>\n\n\n\n<p><em>To make b\u00e9chamel classic:<\/em><\/p>\n\n\n\n<p><em>1 MELT 2 tablespoons worthwhile normal core puns international info and work users tub back stainless stre &#8216;tub fully restricted sized documents allowed uses principle-friendly views data<\/em><\/p>\n\n\n\n<p>\u2026 and it goes on but becomes less coherent.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">temperature=0.2<\/h3>\n\n\n\n<p>Let&#8217;s go in the other direction and reduce the temperature parameter value. Here are the results:<\/p>\n\n\n\n<p><em>Bechamel sauce is a classic French white sauce made with butter, flour, and milk.<br><br>Ingredients:<br><br>&#8211; 2 tablespoons butter<br>&#8211; 2 tablespoons all-purpose flour<br>&#8211; 2 cups milk<br>&#8211; Salt and pepper to taste<br><br>Instructions:<br><br>1. In a medium saucepan, melt the butter over medium heat.<br>2. Add the flour and whisk until the mixture is smooth and bubbly.<br>3. Gradually add the milk, whisking constantly until the mixture is smooth.<br>4. Bring the mixture to a boil, then reduce the heat and simmer for 5 minutes, stirring occasionally.<br>5. Season with salt and pepper to taste.<br>6. Serve over your favorite dish. Enjoy!<\/em><\/p>\n\n\n\n<p>Another great b\u00e9chamel recipe, and this time without nutmeg!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">I don\u2019t believe you. You\u2019re an impasta!<\/h2>\n\n\n\n<p>As we&#8217;ve seen using OpenAI and ChatGPT can have some interesting results. You might wind up with some imposter recipes or you might wind up with some winners. That is true overall, in my opinion, for AI-generated content now though. Could you build an entire application strictly based on results from ChatGPT? Perhaps. Individual results may vary though.<\/p>\n\n\n\n<p>As the models get better, get more use, and &#8220;learn&#8221; more, things might change. And recipe creation is just one small example of this technology. We see folks attempting to use it in areas such as courtrooms, health care, insurance, and more. In comparison, culinary uses seem safe. Just based on the varying results of a b\u00e9chamel sauce recipe, I think we have a ways to go with this technology.<\/p>\n<a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox\" data-provider=\"facebook\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Facebook\" href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1191&#038;t=%EF%BF%BCGet%20Cooking%20with%20ChatGPT&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1191&#038;p&#091;images&#093;&#091;0&#093;=&#038;p&#091;title&#093;=%EF%BF%BCGet%20Cooking%20with%20ChatGPT\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"Facebook\" title=\"Share on Facebook\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/facebook.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox\" data-provider=\"twitter\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Twitter\" href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1191&#038;text=Hey%20check%20this%20out\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"twitter\" title=\"Share on Twitter\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/twitter.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox\" data-provider=\"reddit\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Reddit\" href=\"https:\/\/www.reddit.com\/submit?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1191&#038;title=%EF%BF%BCGet%20Cooking%20with%20ChatGPT\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"reddit\" title=\"Share on Reddit\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/reddit.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox\" data-provider=\"linkedin\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Linkedin\" href=\"https:\/\/www.linkedin.com\/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1191&#038;title=%EF%BF%BCGet%20Cooking%20with%20ChatGPT\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"linkedin\" title=\"Share on Linkedin\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/linkedin.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox\" data-provider=\"mail\" rel=\"nofollow\" title=\"Share by email\" href=\"mailto:?subject=%EF%BF%BCGet%20Cooking%20with%20ChatGPT&#038;body=Hey%20check%20this%20out:%20https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1191\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"mail\" title=\"Share by email\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/mail.png?resize=48%2C48&#038;ssl=1\" \/><\/a>","protected":false},"excerpt":{"rendered":"<p>ChatGPT from OpenAI has been getting a lot of publicity recently. If you haven\u2019t heard of it, ChatGPT is an acronym for Chat Generative Pre-trained Transformer. What does that mean? It\u2019s a chatbot that\u2019s been tuned through reinforcement and supervised learning. There has been much discussion about the accuracy and usability of ChatGPT. ChatGPT Models &hellip; <a href=\"https:\/\/www.kenwalger.com\/blog\/python\/%ef%bf%bcget-cooking-with-chatgpt\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;\ufffcGet Cooking with ChatGPT&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pmpro_default_level":"0","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[7],"tags":[1668,1667],"yst_prominent_words":[],"class_list":["post-1191","post","type-post","status-publish","format-standard","hentry","category-python","tag-ai","tag-chatgpt","pmpro-has-access"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8lx70-jd","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/1191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/comments?post=1191"}],"version-history":[{"count":3,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/1191\/revisions"}],"predecessor-version":[{"id":1197,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/1191\/revisions\/1197"}],"wp:attachment":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/media?parent=1191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/categories?post=1191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/tags?post=1191"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=1191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}