Files
microsoft--promptflow/examples/flex-flows/chat-basic/flow.py
T
wehub-resource-sync e768098d0e
Flake8 Lint / flake8 (push) Waiting to run
Spell check CI / Spell_Check (push) Waiting to run
tools_continuous_delivery / Private PyPI non-main branch release (push) Has been skipped
tools_continuous_delivery / Private PyPI main branch release (push) Failing after 2m42s
Publish Promptflow Doc / Build (push) Has been cancelled
Publish Promptflow Doc / Deploy (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:39:52 +08:00

67 lines
1.8 KiB
Python

import os
from pathlib import Path
from promptflow.tracing import trace
from promptflow.core import AzureOpenAIModelConfiguration, Prompty
BASE_DIR = Path(__file__).absolute().parent
def log(message: str):
verbose = os.environ.get("VERBOSE", "false")
if verbose.lower() == "true":
print(message, flush=True)
class ChatFlow:
def __init__(
self, model_config: AzureOpenAIModelConfiguration, max_total_token=4096
):
self.model_config = model_config
self.max_total_token = max_total_token
@trace
def __call__(
self,
question: str = "What's Azure Machine Learning?",
chat_history: list = None,
) -> str:
"""Flow entry function."""
prompty = Prompty.load(
source=BASE_DIR / "chat.prompty",
model={"configuration": self.model_config},
)
chat_history = chat_history or []
# Try to render the prompt with token limit and reduce the history count if it fails
while len(chat_history) > 0:
token_count = prompty.estimate_token_count(
question=question, chat_history=chat_history
)
if token_count > self.max_total_token:
chat_history = chat_history[1:]
log(
f"Reducing chat history count to {len(chat_history)} to fit token limit"
)
else:
break
# output is a string
output = prompty(question=question, chat_history=chat_history)
return output
if __name__ == "__main__":
from promptflow.tracing import start_trace
start_trace()
config = AzureOpenAIModelConfiguration(
connection="open_ai_connection", azure_deployment="gpt-4o"
)
flow = ChatFlow(config)
result = flow("What's Azure Machine Learning?", [])
print(result)