AIAnytime / AIAnytime/Guardrails-Implementation-in-LLMs

Getting Error "Validate Response as None

Đang mở
#1 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Python
Star
10
Fork
8
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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()

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.