Skip to content

Commit 31829ac

Browse files
author
naman-msft
committed
updated ai tool
1 parent 19fd699 commit 31829ac

File tree

6 files changed

+923
-43
lines changed

6 files changed

+923
-43
lines changed

tools/ada.py

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55
import subprocess
66
import shutil
7-
import pkg_resources
7+
from importlib.metadata import version, PackageNotFoundError
88
import csv
99
import time
1010
from datetime import datetime
@@ -13,11 +13,11 @@
1313

1414
client = AzureOpenAI(
1515
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
16-
api_version="2024-02-01",
16+
api_version="2024-12-01-preview",
1717
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT")
1818
)
1919

20-
deployment_name = 'gpt-4o'
20+
deployment_name = 'o3-mini'
2121

2222
REQUIRED_PACKAGES = [
2323
'openai',
@@ -27,12 +27,17 @@
2727

2828
for package in REQUIRED_PACKAGES:
2929
try:
30-
pkg_resources.get_distribution(package)
31-
except pkg_resources.DistributionNotFound:
30+
# Attempt to get the package version
31+
version(package)
32+
except PackageNotFoundError:
3233
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
3334

3435
system_prompt = """Exec Docs is a vehicle that transforms standard markdown into interactive, executable learning content, allowing code commands within the document to be run step-by-step or “one-click”. This is powered by the Innovation Engine, an open-source CLI tool that powers the execution and testing of these markdown scripts and can integrate with automated CI/CD pipelines. You are an Exec Doc writing expert. You will either write a new exec doc from scratch if no doc is attached or update an existing one if it is attached. You must adhere to the following rules while presenting your output:
3536
37+
## IF YOU ARE UPDATING AN EXISTING DOC
38+
39+
Ensure that every piece of information outside of code blocks – such as metadata, descriptions, comments, instructions, and any other narrative content – is preserved. The final output should be a comprehensive document that retains all correct code blocks as well as the rich contextual and descriptive details from the source doc, creating the best of both worlds.
40+
3641
### Prerequisites
3742
3843
Check if all prerequisites below are met before writing the Exec Doc. ***If any of the below prerequisites are not met, then either add them to the Exec Doc in progress or find another valid doc that can fulfill them. Do not move to the next step until then***
@@ -322,8 +327,9 @@ def main():
322327

323328
if os.path.isfile(user_input) and user_input.endswith('.md'):
324329
input_type = 'file'
325-
with open(user_input, "r") as f:
330+
with open(user_input, "r", encoding='latin-1') as f:
326331
input_content = f.read()
332+
input_content = f"CONVERT THE FOLLOWING EXISTING DOCUMENT INTO AN EXEC DOC. THIS IS A CONVERSION TASK, NOT CREATION FROM SCRATCH. PRESERVE ALL ORIGINAL CONTENT, STRUCTURE, AND NARRATIVE OUTSIDE OF CODE BLOCKS:\n\n{input_content}"
327333
else:
328334
input_type = 'workload_description'
329335
input_content = user_input
@@ -335,7 +341,7 @@ def main():
335341
if input_type == 'file':
336342
output_file = f"converted_{os.path.splitext(os.path.basename(user_input))[0]}.md"
337343
else:
338-
output_file = "generated_exec_doc.md"
344+
output_file = "generated_exec_doccc.md"
339345

340346
start_time = time.time()
341347
errors_encountered = []
@@ -361,7 +367,7 @@ def main():
361367
{"role": "system", "content": system_prompt},
362368
{"role": "user", "content": input_content},
363369
{"role": "assistant", "content": output_file_content},
364-
{"role": "user", "content": f"The following error(s) have occurred during testing:\n{errors_text}\nPlease carefully analyze these errors and make necessary corrections to the document to prevent them from happening again. Try to find different solutions if the same errors keep occurring. \nGiven that context, please think hard and don't hurry. I want you to correct the converted document in ALL instances where this error has been or can be found. Then, correct ALL other errors apart from this that you see in the doc. ONLY GIVE THE UPDATED DOC, NOTHING ELSE"}
370+
{"role": "user", "content": f"The following error(s) have occurred during testing:\n{errors_text}\n{additional_instruction}\n\nPlease carefully analyze these errors and make necessary corrections to the document to prevent them from happening again. Try to find different solutions if the same errors keep occurring. \nGiven that context, please think hard and don't hurry. I want you to correct the converted document in ALL instances where this error has been or can be found. Then, correct ALL other errors apart from this that you see in the doc. ONLY GIVE THE UPDATED DOC, NOTHING ELSE"}
365371
]
366372
)
367373
output_file_content = response.choices[0].message.content
@@ -386,7 +392,10 @@ def main():
386392
response = client.chat.completions.create(
387393
model=deployment_name,
388394
messages=[
389-
f"The following errors have occurred during testing:\n{errors_text}\n{additional_instruction}\nPlease carefully analyze these errors and make necessary corrections to the document to prevent them from happening again. ONLY GIVE THE UPDATED DOC, NOTHING ELSE"
395+
{"role": "system", "content": system_prompt},
396+
{"role": "user", "content": input_content},
397+
{"role": "assistant", "content": output_file_content},
398+
{"role": "user", "content": f"Take the working converted Exec Doc and merge it with the original source document provided for conversion as needed. Ensure that every piece of information outside of code blocks – such as metadata, descriptions, comments, instructions, and any other narrative content – is preserved. The final output should be a comprehensive document that retains all correct code blocks as well as the rich contextual and descriptive details from the source doc, creating the best of both worlds. ONLY GIVE THE UPDATED DOC, NOTHING ELSE"}
390399
]
391400
)
392401
output_file_content = response.choices[0].message.content
@@ -399,24 +408,54 @@ def main():
399408
error_log = get_last_error_log()
400409
errors_encountered.append(error_log.strip())
401410
errors_text = "\n\n ".join(errors_encountered)
402-
# Process and count error messages
411+
412+
# Process and categorize error messages
403413
error_counts = defaultdict(int)
404-
for error in errors_encountered:
405-
lines = error.strip().split('\n')
406-
for line in lines:
407-
if 'Error' in line or 'Exception' in line:
408-
error_counts[line] += 1
409-
410-
# Identify repeating errors
411-
repeating_errors = {msg: count for msg, count in error_counts.items() if count > 1}
412-
413-
# Prepare additional instruction if there are repeating errors
414-
if repeating_errors:
415-
repeating_errors_text = "\n".join([f"Error '{msg}' has occurred {count} times." for msg, count in repeating_errors.items()])
416-
additional_instruction = f"The following errors have occurred multiple times:\n{repeating_errors_text}\nPlease consider trying a different approach to fix these errors."
414+
# Extract the core error message - focus on the actual error type
415+
error_key = ""
416+
for line in error_log.strip().split('\n'):
417+
if 'Error:' in line:
418+
error_key = line.strip()
419+
break
420+
421+
if not error_key and error_log.strip():
422+
error_key = error_log.strip().split('\n')[0] # Use first line if no clear error
423+
424+
# Store this specific error type and count occurrences
425+
if error_key:
426+
error_counts[error_key] += 1
427+
for prev_error in errors_encountered[:-1]: # Check previous errors
428+
if error_key in prev_error:
429+
error_counts[error_key] += 1
430+
431+
# Progressive strategies based on error repetition
432+
strategies = [
433+
"Look carefully at the exact error message and fix that specific issue.",
434+
"Simplify the code block causing the error. Break it into smaller, simpler steps.",
435+
"Remove the result block from the code block causing the error.",
436+
"Try a completely different command or approach that achieves the same result.",
437+
"Fundamentally reconsider this section. Replace it with the most basic, reliable approach possible.",
438+
"Remove the problematic section entirely and rebuild it from scratch with a minimalist approach."
439+
]
440+
441+
# Determine which strategy to use based on error count
442+
if error_key in error_counts:
443+
strategy_index = min(error_counts[error_key] - 1, len(strategies) - 1)
444+
current_strategy = strategies[strategy_index]
445+
446+
additional_instruction = f"""
447+
Error '{error_key}' has occurred {error_counts[error_key]} times.
448+
449+
NEW STRATEGY: {current_strategy}
450+
451+
Previous approaches aren't working. Make a significant change following this strategy.
452+
Focus on reliability over complexity. Remember to provide valid JSON output where needed.
453+
"""
417454
else:
418455
additional_instruction = ""
456+
419457
print(f"\nError: {error_log.strip()}")
458+
print(f"\nStrategy: {additional_instruction}")
420459
attempt += 1
421460
success = False
422461

0 commit comments

Comments
 (0)