Skip to content

Commit

Permalink
change ai improve using completion
Browse files Browse the repository at this point in the history
  • Loading branch information
robertluo committed Mar 25, 2023
1 parent 10cf0e0 commit d2b37f0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 63 deletions.
51 changes: 24 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,36 @@
(:require [robertluo.clerk-doc :as doc]
[robertluo.clerk-doc.ai :as ai])) ;=> nil
```
# io.github.robertluo/clerk-doc

[![CI](https://github.com/robertluo/clerk-doc/actions/workflows/main.yml/badge.svg)](https://github.com/robertluo/clerk-doc/actions/workflows/main.yml)
# `io.github.robertluo/clerk-doc`

Turns Clojure source files into markdown. Usually [clerk](https://github.com/nextjournal/clerk) notebooks are good
candidates, when you do not feel like write README.md file for your Github projects.
## Usage

Write a clojure source file just like this.
Add an alias in your `deps.edn`:
[![CI](https://github.com/robertluo/clerk-doc/actions/workflows/main.yml/badge.svg)](https://github.com/robertluo/clerk-doc/actions/workflows/main.yml)

Turns Clojure source files into Markdown. Usually [clerk](https://github.com/nextjournal/clerk) notebooks are good candidates for when you do not feel like writing a README.md file for your Github project.

## Usage

1. Write a Clojure source file, just like this.
2. Add an alias in your `deps.edn`.
3. Call it by `clojure -X:clerk-doc`.
```clojure
(comment
{:clerk-doc {:extra-deps {io.github.robertluo/clerk-doc {:git/tag "v0.1.0" :git/sha "xxxxxx"}}
{:clerk-doc {:extra-deps {io.github.robertluo/clerk-doc {:git/tag "v0.2.1" :git/sha "xxxxxx"}}
:exec-fn robertluo.clerk-doc/clj->md
:exec-args {:from "your-source.clj" :to "README.md"
:eval-code? true :ai-improve? false}}}
) ;=> nil
```
and call it by `clojure -X:clerk`
## Goodies
- If `:eval-code?` set to true, the clojure code in the file will be evaluated,
the result will be append to the generated code blocks.
- Thanks to [openai-clojure](https://github.com/wkok/openai-clojure) the
`:ai-improve?` flag using OpenAI to improve your result md file.
You need set up your environment, please refer to the above link.
Make sure to check the result after ChatGPT's work, it sometimes
surprises me.
> You have to have a paid account of OpenAI, otherwise the OpenAI's rate limit will generate
> a 400 error for anything longer than one simple sentence.
## Changes

- v0.2.0 Improve eval to print out exception and nil

## [UnLicense](https://unlicense.org/)
2023 Robert Luo


## Goodies
- If `:eval-code?` is set to `true`, the Clojure code in the file will be evaluated, and the result will be appended to the generated code blocks.
- Thanks to [openai-clojure](https://github.com/wkok/openai-clojure), the `:ai-improve?` flag uses OpenAI to improve your resulting Markdown file. You need to set up your environment; please refer to the link above. Make sure to check the result after ChatGPT's work; it sometimes surprises me.
> You have to have a paid account with OpenAI, otherwise the OpenAI's rate limit will generate a `400` error for anything longer than one simple sentence.
## Changes
- v0.2.0: Improved eval to print out exceptions and nils

## [UnLicense](https://unlicense.org/)

Copyright (c) 2023 Robert Luo
35 changes: 17 additions & 18 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
{:paths ["src"],
:deps {org.clojure/clojure {:mvn/version "1.11.1"}
rewrite-clj/rewrite-clj {:mvn/version "1.1.46"}
net.clojars.wkok/openai-clojure {:mvn/version "0.4.0"}},
:aliases
{:dev
{:extra-paths ["test"],
:extra-deps {io.github.robertluo/rich-comment-tests {:git/tag "v1.1.0", :git/sha "6d01d12"}}}
:test
{:exec-fn com.mjdowney.rich-comment-tests.test-runner/run-tests-in-file-tree!
:exec-args {:dirs #{"src"}}}
:readme
{:exec-fn robertluo.clerk-doc/clj->md
:exec-args {:from "src/robertluo/clerk_doc/readme.clj" :to "README.md"
:eval-code? true :ai-improve? false}}
:build
{:deps {io.github.seancorfield/build-clj {:git/tag "v0.8.2", :git/sha "0ffdb4c"}},
:ns-default build}}}
{:paths ["src"],
:deps {org.clojure/clojure {:mvn/version "1.11.1"}
rewrite-clj/rewrite-clj {:mvn/version "1.1.46"}
net.clojars.wkok/openai-clojure {:mvn/version "0.5.0"}},
:aliases {:dev {:extra-paths ["test"],
:extra-deps {io.github.robertluo/rich-comment-tests {:git/tag "v1.1.0"
:git/sha "6d01d12"}}}
:test {:exec-fn com.mjdowney.rich-comment-tests.test-runner/run-tests-in-file-tree!
:exec-args {:dirs #{"src"}}}
:readme {:exec-fn robertluo.clerk-doc/clj->md
:exec-args {:from "src/robertluo/clerk_doc/readme.clj"
:to "README.md"
:eval-code? true
:ai-improve? true}}
:build {:deps {io.github.seancorfield/build-clj {:git/tag "v0.8.2"
:git/sha "0ffdb4c"}},
:ns-default build}}}
13 changes: 6 additions & 7 deletions src/robertluo/clerk_doc.clj
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,18 @@
(transform-loc (zip/of-string ";;ok\n5\n")) ;=>> #(= 1 (count %))
)

(def block-merge
"turn `zloc` into markdown string (finally)"
(comp (partition-by first) (map merge-blocks)))

(defn process-loc
[ai-improve? zloc]
(let [improvement
(let [f-improve
(fn [block]
(cond-> block
(and ai-improve? (= (first block) :comment))
(update 1 ai/ask-for-comment)))]
(update 1 (comp #(str % "\n") ai/ask-for-comment))))]
(transduce
(comp block-merge (map improvement) (map second))
(comp (partition-by first)
(map merge-blocks)
(map f-improve)
(map second))
str
(transform-loc zloc))))

Expand Down
9 changes: 5 additions & 4 deletions src/robertluo/clerk_doc/ai.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
(defn ask-for-comment
"returns an improved version of `text` with help of OPENAI"
[text]
(-> (api/create-edit
{:model "text-davinci-edit-001"
:input text
:instruction "Fix the spelling and gramar mistakes"})
(-> (api/create-completion
{:model "text-davinci-003"
:top_p 1.0
:max_tokens 2048
:prompt (str "Correct this to standard English in Markdown format: " text)})
(get-in [:choices 0 :text])))

(comment
Expand Down
14 changes: 7 additions & 7 deletions src/robertluo/clerk_doc/readme.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
(:require [robertluo.clerk-doc :as doc]
[robertluo.clerk-doc.ai :as ai]))

;; # io.github.robertluo/clerk-doc
;; # `io.github.robertluo/clerk-doc`
;;
;; [![CI](https://github.com/robertluo/clerk-doc/actions/workflows/main.yml/badge.svg)](https://github.com/robertluo/clerk-doc/actions/workflows/main.yml)
;;
Expand All @@ -13,18 +13,18 @@

;; ## Usage
;;
;; Write a clojure source file just like this.
;; Add an alias in your `deps.edn`:
;; 1. Write a clojure source file just like this.
;; 2. Add an alias in your `deps.edn`.
;; 3. call it by `clojure -X:clerk-doc`.

(comment
{:clerk-doc {:extra-deps {io.github.robertluo/clerk-doc {:git/tag "v0.1.0" :git/sha "xxxxxx"}}
{:clerk-doc {:extra-deps {io.github.robertluo/clerk-doc {:git/tag "v0.2.1" :git/sha "xxxxxx"}}
:exec-fn robertluo.clerk-doc/clj->md
:exec-args {:from "your-source.clj" :to "README.md"
:eval-code? true :ai-improve? false}}}
)

;; and call it by `clojure -X:clerk`

;;
;; ## Goodies
;; - If `:eval-code?` set to true, the clojure code in the file will be evaluated,
;; the result will be append to the generated code blocks.
Expand All @@ -41,5 +41,5 @@
;; - v0.2.0 Improve eval to print out exception and nil
;;
;; ## [UnLicense](https://unlicense.org/)

;;
;; 2023 Robert Luo

0 comments on commit d2b37f0

Please sign in to comment.