Use Guardrails from LangChain
You can use Guardrails to add a layer of security around LangChain components. Here's how to use Guardrails with LangChain.
Installing dependencies
Make sure you have both langchain and guardrails installed. If you don't, run the following commands:
Create a RAIL
spec
rail_spec = """
<rail version="0.1">
<output>
<object name="patient_info">
<string description="Patient's gender" name="gender"></string>
<integer format="valid-range: 0 100" name="age"></integer>
<string description="Symptoms that the patient is currently experiencing" name="symptoms"></string>
</object>
</output>
<prompt>
Given the following doctor's notes about a patient, please extract a dictionary that contains the patient's information.
${doctors_notes}
${gr.complete_json_suffix_v2}
</prompt>
</rail>
"""
Create a GuardrailsOutputParser
The GuardrailsOutputParser
contains a Guard
object, which can be used to access the prompt and output schema. E.g., here is the compiled prompt that is stored in GuardrailsOutputParser
:
We can now create a LangChain PromptTemplate
from this output parser. Note that the PromptTemplate
class from LangChain utilizes f-strings. In order to prevent it from trying to treat our example json as variables that should be substituted we will escape our prompt.