Skip to content

Commit e780199

Browse files
author
naman-msft
committed
added new functionalities
1 parent 2819301 commit e780199

File tree

1 file changed

+108
-23
lines changed

1 file changed

+108
-23
lines changed

tools/ada.py

Lines changed: 108 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -863,28 +863,24 @@ def setup_output_folder(input_type, input_name, title=None):
863863

864864
return folder_name
865865

866-
def update_progress_log(log_folder, log_data):
867-
"""Update the JSON progress log with information about the current run."""
866+
def update_progress_log(log_folder, runs_data, user_intent):
867+
"""Update the JSON progress log with the new structure."""
868868
log_file = os.path.join(log_folder, "progress_log.json")
869869

870-
# Load existing log data if it exists
871-
if os.path.exists(log_file):
872-
with open(log_file, 'r') as f:
873-
try:
874-
log_entries = json.load(f)
875-
except json.JSONDecodeError:
876-
log_entries = []
877-
else:
878-
log_entries = []
879-
880-
# Add new entry
881-
log_entries.append(log_data)
870+
# Create the new structure with info and runs sections
871+
log_data = {
872+
"info": {
873+
"User Intent": user_intent,
874+
"Total Attempts": len(runs_data)
875+
},
876+
"runs": runs_data
877+
}
882878

883-
# Write updated log back to file
879+
# Write updated log to file
884880
with open(log_file, 'w') as f:
885-
json.dump(log_entries, f, indent=4)
881+
json.dump(log_data, f, indent=4)
886882

