File size: 2,690 Bytes
5e994a1
250dba9
1487cce
 
5e994a1
e4e56af
 
 
5914582
4e87127
1487cce
5e994a1
580f382
5914582
ab53869
e4e56af
1612f56
e4e56af
 
 
 
 
 
1612f56
1487cce
 
 
 
 
 
 
 
 
 
 
 
 
 
e4e56af
 
 
 
1487cce
 
e4e56af
 
 
 
 
1487cce
 
 
 
 
 
 
 
 
 
 
1612f56
1487cce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from openai import OpenAI
from params import params
from database import get_client

# from add_data import start_troggin_off, create_client

CLIENT = get_client()
APP_NAME: str = "Groove-GPT"
history = []
st.set_page_config(layout="wide")

# INFO
st.title(APP_NAME)

l_col, r_col = st.columns((3, 1))

if "trigger" not in st.session_state:
    st.session_state["trigger"] = False


def on_enter():
    st.session_state["trigger"] = True


# param column
with r_col:
    (
        submit_button,
        remember_chat_history,
        temperature,
        num_samples,
        access_key,
        gpt_type,
    ) = params()

# input & response
with l_col:
    user_question: str = st.text_input(
        "Enter your groovy questions here",
        on_change=on_enter,
    )

    # ON BUTTON CLICK
    if (
        (submit_button | st.session_state["trigger"])
        & (access_key != "")
        & (user_question != "")
    ):
        openai_client = OpenAI(api_key=access_key)

        with st.spinner("Loading..."):
            # Perform the Chromadb query.
            results = CLIENT.query(
                query_texts=[user_question],
                n_results=num_samples,
                include=["documents"],
            )
            documents = results["documents"]
            response = openai_client.chat.completions.create(
                model=gpt_type,
                messages=[
                    {
                        "role": "system",
                        "content": "You are an expert in functional programming in R5RS, with great knowledge on programming paradigms. You wish to teach the user everything you know about programming paradigms in R5RS - so you explain everything thoroughly. Surround Latex equations in dollar signs as such Inline equation: $equation$ & Display equation: $$equation$$.",
                    },
                    {"role": "user", "content": user_question},
                    {"role": "assistant", "content": str(documents)},
                    {"role": "user", "content": f"Conversation History: {history}"},
                ],
                temperature=temperature,
                stream=True,
            )

        st.header("The Super Duper Schemer Says ...")
        text_placeholder = st.empty()

        content = ""
        for i, chunk in enumerate(response):
            if chunk.choices[0].delta.content is not None:
                content += chunk.choices[0].delta.content

                text_placeholder.markdown(content)

        history.append({user_question: content} if remember_chat_history else {})
    else:
        st.write("Please provide an input and (valid) API key")