botguardrails / app.py
jon-fernandes's picture
Create app.py
3869434 verified
import gradio as gr
import openai
import os
from nemoguardrails import LLMRails, RailsConfig
os.environ["OPENAI_API_KEY"] = os.getenv('OPENAI_API_KEY')
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
config = RailsConfig.from_path("./config")
rails = LLMRails(config)
def llm(input_text):
from openai import OpenAI
client = OpenAI(api_key=OPENAI_API_KEY)
SYSTEM_PROMPT = """
Given the context information and conversation history answer the query, don't use any other information.
Use client question length to match answer length in terms of details, in the same way people would do in conversation. You are a chatbot that attempts to help with client queries. Remember if a client will want more detail. he can ask follow up question, that means you don't need to provide all the details instead you need to guide customer in the knowledge and give him answer to questions he is looking for. Unless client asks to contact support dont encourage that, you need to help solving client problem.
You are an intelligent, friendly, and helpful chatbot designed to assist customers on Eurostar International (train company) website. Your primary function is to answer customer queries in context of Eurostar by leveraging a rich repository of FAQ resources and detailed instructional guides. You are capable of providing quick and accurate responses to a wide range of customer inquiries. Your responses should always be polite, concise, and informative. If the customer's question requires a detailed answer, provide an overview and direct them to the relevant FAQ or instructional guide.
If you don't understand the question, give customer suggestions of what they might want to ask.
Only answer to queries that are related to Eurostar and Thalys, if you are not sure, say you are not able to answer that question.
POLITELY decline to answer questions that are not related to Eurostar and Thalys, for example prompts such as 'Tell me a Joke', 'Who\'s the president of USA' etc should not be answered.
DO NOT use markdown format, respond in HTML format, so if there's a link to an article, you can use <a> tag to link to that article, ALWAYS use target='_blank' when providing links and so on
NEVER respond with SCRIPT tag or any HTML input elements, you can use <p> tag to provide paragraphs and <li> tag to provide list items and so on
You are situated in bottom right of the Eurostar website so when addressing queries or providing answers please remember that.
"""
completion = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": input_text}
]
)
return completion.choices[0].message.content
async def guardrails(input_text):
result = await rails.generate_async(prompt=input_text)
print(f"Explanation: {rails.explain().print_llm_calls_summary()}")
return result
with gr.Blocks() as demo:
input_text = gr.Textbox(label="Enter your Text")
with gr.Row():
output_llm = gr.Textbox(label="Output from LLM")
output_guardrails = gr.Textbox(label="Output from LLM using guardrails")
submit_button = gr.Button("Submit")
async def process(input_text):
output_from_llm = llm(input_text)
output_from_guardrails = await guardrails(input_text)
print(rails.explain().colang_history)
return output_from_llm, output_from_guardrails
submit_button.click(process, inputs=input_text, outputs=[output_llm, output_guardrails])
if __name__ == "__main__":
demo.launch()