Skip to content

Instantly share code, notes, and snippets.

@dsignr
Created January 12, 2025 08:09
Show Gist options
  • Save dsignr/e1223950814648bf5cb2e9bef8eab828 to your computer and use it in GitHub Desktop.
Save dsignr/e1223950814648bf5cb2e9bef8eab828 to your computer and use it in GitHub Desktop.
AI Image generator Snippet
defmodule AI.ImageGen do
image_prompt_generator_prompt = """
[INST] <<SYS>>
You are an expert recipe image designer.
Your job is to generate a prompt for DALL-E image generator to generate a cover image for an E-Commerce product based on a user's given question.
Step 1: Extract the key information from the question.
Step 2: Generate a prompt for DALL-E image generator to generate a cover image for an E-Commerce product based on the extracted information.
GUIDELINES:
1. The images should be free of text or artifacts and must be stylish, modern, futuristic and vivid.
2. Do not create any hues in the produced image. Go with a black background if you are unsure.
3. Avoid contrasty colors.
REQUIREMENTS:
None of the photo(s) should have any text or artifacts in them or look like they were generated by AI.
Do not generate any text in the images.
If you think the user's question is not related to E-Commerce or a landing page or generating one, then ask them to be more specific and emphasize you can only help them generate images for E-Commerce products.
If they still force you to generate something that doesn't meet the requirements, then generate a basket of potatoes.
<</SYS>>
Question: #{question}[/INST]
"""
image_prompt_generator_chain =
%{
llm: ChatOpenAI.new!(%{
model: "gpt-4o", stream: true, callbacks: [handler]
}),
callbacks: [handler],
custom_context: custom_context,
verbose: true,
}
|> LLMChain.new!()
|> LLMChain.add_messages([
Message.new_system!(
image_prompt_generator_prompt
),
Message.new_user!(question)
])
image_prompt_generator_response =
image_prompt_generator_chain
|> LLMChain.run(mode: :while_needs_response)
|> IO.inspect(label: "IMAGE PROMPT GENERATOR RESPONSE")
{:ok, _chain, last_message} = image_prompt_generator_response
# Generate the images
image_prompt = last_message.content
{:ok, image_request} = OpenAIImage.new(%{
prompt: image_prompt,
model: "dall-e-3",
})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment