AIAnytime / AIAnytime/Guardrails-Implementation-in-LLMs

Getting Error "Validate Response as None

Open
#1 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
10
Forks
8
PR merge metrics
No merged PRs in 30d

Description

Getting Validate Response as "None". Refer the screen shot . Please Help

Screenshot 2023-12-09 at 11 35 49 AM

import guardrails as gd
from rich import print
import openai
import streamlit as st
from dotenv import load_dotenv
import os

load_dotenv()

openai.api_key = os.getenv("OPENAI_API_KEY")

def without_guardrails(text):
response = openai.Completion.create(
prompt="Translate the texts to English language\n"+text,
engine="text-davinci-003",
max_tokens=2048,
temperature=0)

result = response['choices'][0]['text']
return result

rail_str = """

from guardrails.validators import Validator, EventDetail, register_validator
from typing import Dict, List
from profanity_check import predict
import openai

@register_validator(name="is-profanity-free", data_type="string")

class IsProfanityFree(Validator):
global predict
global EventDetail
def validate(self, key, value, schema) -> Dict:
text = value
prediction = predict([value])
if prediction[0] == 1:
raise EventDetail(
key,
value,
schema,
f"Value {value} contains profanity language",
"Sorry, It contains profanity language. I can't return the result.",
)
return schema

Translate the given statement into english language:

{{statement_to_be_translated}}

@complete_json_suffix

"""

guard = gd.Guard.from_rail_string(rail_str)

def main():

st.title("Guardrails Implementation in LLMs")

text_area = st.text_area("Enter the text to be translated")

if st.button("Translate"):
if len(text_area)>0:
st.info(text_area)

st.warning("Translation Without Guardrails")

without_guardrails_result = without_guardrails(text_area)
st.success(without_guardrails_result)

st.warning("Translation With Guardrails")

raw_llm_response, validated_response = guard(
openai.Completion.create,
prompt_params={"statement_to_be_translated": text_area},
engine="text-davinci-003",
max_tokens=2048,
temperature=0)

st.success(f"Validated Output: {validated_response}")

if __name__ == "__main__":
main()

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.