File size: 1,732 Bytes
d2a3567
73afc0c
d2a3567
 
 
90c5900
 
 
 
 
 
73afc0c
90c5900
73afc0c
d2a3567
14d83f7
 
 
 
 
 
d2a3567
 
 
 
 
 
 
 
73afc0c
14d83f7
73afc0c
 
 
 
 
 
 
d2a3567
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from openai import OpenAI

st.title("Chat Demo")

'''
This application presents a traditional chat interface to a range of open source or open weights models running on the National Research Platform (<https://nrp.ai>).  Unlike the other two demos, this pattern does not use specified data resources.

'''


with st.sidebar:
    model = st.radio("Select an LLM:", ['olmo', 'gemma2', 'phi3', 'llama3', 'embed-mistral', 'mixtral', 'gorilla', 'groq-tools', 'llava'])
    st.session_state["model"] =  model

## dockerized streamlit app wants to read from os.getenv(), otherwise use st.secrets
import os
api_key = os.getenv("LITELLM_KEY")
if api_key is None:
    api_key = st.secrets["LITELLM_KEY"]
        

if "messages" not in st.session_state:
    st.session_state.messages = []

for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

client = OpenAI(
    api_key = api_key, 
    base_url = "https://llm.nrp-nautilus.io"
)

# Button to clear session state
if st.button('Clear History'):
    st.session_state.clear()

if prompt := st.chat_input("What is up?"):
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.markdown(prompt)

    with st.chat_message("assistant"):
        stream = client.chat.completions.create(
            model=st.session_state["model"],
            messages=[
                {"role": m["role"], "content": m["content"]}
                for m in st.session_state.messages
            ],
            stream=True,
        )
        response = st.write_stream(stream)
    st.session_state.messages.append({"role": "assistant", "content": response})