|
|
|
|
|
import gradio as gr |
|
|
|
from guardrail import is_safe |
|
from together import Together |
|
from dotenv import load_dotenv |
|
import os |
|
|
|
|
|
load_dotenv() |
|
api_key = os.getenv("TOGETHER_API_KEY") |
|
|
|
|
|
|
|
client = Together(api_key=api_key) |
|
|
|
|
|
|
|
|
|
def run_action(message, history): |
|
system_prompt = """You are a financial assistant. |
|
- Answer in 50 words. |
|
- Ensure responses adhere to the safety policy.""" |
|
|
|
messages = [{"role": "system", "content": system_prompt}] |
|
|
|
|
|
for entry in history: |
|
if entry["role"] == "user": |
|
messages.append({"role": "user", "content": entry["content"]}) |
|
elif entry["role"] == "assistant": |
|
messages.append({"role": "assistant", "content": entry["content"]}) |
|
|
|
|
|
messages.append({"role": "user", "content": message}) |
|
|
|
|
|
model_output = client.chat.completions.create( |
|
model="meta-llama/Llama-3-70b-chat-hf", |
|
messages=messages, |
|
) |
|
|
|
return model_output.choices[0].message.content |
|
|
|
|
|
def main_loop(message, history): |
|
""" |
|
Main loop for the chatbot to handle user input. |
|
""" |
|
|
|
if not is_safe(message): |
|
return "Your input violates our safety policy. Please try again with a finance-related query." |
|
|
|
|
|
return run_action(message, history) |
|
|
|
|
|
demo = gr.ChatInterface( |
|
main_loop, |
|
chatbot=gr.Chatbot( |
|
height=450, |
|
placeholder="Ask a finance-related question. Type 'exit' to quit.", |
|
type="messages", |
|
), |
|
textbox=gr.Textbox( |
|
placeholder="What do you want to ask about finance?", |
|
container=False, |
|
scale=7, |
|
), |
|
title="Finance Chatbot", |
|
theme="Monochrome", |
|
examples=["What is compound interest?", "How to save for retirement?", "What are tax-saving options?"], |
|
cache_examples=False, |
|
) |
|
|
|
|
|
demo.launch(share=True, server_name="0.0.0.0") |
|
|