File size: 4,077 Bytes
155af2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08c3cb4
155af2c
 
 
 
 
 
 
08c3cb4
155af2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import streamlit as st
import random
import time

from openai import OpenAI
import pandas as pd

import elemeta.nlp.runners.metafeature_extractors_runner as metafeature_extractors_runner

from elemeta.nlp.runners.metafeature_extractors_runner import MetafeatureExtractorsRunner
from elemeta.nlp.extractors.high_level.text_length import TextLength
from elemeta.nlp.extractors.high_level.text_complexity import TextComplexity
from elemeta.nlp.extractors.high_level.word_count import WordCount
from elemeta.nlp.extractors.high_level.detect_language_langdetect import DetectLanguage
from elemeta.nlp.extractors.high_level.sentiment_polarity import SentimentPolarity
from elemeta.nlp.extractors.high_level.toxicity_extractor import ToxicityExtractor
runner = MetafeatureExtractorsRunner(metafeature_extractors=[TextLength(),WordCount(),DetectLanguage()
                                                             ,SentimentPolarity(),TextComplexity(),ToxicityExtractor()])


def ask_gpt(messages,model="gpt-3.5-turbo"):
    ret =  client.chat.completions.create(model=model,
                                          messages=messages
                                          )
    return ret.choices[0].message.content

client = OpenAI()

st.title("GPT Chatbot")
system_prompt = st.text_input("Enter your system's prompt",value="Translate the following into russian")
user_prompt = st.text_input("Enter your user's prompt",value="Hello, how are you?")

messages = [
    {"role": "system", "content": system_prompt},

    {"role": "user", "content": user_prompt}
  ]

output = ask_gpt(messages)


st.header("Output")
st.write(output)

st.header("Metafeatures")


df = pd.DataFrame([
    runner.run(system_prompt),
    runner.run(user_prompt),
    runner.run(output)])
df["prompt"] = ["system","user","output"]
df = df.set_index("prompt")

st.dataframe(df)

st.header("Chat Monitoring")

st.subheader("Chat")
# Initialize chat history
if "messages" not in st.session_state:
    st.session_state.messages = []

# Display chat messages from history on app rerun
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

# Accept user input
if prompt := st.chat_input("What is up?"):
    # Add user message to chat history
    st.session_state.messages.append({"role": "user", "content": prompt})
    # Display user message in chat message container
    with st.chat_message("user"):
        st.markdown(prompt)

    # Display assistant response in chat message container
    with st.chat_message("assistant"):
        message_placeholder = st.empty()
        full_response = ""
        assistant_response = ask_gpt(messages=st.session_state.messages)
        # Simulate stream of response with milliseconds delay
        for chunk in assistant_response.split():
            full_response += chunk + " "
            time.sleep(0.05)
            # Add a blinking cursor to simulate typing
            message_placeholder.markdown(full_response + "▌")
        message_placeholder.markdown(full_response)
    # Add assistant response to chat history
    st.session_state.messages.append({"role": "assistant", "content": full_response})


st.subheader("Chat Logs")

user_messages = [message["content"] for message in st.session_state.messages if message["role"] == "user"]  
assistant_messages = [message["content"] for message in st.session_state.messages if message["role"] == "assistant"]    
# st.write("User Messages",user_messages)
# st.write("Assistant Messages",assistant_messages)

user_df = pd.DataFrame([runner.run(user_prompt) for user_prompt in user_messages])
user_df["prompt"] = user_messages
user_df.columns = 'user_' + user_df.columns.values
# st.dataframe(user_df)



assistant_df = pd.DataFrame([runner.run(assistant_prompt) for assistant_prompt in assistant_messages])
assistant_df["prompt"] = assistant_messages
assistant_df.columns = 'assistant_' + assistant_df.columns.values
# st.dataframe(assistant_df)


st.subheader("Logs Metafeatures")
st.dataframe(pd.concat([user_df,assistant_df],axis=1))