Created
March 30, 2024 17:59
-
-
Save tebba-von-mathenstein/33f66f3182cb8a794dc4c546c8302a44 to your computer and use it in GitHub Desktop.
A simple AI Prompt Engineering example with OpenAI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from openai import OpenAI | |
# Your API key must be saved in an env variable for this to work. | |
client = OpenAI() | |
# Get a prompt, embed it into a classification request to GPT | |
image_subject = input("Subject: ") | |
image_setting = input("Setting: ") | |
image_style = input("Style: ") | |
image_emotion = input("Emotion: ") | |
image_emotion_prompt = f''' | |
Create a 100 word summary of the following emotion. | |
{image_emotion} | |
''' | |
## EMOTION | |
print('============= EMOTIONAL PROMPT ================') | |
print(image_emotion_prompt) | |
emotion_response = client.chat.completions.create( | |
model="gpt-4", | |
messages=[ | |
{ | |
"role": "system", | |
"content": "You are deeply sensitive and in touch with your feelings. Your goal is to help others deeply understand emotions." | |
}, | |
{ | |
"role": "user", | |
"content": image_emotion_prompt | |
}], | |
temperature=1, | |
max_tokens=200, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0 | |
) | |
emotion_details = emotion_response.choices[0].message.content | |
print('--------- response -----------') | |
print(emotion_details) | |
print('------------------------------') | |
# We want the subject and setting to reflect the emotional content | |
image_subject_prompt = f''' | |
Create a detailed physical description of the following subject and setting. | |
Subject: | |
{image_subject} | |
Setting: | |
{image_setting} | |
Generate details that evoke the following emotional content: | |
{emotion_details} | |
''' | |
## SUBJECT | |
print('============= SUBJECT PROMPT ================') | |
print(image_subject_prompt) | |
subject_response = client.chat.completions.create( | |
model="gpt-4", | |
messages=[ | |
{ | |
"role": "system", | |
"content": "You are a keen observer of all things. You notice and care about even the smallest details." | |
}, | |
{ | |
"role": "user", | |
"content": image_subject_prompt | |
}], | |
temperature=1, | |
max_tokens=2048, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0 | |
) | |
subject_details = subject_response.choices[0].message.content | |
print('--------- response -----------') | |
print(subject_details) | |
print('------------------------------') | |
## STYLE | |
image_style_prompt = f''' | |
Create a 100 word summary of the following artistic style. Focus exclusively on the visual components of the style: | |
{image_style} | |
''' | |
print('============= STYLE PROMPT ================') | |
print(image_style_prompt) | |
style_response = client.chat.completions.create( | |
model="gpt-4", | |
messages=[ | |
{ | |
"role": "system", | |
"content": "You are an art historian. Describe artistic styles in detail." | |
}, | |
{ | |
"role": "user", | |
"content": image_style_prompt | |
}], | |
temperature=1, | |
max_tokens=300, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0 | |
) | |
style_details = style_response.choices[0].message.content | |
print('--------- response -----------') | |
print(style_details) | |
print('------------------------------') | |
request_for_image_prompt = f''' | |
Image content: | |
{subject_details} | |
Image Style: | |
{style_details} | |
''' | |
print('============= FINAL IMAGE PROMPT ================') | |
print(request_for_image_prompt) | |
image_prompt_response = client.chat.completions.create( | |
model="gpt-4", | |
messages=[ | |
{ | |
"role": "system", | |
"content": "You are a prompt engineer. Return a prompt that will help DALL-E make a beautiful image. Include generous details about the subject, setting, and style in your prompt." | |
}, | |
{ | |
"role": "user", | |
"content": request_for_image_prompt | |
}], | |
temperature=1, | |
max_tokens=2048, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0 | |
) | |
generated_image_prompt = image_prompt_response.choices[0].message.content | |
print('============ FINAL RESULT ================') | |
print(generated_image_prompt) | |
# Ask for images -- Commented out because | |
response_three = client.images.generate( | |
model="dall-e-3", | |
prompt=generated_image_prompt, | |
size="1024x1024", | |
quality="standard", | |
n=1 | |
) | |
image_url = response_three.data[0].url | |
print(image_url) | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment