|
1 | | -# Chatsky |
| 1 | + |
2 | 2 |
|
3 | 3 | [](https://deeppavlov.github.io/chatsky) |
4 | 4 | [](https://github.com/deeppavlov/chatsky/actions/workflows/codestyle.yml) |
@@ -79,53 +79,47 @@ All the abstractions used in this example are thoroughly explained in the dedica |
79 | 79 | [user guide](https://deeppavlov.github.io/chatsky/user_guides/basic_conceptions.html). |
80 | 80 |
|
81 | 81 | ```python |
82 | | -from chatsky.script import GLOBAL, TRANSITIONS, RESPONSE, Message |
83 | | -from chatsky.pipeline import Pipeline |
84 | | -import chatsky.script.conditions.std_conditions as cnd |
| 82 | +from chatsky import ( |
| 83 | + GLOBAL, |
| 84 | + TRANSITIONS, |
| 85 | + RESPONSE, |
| 86 | + Pipeline, |
| 87 | + conditions as cnd, |
| 88 | + Transition as Tr, |
| 89 | +) |
85 | 90 |
|
86 | 91 | # create a dialog script |
87 | 92 | script = { |
88 | 93 | GLOBAL: { |
89 | | - TRANSITIONS: { |
90 | | - ("flow", "node_hi"): cnd.exact_match("Hi"), |
91 | | - ("flow", "node_ok"): cnd.true() |
92 | | - } |
| 94 | + TRANSITIONS: [ |
| 95 | + Tr( |
| 96 | + dst=("flow", "node_hi"), |
| 97 | + cnd=cnd.ExactMatch("Hi"), |
| 98 | + ), |
| 99 | + Tr( |
| 100 | + dst=("flow", "node_ok") |
| 101 | + ) |
| 102 | + ] |
93 | 103 | }, |
94 | 104 | "flow": { |
95 | | - "node_hi": {RESPONSE: Message("Hi!")}, |
96 | | - "node_ok": {RESPONSE: Message("OK")}, |
| 105 | + "node_hi": {RESPONSE: "Hi!"}, |
| 106 | + "node_ok": {RESPONSE: "OK"}, |
97 | 107 | }, |
98 | 108 | } |
99 | 109 |
|
100 | | -# init pipeline |
101 | | -pipeline = Pipeline.from_script(script, start_label=("flow", "node_hi")) |
| 110 | +# initialize Pipeline (needed to run the script) |
| 111 | +pipeline = Pipeline(script, start_label=("flow", "node_hi")) |
102 | 112 |
|
103 | 113 |
|
104 | | -def turn_handler(in_request: Message, pipeline: Pipeline) -> Message: |
105 | | - # Pass user request into pipeline and get dialog context (message history) |
106 | | - # The pipeline will automatically choose the correct response using script |
107 | | - ctx = pipeline(in_request, 0) |
108 | | - # Get last response from the context |
109 | | - out_response = ctx.last_response |
110 | | - return out_response |
111 | | - |
112 | | - |
113 | | -while True: |
114 | | - in_request = input("Your message: ") |
115 | | - out_response = turn_handler(Message(in_request), pipeline) |
116 | | - print("Response: ", out_response.text) |
| 114 | +pipeline.run() |
117 | 115 | ``` |
118 | 116 |
|
119 | 117 | When you run this code, you get similar output: |
120 | 118 | ``` |
121 | | -Your message: hi |
122 | | -Response: OK |
123 | | -Your message: Hi |
124 | | -Response: Hi! |
125 | | -Your message: ok |
126 | | -Response: OK |
127 | | -Your message: ok |
128 | | -Response: OK |
| 119 | +request: hi |
| 120 | +response: text='OK' |
| 121 | +request: Hi |
| 122 | +response: text='Hi!' |
129 | 123 | ``` |
130 | 124 |
|
131 | 125 | More advanced examples are available as a part of documentation: |
|
0 commit comments