Skip to content

Commit

Permalink
Merge branch 'dev' into merge/extended_conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
NotBioWaste905 committed Oct 9, 2024
2 parents 7dd03a1 + 8eb9acf commit ca79f94
Show file tree
Hide file tree
Showing 210 changed files with 9,322 additions and 8,416 deletions.
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
62 changes: 28 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# 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)
[![Tests](https://github.com/deeppavlov/chatsky/workflows/test_coverage/badge.svg?branch=dev)](https://github.com/deeppavlov/chatsky/actions/workflows/test_coverage.yml)
[![License Apache 2.0](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/deeppavlov/chatsky/blob/master/LICENSE)
![Python 3.8, 3.9, 3.10, 3.11](https://img.shields.io/badge/python-3.8%20%7C%203.9%20%7C%203.10%20%7C%203.11-green.svg)
[![PyPI](https://img.shields.io/pypi/v/chatsky)](https://pypi.org/project/chatsky/)
[![Downloads](https://pepy.tech/badge/chatsky)](https://pepy.tech/project/chatsky)
[![Downloads](https://static.pepy.tech/badge/chatsky)](https://pepy.tech/project/chatsky)

Chatsky allows you to develop conversational services.
Chatsky offers a specialized domain-specific language (DSL) for quickly writing dialogs in pure Python. The service is created by defining a special dialog graph that determines the behavior of the dialog agent. The latter is then leveraged in the Chatsky pipeline.
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

0 comments on commit ca79f94

Please sign in to comment.