Skip to content

Commit

Permalink
docs for llm cpp classes
Browse files Browse the repository at this point in the history
  • Loading branch information
efajardo-nv committed Nov 1, 2023
1 parent 4692529 commit 6080a62
Show file tree
Hide file tree
Showing 9 changed files with 337 additions and 2 deletions.
104 changes: 104 additions & 0 deletions morpheus/_lib/include/morpheus/llm/llm_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,45 +39,149 @@ struct LLMContextState
nlohmann::json values;
};

/**
* @brief Holds and manages information related to LLM tasks and input mappings required for LLMNode execution.
*
*/
class MORPHEUS_EXPORT LLMContext : public std::enable_shared_from_this<LLMContext>
{
public:
/**
* @brief Construct a new LLMContext object.
*
*/
LLMContext();

/**
* @brief Construct a new LLMContext object.
*
* @param task task for new context
* @param message control message for new context
*/
LLMContext(LLMTask task, std::shared_ptr<ControlMessage> message);

/**
* @brief Construct a new LLMContext object.
*
* @param parent parent context
* @param name new context name
* @param inputs input mappings for new context
*/
LLMContext(std::shared_ptr<LLMContext> parent, std::string name, input_mappings_t inputs);

/**
* @brief Destroy the LLMContext object.
*
*/
~LLMContext();

/**
* @brief Get parent context.
*
* @return std::shared_ptr<LLMContext>
*/
std::shared_ptr<LLMContext> parent() const;

/**
* @brief Get name of context.
*
* @return const std::string&
*/
const std::string& name() const;

/**
* @brief Get map of internal mappings for this context.
*
* @return const input_mappings_t&
*/
const input_mappings_t& input_map() const;

/**
* @brief Get task for this context.
*
* @return const LLMTask&
*/
const LLMTask& task() const;

/**
* @brief Get control message for this context.
*
* @return std::shared_ptr<ControlMessage>&
*/
std::shared_ptr<ControlMessage>& message() const;

/**
* @brief Get all output mappings for this context.
*
* @return nlohmann::json::const_reference
*/
nlohmann::json::const_reference all_outputs() const;

/**
* @brief Get full name of context containing parents up to root.
*
* @return std::string
*/
std::string full_name() const;

/**
* @brief Create new context from this context with provided name and input mappings.
*
* @param name name of new context
* @param inputs input mappings for new context
* @return std::shared_ptr<LLMContext>
*/
std::shared_ptr<LLMContext> push(std::string name, input_mappings_t inputs);

/**
* @brief Moves output map from this context to parent context. Outputs to move can be selected using
* set_output_names, otherwise all outputs are noved by default.
*
*/
void pop();

/**
* @brief Get the input value from parent context corresponding to first internal input of this context.
*
* @return nlohmann::json::const_reference
*/
nlohmann::json::const_reference get_input() const;

/**
* @brief Get the parent output value corresponding to given internal input name.
*
* @param node_name internal input name
* @return nlohmann::json::const_reference
*/
nlohmann::json::const_reference get_input(const std::string& node_name) const;

/**
* @brief Get parent output values corresponding to all internal input names.
*
* @return nlohmann::json
*/
nlohmann::json get_inputs() const;

/**
* @brief Set output mappings for this context.
*
* @param outputs output mappings
*/
void set_output(nlohmann::json outputs);

/**
* @brief Set an output value for this context.
*
* @param output_name output name
* @param output output value
*/
void set_output(const std::string& output_name, nlohmann::json output);

/**
* @brief Set the output names to propagate from this context when using pop.
*
* @param output_names output names to propagate
*/
void set_output_names(std::vector<std::string> output_names);

void outputs_complete();
Expand Down
29 changes: 29 additions & 0 deletions morpheus/_lib/include/morpheus/llm/llm_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,43 @@

namespace morpheus::llm {

/**
* @brief A subclass of LLMNode that acts as container for components required in processing LLM service
* requests. For example, a prompt generator and LLM service can each be implemented as an LLMNode and added to
* the engine. Input mappings are used to match node's input to outputs of parent of sibling nodes. Task handlers
* can also be added to the engine to process the outputs of an LLM service to feed back to prompt generator, for
* example, or exit the engine.
*/
class MORPHEUS_EXPORT LLMEngine : public LLMNode
{
public:
/**
* @brief Construct a new LLMEngine object.
*
*/
LLMEngine();

/**
* @brief Destroy the LLMEngine object.
*
*/
~LLMEngine() override;

/**
* @brief Add new task handler to this engine.
*
* @param inputs input mappings that specifies inputs to new task handler
* @param task_handler task handler object
*/
virtual void add_task_handler(user_input_mappings_t inputs, std::shared_ptr<LLMTaskHandler> task_handler);

/**
* @brief Execute nodes in this engine and pass outputs to its task handlers. Must pass this a control message with
* a 'llm_engine' task` containing 'task_type' and 'task_dict' properties required for execution of task(s).
*
* @param input_message input control message
* @return Task<std::vector<std::shared_ptr<ControlMessage>>>
*/
virtual Task<std::vector<std::shared_ptr<ControlMessage>>> run(std::shared_ptr<ControlMessage> input_message);

private:
Expand Down
45 changes: 45 additions & 0 deletions morpheus/_lib/include/morpheus/llm/llm_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,68 @@

namespace morpheus::llm {

/**
* @brief This class is used to implement functionality required in the processing of an LLM
* service request such as a prompt generator or LLM service client. Nodes are added to an LLMEngine
* which manages their execution including mapping of each node's input/output to parent and sibling
* node.
*/
class MORPHEUS_EXPORT LLMNode : public LLMNodeBase
{
public:
/**
* @brief Construct a new LLMNode object.
*
*/
LLMNode();

/**
* @brief Destroy the LLMNode object.
*
*/
~LLMNode() override;

/**
* @brief Add child node to this node and validate user input mappings.
*
* @param name child node name
* @param inputs child node input mappings
* @param node child node object
* @param is_output true if output node
* @return std::shared_ptr<LLMNodeRunner>
*/
virtual std::shared_ptr<LLMNodeRunner> add_node(std::string name,
user_input_mappings_t inputs,
std::shared_ptr<LLMNodeBase> node,
bool is_output = false);

/**
* @brief Get the input names for this node.
*
* @return std::vector<std::string>
*/
std::vector<std::string> get_input_names() const override;

/**
* @brief Get the names of output child nodes.
*
* @return const std::vector<std::string>&
*/
const std::vector<std::string>& get_output_node_names() const;

/**
* @brief Get number of child nodes.
*
* @return size_t
*/
size_t node_count() const;

/**
* @brief Execute all child nodes and save output from output node(s) to context.
*
* @param context context for node's execution
* @return Task<std::shared_ptr<LLMContext>>
*/
Task<std::shared_ptr<LLMContext>> execute(std::shared_ptr<LLMContext> context) override;

private:
Expand Down
22 changes: 21 additions & 1 deletion morpheus/_lib/include/morpheus/llm/llm_node_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,32 @@

namespace morpheus::llm {

/**
* @brief Base class for LLMNode.
*
*/
class MORPHEUS_EXPORT LLMNodeBase
{
public:
/**
* @brief Destroy the LLMNodeBase object.
*
*/
virtual ~LLMNodeBase() = default;

virtual std::vector<std::string> get_input_names() const = 0;
/**
* @brief Virtual method for implementing how task handler gets its input names.
*
* @return std::vector<std::string>
*/
virtual std::vector<std::string> get_input_names() const = 0;

/**
* @brief Virtual method for implementing the execution of a node.
*
* @param context context for node's execution
* @return Task<std::shared_ptr<LLMContext>>
*/
virtual Task<std::shared_ptr<LLMContext>> execute(std::shared_ptr<LLMContext> context) = 0;
};

Expand Down
41 changes: 41 additions & 0 deletions morpheus/_lib/include/morpheus/llm/llm_node_runner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,62 @@

namespace morpheus::llm {

/**
* @brief This class wraps LLMNode and is responsible for node's execution. It also manages mapping of node's
* inputs to parent of sibling nodes.
*/
class MORPHEUS_EXPORT LLMNodeRunner
{
public:
/**
* @brief Construct a new LLMNodeRunner object
*
* @param name
* @param inputs
* @param node
*/
LLMNodeRunner(std::string name, input_mappings_t inputs, std::shared_ptr<LLMNodeBase> node);

/**
* @brief Destroy the LLMNodeRunner object
*
*/
~LLMNodeRunner();

/**
* @brief Execute node and save outputs to context.
*
* @param context context for node's execution
* @return Task<std::shared_ptr<LLMContext>>
*/
virtual Task<std::shared_ptr<LLMContext>> execute(std::shared_ptr<LLMContext> context);

/**
* @brief Get name of node runner typically same as node name.
*
* @return const std::string&
*/
const std::string& name() const;

/**
* @brief Get input mappings for this node.
*
* @return const input_mappings_t&
*/
const input_mappings_t& inputs() const;

/**
* @brief Get input names from node's siblings.
*
* @return const std::vector<std::string>&
*/
const std::vector<std::string>& sibling_input_names() const;

/**
* @brief Get input names from node's parent.
*
* @return const std::vector<std::string>&
*/
const std::vector<std::string>& parent_input_names() const;

private:
Expand Down
Loading

0 comments on commit 6080a62

Please sign in to comment.