Skip to content

Instantly share code, notes, and snippets.

@rabestro
Last active August 31, 2023 16:40
Show Gist options
  • Save rabestro/0ba87fdf591c69a2af4674ee0dbd82cf to your computer and use it in GitHub Desktop.
Save rabestro/0ba87fdf591c69a2af4674ee0dbd82cf to your computer and use it in GitHub Desktop.
GPT-4 Session to Markdown
#!/usr/bin/env bash
#
# Copyright (c) 2023 Jegors Čemisovs
# License: MIT
# Repository: https://github.com/rabestro/gpt4-session-to-markdown
#
# Converts GPT-4 chat session JSON data to markdown format and
# adds YAML front matter to the top of the file to prepare it
# for publishing on the Jekyll site.
#
# Usage:
# ./post <file.json>
#
# set these variables as needed for your environment
readonly POSTS_DIR="../_posts"
readonly TIMEZONE="+0300"
readonly CATEGORIES="AI"
readonly TAGS="ai"
create_file_name() {
jq --raw-output '.history[0].name | ascii_downcase | gsub("[^a-z0-9]+"; "-")' "$1"
}
conversation() {
jq --raw-output '.history[0].messages | map((
if .role == "user" then "👤" else "🤖" end
)+ " \(.content)\n") | join("\n")' "$1"
}
has_mermaid() {
jq 'any(.history[].messages[]; .content | contains("```mermaid"))' "$1"
}
main() {
local -r file_json="$1"
local -r file_name=$(create_file_name "$file_json")
local -r current_date=$(date '+%Y-%m-%d')
local -r current_time=$(date '+%H:%M:%S')
local -r file_post="${POSTS_DIR}/$current_date-$file_name.md"
local -r title=$(jq --raw-output '.history[0].name' "$file_json")
local -r prompt=$(jq --raw-output '.history[0].prompt' "$file_json")
echo "Title: $title"
echo "File: $file_name"
cat <<EOF >"$file_post"
---
title: "$title"
date: $current_date $current_time $TIMEZONE
categories: [$CATEGORIES]
tags: [$TAGS]
mermaid: $(has_mermaid "$file_json")
---
{% raw %}
## The system prompt used
$prompt
## The AI Conversation Log
$(conversation "$file_json")
{% endraw %}
EOF
}
main "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment