Created
June 27, 2024 14:00
-
-
Save uyriq/e18d4adf3facf61e8d73996216d89edd to your computer and use it in GitHub Desktop.
Usage example: Convert-ChatSessionToJson -Filename "chat.json" this will produce chat.json.md file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Convert-ChatSessionToJson { | |
param ( | |
[string]$Filename = "chat.json" | |
) | |
$data = Get-Content -Path $Filename | ConvertFrom-Json | |
$outputFilename = "$Filename.md" | |
$empty = $true | |
$data.requests | ForEach-Object { | |
$request = $_ | |
if ($request.isCanceled) { | |
return | |
} | |
if ($empty) { | |
if ($request.followups -and $request.followups.Count -gt 0) { | |
$title = $request.followups[0].title | |
# Continue with the rest of your logic that depends on $title | |
} | |
else { | |
# Handle the case where there are no followups | |
# This could be setting $title to a default value "somevalue" | |
$title = "not found title" | |
} | |
if ($title) { | |
Add-Content -Path $outputFilename -Value "# $title`n`n" | |
$empty = $false | |
} | |
} | |
$message = $request.message.text | |
if (-not $message) { | |
return | |
} | |
Add-Content -Path $outputFilename -Value "**$message**`n`n" | |
$responses = $request.response | |
foreach ($response in $responses) { | |
$responseValue = $response.value | |
if ($responseValue) { | |
Add-Content -Path $outputFilename -Value "$responseValue`n`n" | |
} | |
} | |
Add-Content -Path $outputFilename -Value "-----------------------------------`n`n" | |
} | |
} | |
# Usage example: | |
# Convert-ChatSessionToJson -Filename "chat.json" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment