Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /promptdriven_pdd /context /change /14 /initial_change.py
| import os | |
| from typing import Tuple | |
| from rich.console import Console | |
| from rich.markdown import Markdown | |
| from .preprocess import preprocess | |
| from .llm_selector import llm_selector | |
| from langchain_core.prompts import PromptTemplate | |
| from langchain_core.output_parsers import StrOutputParser, JsonOutputParser | |
| console = Console() | |
| def change(input_prompt: str, input_code: str, change_prompt: str, strength: float, temperature: float) -> Tuple[str, float, str]: | |
| """ | |
| Processes input prompts using a language model to generate and extract modified prompts. | |
| Args: | |
| input_prompt (str): The initial input prompt. | |
| input_code (str): The input code to be modified. | |
| change_prompt (str): The prompt describing the changes to be made. | |
| strength (float): The strength parameter for the language model. | |
| temperature (float): The temperature parameter for the language model. | |
| Returns: | |
| Tuple[str, float, str]: A tuple containing the modified prompt, total cost, and model name. | |
| """ | |
| try: | |
| # Step 1: Load prompts | |
| pdd_path = os.getenv('PDD_PATH', '') | |
| with open(f'{pdd_path}/prompts/xml/change_LLM.prompt', 'r') as file: | |
| change_llm_prompt = file.read() | |
| with open(f'{pdd_path}/prompts/extract_prompt_change_LLM.prompt', 'r') as file: | |
| extract_prompt = file.read() | |
| # Step 2: Preprocess change_LLM prompt | |
| processed_change_llm = preprocess(change_llm_prompt, recursive=False, double_curly_brackets=False) | |
| # Step 3: Use llm_selector | |
| llm, token_counter, input_cost, output_cost, model_name = llm_selector(strength, temperature) | |
| # Step 4: Create and run LCEL template for change_LLM | |
| change_template = PromptTemplate.from_template(processed_change_llm) | |
| change_chain = change_template | llm | StrOutputParser() | |
| processed_change_prompt = preprocess(change_prompt, recursive=False, double_curly_brackets=False) | |
| change_result = change_chain.invoke({ | |
| "input_prompt": input_prompt, | |
| "input_code": input_code, | |
| "change_prompt": processed_change_prompt | |
| }) | |
| input_tokens = token_counter(processed_change_llm + input_prompt + input_code + change_prompt) | |
| output_tokens = token_counter(change_result) | |
| change_cost = (input_tokens * input_cost + output_tokens * output_cost) / 1_000_000 | |
| console.print(f"[bold]Change LLM Output:[/bold]") | |
| console.print(change_result) | |
| console.print(f"Input tokens: {input_tokens}, Output tokens: {output_tokens}") | |
| console.print(f"Estimated cost: ${change_cost:.6f}") | |
| # Step 5: Create and run LCEL template for extract_prompt | |
| extract_template = PromptTemplate.from_template(extract_prompt) | |
| extract_chain = extract_template | llm | JsonOutputParser() | |
| extract_result = extract_chain.invoke({"llm_output": change_result}) | |
| input_tokens = token_counter(extract_prompt + change_result) | |
| output_tokens = token_counter(str(extract_result)) | |
| extract_cost = (input_tokens * input_cost + output_tokens * output_cost) / 1_000_000 | |
| console.print(f"[bold]Extract Prompt Output:[/bold]") | |
| console.print(extract_result) | |
| console.print(f"Input tokens: {input_tokens}, Output tokens: {output_tokens}") | |
| console.print(f"Estimated cost: ${extract_cost:.6f}") | |
| # Step 6: Extract and print modified_prompt | |
| modified_prompt = extract_result.get('modified_prompt', '') | |
| console.print(Markdown(f"[bold]Modified Prompt:[/bold]\n\n{modified_prompt}")) | |
| # Step 7: Calculate total cost and return | |
| total_cost = change_cost + extract_cost | |
| return modified_prompt, total_cost, model_name | |
| except FileNotFoundError as e: | |
| console.print(f"[bold red]Error: Prompt file not found.[/bold red]\n{str(e)}") | |
| raise Exception(f"Error: Prompt file not found. {str(e)}") | |
| except KeyError as e: | |
| console.print(f"[bold red]Error: Missing key in JSON output.[/bold red]\n{str(e)}") | |
| raise | |
| except Exception as e: | |
| console.print(f"[bold red]An unexpected error occurred:[/bold red]\n{str(e)}") | |
| raise |
Xet Storage Details
- Size:
- 4.22 kB
- Xet hash:
- 0173a49efbb8fbb9c1760761485978a78e3adae9e05b993edba884cebc0203fb
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.