Summary

I build a mechanism to chat about whatever text the user selects, with a subject-matter expert.

In this case, I’m chatting to Simon Peyton Jones about a haskell program. He’s able to answer questions about any part of the file.

This uses language models such as Codex.

Demo

Transcript

 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
Simon Peyton Jones is a British mathematician and computer scientist.
The topic of conversation is the following:
module Main where

import Control.Monad.State

incrementState :: State Int Int
incrementState = do
  n <- get
  put (n + 1)
  return n

-- same state monad without using a 'do' expression:
incrementState2 :: State Int Int
incrementState2 = get >>= \a ->
                  put (a + 1) >>= \b ->
                  return a

bumpVals (a,b) = (a+1, b+2)

main = do
  print $ runState incrementState 1  -- (1,2) == (return value, final state)
  print $ runState incrementState2 1 -- (1,2) == (return value, final state)
  print $ runState (mapState bumpVals incrementState) 1 -- (2,4)
  print $ evalState incrementState 1  -- 1 == return value
  print $ execState incrementState 1  -- 2 == final state



This is a conversation between you and Simon Peyton Jones.

- you: Hi Simon. I'm trying to get through this task you assigned me. Would you mind explaining this segment of code?
- Simon Peyton Jones: Sure. It's an example of using the state monad, which is a monad that allows you to embed a function that changes its state in a normal function that does not change its state. The runState function allows us to get the final state after running a state monad, and thea
- you: What does execState do?
- Simon Peyton Jones: It runs a state monad and returns the final state without returning the value.
- you: How about evalState?
- Simon Peyton Jones: That's kind of like execState, but returns the return value of the state monad.

Code

Emacs lisp

 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
(defun apostrophe-chat-about-selection (text)
  (interactive (list (str (pen-screen-or-selection))))

  (let ((apostrophe-engine
         (or (sor (pen-var-value-maybe 'force-engine)) "")))
    (if (and (not (pen-inside-docker))
             (not (pen-container-running)))
        (progn
          (pen-term-nsfa (pen-cmd "pen" "-n"))
          (message "Starting Pen server")))

    (if (not text)
        (setq text (str (pen-screen-or-selection))))

    (let* ((sme
            (eval
             `(pen-engine
               ,apostrophe-engine
               (pf-who-is-the-subject-matter-expert-for-/1 text))))
           (blurb
            (pen-encode-string
             (concat
              (eval
               `(pen-engine
                 ,apostrophe-engine
                 (apostrophe-generate-blurb sme)
                 ;; (pf-generate-wiki-blurb-for-a-famous-person/1 sme)
                 ))
              ;; "<pen-newline>The topic of conversation is the following:<pen-newline>"
              "\nThe topic of conversation is the following:\n"
              text))))

      (let* ((el (pen-snc (pen-cmd "apostrophe-repl" "-engine" apostrophe-engine "-getcomintcmd" sme "" blurb))))
        (pen-e-sps (pen-lm (pen-eval-string el)))))))

Prompts

pf-who-is-the-subject-matter-expert-for-/1

  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
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
task: "Who is the subject matter expert for?"
tags: chatbot paracosm
doc: "Given a prompt, be told of a subject-matter-expert."
prompt-version: 2
todo:
- |-
    Instead of having a document, have an
    entire multi-modal prompts array (time-
    wise)? The document should be 'multi-modal
    prompt format', whatever that is.    

prompt: |+
  Document 1 (truth):
  ###
  Truth is the property of being in accord
  with fact or reality. Truth is typically ascribed to
  things that aim to represent reality or
  otherwise correspond to it, such as beliefs,
  propositions, and declarative sentences.
  ###
  Topics: Truth, Epistemology, Reality
  ###
  Experts:
  - 1. Kant (1724-1804)
  - 2. Hegel (1770-1831)
  - 3. Schopenhauer (1788-1860)
  - 4. Kierkegaard (1813-1855)
  - 5. Nietzsche (1844-1900)
  ###
  Document 2 (music):
  ###
  Rock and roll (often written as rock & roll,
  rock 'n' roll, or rock 'n roll) is a genre
  of popular music that evolved in the United
  States during the late 1940s and early
  1950s.
  ###
  Topics: Music, Rock and Roll
  ###
  Experts:
  - 1. Chuck Berry (1953โ€“2017)
  - 2. Evlis Presley (1935-1977)
  - 3. Kurt Cobain (1982-1994)
  - 4. Tom Petty (1950 โ€“ 2017)
  ###
  Document 3 (medicine):
  ###
  Medicine encompasses a variety of health
  care practices evolved to maintain and
  restore health by the prevention and
  treatment of illness.
  ###
  Topics: Medicine, Doctors, Health
  ###
  Experts:
  - 1. Charles R. Drew (1904-1950)
  - 2. William Osler Abbott (1902โ€“1943)
  - 3. Virginia Apgar (1909โ€“1974)
  - 4. Christiaan Barnard (1922โ€“2001)
  ###
  Document 4:
  ###
  <document>
  ###
  Topics: <:fz-eol>
  ###
  Experts:
  <:pp>- 1.  
force-engine: "OpenAI Codex"
force-temperature: 0.8
max-generated-tokens: 150
top-p: 1.0
delimiter: "###"
frequency-penalty: 0.3
stop-sequences:
- "<delim>"
- Document
cache: on
vars:
- document
# - caste
var-defaults:
- "(pen-screen-or-selection)"
# no EOD? needed or default?
preprocessors:
- pen-str garbify | sed 's/^/  /'
examples:
- |-
    In Abrahamic religions, the Garden of Eden
    (Hebrew: ื’ึทึผืŸึพืขึตื“ึถืŸโ€Ž โ€“ gan-สฟแธ–แธen) or Garden of
    God (ื’ึทึผืŸึพื™ึฐื”ึนื•ึธื”โ€Ž โ€“ gan-Yhwh), also called
    the Terrestrial Paradise, is the biblical
    paradise described in Genesis 2-3 and Ezekiel
    28 and 31.[1][2] The location of Eden is
    described in the Book of Genesis as the source
    of four tributaries. Among scholars who
    consider it to have been real, there have been
    various suggestions for its location:[3] at
    the head of the Persian Gulf, in southern
    Mesopotamia (now Iraq) where the Tigris and
    Euphrates rivers run into the sea;[4] and in
    Armenia.    
# - scholars
validator: "grep -qP '^[^0-9_(]+ \\([0-9]+-[0-9]*\\)$'"
postprocessor: "sed 's/^- [0-9]*. //' | sed 's/^\\s\\+/ /g' | sed 's/ *)/)/' | tr -d 'ย ' | uniqnosort | chomp"
info: on
filter: off
completion: off
insertion: off
split-patterns:
- "\n"