File size: 1,245 Bytes
47477d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from g4f.client import Client

client = Client()
st.title("GPT-4o-Mini")

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'])

if prompt := st.chat_input("Enter a prompt"):
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.markdown(prompt)
    
    with st.chat_message("assistant"):
        chatbot_msg = st.empty()
        full_response = ""
        stream = client.chat.completions.create(
            model="gpt-4o-mini",
            messages=[
                {"role": msg["role"], "content": msg["content"]}
                for msg in st.session_state['messages']
            ],
            stream=True
        )
        for chunk in stream:
            token = chunk.choices[0].delta.content
            if token is not None:
                full_response += token
                chatbot_msg.markdown(full_response)
        chatbot_msg.markdown(full_response)
    st.session_state.messages.append({"role": "assistant", "content": full_response})