|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 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 | +import os |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from nemoguardrails import LLMRails, RailsConfig |
| 21 | +from nemoguardrails.imports import check_optional_dependency |
| 22 | +from nemoguardrails.rails.llm.options import GenerationResponse |
| 23 | + |
| 24 | +has_langchain_openai = check_optional_dependency("langchain_openai") |
| 25 | + |
| 26 | +has_openai_key = bool(os.getenv("OPENAI_API_KEY")) |
| 27 | + |
| 28 | +skip_if_no_openai = pytest.mark.skipif( |
| 29 | + not (has_langchain_openai and has_openai_key), |
| 30 | + reason="Requires langchain_openai and OPENAI_API_KEY environment variable", |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +@skip_if_no_openai |
| 35 | +def test_task_specific_model_for_generate_user_intent_and_generate_next_steps(): |
| 36 | + config = RailsConfig.from_content( |
| 37 | + colang_content=""" |
| 38 | + define user express greeting |
| 39 | + "hi" |
| 40 | +
|
| 41 | + define flow |
| 42 | + user express greeting |
| 43 | + bot express greeting |
| 44 | +
|
| 45 | + define bot express greeting |
| 46 | + "Hello! How can I assist you today?" |
| 47 | + """, |
| 48 | + yaml_content=""" |
| 49 | + models: |
| 50 | + - type: main |
| 51 | + engine: openai |
| 52 | + model: gpt-3.5-turbo-instruct |
| 53 | +
|
| 54 | + - type: generate_user_intent |
| 55 | + engine: openai |
| 56 | + model: gpt-4o-mini |
| 57 | +
|
| 58 | + - type: generate_next_steps |
| 59 | + engine: openai |
| 60 | + model: gpt-4o-mini |
| 61 | + """, |
| 62 | + ) |
| 63 | + |
| 64 | + rails = LLMRails(config) |
| 65 | + |
| 66 | + res = rails.generate( |
| 67 | + messages=[{"role": "user", "content": "what can you do?"}], |
| 68 | + options={"log": {"llm_calls": True}}, |
| 69 | + ) |
| 70 | + |
| 71 | + assert isinstance(res, GenerationResponse) |
| 72 | + assert res.log is not None |
| 73 | + assert res.log.llm_calls is not None |
| 74 | + assert len(res.log.llm_calls) > 0 |
| 75 | + |
| 76 | + task_specific_tasks = ["generate_user_intent", "generate_next_steps"] |
| 77 | + |
| 78 | + generate_user_intent_calls = [call for call in res.log.llm_calls if call.task == "generate_user_intent"] |
| 79 | + assert len(generate_user_intent_calls) > 0 |
| 80 | + for call in generate_user_intent_calls: |
| 81 | + assert call.llm_model_name == "gpt-4o-mini" |
| 82 | + assert call.llm_provider_name == "openai" |
| 83 | + |
| 84 | + generate_next_steps_calls = [call for call in res.log.llm_calls if call.task == "generate_next_steps"] |
| 85 | + assert len(generate_next_steps_calls) > 0 |
| 86 | + for call in generate_next_steps_calls: |
| 87 | + assert call.llm_model_name == "gpt-4o-mini" |
| 88 | + assert call.llm_provider_name == "openai" |
| 89 | + |
| 90 | + other_calls = [call for call in res.log.llm_calls if call.task not in task_specific_tasks] |
| 91 | + for call in other_calls: |
| 92 | + assert call.llm_model_name == "gpt-3.5-turbo-instruct" |
0 commit comments