File size: 2,292 Bytes
0965dd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# app.py (Finance Chatbot)

import gradio as gr
from helper import get_together_api_key
from guardrail import is_safe
from together import Together

# Initialize Together client
client = Together(api_key=get_together_api_key())

# Function to handle the chatbot's response to user queries
# You can only answer finance-related queries.
# - Do not answer non-finance questions.
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}]

    # Convert history into the appropriate format
    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"]})

    # Add the user's current action
    messages.append({"role": "user", "content": message})

    # Get the model's response
    model_output = client.chat.completions.create(
        model="meta-llama/Llama-3-70b-chat-hf",
        messages=messages,
    )

    return model_output.choices[0].message.content

# Main loop for the chatbot to handle user input
def main_loop(message, history):
    """
    Main loop for the chatbot to handle user input.
    """
    # Validate the user's input for safety
    if not is_safe(message):
        return "Your input violates our safety policy. Please try again with a finance-related query."
    
    # Generate and validate the response
    return run_action(message, history)

# Gradio Chat Interface
demo = gr.ChatInterface(
    main_loop,
    chatbot=gr.Chatbot(
        height=450,
        placeholder="Ask a finance-related question. Type 'exit' to quit.",
        type="messages",  # Proper rendering of chat format
    ),
    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,
)

# Launch the Gradio app
demo.launch(share=True, server_name="0.0.0.0")