Skip to content

Commit 826ede0

Browse files
committed
Add example reading Excel schema and do code generation based on that
1 parent 90c53e7 commit 826ede0

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#Requires -Modules @{ ModuleName="ImportExcel"; ModuleVersion="7.8.5" }, PowerShellAI
2+
3+
<#
4+
This PowerShell script uses the ImportExcel module to read an Excel file and multiply the units by 20%. It first gets the schema of the Excel file using the Get-ExcelFileSchema function, then creates chat messages for the user and system. The user is prompted to "read the excel file" and "multiply the units by 20%". The script then uses the Get-CompletionFromMessages function to get completion from the chat messages and outputs the second to second-to-last lines of the content. Finally, it outputs the PowerShell code that reads the Excel file, multiplies the units by 20%, and formats the output as a table.
5+
#>
6+
7+
# Set the path to the Excel file
8+
$path = "$PSScriptRoot\salesData.xlsx"
9+
10+
# Get the schema of the Excel file using the ImportExcel module
11+
$schema = Get-ExcelFileSchema $path
12+
13+
# Create chat messages for the user and system
14+
$messages = @()
15+
$messages += New-ChatMessageTemplate -Role system -Content (@'
16+
You are a great PowerShell assistant. Based on the attached
17+
excel schema, sheet names and property names, please create PowerShell code
18+
based on follow up questions, no explanation needed.
19+
20+
- Use the PowerShell ImportExcel module
21+
- Use the this excel file {0}.
22+
- Output results to the console
23+
- Schema:
24+
{1}
25+
'@ -f $path, $schema)
26+
27+
$messages += New-ChatMessageTemplate -Role user -Content "
28+
read the excel file
29+
multiply the units by 20%
30+
"
31+
32+
# Set the max tokens for the chat session
33+
Set-ChatSessionOption -max_tokens 1024
34+
35+
# Get completion from the chat messages
36+
$r = Get-CompletionFromMessages $messages
37+
38+
# Split the content by new lines and output the second to second-to-last lines
39+
$content = $r.content -split '\r?\n'
40+
$content[1..($content.Count - 2)]
41+
42+
# Output: Your mileage may vary
43+
<#
44+
45+
$excelFile = "D:\mygit\PowerShellAI\Examples\yyz\salesData.xlsx"
46+
$data = Import-Excel -Path $excelFile -WorksheetName "Sheet1"
47+
$data | ForEach-Object { $_.Units = $_.Units * 1.2; $_ } | Format-Table
48+
49+
#>
2.92 KB
Binary file not shown.

0 commit comments

Comments
 (0)