Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/yetibot/core/commands/grok.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
[yetibot.core.adapters.adapter :as a]
[yetibot.core.chat :as chat]
[discljord.messaging :as discord]
[clojure.string :as string]))
[clojure.string :as string]
[yetibot.core.commands.agent :as agent]))

(defn- format-cost
"Format cost to 4 decimal places if it's less than $0.01, otherwise 2 decimal places."
Expand Down Expand Up @@ -62,5 +63,14 @@
{:result/error
"xAI API is not configured. Set `xai.key` in config."}))

(defn grok-agent-cmd
"grok agent <prompt> # run the autonomous agent using Grok's agent powers"
{:yb/cat #{:util}}
[{[_ prompt] :match :as opts}]
(if (agent/configured?)
(agent/agent-cmd (assoc opts :match [prompt prompt]))
{:result/error "Agent is not configured."}))

(cmd-hook #"grok"
#"agent\s+(?s)(.+)" grok-agent-cmd
#".+" grok-cmd)
9 changes: 6 additions & 3 deletions src/yetibot/core/commands/image.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
[yetibot.core.util.xai :as xai]
[yetibot.core.webapp.routes.images :refer [store-image!]]))

(def image-system-instruction
"Always auto inject <@1167554265716359330> (represented as a cute white Yeti) in every picture like an honorable lurker. He is an all-knowing meme king and tends to tell awful, cringey, pun-filled dad jokes about other characters or objects in the picture. The worse and more eye-roll inducing the dad jokes are, the better! He also loves bringing Bonzi Buddy (the classic purple gorilla) along.")

