-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRakefile
More file actions
78 lines (68 loc) · 2.38 KB
/
Rakefile
File metadata and controls
78 lines (68 loc) · 2.38 KB
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
69
70
71
72
73
74
75
76
77
78
API_KEY = ENV['GPT4_KEY']
def call_gpt4_api(prompt)
url = "https://api.openai.com/v1/chat/completions"
body = { "model" => "gpt-4",
"max_tokens" => 1000,
"temperature" => 0,
"messages" => [{"role" => "user", "content" => prompt}],
}.to_json
headers = { "Content-Type" => "application/json", "Authorization" => "Bearer #{API_KEY}" }
response = RestClient.post(url, body, headers)
json_response = JSON.parse(response.body)
response = json_response["choices"].first["message"]["content"].strip
rescue RestClient::BadRequest
"Bad Request Error: #{$!.message}"
rescue
"Error: #{$!.message}"
end
INSTRUCTIONS = <<~INSTRUCTIONS
I'm building a collection of Postgresql + Timescaledb related snippets.
This website works with markdown files and I'm using mkdocs with mkdocs-material
to render the markdown files to html.
Use all types of formatting in the markdown to make it very intuitive and attractive for the end user. Add tips for Postgresql
and security concerns as warning blocks.
Build a markdown file walking through the snippet.
Put a header explaining the goal of that snippet and what it does.
Also, if the snippet contains several sql commands, break down the snippet parts and
explain part by part instead of throwing all commands in the same code block.
Add table creating and missing details to have a runnable example from the snippet.
Here is the snippet:
```sql
INSTRUCTIONS
def chat_gpt_describe_this_snippet(sql)
call_gpt4_api(INSTRUCTIONS + "\n" + sql + "\n```")
end
def info(content)
puts TTY::Markdown.parse(content)
end
task :default do
require 'bundler/inline'
gemfile(true) do
gem 'rest-client'
gem 'tty-markdown'
gem 'pry-rescue'
end
require 'json'
# iterate over all sql files
Dir.glob('*.sql') do |file|
# create markdown from sql file
markdown_file = "docs/#{file.gsub(/\.sql$/, '.md')}"
if File.exist?(markdown_file)
puts "Skipping #{markdown_file} as it already exists"
next
end
puts "Converting #{file} to #{markdown_file}"
content = chat_gpt_describe_this_snippet(IO.read(file))
title = content[/# (.*)$/,1]
info(content)
File.open("docs/#{markdown_file}", 'w+') do |f|
f.puts content
end
path = file.gsub(/\.sql$/, '')
File.open('mkdocs.yaml', 'a+') do |f|
f.puts " - #{title}: #{path}"
end
end
# render snippets to html
# start mkdocs server
end