Code Monkey home page Code Monkey logo

examples's Introduction

CoCo Logo

Conversational components with CoCo examples

An example of how to use Conversational Components in your chatbot, with integration to google dialogflow.

  1. Background
  2. Getting Started
  3. High Level Architercure
    1. add new comp
    2. component and action functions
    3. contol transfer
    4. customize the action
    5. share content with comp
    6. customize the comp

Background

Conversational Components

Conversational Components is a concept developed to address the problem of reusability in chatbots.

Each Conversational Component maintains its own state, performs its own understanding, includes actions (+responses) and exposes a small interface to pass inputs, responses and context back and forth.

CoCo

Conversational Components (CoCo) is the first vendor to offer pre-built components

CoCo website
CoCo developers console

Getting Started

python3 is required to run the examples and we recommend using a virtualenv

1. git clone https://github.com/chenb67/coco_examples
2. pip install -r requirements.txt
3. generate a dialogflow bot and import the included intents and entities
4. create and download service account json and place it under root - dialogflow_serviceaccount.json
5. python console_tester.py

More About The Examples

In this repo we demostrate integration with CoCo, nlu platforms and other services to create a chatbot Below is some general info about the architecture

Live bot demo: https://webdemo1-dot-coco-235210.appspot.com

Adding new comp

Call CoCo with the user input and get the component's response.

def coco_exchange(component_id, session_id, **kwargs):
    return requests.post(
        "https://app.coco.imperson.com/api/exchange/" 
        f"{component_id}/{session_id}",
        json=kwargs,
    ).json()

response = coco_exchange("namer_vp3", state["session_id"], user_input=user_input)

When CoCo returns component_done=true it means the control goes back to the calling bot/component. This a good time to collect variables and update the state

if response["component_done"]:
        state["user"] = response["updated_context"].get("user")
        first_name = response["updated_context"].get("user", {}).get("firstName", "")
        return [get_drink_action(first_name)], None, True
return [response["response"]], None, False

Component and actions functions

def component_name(state, user_input):
    return [], str, bool

def action_name(**kwargs):
    return str

Component function takes two params - the state and the user_input it returns a tuple:

  1. a list of responses
  2. a new component to call and add to the stack or none
  3. if done and should stop call this comp

Available components registery is under __init__.py:

from coffeeshop.components_coco import namer_comp

Condition to control transfer to a component

if (
        interpretation.intent.display_name == "Default Welcome Intent"
        and not "name" in state
    ):
return [], "namer_comp", False

Return statement with no responses means recursively call the stack again with the new comp on top now.

U: hi <--- "Default Welcome Intent" calling CoCo
B: Can you please tell me your name?
U: Mor Buskilla
B: Hello mor. Nice to meet you. Is your last name Buskilla?
U: yes

Customize The Action (optional)

Best to have the actions under functions that use the state to determine how the text will look.

def get_drink_action(first_name=""):
    if first_name:
        return f"Nice to meet you {first_name}, What can I get you to drink?"
return "Hi, What can I get you to drink?"

U: hi
B: Can you please tell me your name?
U: Mor Buskilla
B: Hello mor. Nice to meet you. Is your last name Buskilla?
U: yes
B: Nice to meet you mor, What can I get you to drink? <--- custome response
U: coffee

Share Content With Comp

Many of the components make use of available information like name Here how to pass this data to Coco:

response = call_coco(
            "CoCoSurvey_619c51d02b6eb5", 
            state["session_id"], 
            user_input=user_input, 
            context={
                "user": state["user"] # {"firstName": "SomeName"}
                }
            )

U: hi
B: Can you please tell me your name?
U: Mor Buskilla
B: Hello mor. Nice to meet you. Is your last name Buskilla?
U: yes
B: Nice to meet you mor, What can I get you to drink?
U: coffee
B: Would you like a delivery or pick-up?
U: delivery
B: So, mor, I would like your address so that I can send you mail. I promise not to send anything without your prior consent. Do you live in the US? <--- using the name from CoCoNamer

Customize The Comp

@ app.coco.imperson.com

examples's People

Contributors

chenb67 avatar mor91 avatar jfg-boop avatar

Stargazers

Sivasurya Santhanam avatar  avatar  avatar  avatar

Watchers

James Cloos avatar  avatar  avatar  avatar  avatar Beloglazov Denis avatar

Forkers

jfg-boop

examples's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.