-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt_enhancer.py
More file actions
51 lines (46 loc) · 1.93 KB
/
prompt_enhancer.py
File metadata and controls
51 lines (46 loc) · 1.93 KB
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
import openai
from google import genai
from google.genai import types
from config import OPENAI_LLM, THINK_LLM
class PromptEnhancer:
"Enhance prompts with different AI models"
@staticmethod
def enhance_with_openai(prompt: str) -> str:
try:
# Create a chat completion with the user's prompt
messages = [
{
"role": "system",
"content": "You are a talented prompt enhancer specializing in image generation prompts. \
Enhance the user's prompt to be more vivid, detailed, and imaginative while keeping \
their original intent intact.",
},
{"role": "user", "content": prompt},
]
response = openai.chat.completions.create(
model=OPENAI_LLM, messages=messages, temperature=0.7, max_tokens=500
)
return response.choices[0].message.content
except Exception as e:
print(f"OpenAI enhancement error: {e}")
return prompt
@staticmethod
def enhance_with_genai(prompt: str) -> str:
try:
# Initialize the GenAI client
client = genai.Client(http_options={"api_version": "v1alpha"})
# Enhance prompt content with the THINK_LLM model
response = client.models.generate_content(
model=THINK_LLM,
contents=prompt,
config=types.GenerateContentConfig(
system_instruction=[
"Enhance the user's prompt for image generation into a detailed, vivid single paragraph. "
"Maintain the original intent while adding rich descriptive elements."
]
),
)
return response.text
except Exception as e:
print(f"GenAI enhancement error: {e}")
return prompt