Skip to content

Instantly share code, notes, and snippets.

@irfanns
Created June 21, 2023 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irfanns/f1fa24426d61b7624c7052d3c8a068c1 to your computer and use it in GitHub Desktop.
Save irfanns/f1fa24426d61b7624c7052d3c8a068c1 to your computer and use it in GitHub Desktop.
(ns demo-openai.prompt
(:require [wkok.openai-clojure.api :as api]))
(def open-ai-key (System/getenv "OPENAIKEY") ) ;; Please put your own OpenAI API key in the system environment.
(defn gpt-query->resp [query]
(let [resp (api/create-chat-completion {:model "gpt-3.5-turbo"
:messages [{:role "system" :content "You are a coding Assistant focusing on Clojure language."}
{:role "user" :content query}]}
{:api-key open-ai-key})]
(get-in resp [:choices 0 :message :content])))
(def prompt-pre "Please answer concisely and directly.")
(defn write-prompt [text]
(str prompt-pre " " text))
; basic prompt
(-> (write-prompt "Describe what Clojure is!")
(gpt-query->resp))
;; => "Clojure is a modern programming language that runs on the Java Virtual Machine (JVM), the JavaScript engine, and the Microsoft Common Language Runtime (CLR). It is a functional programming language that emphasizes immutability, persistent data structures, and the use of higher-order functions. Clojure is known for its simplicity, expressiveness, and focus on concurrency and parallelism."
; detailed prompt
(gpt-query->resp
(write-prompt "I have an early-stage startup with 3 people, tell me \n
why I should use Clojure for my frontend and backend?
What books should I read and what libraries should I focus
in order to create a fast startup?"))
;; => "Clojure offers concise syntax, strong functional programming features, and easy parallelization, making it an excellent choice for building scalable and fast web applications.\n\nTo get started with Clojure, I recommend reading "Clojure for the Brave and True" by Daniel Higginbotham. Additionally, some libraries to focus on for creating a fast startup with Clojure include Ring for web applications, HugSQL for database access, and Pedestal for building web services."
; hallucination
(gpt-query->resp
(write-prompt "Give me the top 3 reference papers for understanding GPT-3.5"))
;; => "I'm sorry but GPT-3.5 is not a widely known or recognized model in the natural language processing (NLP) community. However, here are the top 3 reference papers for understanding GPT-3:\n\n1. "Language Models are Few-Shot Learners" by Tom B. Brown et al. This paper presents GPT-3 and its impressive few-shot learning capabilities.\n\n2. "GPT-3: Language Models are Few-Shot Learners" by Natalia Ponomareva and Martin Volkov. This paper provides an in-depth analysis and review of GPT-3.\n\n3. "The GPT-3 Architecture Explained" by James Falls. This article provides a comprehensive explanation of the architecture and components of GPT-3."
; logical reasoning
(gpt-query->resp
(write-prompt "Resources: [All you need is Attention, (2016)]
Rule: Act as a research assistant
Purpose: Please tell me the best way to understand
GPT-3 and Transformers architecture by reading the above papers
and tell me the reasons why, step-by-step!!"))
; Few Shot Learning
;; example from Nikkei Asia 2023-06-15
(def email-text "The decision by OpenAI's Altman to set up an office in Japan and expand business in
the country underscores how Japan is having something of an investment moment. Indeed,
we have written a lot about the recent interest in the country among global investors
and the rise of the Japanese stock market. But we have yet to answer the question of how
attractive Japan will ultimately become. Both our Business Spotlight and Asia Insight features
this week seek to answer that question.")
(gpt-query->resp
(write-prompt (str "Resources: " email-text
"Purpose: Please convert the above paragraph into a summary and then display it as an EDN with the following format:
{:keyword-1 "summary-1" :keyword-2 "summary-2" :keyword-3 "summary-3"}
Output EDN only without any accompanying words.")))
;; => "{:resources "OpenAI's Altman sets up office in Japan, indicating Japan is experiencing an investment moment. The country is gaining interest from global investors and the Japanese stock market is rising. Business Spotlight and Asia Insight features aim to determine Japan's ultimate attractiveness."}"
;; => "{:resources "The decision by OpenAI's Altman to set up an office in Japan and expand business in the country underscores how Japan is having something of an investment moment. Indeed, we have written a lot about the recent interest in the country among global investors and the rise of the Japanese stock market. But we have yet to answer the question of how attractive Japan will ultimately become. Both our Business Spotlight and Asia Insight features this week seek to answer that question."}"
(write-prompt (str "Resources: " email-text
"Purpose: Please do sentiment analysis of the previous paragraph, is it
happy, sad, angry, remorseful, or informative?"))
;; => "Informative"
;; documenting Clojure code
;; from the `recur` docs
(def code-factorial "(def factorial
(fn [n]
(loop [cnt n
acc 1]
(if (zero? cnt)
acc
(recur (dec cnt) (* acc cnt))))))")
(gpt-query->resp
(write-prompt (str "Given the following code: "
code-factorial
"Please document it and guess if its O(n) or O(n^2)?")))
;; => "The code is a function that calculates the factorial of a given number using a loop and recursion. It initializes a counter 'cnt' and an accumulator 'acc' to 1. The loop checks if the counter is zero, and if so, returns the accumulator. Otherwise, it calls itself with the decremented counter and the product of the current accumulator and counter. \n\nThe time complexity of the function is O(n)."
;; Generate Clojure code (Babashka)
(gpt-query->resp
(write-prompt "Please write a babashka clojure code
that calls up yt-dlp
and then log the timestamp,
downloaded URL, and
downloaded video title."))
;; => "To execute yt-dlp from clojure using the Shell library and log the timestamp, downloaded URL, and video title, you can use the following babashka code:\n\n```clojure\n
;; generated by GPT-3.5
(require '[clojure.java.shell :as shell])
(defn download-video [url]
(let [output (shell/sh "yt-dlp" "-o" "%(title)s.%(ext)s" "-f" "best" url)
timestamp (str (java.time.Instant/now))
title (-> output :out str)
video-url (-> output :err str)]
(spit "download.log" (str "timestamp: " timestamp ", title: " title ", url: " video-url "\\n") :append)))
(download-video "https://www.youtube.com/watch?v=dQw4w9WgXcQ")
;; => This code defines a function `download-video` that takes a URL and uses the `shell/sh` function to execute `yt-dlp`. The output of `yt-dlp` is captured and parsed to obtain the timestamp, downloaded video title, and downloaded video URL. These values are then logged to a file called `download.log`. In this example, we are downloading Rick Astley's classic hit "Never Gonna Give You Up"."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment