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

v1.0.0rc1 #388

Merged
merged 13 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .github/process_github_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def post_comment_on_pr(comment: str, pr_number: int):
- [ ] Change PR merge option
- [ ] Update template repo
- [ ] Search for objects to be deprecated
- [ ] Test parts not covered with pytest:
- [ ] web_api tutorials
- [ ] Test integrations with external services (telegram; stats)
"""


Expand Down
60 changes: 27 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Chatsky
![Chatsky](docs/source/_static/images/Chatsky-full-dark.svg)

[![Documentation Status](https://github.com/deeppavlov/chatsky/workflows/build_and_publish_docs/badge.svg?branch=dev)](https://deeppavlov.github.io/chatsky)
[![Codestyle](https://github.com/deeppavlov/chatsky/workflows/codestyle/badge.svg?branch=dev)](https://github.com/deeppavlov/chatsky/actions/workflows/codestyle.yml)
Expand Down Expand Up @@ -79,53 +79,47 @@ All the abstractions used in this example are thoroughly explained in the dedica
[user guide](https://deeppavlov.github.io/chatsky/user_guides/basic_conceptions.html).

```python
from chatsky.script import GLOBAL, TRANSITIONS, RESPONSE, Message
from chatsky.pipeline import Pipeline
import chatsky.script.conditions.std_conditions as cnd
from chatsky import (
GLOBAL,
TRANSITIONS,
RESPONSE,
Pipeline,
conditions as cnd,
Transition as Tr,
)

# create a dialog script
script = {
GLOBAL: {
TRANSITIONS: {
("flow", "node_hi"): cnd.exact_match("Hi"),
("flow", "node_ok"): cnd.true()
}
TRANSITIONS: [
Tr(
dst=("flow", "node_hi"),
cnd=cnd.ExactMatch("Hi"),
),
Tr(
dst=("flow", "node_ok")
)
]
},
"flow": {
"node_hi": {RESPONSE: Message("Hi!")},
"node_ok": {RESPONSE: Message("OK")},
"node_hi": {RESPONSE: "Hi!"},
"node_ok": {RESPONSE: "OK"},
},
}

# init pipeline
pipeline = Pipeline.from_script(script, start_label=("flow", "node_hi"))
# initialize Pipeline (needed to run the script)
pipeline = Pipeline(script, start_label=("flow", "node_hi"))


def turn_handler(in_request: Message, pipeline: Pipeline) -> Message:
# Pass user request into pipeline and get dialog context (message history)
# The pipeline will automatically choose the correct response using script
ctx = pipeline(in_request, 0)
# Get last response from the context
out_response = ctx.last_response
return out_response


while True:
in_request = input("Your message: ")
out_response = turn_handler(Message(in_request), pipeline)
print("Response: ", out_response.text)
pipeline.run()
```

When you run this code, you get similar output:
```
Your message: hi
Response: OK
Your message: Hi
Response: Hi!
Your message: ok
Response: OK
Your message: ok
Response: OK
request: hi
response: text='OK'
request: Hi
response: text='Hi!'
```

More advanced examples are available as a part of documentation:
Expand Down
39 changes: 35 additions & 4 deletions chatsky/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,42 @@
__version__ = version(__name__)


import nest_asyncio
import nest_asyncio as __nest_asyncio__

nest_asyncio.apply()
__nest_asyncio__.apply()

from chatsky.core import (
GLOBAL,
LOCAL,
RESPONSE,
TRANSITIONS,
MISC,
PRE_RESPONSE,
PRE_TRANSITION,
BaseCondition,
AnyCondition,
BaseResponse,
AnyResponse,
BaseDestination,
AnyDestination,
BaseProcessing,
BasePriority,
AnyPriority,
Pipeline,
Context,
Message,
Transition,
Transition as Tr,
MessageInitTypes,
NodeLabel,
NodeLabelInitTypes,
AbsoluteNodeLabel,
AbsoluteNodeLabelInitTypes,
)
import chatsky.conditions as cnd
import chatsky.destinations as dst
import chatsky.responses as rsp
import chatsky.processing as proc

from chatsky.pipeline import Pipeline
from chatsky.script import Context, Script

import chatsky.__rebuild_pydantic_models__
11 changes: 8 additions & 3 deletions chatsky/__rebuild_pydantic_models__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# flake8: noqa: F401

from chatsky.pipeline import Pipeline
from chatsky.pipeline.types import ExtraHandlerRuntimeInfo
from chatsky.script import Context, Script
from chatsky.core.service.types import ExtraHandlerRuntimeInfo, StartConditionCheckerFunction, ComponentExecutionState
from chatsky.core import Context, Script
from chatsky.core.script import Node
from chatsky.core.pipeline import Pipeline
from chatsky.slots.slots import SlotManager
from chatsky.core.context import FrameworkData

Pipeline.model_rebuild()
Script.model_rebuild()
Context.model_rebuild()
ExtraHandlerRuntimeInfo.model_rebuild()
FrameworkData.model_rebuild()
12 changes: 12 additions & 0 deletions chatsky/conditions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from chatsky.conditions.standard import (
ExactMatch,
HasText,
Regexp,
Any,
All,
Negation,
CheckLastLabels,
Not,
HasCallbackQuery,
)
from chatsky.conditions.slots import SlotsExtracted
38 changes: 38 additions & 0 deletions chatsky/conditions/slots.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Slot Conditions
---------------------------
Provides slot-related conditions.
"""

from __future__ import annotations
from typing import Literal, List

from chatsky.core import Context, BaseCondition
from chatsky.slots.slots import SlotName


class SlotsExtracted(BaseCondition):
"""
Check if :py:attr:`.slots` are extracted.

:param mode: Whether to check if all slots are extracted or any slot is extracted.
"""

slots: List[SlotName]
"""
Names of the slots that need to be checked.
"""
mode: Literal["any", "all"] = "all"
"""
Whether to check if all slots are extracted or any slot is extracted.
"""

def __init__(self, *slots: SlotName, mode: Literal["any", "all"] = "all"):
super().__init__(slots=slots, mode=mode)

async def call(self, ctx: Context) -> bool:
manager = ctx.framework_data.slot_manager
if self.mode == "all":
return all(manager.is_slot_extracted(slot) for slot in self.slots)
elif self.mode == "any":
return any(manager.is_slot_extracted(slot) for slot in self.slots)
Loading
Loading