long / prompts /tool_parser.py
deeme's picture
Upload 111 files
217acfe verified
from promptflow.core import tool
from enum import Enum
class ResponseType(str, Enum):
CONTENT = "content"
SEPARATORS = "separators"
CODEBLOCK = "codeblock"
import sys, os
root_path = os.path.abspath(os.path.join(os.path.abspath(__file__), "../.."))
if root_path not in sys.path:
sys.path.append(root_path)
# The inputs section will change based on the arguments of the tool function, after you save the code
# Adding type to arguments and return value will help the system show the types properly
# Please update the function name/signature per need
@tool
def parse_response(response_msgs, response_type: Enum):
from prompts.prompt_utils import parse_chunks_by_separators, match_code_block
content = response_msgs[-1]['content']
if response_type == ResponseType.CONTENT:
return content
elif response_type == ResponseType.CODEBLOCK:
codeblock = match_code_block(content)
if codeblock:
return codeblock[-1]
else:
raise Exception("ζ— ζ³•θ§£ζžε›žη­”οΌŒζœͺεŒ…ε«δΈ‰εΌ•ε·δ»£η ε—γ€‚")
elif response_type == ResponseType.SEPARATORS:
chunks = parse_chunks_by_separators(content, [r'\S*', ])
return chunks
else:
raise Exception(f"ζ— ζ•ˆηš„θ§£ζžη±»εž‹οΌš{response_type}")