File size: 1,376 Bytes
09b8dd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Copyright (c) 2022 Cohere Inc. and its affiliates.
#
# Licensed under the MIT License (the "License");
# you may not use this file except in compliance with the License.
#
# You may obtain a copy of the License in the LICENSE file at the top
# level of this repository.

from abc import ABC, abstractmethod
from typing import Dict, List, NewType

import cohere

Interaction = NewType("Interaction", Dict[str, str])


class Chatbot(ABC):
    """Defines bots that reply to users in a 1:1 text conversation"""

    def __init__(self, client: cohere.Client):
        """A Chatbot should be passed a Cohere Client for API calls.

        Args:
            client (cohere.Client): Provides access to Cohere API via the Python SDK
        """

        self.co = client

        # Holds the full, formatted chat history with type of
        # List[Interaction]. Each dict object represents a
        # conversation turn and has two keys: "speaker_name" and
        # "utterance".
        self.chat_history: List[Interaction] = []

    @abstractmethod
    def reply(self, query: str) -> Interaction:
        """Replies to a user given some input and context.

        Args:
            query (str): Most recent message from the user.

        Returns:
            Interaction: A dictionary consisting of the interaction, including the
                query and response.
        """