-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #253 from NVIDIA/feature/server-side-threads
Add support for server-side threads.
- Loading branch information
Showing
14 changed files
with
552 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Threads | ||
|
||
This is a sample server config folder with server-side threads enabled. | ||
|
||
To enable server-side threads, you must register a `DataStore` inside the `config.py` file, which should be placed at the root of the folder containing the rails configurations. | ||
|
||
```python | ||
from nemoguardrails.server.api import register_datastore | ||
from nemoguardrails.server.datastore.memory_store import MemoryStore | ||
|
||
register_datastore(MemoryStore()) | ||
``` | ||
|
||
## Rails Configurations | ||
|
||
For demo purposes, this configuration uses a single dialog rail, which makes the bot respond slightly differently when the user greets the bot the second time in a row. | ||
|
||
```colang | ||
define user express greeting | ||
"hi" | ||
define bot express greeting | ||
"Hello!" | ||
define bot express greeting again | ||
"Hello again!" | ||
define flow | ||
user express greeting | ||
bot express greeting | ||
user express greeting | ||
bot express greeting again | ||
``` | ||
|
||
## Running the server | ||
|
||
To run the server, use the following command from the root of the project: | ||
|
||
```bash | ||
nemoguardrails server --config=examples/configs/threads | ||
``` | ||
|
||
## Testing | ||
|
||
When sending "hi" to the server without a thread ID, it always responds with "Hello!" | ||
|
||
```bash | ||
curl -X POST -H "Content-Type: application/json" -d '{"config_id": "config_1", "messages": [{"content": "hi", "role": "user"}]}' http://localhost:8000/v1/chat/completions | ||
``` | ||
|
||
``` | ||
{"messages":[{"role":"assistant","content":"Hello!"}]} | ||
``` | ||
|
||
```bash | ||
curl -X POST -H "Content-Type: application/json" -d '{"config_id": "config_1", "messages": [{"content": "hi", "role": "user"}]}' http://localhost:8000/v1/chat/completions | ||
``` | ||
|
||
``` | ||
{"messages":[{"role":"assistant","content":"Hello!"}]} | ||
``` | ||
|
||
If you use a `thread_id`, then the conversation gets stored on the server side, and the second time we get the response "Hello again!". | ||
|
||
```bash | ||
curl -X POST -H "Content-Type: application/json" -d '{"config_id": "config_1", "thread_id": "1231231231231231", "messages": [{"content": "hi", "role": "user"}]}' http://localhost:8000/v1/chat/completions | ||
``` | ||
|
||
``` | ||
{"messages":[{"role":"assistant","content":"Hello!"}]} | ||
``` | ||
|
||
```bash | ||
curl -X POST -H "Content-Type: application/json" -d '{"config_id": "config_1", "thread_id": "1231231231231231", "messages": [{"content": "hi", "role": "user"}]}' http://localhost:8000/v1/chat/completions | ||
``` | ||
|
||
``` | ||
{"messages":[{"role":"assistant","content":"Hello again!"}]} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from nemoguardrails.server.api import register_datastore | ||
from nemoguardrails.server.datastore.memory_store import MemoryStore | ||
|
||
# This example uses an in-memory data store. | ||
register_datastore(MemoryStore()) | ||
|
||
# You can also use RedisStore | ||
# register_datastore(RedisStore("redis://localhost/1")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
models: [] | ||
|
||
# We set the embeddings only flag so that we don't need to use the LLM. | ||
# The test flow will use pre-defined flows and messages. | ||
rails: | ||
dialog: | ||
user_messages: | ||
embeddings_only: True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
define user express greeting | ||
"hi" | ||
|
||
define bot express greeting | ||
"Hello!" | ||
|
||
define bot express greeting again | ||
"Hello again!" | ||
|
||
define flow | ||
user express greeting | ||
bot express greeting | ||
user express greeting | ||
bot express greeting again |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import Optional | ||
|
||
|
||
class DataStore: | ||
"""A basic data store interface.""" | ||
|
||
async def set(self, key: str, value: str): | ||
"""Save data into the datastore. | ||
Args: | ||
key: The key to use. | ||
value: The value associated with the key. | ||
Returns: | ||
None | ||
""" | ||
raise NotImplementedError() | ||
|
||
async def get(self, key: str) -> Optional[str]: | ||
"""Return the value for the specified key. | ||
Args: | ||
key: The key to lookup. | ||
Returns: | ||
None if the key does not exist. | ||
""" | ||
raise NotImplementedError() |
Oops, something went wrong.