From 68db70c96b4f34b1af3aae9597674f3b8c4b322d Mon Sep 17 00:00:00 2001 From: Jeremiah Lowin <153965+jlowin@users.noreply.github.com> Date: Tue, 30 Jan 2024 08:19:10 -0500 Subject: [PATCH 1/4] Add call routing example --- docs/examples/call_routing.md | 44 +++++++++++++++++++++++++++++++++++ mkdocs.yml | 1 + 2 files changed, 45 insertions(+) create mode 100644 docs/examples/call_routing.md diff --git a/docs/examples/call_routing.md b/docs/examples/call_routing.md new file mode 100644 index 000000000..550c02ebe --- /dev/null +++ b/docs/examples/call_routing.md @@ -0,0 +1,44 @@ +# Customer call routing + +Automatically route customer calls to the right department. + +!!! example "Call routing" + ```python + import marvin + from enum import Enum + + # define departments as an Enum, with some additional instructions + class Department(Enum): + """Use `agent` when no other department is applicable.""" + SALES = "sales" + SUPPORT = "support" + BILLING = "billing" + AGENT = "agent" + + + # define a convenience function to route calls to the right department + def router(text: str) -> Department: + return marvin.classify( + text, + labels=Department, + instructions="Select the best department for the customer request", + ) + ``` + + !!! success "Update payment method" + ```python + department = router("I need to update my payment method") + assert department == Department.BILLING + ``` + + !!! success "Price matching" + ```python + department = router("Do you price match?") + assert department == Department.SALES + ``` + + !!! success "Angry noises" + ```python + department = router("*angry noises*") + assert department == Department.SUPPORT + ``` \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index 98f634886..ebbe7c039 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -83,6 +83,7 @@ nav: - examples/xkcd_bird.md - examples/michael_scott_business/michael_scott_business.md - examples/hogwarts_sorting_hat/hogwarts_sorting_hat.md + - examples/call_routing.md - Community: From 7ca3ec835358c1e2c7c7b6937e69fe6faa3991d6 Mon Sep 17 00:00:00 2001 From: Jeremiah Lowin <153965+jlowin@users.noreply.github.com> Date: Tue, 30 Jan 2024 08:21:07 -0500 Subject: [PATCH 2/4] Update test_classify.py --- tests/ai/test_classify.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/ai/test_classify.py b/tests/ai/test_classify.py index 286542680..e39babda7 100644 --- a/tests/ai/test_classify.py +++ b/tests/ai/test_classify.py @@ -82,3 +82,32 @@ async def test_hogwarts_sorting_hat(self): ) assert house == "Gryffindor" + + async def test_call_routing(self): + from enum import Enum + + import marvin + + class Department(Enum): + """Use `agent` when no other department is applicable.""" + + SALES = "sales" + SUPPORT = "support" + BILLING = "billing" + AGENT = "agent" + + def router(text: str) -> Department: + return marvin.classify( + text, + labels=Department, + instructions="Select the best department for the customer request", + ) + + department = router("I need to update my payment method") + assert department == Department.BILLING + + department = router("Do you price match?") + assert department == Department.SALES + + department = router("*angry noises*") + assert department == Department.SUPPORT From a2f11ebef77dc2f467565df336d1b6ab2937b16d Mon Sep 17 00:00:00 2001 From: Jeremiah Lowin <153965+jlowin@users.noreply.github.com> Date: Tue, 30 Jan 2024 14:14:36 -0500 Subject: [PATCH 3/4] Clean up --- docs/examples/call_routing.md | 22 ++++++++++------------ tests/ai/test_classify.py | 7 ++----- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/docs/examples/call_routing.md b/docs/examples/call_routing.md index 550c02ebe..20b8c8280 100644 --- a/docs/examples/call_routing.md +++ b/docs/examples/call_routing.md @@ -7,38 +7,36 @@ Automatically route customer calls to the right department. import marvin from enum import Enum - # define departments as an Enum, with some additional instructions + class Department(Enum): - """Use `agent` when no other department is applicable.""" SALES = "sales" SUPPORT = "support" BILLING = "billing" - AGENT = "agent" - # define a convenience function to route calls to the right department - def router(text: str) -> Department: + # define a convenience function + def route_call(transcript: str) -> Department: return marvin.classify( - text, + transcript, labels=Department, instructions="Select the best department for the customer request", ) ``` - !!! success "Update payment method" + !!! success "💳 Update payment method" ```python - department = router("I need to update my payment method") + department = route_call("I need to update my payment method") assert department == Department.BILLING ``` - !!! success "Price matching" + !!! success "💵 Price matching" ```python - department = router("Do you price match?") + department = route_call("Do you price match?") assert department == Department.SALES ``` - !!! success "Angry noises" + !!! success "🤬 Angry noises" ```python - department = router("*angry noises*") + department = route_call("*angry noises*") assert department == Department.SUPPORT ``` \ No newline at end of file diff --git a/tests/ai/test_classify.py b/tests/ai/test_classify.py index e39babda7..af02f344e 100644 --- a/tests/ai/test_classify.py +++ b/tests/ai/test_classify.py @@ -89,16 +89,13 @@ async def test_call_routing(self): import marvin class Department(Enum): - """Use `agent` when no other department is applicable.""" - SALES = "sales" SUPPORT = "support" BILLING = "billing" - AGENT = "agent" - def router(text: str) -> Department: + def router(transcript: str) -> Department: return marvin.classify( - text, + transcript, labels=Department, instructions="Select the best department for the customer request", ) From 4d14ca4c7f9bd34f1c5de611d189a746265a4c88 Mon Sep 17 00:00:00 2001 From: Nathan Nowack Date: Tue, 30 Jan 2024 15:55:17 -0500 Subject: [PATCH 4/4] fix tests --- docs/examples/call_routing.md | 2 +- tests/ai/test_classify.py | 24 +++++++++++------------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/docs/examples/call_routing.md b/docs/examples/call_routing.md index 20b8c8280..547ae26a3 100644 --- a/docs/examples/call_routing.md +++ b/docs/examples/call_routing.md @@ -31,7 +31,7 @@ Automatically route customer calls to the right department. !!! success "💵 Price matching" ```python - department = route_call("Do you price match?") + department = route_call("Well FooCo offered me a better deal") assert department == Department.SALES ``` diff --git a/tests/ai/test_classify.py b/tests/ai/test_classify.py index af02f344e..2bcee5167 100644 --- a/tests/ai/test_classify.py +++ b/tests/ai/test_classify.py @@ -2,6 +2,7 @@ from typing import Literal import marvin +import pytest Sentiment = Literal["Positive", "Negative"] @@ -83,11 +84,15 @@ async def test_hogwarts_sorting_hat(self): assert house == "Gryffindor" - async def test_call_routing(self): - from enum import Enum - - import marvin - + @pytest.mark.parametrize( + "user_input, expected_selection", + [ + ("I need to update my payment method", "billing"), + ("Well FooCo offered me a better deal", "sales"), + ("*angry noises*", "support"), + ], + ) + async def test_call_routing(self, user_input, expected_selection): class Department(Enum): SALES = "sales" SUPPORT = "support" @@ -100,11 +105,4 @@ def router(transcript: str) -> Department: instructions="Select the best department for the customer request", ) - department = router("I need to update my payment method") - assert department == Department.BILLING - - department = router("Do you price match?") - assert department == Department.SALES - - department = router("*angry noises*") - assert department == Department.SUPPORT + assert router(user_input).value == expected_selection