File size: 2,734 Bytes
a862012
3ffccb1
a862012
2eaf991
a862012
 
 
 
2eaf991
 
db11e43
 
 
 
 
 
 
 
 
 
 
 
2eaf991
db11e43
 
2eaf991
 
 
db11e43
2eaf991
a862012
2eaf991
a862012
db11e43
 
 
 
 
 
a862012
db11e43
a862012
 
db11e43
 
a862012
 
db11e43
a862012
db11e43
a862012
 
2eaf991
 
 
 
 
 
 
 
 
 
a862012
 
2eaf991
a862012
 
 
 
2eaf991
 
a862012
 
2eaf991
a862012
 
 
2eaf991
a862012
2eaf991
a862012
 
 
 
2eaf991
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
73
74
75
76
77
78
79
80
81
82
83
84
85
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())

# Gradio interface functions
def run_action(message, history):
    global game_state, game_running  # Access the global game state and game status

    if not game_running:
        return "The game has ended. Type 'restart the game' to play again."

    if message.lower() == "start game":
        return game_state["start"]

    if message.lower() == "restart the game":
        game_state = initialize_game_state()
        return "Game restarted! " + game_state["start"]

    if message.lower() == "exit":
        game_running = False
        return "The game has ended. Type 'restart the game' to play again."

    system_prompt = """You are a financial assistant. You can only answer finance-related queries.
    - Do not answer non-finance questions.
    - 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


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