-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into wanhan/flow_as_func
- Loading branch information
Showing
23 changed files
with
362 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/promptflow/promptflow/_sdk/data/executable/flow_test_main.py.jinja2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
{# This template is added only for chat flow with single input and output. #} | ||
import json | ||
import os | ||
from pathlib import Path | ||
from PIL import Image | ||
import streamlit as st | ||
from streamlit_quill import st_quill | ||
|
||
from promptflow._sdk._serving.flow_invoker import FlowInvoker | ||
|
||
from utils import dict_iter_render_message, parse_list_from_html, parse_image_content | ||
|
||
invoker = None | ||
{% set indent_level = 4 %} | ||
|
||
def start(): | ||
def clear_chat() -> None: | ||
st.session_state.messages = [] | ||
|
||
def render_message(role, message_items): | ||
with st.chat_message(role): | ||
dict_iter_render_message(message_items) | ||
|
||
def show_conversation() -> None: | ||
if "messages" not in st.session_state: | ||
st.session_state.messages = [] | ||
st.session_state.history = [] | ||
if st.session_state.messages: | ||
for role, message_items in st.session_state.messages: | ||
render_message(role, message_items) | ||
|
||
|
||
def get_chat_history_from_session(): | ||
if "history" in st.session_state: | ||
return st.session_state.history | ||
return [] | ||
|
||
|
||
def submit(**kwargs) -> None: | ||
st.session_state.messages.append(("user", kwargs)) | ||
session_state_history = dict() | ||
session_state_history.update({"inputs": kwargs}) | ||
with container: | ||
render_message("user", kwargs) | ||
# Append chat history to kwargs | ||
response = run_flow({'{{chat_history_input_name}}': get_chat_history_from_session(), **kwargs}) | ||
# Get base64 for multi modal object | ||
resolved_outputs = invoker._convert_multimedia_data_to_base64(response) | ||
st.session_state.messages.append(("assistant", resolved_outputs)) | ||
session_state_history.update({"outputs": response.output}) | ||
st.session_state.history.append(session_state_history) | ||
invoker._dump_invoke_result(response) | ||
with container: | ||
render_message("assistant", resolved_outputs) | ||
|
||
|
||
def run_flow(data: dict) -> dict: | ||
global invoker | ||
if not invoker: | ||
flow = Path('{{flow_path}}') | ||
dump_path = Path('{{flow_path}}').parent | ||
if flow.is_dir(): | ||
os.chdir(flow) | ||
else: | ||
os.chdir(flow.parent) | ||
invoker = FlowInvoker(flow, connection_provider="""{{ connection_provider }}""", dump_to=dump_path) | ||
result = invoker._invoke(data) | ||
return result | ||
|
||
image = Image.open(Path(__file__).parent / "logo.png") | ||
st.set_page_config( | ||
layout="wide", | ||
page_title="{{flow_name}} - Promptflow App", | ||
page_icon=image, | ||
menu_items={ | ||
'About': """ | ||
# This is a Promptflow App. | ||
|
||
You can refer to [promptflow](https://github.com/microsoft/promptflow) for more information. | ||
""" | ||
} | ||
) | ||
# Set primary button color here since button color of the same form need to be identical in streamlit, but we only need Run/Chat button to be blue. | ||
st.config.set_option("theme.primaryColor", "#0F6CBD") | ||
st.title("{{flow_name}}") | ||
st.divider() | ||
st.chat_message("assistant").write("Hello, please input following flow inputs.") | ||
container = st.container() | ||
with container: | ||
show_conversation() | ||
|
||
with st.form(key='input_form', clear_on_submit=True): | ||
st.text('{{chat_input_name}}') | ||
{% if chat_input_value_type == "list" %} | ||
{{chat_input_name}} = st_quill(html=True, toolbar=["image"], key='{{chat_input_name}}', placeholder='Please enter the list values and use the image icon to upload a picture. Make sure to format each list item correctly with line breaks') | ||
{% elif chat_input_value_type == "string" %} | ||
{{chat_input_name}} = st.text_input(label='{{chat_input_name}}', placeholder='{{chat_input_default_value}}') | ||
{% else %} | ||
{{chat_input_name}} = st.text_input(label='{{chat_input_name}}', placeholder={{chat_input_default_value}}) | ||
{% endif %} | ||
|
||
cols = st.columns(7) | ||
submit_bt = cols[0].form_submit_button(label='Chat', type='primary') | ||
clear_bt = cols[1].form_submit_button(label='Clear') | ||
|
||
if submit_bt: | ||
with st.spinner("Loading..."): | ||
{% if chat_input_value_type == "list" %} | ||
{{chat_input_name}} = parse_list_from_html({{chat_input_name}}) | ||
{% endif %} | ||
submit({{flow_inputs_params}}) | ||
|
||
if clear_bt: | ||
with st.spinner("Cleaning..."): | ||
clear_chat() | ||
st.rerun() | ||
|
||
if __name__ == "__main__": | ||
start() |
Oops, something went wrong.