Skip to main content

Langchain implementation for Guardrails: Nemo

Project description

Nemo Integration with LangChain

This package provides NeMo integration with LangChain, addressing a key limitation in existing NeMo runnables. Specifically, it enables configurable options for NeMo, offering greater control over what is generated. Additionally, it allows flexibility in using different LLMs for NeMo and text generation, enhancing adaptability for various use cases.

Features

  • Customizable NeMo options: Control what NeMo generates by utilizing generation options described here
  • Flexible LLM integration: Use different LLMs for NeMo and response generation. Utilize tools binding as needed.
  • Guardrails integration: Ensure AI-generated responses adhere to predefined constraints.

Installation

Ensure you have the necessary dependencies installed:

pip install langchain_guardrails

Input

Default behaviour is to execute only rails and generation is left for surround to handle i.e default for options is set to {"rails": ["input"]}

Output

Output from guarrail is a dictionary:

{"original": <original message", "stop": True/False}

Utilize stop signal to execute next steps.

Usage

Below is an example demonstrating how to integrate NeMo with LangChain and apply guardrails to control responses. Here we use custom generator function passthrough_or_exit

Inbuilt generator function

This package has inbuilt function for generation - generate_or_exit - which is enabled when generator_llm is passed. You can use that as well to complete your generation.

# Create the guardrail processing chain
guardrail_chain = nemorails.create_guardrail_chain()
res = ChatPromptTemplate.from_messages(test_input) 
chain = res | guardrail_chain | nemorails.generate_or_exit

# Invoke the guardrail chain
response = [chain.invoke({})]

Custom generator function

from langchain_openai import ChatOpenAI
from nemoguardrails import RailsConfig
from langchain_guardrails import NemoRails
from langchain.schema import HumanMessage
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import chain

# Initialize OpenAI model
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.7)

# Load RailsConfig (configure with the actual path to your NeMo guardrails config)
rails_config = RailsConfig.from_path("./tests/config")

# Instantiate NemoRails with configuration and LLM
nemorails = NemoRails(config=rails_config, llm=llm, options={"rails": ["input"]})

# Sample user input
user_input = [HumanMessage(content="Tell me about the Avengers movie")]

# Define a simple passthrough function with an exit condition
@chain
def passthrough_or_exit(message_dict):
    if message_dict["stop"]:
        return "I'm sorry, I can't respond to that."
    return llm.invoke(message_dict["original"])

# Create the guardrail processing chain
guardrail_chain = nemorails.create_guardrail_chain()
response_chain = ChatPromptTemplate.from_messages(user_input) | guardrail_chain | passthrough_or_exit

# Invoke the chain and generate response
response = response_chain.invoke({})

# Print the output
print(response)

Notes

  • The RailsConfig must be configured with the correct NeMo guardrails path.
  • Modify options to customize how NeMo processes inputs.
  • Tools messages are not checked

Credits

This package uses code from nemoguardrails, licensed under [ Apache-2.0].

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_guardrails-0.0.4.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_guardrails-0.0.4-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file langchain_guardrails-0.0.4.tar.gz.

File metadata

  • Download URL: langchain_guardrails-0.0.4.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for langchain_guardrails-0.0.4.tar.gz
Algorithm Hash digest
SHA256 528ec43b96225efa5d782e0a8384fe97344031bd54508f7ca831af622cfaa353
MD5 afbe6466f3bf0ff70fd7a28ef921a425
BLAKE2b-256 7936df594b112053609b2a4c7edc9ced165928c10c006205a3535806358c84df

See more details on using hashes here.

File details

Details for the file langchain_guardrails-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_guardrails-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fc6c6707bbf0421aa774fb80286165accd463cdcba16ffc7ab28289975d0ee67
MD5 bd3f016e26669403b4109ab9f55e2de8
BLAKE2b-256 805c58f40866c629291df93d3651de8969c8253f6c555bc3b2bdc1e0066d3e26

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page