887-
def collect_iteration_data(input_type, user_input, output_file, attempt, errors, start_time, success, user_intent):
883+
def collect_iteration_data(input_type, user_input, output_file, attempt, errors, start_time, success):
888884
"""Collect data for a single iteration."""
889885
return {
890886
'Timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
@@ -894,8 +890,7 @@ def collect_iteration_data(input_type, user_input, output_file, attempt, errors,
894890
'Attempt Number': attempt,
895891
'Errors Encountered': errors,
896892
'Execution Time (in seconds)': round(time.time() - start_time), # Rounded to nearest second
897-
'Result': "Success" if success else "Failure",
898-
'User Intent': user_intent
893+
'Success/Failure': "Success" if success else "Failure"
899894
}
900895

901896
def generate_title_from_description(description):
@@ -1007,6 +1002,81 @@ def perform_security_check(doc_path):
10071002
print(f"\nError saving security report: {e}")
10081003
return None
10091004

1005+
def perform_seo_check(doc_path, checklist_path="seo-checklist.md"):
1006+
"""Perform an SEO optimization check on an Exec Doc using the SEO checklist."""
1007+
if not os.path.isfile(doc_path):
1008+
print(f"\nError: The file {doc_path} does not exist.")
1009+
return None
1010+
1011+
if not os.path.isfile(checklist_path):
1012+
print(f"\nError: The SEO checklist file {checklist_path} does not exist.")
1013+
return None
1014+
1015+
try:
1016+
with open(doc_path, "r") as f:
1017+
doc_content = f.read()
1018+
1019+
with open(checklist_path, "r") as f:
1020+
checklist_content = f.read()
1021+
except Exception as e:
1022+
print(f"\nError reading files: {e}")
1023+
return None
1024+
1025+
# Create output filename
1026+
doc_name = os.path.splitext(os.path.basename(doc_path))[0]
1027+
output_file = f"{doc_name}_seo_optimized.md"
1028+
1029+
print("\nPerforming SEO optimization check...")
1030+
1031+
# Use the LLM to analyze and optimize the document for SEO
1032+
seo_prompt = """You are an SEO optimization expert. Analyze and optimize the provided document according to the SEO checklist.
1033+
1034+
For each item in the checklist:
1035+
1. Check if the document meets the criteria
1036+
2. If not, optimize the document to meet the criteria
1037+
3. Comment on the changes you made
1038+
1039+
When optimizing:
1040+
- Preserve the document's original meaning and technical accuracy
1041+
- Make sure the document flows naturally and reads well
1042+
- Only change what needs to be changed for SEO purposes
1043+
1044+
Provide your output as the fully optimized document, followed by a summary of changes made.
1045+
1046+
SEO Checklist:
1047+
1048+
{checklist_content}
1049+
1050+
Document to optimize:
1051+
1052+
{doc_content}
1053+
"""
1054+
1055+
seo_prompt = seo_prompt.format(
1056+
checklist_content=checklist_content,
1057+
doc_content=doc_content
1058+
)
1059+
1060+
response = client.chat.completions.create(
1061+
model=deployment_name,
1062+
messages=[
1063+
{"role": "system", "content": "You are an AI specialized in SEO optimization for technical documentation."},
1064+
{"role": "user", "content": seo_prompt}
1065+
]
1066+
)
1067+
1068+
optimized_content = response.choices[0].message.content
1069+
1070+
# Save the optimized document
1071+
try:
1072+
with open(output_file, "w") as f:
1073+
f.write(optimized_content)
1074+
print(f"\nSEO optimized document saved to: {output_file}")
1075+
return output_file
1076+
except Exception as e:
1077+
print(f"\nError saving optimized document: {e}")
1078+
return None
1079+
10101080
def analyze_user_intent(user_input, input_type):
10111081
"""Analyze the user's intent based on their input."""
10121082
if input_type == 'file':
@@ -1048,6 +1118,7 @@ def main():
10481118
print(" 3. Add descriptions to a shell script as an Exec Doc")
10491119
print(" 4. Redact PII from an existing Exec Doc")
10501120
print(" 5. Generate a security analysis report for an Exec Doc")
1121+
print(" 6. Perform SEO optimization check on an Exec Doc")
10511122
choice = input("\nEnter the number corresponding to your choice: ")
10521123

10531124
if choice == "1":
@@ -1100,6 +1171,19 @@ def main():
11001171
if output_file:
11011172
print(f"\nSecurity analysis complete. Report saved to: {output_file}")
11021173
sys.exit(0)
1174+
elif choice == "6":
1175+
user_input = input("\nEnter the path to your Exec Doc for SEO optimization: ")
1176+
checklist_path = input("\nEnter the path to the SEO checklist (default: seo-checklist.md): ") or "seo-checklist.md"
1177+
1178+
if not os.path.isfile(user_input) or not user_input.endswith('.md'):
1179+
print(f"\nError: {user_input} is not a valid markdown file.")
1180+
sys.exit(1)
1181+
1182+
input_type = 'seo_optimization'
1183+
output_file = perform_seo_check(user_input, checklist_path)
1184+
if output_file:
1185+
remove_backticks_from_file(output_file)
1186+
sys.exit(0)
11031187
else:
11041188
print("\nInvalid choice. Exiting.")
11051189
sys.exit(1)
@@ -1236,8 +1320,7 @@ def main():
12361320
attempt,
12371321
"", # No errors in successful run
12381322
iteration_start_time,
1239-
True,
1240-
user_intent # Add user intent
1323+
True
12411324
)
12421325
all_iterations_data.append(iteration_data)
12431326

@@ -1373,8 +1456,7 @@ def main():
13731456
attempt,
13741457
iteration_errors_text, # Only errors from this iteration
13751458
iteration_start_time,
1376-
False,
1377-
user_intent # Add user intent
1459+
False
13781460
)
13791461
all_iterations_data.append(iteration_data)
13801462

@@ -1383,6 +1465,9 @@ def main():
13831465
attempt += 1
13841466
success = False
13851467

1468+
1469+
update_progress_log(output_folder, all_iterations_data, user_intent)
1470+
13861471
# After the while loop (when a successful run is found or max attempts are reached):
13871472
final_status = "success" if success else "failure_final"
13881473
final_file = os.path.join(output_folder, f"FINAL_OUTPUT_{final_status}.md")

0 commit comments

Comments
 (0)