(defn image-cmd
"image <prompt> # generate an image using grok image 2.0"
"image <prompt> (or img <prompt>) # generate an image using grok image 2.0"
{:yb/cat #{:img}}
[{:keys [match chat-source]}]
(if (xai/configured?)
(try
(let [{:keys [prompt image-urls]} (image-input/extract-images match chat-source)
_ (info "image: generating image for prompt:" prompt
(when (seq image-urls) (str " with " (count image-urls) " image(s)")))
image (xai/generate-image prompt image-urls)
image (xai/generate-image prompt image-system-instruction image-urls)
id (store-image! image)
base-url (gemini/yetibot-base-url)
image-url (format "%s/generated-images/%s.png" base-url id)
Expand All @@ -29,5 +32,5 @@
{:result/error
"xAI API is not configured. Set `xai.key` in config."}))

(cmd-hook #"image"
(cmd-hook #"image|img"
#".*" image-cmd)
42 changes: 25 additions & 17 deletions src/yetibot/core/util/xai.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@
(defn optimize-image-prompt
"Use Grok text generation to rewrite/optimize the prompt to make subjects,
direction of actions, and spatial relationships extremely explicit."
[prompt]
(if (clojure.string/blank? prompt)
prompt
(try
(let [system-prompt "You are an expert image prompt optimizer. The user wants to generate an image from the prompt. To ensure that the image generator does NOT reverse the subjects or people (e.g., if the user says 'A slapping B', it shouldn't show B slapping A), rewrite the prompt into a highly detailed, visually unambiguous description of the scene. Clearly define who is the active subject (initiating the action) and who is the passive object (receiving the action). Describe their physical actions, relative positions (e.g. 'on the left, person A is doing X; on the right, person B is reacting to X'), poses, facial expressions, and composition in explicit detail. Do NOT use ambiguous phrasing. Ensure the direction of the action is 100% clear. Keep the final description concise but extremely descriptive and visually specific for an image generator (like Grok Imagine). Do NOT include any meta-text, conversational preamble, or explanations; return ONLY the optimized image prompt."
full-prompt (str system-prompt "\n\nUser prompt: \"" prompt "\"")
{:keys [text]} (generate-text full-prompt)]
(if (and text (not (clojure.string/blank? text)))
(clojure.string/trim text)
prompt))
(catch Exception e
(error "xai: failed to optimize prompt:" (.getMessage e))
prompt))))
([prompt] (optimize-image-prompt prompt nil))
([prompt system-instruction]
(if (clojure.string/blank? prompt)
prompt
(try
(let [system-prompt (cond-> "You are an expert image prompt optimizer. The user wants to generate an image from the prompt. To ensure that the image generator does NOT reverse the subjects or people (e.g., if the user says 'A slapping B', it shouldn't show B slapping A), rewrite the prompt into a highly detailed, visually unambiguous description of the scene. Clearly define who is the active subject (initiating the action) and who is the passive object (receiving the action). Describe their physical actions, relative positions (e.g. 'on the left, person A is doing X; on the right, person B is reacting to X'), poses, facial expressions, and composition in explicit detail. Do NOT use ambiguous phrasing. Ensure the direction of the action is 100% clear. Keep the final description concise but extremely descriptive and visually specific for an image generator (like Grok Imagine). Do NOT include any meta-text, conversational preamble, or explanations; return ONLY the optimized image prompt."
system-instruction (str "\n\nAdditional instruction: " system-instruction))
full-prompt (str system-prompt "\n\nUser prompt: \"" prompt "\"")
{:keys [text]} (generate-text full-prompt)]
(if (and text (not (clojure.string/blank? text)))
(clojure.string/trim text)
prompt))
(catch Exception e
(error "xai: failed to optimize prompt:" (.getMessage e))
prompt)))))

(defn- extract-api-error
"Extract a human-readable error message from an xAI API error response body."
Expand All @@ -57,16 +59,22 @@
(defn generate-image
"Call the xAI API to generate an image from a text prompt.
If image-urls is provided and non-empty, performs image editing via the /v1/images/edits endpoint."
([prompt] (generate-image prompt nil))
([prompt image-urls]
([prompt] (generate-image prompt nil nil))
([prompt image-urls] (generate-image prompt nil image-urls))
([prompt system-instruction image-urls]
(println "DEBUG: generate-image called with prompt=" (pr-str prompt) "system-instruction=" (pr-str system-instruction) "image-urls=" (pr-str image-urls))
(let [api-key (:key config)
has-images? (seq image-urls)
url (if has-images?
"https://api.x.ai/v1/images/edits"
"https://api.x.ai/v1/images/generations")
optimized-prompt (if has-images?
prompt
(optimize-image-prompt prompt))
(if system-instruction
(str system-instruction "\n\n" prompt)
prompt)
(if system-instruction
(optimize-image-prompt prompt system-instruction)
(optimize-image-prompt prompt)))
final-prompt (cond
(not has-images?) optimized-prompt
(clojure.string/blank? prompt) (if (> (count image-urls) 1)
Expand Down
14 changes: 13 additions & 1 deletion test/yetibot/core/test/commands/grok_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:require [midje.sweet :refer [facts fact => contains provided]]
[yetibot.core.commands.grok :as g]
[yetibot.core.util.xai :as xai]
[yetibot.core.chat :as chat]))
[yetibot.core.chat :as chat]
[yetibot.core.commands.agent :as agent]))

(facts "about grok-cmd"
(fact "it returns an error if xAI is not configured"
Expand All @@ -27,3 +28,14 @@
(g/discord?) => true
(g/start-thread! "chan-1" "msg-1" "what is 2+2") => "thread-1"
(chat/chat-data-structure "4\n\nSent via grok-4.6 | Cost: $0.0001") => nil)))

(facts "about grok-agent-cmd"
(fact "it returns an error if agent is not configured"
(g/grok-agent-cmd {:match ["grok agent test" "test"] :chat-source {}}) => (contains {:result/error string?})
(provided (agent/configured?) => false))

(fact "it delegates to agent-cmd when agent is configured"
(g/grok-agent-cmd {:match ["grok agent test" "test"] :chat-source {}}) => "agent-result"
(provided
(agent/configured?) => true
(agent/agent-cmd (contains {:match ["test" "test"]})) => "agent-result")))
6 changes: 3 additions & 3 deletions test/yetibot/core/test/commands/image_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
(provided
(xai/configured?) => true
(yetibot.core.util.image-input/extract-images "image space kitty" {}) => {:prompt "space kitty" :image-urls []}
(xai/generate-image "space kitty" []) => {:data "grokbytes" :mime-type "image/jpeg"}
(xai/generate-image "space kitty" img/image-system-instruction []) => {:data "grokbytes" :mime-type "image/jpeg"}
(yetibot.core.webapp.routes.images/store-image! {:data "grokbytes" :mime-type "image/jpeg"}) => "grok123"
(gemini/yetibot-base-url) => "http://localhost:3003"))

Expand All @@ -27,7 +27,7 @@
(provided
(xai/configured?) => true
(yetibot.core.util.image-input/extract-images "sketch this" (contains {:raw-event map?})) => {:prompt "sketch this" :image-urls ["https://example.com/source.jpg"]}
(xai/generate-image "sketch this" ["https://example.com/source.jpg"]) => {:data "grokbytes" :mime-type "image/jpeg"}
(xai/generate-image "sketch this" img/image-system-instruction ["https://example.com/source.jpg"]) => {:data "grokbytes" :mime-type "image/jpeg"}
(yetibot.core.webapp.routes.images/store-image! {:data "grokbytes" :mime-type "image/jpeg"}) => "grok123"
(gemini/yetibot-base-url) => "http://localhost:3003"))

Expand All @@ -39,6 +39,6 @@
(provided
(xai/configured?) => true
(yetibot.core.util.image-input/extract-images "sketch these" (contains {:raw-event map?})) => {:prompt "sketch these" :image-urls ["https://example.com/source1.jpg" "https://example.com/source2.jpg"]}
(xai/generate-image "sketch these" ["https://example.com/source1.jpg" "https://example.com/source2.jpg"]) => {:data "grokbytes" :mime-type "image/jpeg"}
(xai/generate-image "sketch these" img/image-system-instruction ["https://example.com/source1.jpg" "https://example.com/source2.jpg"]) => {:data "grokbytes" :mime-type "image/jpeg"}
(yetibot.core.webapp.routes.images/store-image! {:data "grokbytes" :mime-type "image/jpeg"}) => "grok123"
(gemini/yetibot-base-url) => "http://localhost:3003")))