|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +""" |
| 17 | +Tests for basic RunnableRails operations (invoke, async, batch, stream). |
| 18 | +""" |
| 19 | + |
| 20 | +import pytest |
| 21 | +from langchain_core.messages import AIMessage, HumanMessage |
| 22 | +from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder |
| 23 | +from langchain_core.runnables import RunnablePassthrough |
| 24 | + |
| 25 | +from nemoguardrails import RailsConfig |
| 26 | +from nemoguardrails.integrations.langchain.runnable_rails import RunnableRails |
| 27 | +from tests.utils import FakeLLM |
| 28 | + |
| 29 | + |
| 30 | +def test_updated_runnable_rails_basic(): |
| 31 | + """Test basic functionality of updated RunnableRails.""" |
| 32 | + llm = FakeLLM( |
| 33 | + responses=[ |
| 34 | + "Hello there! How can I help you today?", |
| 35 | + ] |
| 36 | + ) |
| 37 | + config = RailsConfig.from_content(config={"models": []}) |
| 38 | + model_with_rails = RunnableRails(config, llm=llm) |
| 39 | + |
| 40 | + result = model_with_rails.invoke("Hi there") |
| 41 | + |
| 42 | + assert isinstance(result, str) |
| 43 | + assert "Hello there" in result |
| 44 | + |
| 45 | + |
| 46 | +async def test_updated_runnable_rails_async(): |
| 47 | + """Test async functionality of updated RunnableRails.""" |
| 48 | + llm = FakeLLM( |
| 49 | + responses=[ |
| 50 | + "Hello there! How can I help you today?", |
| 51 | + ] |
| 52 | + ) |
| 53 | + config = RailsConfig.from_content(config={"models": []}) |
| 54 | + model_with_rails = RunnableRails(config, llm=llm) |
| 55 | + |
| 56 | + result = await model_with_rails.ainvoke("Hi there") |
| 57 | + |
| 58 | + assert isinstance(result, str) |
| 59 | + assert "Hello there" in result |
| 60 | + |
| 61 | + |
| 62 | +def test_updated_runnable_rails_batch(): |
| 63 | + """Test batch functionality of updated RunnableRails.""" |
| 64 | + llm = FakeLLM( |
| 65 | + responses=[ |
| 66 | + "Response 1", |
| 67 | + "Response 2", |
| 68 | + ] |
| 69 | + ) |
| 70 | + config = RailsConfig.from_content(config={"models": []}) |
| 71 | + model_with_rails = RunnableRails(config, llm=llm) |
| 72 | + |
| 73 | + results = model_with_rails.batch(["Question 1", "Question 2"]) |
| 74 | + |
| 75 | + assert len(results) == 2 |
| 76 | + assert results[0] == "Response 1" |
| 77 | + assert results[1] == "Response 2" |
| 78 | + |
| 79 | + |
| 80 | +def test_updated_runnable_rails_stream(): |
| 81 | + """Test streaming functionality of updated RunnableRails.""" |
| 82 | + llm = FakeLLM( |
| 83 | + responses=[ |
| 84 | + "Hello there!", |
| 85 | + ] |
| 86 | + ) |
| 87 | + config = RailsConfig.from_content(config={"models": []}) |
| 88 | + model_with_rails = RunnableRails(config, llm=llm) |
| 89 | + |
| 90 | + chunks = [] |
| 91 | + for chunk in model_with_rails.stream("Hi there"): |
| 92 | + chunks.append(chunk) |
| 93 | + |
| 94 | + assert len(chunks) == 2 |
| 95 | + assert chunks[0].content == "Hello " |
| 96 | + assert chunks[1].content == "there!" |
| 97 | + |
| 98 | + |
| 99 | +def test_runnable_rails_with_message_history(): |
| 100 | + """Test handling of message history with updated RunnableRails.""" |
| 101 | + llm = FakeLLM( |
| 102 | + responses=[ |
| 103 | + "Yes, Paris is the capital of France.", |
| 104 | + ] |
| 105 | + ) |
| 106 | + config = RailsConfig.from_content(config={"models": []}) |
| 107 | + model_with_rails = RunnableRails(config, llm=llm) |
| 108 | + |
| 109 | + history = [ |
| 110 | + HumanMessage(content="Hello"), |
| 111 | + AIMessage(content="Hi there!"), |
| 112 | + HumanMessage(content="What's the capital of France?"), |
| 113 | + ] |
| 114 | + |
| 115 | + result = model_with_rails.invoke(history) |
| 116 | + |
| 117 | + assert isinstance(result, AIMessage) |
| 118 | + assert "Paris" in result.content |
| 119 | + |
| 120 | + |
| 121 | +def test_runnable_rails_with_chat_template(): |
| 122 | + """Test updated RunnableRails with chat templates.""" |
| 123 | + llm = FakeLLM( |
| 124 | + responses=[ |
| 125 | + "Yes, Paris is the capital of France.", |
| 126 | + ] |
| 127 | + ) |
| 128 | + config = RailsConfig.from_content(config={"models": []}) |
| 129 | + model_with_rails = RunnableRails(config, llm=llm) |
| 130 | + |
| 131 | + prompt = ChatPromptTemplate.from_messages( |
| 132 | + [ |
| 133 | + MessagesPlaceholder(variable_name="history"), |
| 134 | + ("human", "{question}"), |
| 135 | + ] |
| 136 | + ) |
| 137 | + |
| 138 | + chain = prompt | model_with_rails |
| 139 | + |
| 140 | + result = chain.invoke( |
| 141 | + { |
| 142 | + "history": [ |
| 143 | + HumanMessage(content="Hello"), |
| 144 | + AIMessage(content="Hi there!"), |
| 145 | + ], |
| 146 | + "question": "What's the capital of France?", |
| 147 | + } |
| 148 | + ) |
| 149 | + |
| 150 | + assert isinstance(result, AIMessage) |
| 151 | + assert "Paris" in result.content |
0 commit comments