-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix LLM Agents Kafka pipeline (#1793)
* Simple agents pipeline moved to `common.py` to be shared with the kafka pipeline * Add `--bootstrap_servers` and `--topic` flags. * Fix mis-named "Upload rate" monitor description with "Agent rate" Closes #1791 ## By Submitting this PR I confirm: - I am familiar with the [Contributing Guidelines](https://github.com/nv-morpheus/Morpheus/blob/main/docs/source/developer_guide/contributing.md). - When the PR is ready for review, new or existing tests cover these changes. - When the PR is ready for review, the documentation is up to date with these changes. Authors: - David Gardner (https://github.com/dagardner-nv) Approvers: - Michael Demoret (https://github.com/mdemoret-nv) URL: #1793
- Loading branch information
1 parent
3e067c2
commit 84efdc6
Showing
6 changed files
with
116 additions
and
106 deletions.
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,81 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
# | ||
# 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. | ||
|
||
import logging | ||
|
||
from langchain.agents import AgentType | ||
from langchain.agents import initialize_agent | ||
from langchain.agents import load_tools | ||
from langchain.agents.agent import AgentExecutor | ||
from langchain.llms.openai import OpenAI | ||
|
||
from morpheus.config import Config | ||
from morpheus.llm import LLMEngine | ||
from morpheus.llm.nodes.extracter_node import ExtracterNode | ||
from morpheus.llm.nodes.langchain_agent_node import LangChainAgentNode | ||
from morpheus.llm.task_handlers.simple_task_handler import SimpleTaskHandler | ||
from morpheus.messages import ControlMessage | ||
from morpheus.pipeline.linear_pipeline import LinearPipeline | ||
from morpheus.stages.general.monitor_stage import MonitorStage | ||
from morpheus.stages.llm.llm_engine_stage import LLMEngineStage | ||
from morpheus.stages.output.in_memory_sink_stage import InMemorySinkStage | ||
from morpheus.stages.preprocess.deserialize_stage import DeserializeStage | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def _build_agent_executor(model_name: str) -> AgentExecutor: | ||
|
||
llm = OpenAI(model=model_name, temperature=0.0, client=None) | ||
|
||
tools = load_tools(["serpapi", "llm-math"], llm=llm) | ||
|
||
agent_executor = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) | ||
|
||
return agent_executor | ||
|
||
|
||
def _build_engine(model_name: str) -> LLMEngine: | ||
|
||
engine = LLMEngine() | ||
|
||
engine.add_node("extracter", node=ExtracterNode()) | ||
|
||
engine.add_node("agent", | ||
inputs=[("/extracter")], | ||
node=LangChainAgentNode(agent_executor=_build_agent_executor(model_name=model_name))) | ||
|
||
engine.add_task_handler(inputs=["/agent"], handler=SimpleTaskHandler()) | ||
|
||
return engine | ||
|
||
|
||
def build_common_pipeline(config: Config, pipe: LinearPipeline, task_payload: dict, | ||
model_name: str) -> InMemorySinkStage: | ||
""" | ||
Construct the elements of the pipeline common to the simple and kafka agent pipelines. | ||
This method should be called after the source stage has been set. | ||
""" | ||
pipe.add_stage( | ||
DeserializeStage(config, message_type=ControlMessage, task_type="llm_engine", task_payload=task_payload)) | ||
|
||
pipe.add_stage(MonitorStage(config, description="Source rate", unit='questions')) | ||
|
||
pipe.add_stage(LLMEngineStage(config, engine=_build_engine(model_name=model_name))) | ||
|
||
sink = pipe.add_stage(InMemorySinkStage(config)) | ||
|
||
pipe.add_stage(MonitorStage(config, description="Agent rate", unit="events", delayed_start=True)) | ||
|
||
return sink |
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