microsoft/TypeAgent
Publicmirrored from https://github.com/microsoft/TypeAgentAvailable
python/knowpro/kplib.py
83lines · modecode
| 1 | # Copyright (c) Microsoft Corporation. |
| 2 | # Licensed under the MIT License. |
| 3 | |
| 4 | # TODO: |
| 5 | # - What does the comment "Very concise values" in class Facet mean? |
| 6 | # - Do the protocols need to be @runtime_checkable? |
| 7 | # - Should the field names be camelCase to match the JSON schema? |
| 8 | # - For things of type float, should we add `| int` to emphasize that int is okay? |
| 9 | # - How to allow totally missing attributes? (facets, params, subject_entity_facet) |
| 10 | # - Should we use ABC instead of Protocol for certain classes? |
| 11 | |
| 12 | from typing import Literal, Protocol, runtime_checkable |
| 13 | |
| 14 | |
| 15 | @runtime_checkable |
| 16 | class Quantity(Protocol): |
| 17 | amount: float |
| 18 | units: str |
| 19 | |
| 20 | |
| 21 | type Value = str | float | bool | Quantity |
| 22 | |
| 23 | |
| 24 | @runtime_checkable |
| 25 | class Facet(Protocol): |
| 26 | name: str |
| 27 | # Very concise values. |
| 28 | value: Value |
| 29 | |
| 30 | |
| 31 | # Specific, tangible people, places, institutions or things only |
| 32 | @runtime_checkable |
| 33 | class ConcreteEntity(Protocol): |
| 34 | # The name of the entity or thing such as "Bach", "Great Gatsby", |
| 35 | # "frog" or "piano". |
| 36 | name: str |
| 37 | # The types of the entity such as "speaker", "person", "artist", "animal", |
| 38 | # "object", "instrument", "school", "room", "museum", "food" etc. |
| 39 | # An entity can have multiple types; entity types should be single words. |
| 40 | type: list[str] |
| 41 | # A specific, inherent, defining, or non-immediate facet of the entity |
| 42 | # such as "blue", "old", "famous", "sister", "aunt_of", "weight: 4 kg". |
| 43 | # Trivial actions or state changes are not facets. |
| 44 | # Facets are concise "properties". |
| 45 | facets: list[Facet] | None = None |
| 46 | |
| 47 | |
| 48 | @runtime_checkable |
| 49 | class ActionParam(Protocol): |
| 50 | name: str |
| 51 | value: Value |
| 52 | |
| 53 | |
| 54 | type VerbTense = Literal["past", "present", "future"] |
| 55 | |
| 56 | |
| 57 | @runtime_checkable |
| 58 | class Action(Protocol): |
| 59 | # Each verb is typically a word. |
| 60 | verb: list[str] |
| 61 | verb_tense: VerbTense |
| 62 | subject_entity_name: str | Literal["none"] |
| 63 | object_entity_name: str | Literal["none"] |
| 64 | indirect_object_entity_name: str | Literal["none"] |
| 65 | params: list[str | ActionParam] | None = None |
| 66 | # If the action implies this additional facet or property of the |
| 67 | # subject entity, such as hobbies, activities, interests, personality. |
| 68 | subject_entity_facet: Facet | None = None |
| 69 | |
| 70 | |
| 71 | # Detailed and comprehensive knowledge response. |
| 72 | @runtime_checkable |
| 73 | class KnowledgeResponse(Protocol): |
| 74 | entities: list[ConcreteEntity] |
| 75 | # The 'subject_entity_name' and 'object_entity_name' must correspond |
| 76 | # to the 'name' of an entity listed in the 'entities' array. |
| 77 | actions: list[Action] |
| 78 | # Some actions can ALSO be expressed in a reverse way... |
| 79 | # E.g. (A give to B) --> (B receive from A) and vice versa. |
| 80 | # If so, also return the reverse form of the action, full filled out. |
| 81 | inverse_actions: list[Action] |
| 82 | # Detailed, descriptive topics and keywords. |
| 83 | topics: list[str] |
| 84 | |