{"id":1108,"date":"2023-11-20T12:57:54","date_gmt":"2023-11-20T20:57:54","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/powershell-community\/?p=1108"},"modified":"2023-11-20T12:57:54","modified_gmt":"2023-11-20T20:57:54","slug":"automate-text-summarization-with-openai-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell-community\/automate-text-summarization-with-openai-powershell\/","title":{"rendered":"Automate Text Summarization with OpenAI and PowerShell"},"content":{"rendered":"<p>Automating tasks is the core of PowerShell scripting. Adding artificial intelligence into the mix takes automation to a whole new level. Today, we&#8217;ll simplify the process of connecting to OpenAI&#8217;s powerful text summarization API from PowerShell. Let&#8217;s turn complex AI interaction into a straightforward script.<\/p>\n<p>To follow this guide, you&#8217;ll need an OpenAI API key. If you don&#8217;t already have one, you&#8217;ll need to create an <a href=\"https:\/\/platform.openai.com\/signup\">OpenAI account<\/a> or <a href=\"https:\/\/platform.openai.com\/login\">sign in<\/a> to an existing one. Next, navigate to the <a href=\"https:\/\/platform.openai.com\/account\/api-keys\">API key page<\/a> and create a new secret key to use.<\/p>\n<h2>Step-by-Step Function Creation<\/h2>\n<h3>Step 1: Define the Function and Parameters<\/h3>\n<p>We&#8217;ll start by setting up our function with parameters such as the API key and text to summarize:<\/p>\n<pre><code class=\"language-powershell\">function Invoke-OpenAISummarize {\n    param(\n        [string]$apiKey,\n        [string]$textToSummarize,\n        [int]$maxTokens = 60,\n        [string]$engine = 'davinci'\n    )\n    # You can add or remove parameters as per your requirements\n}<\/code><\/pre>\n<h3>Step 2: Set Up API Connection Details<\/h3>\n<p>Next, we&#8217;ll prepare our connection to OpenAI&#8217;s API by specifying the URL and headers:<\/p>\n<pre><code class=\"language-powershell\">    $uri = \"https:\/\/api.openai.com\/v1\/engines\/$engine\/completions\"\n    $headers = @{\n        'Authorization' = \"Bearer $apiKey\"\n        'Content-Type' = 'application\/json'\n    }<\/code><\/pre>\n<h3>Step 3: Construct the Body of the Request<\/h3>\n<p>We need to tell the API what we want it to do: summarize text. We do this in the request body:<\/p>\n<pre><code class=\"language-powershell\">    $body = @{\n        prompt = \"Summarize the following text: `\"$textToSummarize`\"\"\n        max_tokens = $maxTokens\n        n = 1\n    } | ConvertTo-Json<\/code><\/pre>\n<h3>Step 4: Make the API Request and Return the Summary<\/h3>\n<p>The final part of the function sends the request and then gets the summary back from the API:<\/p>\n<pre><code class=\"language-powershell\">    $parameters = @{\n        Method      = 'POST'\n        URI         = $uri\n        Headers     = $headers\n        Body        = $body\n        ErrorAction = 'Stop'\n    }\n\n    try {\n        $response = Invoke-RestMethod @parameters\n        return $response.choices[0].text.Trim()\n    } catch {\n        Write-Error \"Failed to invoke OpenAI API: $_\"\n        return $null\n    }\n}<\/code><\/pre>\n<h2>Running the Function<\/h2>\n<p>Now, to use the function, you just need two pieces of information: your OpenAI API key and the text to summarize.<\/p>\n<pre><code class=\"language-powershell\">$summary = Invoke-OpenAISummarize -apiKey 'Your_Key' -textToSummarize 'Your text...'\nWrite-Output \"Summary: $summary\"<\/code><\/pre>\n<p>Replace <code>'Your__Key'<\/code> with your actual key and <code>'Your text...'<\/code> with what you want to summarize.<\/p>\n<p>Here&#8217;s a how I am running this function in my local PowerShell prompt, I copied the text from Wikipedia:<\/p>\n<pre><code class=\"language-powershell\">$summary = Invoke-OpenAISummarize -apiKey '*********' -textToSummarize @'\nPowerShell is a task automation and configuration management program from\nMicrosoft, consisting of a command-line shell and the associated scripting\nlanguage. Initially a Windows component only, known as Windows PowerShell,\nit was made open-source and cross-platform on August 18, 2016, with the\nintroduction of PowerShell Core.[5] The former is built on the .NET Framework,\nthe latter on .NET (previously .NET Core).\n'@<\/code><\/pre>\n<p>and I get the following result:<\/p>\n<pre><code>PowerShell, initially Windows-only, is a Microsoft automation tool that became\ncross-platform as open-source PowerShell Core, transitioning from .NET Framework\nto .NET.\n<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>Combining AI with PowerShell scripting is like giving superpowers to your computer. By breaking down each step and keeping it simple, you can see how easy it is to automate text summarization using OpenAI&#8217;s GPT-3.5 API. Now, try it out and see how you can make this script work for you!<\/p>\n<p>Remember, the beauty of scripts is in their flexibility, so feel free to tweak and expand the function to fit your needs.<\/p>\n<p>Happy scripting and enjoy the power of AI at your fingertips!<\/p>\n<h2>References<\/h2>\n<ul>\n<li><a href=\"https:\/\/platform.openai.com\/docs\/api-reference\">OpenAI API reference documentation<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This easy-to-follow guide shows you how to use PowerShell to summarize text using OpenAI&#8217;s GPT-3.5 API.<\/p>\n","protected":false},"author":118858,"featured_media":77,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[13],"tags":[96,95,94,97],"class_list":["post-1108","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-api","tag-gpt-3-5","tag-openai","tag-summarization"],"acf":[],"blog_post_summary":"<p>This easy-to-follow guide shows you how to use PowerShell to summarize text using OpenAI&#8217;s GPT-3.5 API.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts\/1108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/users\/118858"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/comments?post=1108"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts\/1108\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/media\/77"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/media?parent=1108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/categories?post=1108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/tags?post=1108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}