]> gitweb.factorcode.org Git - factor.git/commitdiff
discord.chatgpt-bot: Add a discord.chatgpt-bot vocabulary
authorDoug Coleman <doug.coleman@gmail.com>
Sat, 15 Apr 2023 23:22:48 +0000 (18:22 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Sat, 15 Apr 2023 23:22:48 +0000 (18:22 -0500)
example:

discord-bot-config get
[ discord-chatgpt-bot ] >>user-callback
HS{ "erg#5983" } >>obey-names
discord-connect

extra/discord/chatgpt-bot/authors.txt [new file with mode: 0644]
extra/discord/chatgpt-bot/chatgpt-bot-tests.factor [new file with mode: 0644]
extra/discord/chatgpt-bot/chatgpt-bot.factor [new file with mode: 0644]

diff --git a/extra/discord/chatgpt-bot/authors.txt b/extra/discord/chatgpt-bot/authors.txt
new file mode 100644 (file)
index 0000000..7c1b2f2
--- /dev/null
@@ -0,0 +1 @@
+Doug Coleman
diff --git a/extra/discord/chatgpt-bot/chatgpt-bot-tests.factor b/extra/discord/chatgpt-bot/chatgpt-bot-tests.factor
new file mode 100644 (file)
index 0000000..cc6dbb5
--- /dev/null
@@ -0,0 +1,44 @@
+! Copyright (C) 2023 Doug Coleman.
+! See https://factorcode.org/license.txt for BSD license.
+USING: sequences tools.test ;
+IN: discord.chatgpt-bot
+
+! "What's the best mix of dogs that is not recognized by the AKC?"
+! '{ { "role" "user" } { "content" _ } } >hashtable <chat-completion> chat-completions
+CONSTANT: chatgpt-response H{
+    { "id" "chatcmpl-75jBVGPgxxmuTOLvGVprcuSW3Vol6" }
+    {
+        "usage"
+        H{
+            { "prompt_tokens" 24 }
+            { "total_tokens" 217 }
+            { "completion_tokens" 193 }
+        }
+    }
+    { "created" 1681599685 }
+    { "model" "gpt-3.5-turbo-0301" }
+    { "object" "chat.completion" }
+    {
+        "choices"
+        {
+            H{
+                {
+                    "message"
+                    H{
+                        { "role" "assistant" }
+                        {
+                            "content"
+                            "As an AI language model, I do not have personal preferences. However, some breeds that are not currently recognized by the AKC but are popular among many people include:\n\n1. Dorgi - Dachshund and Corgi mix\n2. Pomsky - Pomeranian and Husky mix \n3. Labradoodle - Labrador Retriever and Poodle mix \n4. Cavachon - Cavalier King Charles Spaniel and Bichon Frise mix \n5. Goldendoodle - Golden Retriever and Poodle mix \n6. Cockapoo - Cocker Spaniel and Poodle mix \n7. Bernedoodle - Bernese Mountain Dog and Poodle mix \n8. Maltipoo - Maltese and Poodle mix \n9. Shichon - Shih Tzu and Bichon Frise mix \n10. Chiweenie - Chihuahua and Dachshund mix."
+                        }
+                    }
+                }
+                { "finish_reason" "stop" }
+                { "index" 0 }
+            }
+        }
+    }
+}
+
+{ t } [
+    chatgpt-response first-chat-completion "As an AI language model," head?
+] unit-test
diff --git a/extra/discord/chatgpt-bot/chatgpt-bot.factor b/extra/discord/chatgpt-bot/chatgpt-bot.factor
new file mode 100644 (file)
index 0000000..7f1a2df
--- /dev/null
@@ -0,0 +1,30 @@
+! Copyright (C) 2023 Doug Coleman.
+! See https://factorcode.org/license.txt for BSD license.
+USING: arrays assocs discord hashtables kernel openai sequences
+splitting unicode ;
+IN: discord.chatgpt-bot
+
+GENERIC: discord-chatgpt-bot ( json opcode -- )
+
+M: object discord-chatgpt-bot 2drop ;
+
+: first-chat-completion ( json -- string/f )
+    "choices" of [
+        "chatgpt gave empty response" reply-message f
+    ] [
+        first "message" of "content" of
+    ] if-empty ;
+
+M: MESSAGE_CREATE discord-chatgpt-bot drop
+    dup obey-message? [
+        "content" of "chatgpt: " ?head [
+            [ "gpt-3.5-turbo" ] dip
+            [ blank? ] trim '{ { "role" "user" } { "content" _ } } >hashtable 1array <chat-completion>
+            [ g... gflush ] [ chat-completions ] bi
+            first-chat-completion reply-message
+        ] [
+            drop
+        ] if
+    ] [
+        drop
+    ] if ;