Refactor Prompt

Instructions

Refactor the Python code below, focusing on:

Target File

scripts/prompt/add_prompt.py

Code to Refactor

import os
import pyperclip

# Get the current directory of the script
script_dir = os.path.dirname(os.path.abspath(__file__))

# Navigate to the scripts/prompt_md/ directory
prompt_dir = os.path.join(os.path.dirname(script_dir), 'prompt_md')

# Create the directory if it doesn't exist
os.makedirs(prompt_dir, exist_ok=True)

# Path to the prompt.md file
prompt_file = os.path.join(prompt_dir, 'prompt.md')

# Get the prompt from clipboard
new_prompt = pyperclip.paste().strip()

# Check if the file exists and has content
if os.path.exists(prompt_file) and os.path.getsize(prompt_file) > 0:
    separator = '\n---\n'
else:
    separator = ''

# Append the new prompt
with open(prompt_file, 'a') as f:
    f.write(separator + new_prompt)

Sample Reference Code

import os
import requests

MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY")
MISTRAL_API_URL = "https://api.mistral.ai/v1/chat/completions"

def call_mistral_api(prompt):
    api_key = MISTRAL_API_KEY
    if not api_key:
        print("Error: MISTRAL_API_KEY environment variable not set.")
        return None
    
    url = MISTRAL_API_URL
    headers = {
        "Content-Type": "application/json",
        "Accept": "application/json",
        "Authorization": f"Bearer {api_key}"
    }
    data = {
        "model": "mistral-small-latest",
        "messages": [
            {
                "role": "user",
                "content": prompt
            }
        ]
    }
    try:
        response = requests.post(url, headers=headers, json=data)
        response.raise_for_status()
        response_json = response.json()
        if response_json and response_json['choices']:
            content = response_json['choices'][0]['message']['content']
            return content
        else:
            print(f"Mistral API Error: Invalid response format: {response_json}")
            return None
    except requests.exceptions.RequestException as e:
        print(f"Mistral API Error: {e}")
        if e.response:
            print(f"Response status code: {e.response.status_code}")
            print(f"Response content: {e.response.text}")
        return None

if __name__ == "__main__":
    print(call_mistral_api('hi'))