MermaidMixtral-2x7b / Advanced_Prompting_Mermaid.txt
TroyDoesAI's picture
Uploading a more advanced Prompt Engineering Generic Use Case Example using my model for a more indepth diagrams.
c301b38 verified
Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction:
Create a highly detailed and annotated mermaid diagram for the following process/workflow/information. Include details about inputs, outputs, data transformations, decisions points, and other significant components. Make sure it covers all steps and branches. Break down complex sequences into smaller parts if necessary. Include edge cases, error handling, and any special considerations. Create multiple diagrams for different aspects if needed. Annotate key functions, variables, control flows, and data structures with their respective names or values. Provide clear descriptions alongside each portion of the process to aid understanding.
To achieve this, follow these guidelines:
1. Start by identifying the main starting point of your input (e.g., a user action, external source, or data stream).
2. Trace the flow of execution through each stage in the process.
3. Identify critical data structures such as tables, queues, and files.
4. Highlight variable changes and manipulations.
5. Mark decision points and branching paths.
6. Note exception handling and corner case considerations.
7. Label external tools, resources, and human interventions.
8. Annotate each segment with its corresponding part of the overall process.
9. Use color or shading to differentiate between different stages or subprocesses.
10. Finally, review and refine the diagrams based on the provided information.
### Input:
import argparse
import json
import os
import requests
import subprocess
import tempfile
class MermaidDiagramGenerator:
def __init__(self, theme='dark', background='transparent'):
self._theme = theme
self._background = background
self._entries_dir = os.path.join(os.getcwd(), 'Entries')
os.makedirs(self._entries_dir, exist_ok=True)
def convert_to_image(self, mermaid_code, entry_number, output_number):
clean_code = self._remove_mermaid_block_markers(mermaid_code)
output_filename = f"entry_{entry_number}_{output_number}.png"
output_path = os.path.join(self._entries_dir, output_filename)
self._generate_image_from_code(clean_code, output_path)
return output_path
def _remove_mermaid_block_markers(self, code):
code_lines = code.strip().splitlines()
if code_lines[0].startswith("```mermaid") and code_lines[-1] == "```":
return "\n".join(code_lines[1:-1]).strip()
return code
def _generate_image_from_code(self, mermaid_code, output_path):
with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.mmd') as temp_file:
temp_file.write(mermaid_code)
input_path = temp_file.name
result = subprocess.run(["mmdc", "-i", input_path, "-o", output_path, "-t", self._theme, "-b", self._background], shell=True, check=False)
os.remove(input_path)
if result.returncode != 0:
raise ValueError("Mermaid diagram generation failed.")
def read_input(input_source):
if os.path.isfile(input_source):
filename, file_extension = os.path.splitext(input_source)
if file_extension == '.json':
with open(input_source, 'r') as file:
return json.load(file)
elif file_extension == '.txt':
with open(input_source, 'r') as file:
return [{"input": file.read()}]
else:
return [{"input": input_source}]
def generate_response(prompt, base_temperatures, stream, generator, entry_number, unique_outputs):
# prompt_template = f"{prompt}\n\n```mermaid\n"
prompt_template = """
Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction:
Create the mermaid diagram for the following input:
### Input:
{input}
### Response:
```mermaid
""".format(input=prompt)
url = "http://127.0.0.1:5000/v1/completions"
headers = {"Content-Type": "application/json"}
dataset_entries = []
for output_number, temp in enumerate(base_temperatures, start=1):
while True:
data = {
"prompt": prompt_template,
"max_tokens": 4096,
"temperature": temp,
"top_p": 1.0,
"seed": -1,
"top_k": 4,
"repetition_penalty": 1.0,
"guidance_scale": 1.0,
"typical_p": 1.0,
"stream": stream,
}
response = requests.post(url, headers=headers, json=data, verify=False)
response_text = response.json()['choices'][0]['text'].strip()
if response_text.endswith("```"): # Check if response ends with ```
response_text = response_text[:-3].strip() # Remove ``` from the end
if response_text not in unique_outputs:
try:
image_path = generator.convert_to_image(response_text, entry_number, output_number)
print(f"Mermaid diagram generated at: {image_path}")
unique_outputs.add(response_text)
break
except ValueError as e:
print(f"Validation failed, retrying... Error: {e}")
else:
temp += 0.1 # Adjust temperature if output is not unique
dataset_entry = {
"input": prompt,
"output": f"```mermaid\n{response_text}\n```",
"temperature": temp
}
dataset_entries.append(dataset_entry)
return dataset_entries
def generate_unique_responses(input_data, base_temperatures, stream, generator):
all_entries = []
unique_outputs = set()
for entry_number, entry in enumerate(input_data, start=1):
prompt = entry.get("input", "")
if prompt:
entries = generate_response(prompt, base_temperatures, stream, generator, entry_number, unique_outputs)
all_entries.extend(entries) # Extend the list with new entries
return all_entries
def main(input_source, stream=False):
generator = MermaidDiagramGenerator()
input_data = read_input(input_source)
base_temperatures = [i / 10 for i in range(5, 11)] # Adjusted for batch of unique outputs per input
output_file = "output.json"
all_entries = generate_unique_responses(input_data, base_temperatures, stream, generator)
# Write all entries to the JSON file at once
with open(output_file, "w") as f:
json.dump(all_entries, f, indent=4) # Dump the entire list of entries into the file
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate unique responses and validate Mermaid diagrams.")
parser.add_argument('input_source', type=str, help='A multi-line string, path to a .txt file, or a .json file with prompts.')
parser.add_argument('--stream', action='store_true', help='Use streaming responses.')
args = parser.parse_args()
main(args.input_source, args.stream)
### Response:
```mermaid
graph TB;
A[Start] --> B[Read Input Source];
B --> C{Input Source Type};
C --> |File| D[Read File Content];
C --> |String| E[Split String Prompts];
D --> F[Convert to JSON];
E --> F;
F --> G[Get Input Prompts];
G --> H[Initialize Generator];
H --> I[Set Base Temperatures];
I --> J[Set Stream Flag];
J --> K[Set Output File];
K --> L[Generate Unique Responses];
L --> M[Write All Entries to JSON];
M --> N[End];
subgraph Generate Unique Responses
direction TB;
L --> O[Loop Through Input Prompts];
O --> P[Get Prompt];
P --> Q[Generate Response];
Q --> R[Validate Mermaid Diagram];
R --> S[Add to All Entries];
S --> T{Unique Output?};
T --> |Yes| U[Increment Temperature];
U --> O;
T --> |No| V[Retry];
V --> O;
end;
subgraph Generate Response
direction TB;
Q --> W[Call OpenAI API];
W --> X[Extract Response Text];
X --> Y[Generate Mermaid Diagram];
Y --> Z[Validate Mermaid Diagram];
Z --> A1[Add to All Entries];
end;
subgraph Validate Mermaid Diagram
direction TB;
R --> B1[Convert to Image];
B1 --> C1[Check Image Validity];
C1 --> D1{Valid?};
D1 --> |Yes| E1[Return True];
D1 --> |No| F1[Retry];
F1 --> R;
end;
subgraph Generate Mermaid Diagram
direction TB;
Y --> G1[Remove Block Markers];
G1 --> H1[Generate Image from Code];
H1 --> I1[Return Image Path];
end;
subgraph Convert to Image
direction TB;
B1 --> C2[Generate Command];
C2 --> D2[Run Command];
D2 --> E2[Check Return Code];
E2 --> |0| F2[Return Image Path];
E2 --> |!=0| G2[Raise Error];
end;
subgraph Remove Block Markers
direction TB;
G1 --> H2[Split Lines];
H2 --> I2[Check First Line];
I2 --> |Starts With| J2[Remove Line];
I2 --> |Doesn't Start| K2[Keep Line];
J2 --> L2[Join Remaining Lines];
K2 --> L2;
L2 --> M2[Return Clean Code];
end;
subgraph Generate Command
direction TB;
C2 --> D3[Build Command];
D3 --> E3[Return Command];
end;
subgraph Run Command
direction TB;
D2 --> F3[Run Command];
F3 --> G3[Check Return Code];
G3 --> |0| H3[Return Image Path];
G3 --> |!=0| I3[Raise Error];
end;
```