about summary refs log tree commit diff
path: root/.venv/lib/python3.12/site-packages/azure/ai/inference/prompts/_utils.py
diff options
context:
space:
mode:
authorS. Solomon Darnell2025-03-28 21:52:21 -0500
committerS. Solomon Darnell2025-03-28 21:52:21 -0500
commit4a52a71956a8d46fcb7294ac71734504bb09bcc2 (patch)
treeee3dc5af3b6313e921cd920906356f5d4febc4ed /.venv/lib/python3.12/site-packages/azure/ai/inference/prompts/_utils.py
parentcc961e04ba734dd72309fb548a2f97d67d578813 (diff)
downloadgn-ai-master.tar.gz
two version of R2R are here HEAD master
Diffstat (limited to '.venv/lib/python3.12/site-packages/azure/ai/inference/prompts/_utils.py')
-rw-r--r--.venv/lib/python3.12/site-packages/azure/ai/inference/prompts/_utils.py100
1 files changed, 100 insertions, 0 deletions
diff --git a/.venv/lib/python3.12/site-packages/azure/ai/inference/prompts/_utils.py b/.venv/lib/python3.12/site-packages/azure/ai/inference/prompts/_utils.py
new file mode 100644
index 00000000..22f28418
--- /dev/null
+++ b/.venv/lib/python3.12/site-packages/azure/ai/inference/prompts/_utils.py
@@ -0,0 +1,100 @@
+# ------------------------------------
+# Copyright (c) Microsoft Corporation.
+# Licensed under the MIT License.
+# ------------------------------------
+# mypy: disable-error-code="import-untyped,return-value"
+# pylint: disable=line-too-long,R,wrong-import-order,global-variable-not-assigned)
+import json
+import os
+import re
+import sys
+from typing import Any, Dict
+from pathlib import Path
+
+
+_yaml_regex = re.compile(
+    r"^\s*" + r"(?:---|\+\+\+)" + r"(.*?)" + r"(?:---|\+\+\+)" + r"\s*(.+)$",
+    re.S | re.M,
+)
+
+
+def load_text(file_path, encoding="utf-8"):
+    with open(file_path, "r", encoding=encoding) as file:
+        return file.read()
+
+
+def load_json(file_path, encoding="utf-8"):
+    return json.loads(load_text(file_path, encoding=encoding))
+
+
+def load_global_config(prompty_path: Path = Path.cwd(), configuration: str = "default") -> Dict[str, Any]:
+    prompty_config_path = prompty_path.joinpath("prompty.json")
+    if os.path.exists(prompty_config_path):
+        c = load_json(prompty_config_path)
+        if configuration in c:
+            return c[configuration]
+        else:
+            raise ValueError(f'Item "{configuration}" not found in "{prompty_config_path}"')
+    else:
+        return {}
+
+
+def load_prompty(file_path, encoding="utf-8") -> Dict[str, Any]:
+    contents = load_text(file_path, encoding=encoding)
+    return parse(contents)
+
+
+def parse(contents):
+    try:
+        import yaml  # type: ignore
+    except ImportError as exc:
+        raise ImportError("Please install pyyaml to use this function. Run `pip install pyyaml`.") from exc
+
+    global _yaml_regex
+
+    fmatter = ""
+    body = ""
+    result = _yaml_regex.search(contents)
+
+    if result:
+        fmatter = result.group(1)
+        body = result.group(2)
+    return {
+        "attributes": yaml.load(fmatter, Loader=yaml.SafeLoader),
+        "body": body,
+        "frontmatter": fmatter,
+    }
+
+
+def remove_leading_empty_space(multiline_str: str) -> str:
+    """
+    Processes a multiline string by:
+    1. Removing empty lines
+    2. Finding the minimum leading spaces
+    3. Indenting all lines to the minimum level
+
+    :param multiline_str: The input multiline string.
+    :type multiline_str: str
+    :return: The processed multiline string.
+    :rtype: str
+    """
+    lines = multiline_str.splitlines()
+    start_index = 0
+    while start_index < len(lines) and lines[start_index].strip() == "":
+        start_index += 1
+
+    # Find the minimum number of leading spaces
+    min_spaces = sys.maxsize
+    for line in lines[start_index:]:
+        if len(line.strip()) == 0:
+            continue
+        spaces = len(line) - len(line.lstrip())
+        spaces += line.lstrip().count("\t") * 2  # Count tabs as 2 spaces
+        min_spaces = min(min_spaces, spaces)
+
+    # Remove leading spaces and indent to the minimum level
+    processed_lines = []
+    for line in lines[start_index:]:
+        processed_lines.append(line[min_spaces:])
+
+    return "\n".join(processed_lines)