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.
- Flexible LLM integration: Use different LLMs for NeMo and response generation.
- Guardrails integration: Ensure AI-generated responses adhere to predefined constraints.
Installation
Ensure you have the necessary dependencies installed:
pip install langchain_openai nemoguardrails langchain_guardrails
Usage
Below is an example demonstrating how to integrate NeMo with LangChain and apply guardrails to control responses.
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
RailsConfigmust be configured with the correct NeMo guardrails path. - Modify
optionsto customize how NeMo processes inputs. - The
passthrough_or_exitfunction ensures controlled responses.
This package enhances NeMo's capabilities within LangChain, making it more configurable and adaptable for diverse AI applications.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_guardrails-0.0.2.tar.gz.
File metadata
- Download URL: langchain_guardrails-0.0.2.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d20b64d587d526768a1cc029fa9df99916aded1a3a286f0aa727d4812d152c6
|
|
| MD5 |
9e4c6f8794b842ea576c484940c5d76a
|
|
| BLAKE2b-256 |
1601b300e74de3bb0351992bc8d64e9af77b2e0bfd6207635ca799e4c34b73ff
|
File details
Details for the file langchain_guardrails-0.0.2-py3-none-any.whl.
File metadata
- Download URL: langchain_guardrails-0.0.2-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
759efe4870d21dfdc04c846524d19fb6c2f1c0f4d55cd56721a10929d5eaa6fd
|
|
| MD5 |
1e2d5a0a29ce9bfa5cfcc1e1ee15f85b
|
|
| BLAKE2b-256 |
41efc658951f9ed0a3239488b7f2ca4834c0923706d62e310e68fa44f2ca769f
|