Skip to content

Commit

Permalink
feat: Integrate slots parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramimashkouk committed Jul 22, 2024
1 parent 0c53232 commit d787906
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
19 changes: 18 additions & 1 deletion chatsky/pipeline/pipeline/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import asyncio
import logging
from typing import Union, List, Dict, Optional, Hashable, Callable
from collections import defaultdict

from chatsky.context_storages import DBContextStorage
from chatsky.script import Script, Context, ActorStage
Expand All @@ -25,7 +26,7 @@

from chatsky.messengers.console import CLIMessengerInterface
from chatsky.messengers.common import MessengerInterface
from chatsky.slots.slots import GroupSlot
from chatsky.slots.slots import GroupSlot, RegexpSlot, FunctionSlot
from ..service.group import ServiceGroup
from ..types import (
ServiceBuilder,
Expand Down Expand Up @@ -294,11 +295,27 @@ def to_tuple(i):
if isinstance(i, list):
return tuple(i)
return i

def dict2slots(slots_dict: dict):
slots = defaultdict(dict)
for group_slot_name, group_slot_value in slots_dict.items():
for slot_type, slot_value in group_slot_value.items():
if slot_class := globals().get(slot_type):
slots[group_slot_name] = GroupSlot(**{k: slot_class(**v) for k, v in slot_value.items()})
else:
nested_group_slots = dict2slots({slot_type: slot_value})
slots[group_slot_name] = GroupSlot(**nested_group_slots)
return slots

slots = script["CONFIG"]["slots"]
slots = dict2slots(slots)

params = {param: to_tuple(script["CONFIG"].get(param)) for param in ("start_label", "fallback_label", "label_priority")}
del script["CONFIG"] # todo: add support for CONFIG
return cls(
script=script,
**params,
slots=slots,
# validation_stage=validation_stage,
condition_handler=condition_handler,
# verbose=verbose,
Expand Down
16 changes: 16 additions & 0 deletions json_import_examples/yaml/script.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
CONFIG:
custom_dir: custom
start_label: [flow, node]
slots:
person:
specific_person:
RegexpSlot:
username:
regexp: r"username is ([a-zA-Z]+)"
match_group_idx: 1
email:
regexp: r"email is ([a-z@\.A-Z]+)"
match_group_idx: 1
friend:
RegexpSlot:
first_name:
regexp: r"^[A-Z][a-z]+?(?= )"
last_name:
regexp: r"(?<= )[A-Z][a-z]+"
flow:
node:
RESPONSE:
Expand Down
3 changes: 2 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ python-telegram-bot = { version = "~=21.3", extras = ["all"], optional = true }
opentelemetry-instrumentation = { version = "*", optional = true }
sqlalchemy = { version = "*", extras = ["asyncio"], optional = true }
opentelemetry-exporter-otlp = { version = ">=1.20.0", optional = true } # log body serialization is required
pyyaml = "^6.0.1"

[tool.poetry.extras]
json = ["aiofiles"]
Expand Down

0 comments on commit d787906

Please sign in to comment.