File size: 2,370 Bytes
5587b35
 
f1f86cb
5587b35
f1f86cb
5587b35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from groq import Groq
import streamlit as st
from dotenv import load_dotenv

load_dotenv()

client = Groq(
    api_key=os.getenv("GROQ_API_KEY"),
)

models = client.models.list()
available_models = [x.to_dict()["id"] for x in models.data]

system_prompt = "You are a part of chatbot application, where multiple chatbots are available for user to select. You are a helpful assistant currently selected by user. Your major task is to help user with his queries"

if "models" not in st.session_state:
    st.session_state.models = available_models

if "messages" not in st.session_state:
    st.session_state.messages = [
        {
            "role": "system",
            "content": system_prompt,
        },
    ]

if "selected_model" not in st.session_state:
    st.session_state.selected_model = ''

st.set_page_config(layout="wide")

selected_model = st.sidebar.selectbox(
    label="Select Model",
    options=st.session_state.models,
)

if selected_model != st.session_state.selected_model:
    st.session_state.selected_model = selected_model

st.header("Hey! 👋 I am GROQ Bot")

if selected_model:
    st.markdown(f"##### You are talking to **```{selected_model}```**")

    if st.sidebar.button("Clear History", icon=':material/delete:', type="primary", help="Bot will forget what ever you talked" ):
        st.session_state.messages = [
            {
                "role": "system",
                "content": system_prompt,
            },
        ]

    for message in st.session_state.messages:
        if message["role"] != "system":
            with st.chat_message(message["role"]):
                st.write(message["content"])

    if prompt := st.chat_input("Ask anything to me"):

        st.session_state.messages.append({"role": "user", "content": prompt})

        with st.chat_message("user"):
            st.markdown(prompt)

        try:
            response = client.chat.completions.create(
                messages=st.session_state.messages,
                model=selected_model,
            )
            response = response.choices[0].message.content
        except :
            response = "Model is having some issue it cann't answer now select any other model"

        st.session_state.messages.append({"role": "assistant", "content": response})

        with st.chat_message("assistant"):
            st.markdown(response)