Spaces:
Running
Running
Update prompt_refiner.py
Browse files- prompt_refiner.py +200 -190
prompt_refiner.py
CHANGED
@@ -7,207 +7,217 @@ from huggingface_hub.errors import HfHubHTTPError
|
|
7 |
from variables import *
|
8 |
|
9 |
class LLMResponse(BaseModel):
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
class PromptRefiner:
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
|
|
7 |
from variables import *
|
8 |
|
9 |
class LLMResponse(BaseModel):
|
10 |
+
initial_prompt_evaluation: str = Field(..., description="Evaluation of the initial prompt")
|
11 |
+
refined_prompt: str = Field(..., description="The refined version of the prompt")
|
12 |
+
explanation_of_refinements: Union[str, List[str]] = Field(..., description="Explanation of the refinements made")
|
13 |
+
response_content: Optional[Union[Dict[str, Any], str]] = Field(None, description="Raw response content")
|
14 |
|
15 |
+
@validator('response_content', pre=True)
|
16 |
+
def validate_response_content(cls, v):
|
17 |
+
if isinstance(v, str):
|
18 |
+
try:
|
19 |
+
return json.loads(v)
|
20 |
+
except json.JSONDecodeError:
|
21 |
+
return {"raw_content": v}
|
22 |
+
return v
|
23 |
|
24 |
+
@validator('initial_prompt_evaluation', 'refined_prompt')
|
25 |
+
def clean_text_fields(cls, v):
|
26 |
+
if isinstance(v, str):
|
27 |
+
return v.strip().replace('\\n', '\n').replace('\\"', '"')
|
28 |
+
return v
|
29 |
+
|
30 |
+
@validator('explanation_of_refinements')
|
31 |
+
def clean_refinements(cls, v):
|
32 |
+
if isinstance(v, str):
|
33 |
+
return v.strip().replace('\\n', '\n').replace('\\"', '"')
|
34 |
+
elif isinstance(v, list):
|
35 |
+
return [item.strip().replace('\\n', '\n').replace('\\"', '"').replace('•', '-')
|
36 |
+
for item in v if isinstance(item, str)]
|
37 |
+
return v
|
38 |
|
39 |
class PromptRefiner:
|
40 |
+
def __init__(self, api_token: str, meta_prompts: dict):
|
41 |
+
self.client = InferenceClient(token=api_token, timeout=120)
|
42 |
+
self.meta_prompts = meta_prompts
|
43 |
|
44 |
+
def _clean_json_string(self, content: str) -> str:
|
45 |
+
"""Clean and prepare JSON string for parsing."""
|
46 |
+
content = content.replace('•', '-') # Replace bullet points
|
47 |
+
content = re.sub(r'\s+', ' ', content) # Normalize whitespace
|
48 |
+
content = content.replace('\\"', '"') # Fix escaped quotes
|
49 |
+
return content.strip()
|
50 |
|
51 |
+
def _parse_response(self, response_content: str) -> dict:
|
52 |
+
"""Parse the LLM response with enhanced error handling."""
|
53 |
+
try:
|
54 |
+
# Extract content between <json> tags
|
55 |
+
json_match = re.search(r'<json>\s*(.*?)\s*</json>', response_content, re.DOTALL)
|
56 |
+
if json_match:
|
57 |
+
json_str = self._clean_json_string(json_match.group(1))
|
58 |
+
try:
|
59 |
+
# Try parsing the cleaned JSON
|
60 |
+
parsed_json = json.loads(json_str)
|
61 |
+
if isinstance(parsed_json, str):
|
62 |
+
parsed_json = json.loads(parsed_json)
|
63 |
+
|
64 |
+
return {
|
65 |
+
"initial_prompt_evaluation": parsed_json.get("initial_prompt_evaluation", ""),
|
66 |
+
"refined_prompt": parsed_json.get("refined_prompt", ""),
|
67 |
+
"explanation_of_refinements": parsed_json.get("explanation_of_refinements", ""),
|
68 |
+
"response_content": parsed_json if isinstance(parsed_json, dict) else {"raw_content": parsed_json}
|
69 |
+
}
|
70 |
+
except json.JSONDecodeError:
|
71 |
+
# If JSON parsing fails, try regex parsing
|
72 |
+
return self._parse_with_regex(json_str)
|
73 |
+
|
74 |
+
# If no JSON tags found, try regex parsing
|
75 |
+
return self._parse_with_regex(response_content)
|
76 |
|
77 |
+
except Exception as e:
|
78 |
+
print(f"Error parsing response: {str(e)}")
|
79 |
+
print(f"Raw content: {response_content}")
|
80 |
+
return self._create_error_dict(str(e))
|
81 |
|
82 |
+
def _parse_with_regex(self, content: str) -> dict:
|
83 |
+
"""Parse content using regex when JSON parsing fails."""
|
84 |
+
output = {}
|
85 |
+
|
86 |
+
# Handle explanation_of_refinements list format
|
87 |
+
refinements_match = re.search(r'"explanation_of_refinements":\s*$(.*?)$', content, re.DOTALL)
|
88 |
+
if refinements_match:
|
89 |
+
refinements_str = refinements_match.group(1)
|
90 |
+
refinements = [
|
91 |
+
item.strip().strip('"').strip("'").replace('•', '-')
|
92 |
+
for item in re.findall(r'[•"]([^"•]+)[•"]', refinements_str)
|
93 |
+
]
|
94 |
+
output["explanation_of_refinements"] = refinements
|
95 |
+
else:
|
96 |
+
# Try single string format
|
97 |
+
pattern = r'"explanation_of_refinements":\s*"(.*?)"(?:,|\})'
|
98 |
+
match = re.search(pattern, content, re.DOTALL)
|
99 |
+
output["explanation_of_refinements"] = match.group(1).strip() if match else ""
|
100 |
|
101 |
+
# Extract other fields
|
102 |
+
for key in ["initial_prompt_evaluation", "refined_prompt"]:
|
103 |
+
pattern = rf'"{key}":\s*"(.*?)"(?:,|\}})'
|
104 |
+
match = re.search(pattern, content, re.DOTALL)
|
105 |
+
output[key] = match.group(1).strip() if match else ""
|
106 |
+
|
107 |
+
# Store the original content in a structured way
|
108 |
+
output["response_content"] = {"raw_content": content}
|
109 |
+
return output
|
110 |
|
111 |
+
def _create_error_dict(self, error_message: str) -> dict:
|
112 |
+
"""Create a standardized error response dictionary."""
|
113 |
+
return {
|
114 |
+
"initial_prompt_evaluation": f"Error parsing response: {error_message}",
|
115 |
+
"refined_prompt": "",
|
116 |
+
"explanation_of_refinements": "",
|
117 |
+
"response_content": {"error": error_message}
|
118 |
+
}
|
119 |
|
120 |
+
def refine_prompt(self, prompt: str, meta_prompt_choice: str) -> Tuple[str, str, str, dict]:
|
121 |
+
"""Refine the given prompt using the selected meta prompt."""
|
122 |
+
try:
|
123 |
+
selected_meta_prompt = self.meta_prompts.get(
|
124 |
+
meta_prompt_choice,
|
125 |
+
self.meta_prompts["star"]
|
126 |
+
)
|
127 |
+
|
128 |
+
messages = [
|
129 |
+
{
|
130 |
+
"role": "system",
|
131 |
+
"content": 'You are an expert at refining and extending prompts. Given a basic prompt, provide a more relevant and detailed prompt.'
|
132 |
+
},
|
133 |
+
{
|
134 |
+
"role": "user",
|
135 |
+
"content": selected_meta_prompt.replace("[Insert initial prompt here]", prompt)
|
136 |
+
}
|
137 |
+
]
|
138 |
+
|
139 |
+
response = self.client.chat_completion(
|
140 |
+
model=prompt_refiner_model,
|
141 |
+
messages=messages,
|
142 |
+
max_tokens=3000,
|
143 |
+
temperature=0.8
|
144 |
+
)
|
145 |
+
|
146 |
+
response_content = response.choices[0].message.content.strip()
|
147 |
+
result = self._parse_response(response_content)
|
148 |
+
|
149 |
+
try:
|
150 |
+
llm_response = LLMResponse(**result)
|
151 |
+
return (
|
152 |
+
llm_response.initial_prompt_evaluation,
|
153 |
+
llm_response.refined_prompt,
|
154 |
+
llm_response.explanation_of_refinements,
|
155 |
+
llm_response.dict()
|
156 |
+
)
|
157 |
+
except Exception as e:
|
158 |
+
print(f"Error creating LLMResponse: {e}")
|
159 |
+
return self._create_error_response(f"Error validating response: {str(e)}")
|
160 |
|
161 |
+
except HfHubHTTPError as e:
|
162 |
+
return self._create_error_response("Model timeout. Please try again later.")
|
163 |
+
except Exception as e:
|
164 |
+
return self._create_error_response(f"Unexpected error: {str(e)}")
|
165 |
|
166 |
+
def _create_error_response(self, error_message: str) -> Tuple[str, str, str, dict]:
|
167 |
+
"""Create a standardized error response tuple."""
|
168 |
+
return (
|
169 |
+
f"Error: {error_message}",
|
170 |
+
"The selected model is currently unavailable.",
|
171 |
+
"An error occurred during processing.",
|
172 |
+
{"error": error_message}
|
173 |
+
)
|
174 |
|
175 |
+
def apply_prompt(self, prompt: str, model: str) -> str:
|
176 |
+
"""Apply formatting to the prompt using the specified model."""
|
177 |
+
try:
|
178 |
+
messages = [
|
179 |
+
{
|
180 |
+
"role": "system",
|
181 |
+
"content": """You are a markdown formatting expert. Format your responses with proper spacing and structure following these rules:
|
182 |
+
1. Paragraph Spacing:
|
183 |
+
- Add TWO blank lines between major sections (##)
|
184 |
+
- Add ONE blank line between subsections (###)
|
185 |
+
- Add ONE blank line between paragraphs within sections
|
186 |
+
- Add ONE blank line before and after lists
|
187 |
+
- Add ONE blank line before and after code blocks
|
188 |
+
- Add ONE blank line before and after blockquotes
|
189 |
+
|
190 |
+
2. Section Formatting:
|
191 |
+
# Title
|
192 |
+
|
193 |
+
## Major Section
|
194 |
+
|
195 |
+
[blank line]
|
196 |
+
Content paragraph 1
|
197 |
+
[blank line]
|
198 |
+
Content paragraph 2
|
199 |
+
[blank line]"""
|
200 |
+
},
|
201 |
+
{
|
202 |
+
"role": "user",
|
203 |
+
"content": prompt
|
204 |
+
}
|
205 |
+
]
|
206 |
+
|
207 |
+
response = self.client.chat_completion(
|
208 |
+
model=model,
|
209 |
+
messages=messages,
|
210 |
+
max_tokens=3000,
|
211 |
+
temperature=0.8,
|
212 |
+
stream=True
|
213 |
+
)
|
214 |
+
|
215 |
+
full_response = ""
|
216 |
+
for chunk in response:
|
217 |
+
if chunk.choices[0].delta.content is not None:
|
218 |
+
full_response += chunk.choices[0].delta.content
|
219 |
+
|
220 |
+
return full_response.replace('\n\n', '\n').strip()
|
221 |
+
|
222 |
+
except Exception as e:
|
223 |
+
return f"Error: {str(e)}"
|