Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix custom messenger interface #206

Merged
merged 17 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions tests/pipeline/test_tutorials.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from tests.test_utils import get_path_from_tests_to_current_dir
from dff.utils.testing import check_happy_path, HAPPY_PATH
from dff.script import Message

dot_path_to_addon = get_path_from_tests_to_current_dir(__file__, separator=".")

Expand All @@ -19,22 +18,14 @@
"4_groups_and_conditions_full",
"5_asynchronous_groups_and_services_basic",
"5_asynchronous_groups_and_services_full",
"6_custom_messenger_interface",
"7_extra_handlers_basic",
"7_extra_handlers_full",
"8_extra_handlers_and_extensions",
"6_extra_handlers_basic",
"6_extra_handlers_full",
"7_extra_handlers_and_extensions",
],
)
def test_tutorials(tutorial_module_name: str):
try:
tutorial_module = importlib.import_module(f"tutorials.{dot_path_to_addon}.{tutorial_module_name}")
except ModuleNotFoundError as e:
pytest.skip(f"dependencies unavailable: {e.msg}")
if tutorial_module_name == "6_custom_messenger_interface":
happy_path = tuple(
(req, Message(misc={"webpage": tutorial_module.construct_webpage_by_response(res.text)}))
for req, res in HAPPY_PATH
)
check_happy_path(tutorial_module.pipeline, happy_path)
else:
check_happy_path(tutorial_module.pipeline, HAPPY_PATH)
check_happy_path(tutorial_module.pipeline, HAPPY_PATH)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# %% [markdown]
"""
# 6. Custom messenger interface
# Web API: 1. flask

The following tutorial shows messenger interfaces usage.
"""
Expand All @@ -12,7 +12,7 @@

from dff.messengers.common.interface import CallbackMessengerInterface
from dff.script import Context, Message
from flask import Flask, request, Request
from flask import Flask, request, Request, redirect

from dff.pipeline import Pipeline, ACTOR
from dff.utils.testing import is_interactive_mode, TOY_SCRIPT
Expand Down Expand Up @@ -81,16 +81,27 @@ def construct_webpage_by_response(response: str) -> str:
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
p {{text-align: center;}}
p {{
text-align: center;
margin-bottom: 2rem;
}}
img {{
display: block;
margin-left: auto;
margin-right: auto;
}}
form {{
text-align: center;
display: block;
margin-left: auto;
margin-right: auto;
margin-bottom: 2rem;
}}
</style>
</head>
<body>
<p><b>{response}</b></p>
<form method="get"><input name="request" type="text"/><button>Submit</button></form>
<img
src="https://source.unsplash.com/random?{response}"
alt="Response picture" style="width:50%;height:50%;"
Expand Down Expand Up @@ -134,10 +145,20 @@ def cat_response2webpage(ctx: Context):
}


@app.route("/")
@app.route("/chat")
async def to_interface():
return redirect("/pipeline_web_interface")


@app.route("/pipeline_web_interface")
async def route():
if not request.args or not request.args.get("request"):
message = Message(text="Hi")
else:
message = Message(text=request.args.get("request"))
ctx_id = 0 # 0 will be current dialog (context) identification.
return messenger_interface.on_request(request, ctx_id).last_response.text
return messenger_interface.on_request(message, ctx_id).last_response.misc["webpage"]


# %%
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# %% [markdown]
"""
# Web API: 1. FastAPI
# Web API: 2. FastAPI

This tutorial shows how to create an API for DFF using FastAPI.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# %% [markdown]
"""
# Web API: 2. WebSocket Chat
# Web API: 3. WebSocket Chat

This tutorial shows how to create a Web chat on FastAPI using websockets.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# %% [markdown]
"""
# Web API: 3. Load testing with Locust
# Web API: 4. Load testing with Locust

This tutorial shows how to use an API endpoint created in the FastAPI tutorial in load testing.
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# %% [markdown]
# # Web API: 4. Streamlit chat interface
#
# This tutorial shows how to use an API endpoint created in the FastAPI tutorial
# in a Streamlit chat.
#
# A demonstration of the chat:
# ![demo](https://user-images.githubusercontent.com/61429541/238721597-ef88261d-e9e6-497d-ba68-0bcc9a765808.png)
"""
# Web API: 5. Streamlit chat interface

This tutorial shows how to use an API endpoint created in the FastAPI tutorial
in a Streamlit chat.

A demonstration of the chat:
![demo](
https://user-images.githubusercontent.com/61429541/238721597-ef88261d-e9e6-497d-ba68-0bcc9a765808.png
)
"""

# %pip install dff streamlit streamlit-chat

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# %% [markdown]
"""
# 7. Extra Handlers (basic)
# 6. Extra Handlers (basic)

The following tutorial shows extra handlers possibilities and use cases.
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# %% [markdown]
"""
# 7. Extra Handlers (full)
# 6. Extra Handlers (full)

The following tutorial shows extra handlers possibilities and use cases.
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# %% [markdown]
"""
# 8. Extra Handlers and Extensions
# 7. Extra Handlers and Extensions

The following tutorial shows how pipeline can be extended
by global extra handlers and custom functions.
Expand Down