|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025, 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 | +import pytest |
| 16 | +from langchain_core.messages import AIMessage |
| 17 | +from langchain_core.prompts import ChatPromptTemplate |
| 18 | + |
| 19 | +from aiq.builder.framework_enum import LLMFrameworkEnum |
| 20 | +from aiq.builder.workflow_builder import WorkflowBuilder |
| 21 | +from aiq.llm.aws_bedrock_llm import AWSBedrockModelConfig |
| 22 | +from aiq.llm.nim_llm import NIMModelConfig |
| 23 | +from aiq.llm.openai_llm import OpenAIModelConfig |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.integration |
| 27 | +async def test_nim_langchain_agent(): |
| 28 | + """ |
| 29 | + Test NIM LLM with LangChain agent. Requires NVIDIA_API_KEY to be set. |
| 30 | + """ |
| 31 | + |
| 32 | + prompt = ChatPromptTemplate.from_messages([("system", "You are a helpful AI assistant."), ("human", "{input}")]) |
| 33 | + |
| 34 | + llm_config = NIMModelConfig(model_name="meta/llama-3.1-70b-instruct", temperature=0.0) |
| 35 | + |
| 36 | + async with WorkflowBuilder() as builder: |
| 37 | + await builder.add_llm("nim_llm", llm_config) |
| 38 | + llm = await builder.get_llm("nim_llm", wrapper_type=LLMFrameworkEnum.LANGCHAIN) |
| 39 | + |
| 40 | + agent = prompt | llm |
| 41 | + |
| 42 | + response = await agent.ainvoke({"input": "What is 1+2?"}) |
| 43 | + assert isinstance(response, AIMessage) |
| 44 | + assert response.content is not None |
| 45 | + assert isinstance(response.content, str) |
| 46 | + assert "3" in response.content.lower() |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.integration |
| 50 | +async def test_openai_langchain_agent(): |
| 51 | + """ |
| 52 | + Test OpenAI LLM with LangChain agent. Requires OPENAI_API_KEY to be set. |
| 53 | + """ |
| 54 | + prompt = ChatPromptTemplate.from_messages([("system", "You are a helpful AI assistant."), ("human", "{input}")]) |
| 55 | + |
| 56 | + llm_config = OpenAIModelConfig(model_name="gpt-3.5-turbo", temperature=0.0) |
| 57 | + |
| 58 | + async with WorkflowBuilder() as builder: |
| 59 | + await builder.add_llm("openai_llm", llm_config) |
| 60 | + llm = await builder.get_llm("openai_llm", wrapper_type=LLMFrameworkEnum.LANGCHAIN) |
| 61 | + |
| 62 | + agent = prompt | llm |
| 63 | + |
| 64 | + response = await agent.ainvoke({"input": "What is 1+2?"}) |
| 65 | + assert isinstance(response, AIMessage) |
| 66 | + assert response.content is not None |
| 67 | + assert isinstance(response.content, str) |
| 68 | + assert "3" in response.content.lower() |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.integration |
| 72 | +async def test_aws_bedrock_langchain_agent(): |
| 73 | + """ |
| 74 | + Test AWS Bedrock LLM with LangChain agent. |
| 75 | + Requires AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to be set. |
| 76 | + See https://docs.aws.amazon.com/bedrock/latest/userguide/setting-up.html for more information. |
| 77 | + """ |
| 78 | + prompt = ChatPromptTemplate.from_messages([("system", "You are a helpful AI assistant."), ("human", "{input}")]) |
| 79 | + |
| 80 | + llm_config = AWSBedrockModelConfig(model_name="meta.llama3-3-70b-instruct-v1:0", |
| 81 | + temperature=0.0, |
| 82 | + region_name="us-east-2", |
| 83 | + max_tokens=1024) |
| 84 | + |
| 85 | + async with WorkflowBuilder() as builder: |
| 86 | + await builder.add_llm("aws_bedrock_llm", llm_config) |
| 87 | + llm = await builder.get_llm("aws_bedrock_llm", wrapper_type=LLMFrameworkEnum.LANGCHAIN) |
| 88 | + |
| 89 | + agent = prompt | llm |
| 90 | + |
| 91 | + response = await agent.ainvoke({"input": "What is 1+2?"}) |
| 92 | + assert isinstance(response, AIMessage) |
| 93 | + assert response.content is not None |
| 94 | + assert isinstance(response.content, str) |
| 95 | + assert "3" in response.content.lower() |
0 commit comments