File size: 2,317 Bytes
9a7b741
 
 
 
 
 
78a3087
9a7b741
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78a3087
9a7b741
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fec7628
9a7b741
 
 
 
 
 
 
 
 
 
 
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
# chat.py
import streamlit as st
from huggingface_hub import InferenceClient
import os
import json

st.header("Multi-turn feedback chatbot demo")

# import the inference client
hf_token = os.getenv("HF_TOKEN")
client = InferenceClient(model="openai/gpt-oss-20b", token=hf_token)

#### Initialize chat history
if "messages" not in st.session_state:
    st.session_state.messages = []
    
#### set basic parameters
# hard-coded for now but can be changed
max_tokens = 60000
temperature = 0.7
top_p = 0.95

if "chat_history" not in st.session_state:
    st.session_state.chat_history = [{"role": "assistant", "content": "Ask me anything!"}]

# Display chat
for message in st.session_state.chat_history:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

if prompt := st.chat_input("Send a message"):
    # add message to chat history
    st.session_state.chat_history.append({"role": "user", "content": prompt})
    
    # show message
    with st.chat_message("user"):
        st.markdown(prompt)     # for markdown of user text
    
    # Prepare messages for API call
    messages_for_api = [{"role": "system", "content": "You are a helpful assistant."}]
    messages_for_api.extend(st.session_state.chat_history)
    
    # Generate and display assistant response
    with st.chat_message("assistant"):
        with st.spinner("Thinking..."):
            try:
                # Call Hugging Face API
                response = client.chat_completion(messages=messages_for_api,
                    max_tokens=max_tokens,
                    temperature=temperature,
                    top_p=top_p,
                    stream=False,
                )
                
                # show the response
                reply = response.choices[0].message["content"]
                st.markdown(reply)
                # print(reply)
                    
                # Add assistant response to chat history
                st.session_state.chat_history.append(
                    {"role": "assistant", "content": reply}
                )
                
            except Exception as e:
                st.error(f"Error generating response: {str(e)}")
                # Don't add the user message if there was an error
                st.session_state.chat_history.pop()