-
Notifications
You must be signed in to change notification settings - Fork 1
/
post
68 lines (56 loc) · 1.56 KB
/
post
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/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"