import streamlit as st from streamlit_chat import message import requests url = 'https://api.webraft.in/v1/chat/completions' import json # Setting page title and header st.set_page_config(page_title="WebraftAI Chat", page_icon=":robot_face:") st.markdown("

WebraftAI Chat/h1>", unsafe_allow_html=True) # Initialise session state variables if 'generated' not in st.session_state: st.session_state['generated'] = [] if 'past' not in st.session_state: st.session_state['past'] = [] if 'messages' not in st.session_state: st.session_state['messages'] = [ {"role": "system", "content": "You are a helpful assistant."} ] if 'model_name' not in st.session_state: st.session_state['model_name'] = [] if 'cost' not in st.session_state: st.session_state['cost'] = [] if 'total_tokens' not in st.session_state: st.session_state['total_tokens'] = [] if 'total_cost' not in st.session_state: st.session_state['total_cost'] = 1 if 'api_key' not in st.session_state: st.session_state['api_key']="" # Sidebar - let user choose model, show total cost of current conversation, and let user clear the current conversation st.sidebar.title("Settings") model_name = st.sidebar.selectbox("Model:", ("gpt3.5","gpt4-32k")) counter_placeholder = st.sidebar.empty() api_key = st.sidebar.text_input("API_Key", value=st.session_state['api_key'], max_chars=None, key=None, type="password", label_visibility="visible") st.session_state['api_key'] = api_key clear_button = st.sidebar.button("Clear Conversation", key="clear") headers = { 'Content-Type': 'application/json', 'Authorization': f'Bearer {api_key}', } # Map model names to OpenAI model IDs if model_name == "gpt3.5": model = "gpt-3.5-turbo" else: model = "gpt4-32k" # reset everything if clear_button: st.session_state['generated'] = [] st.session_state['past'] = [] st.session_state['messages'] = [ {"role": "system", "content": "You are a helpful assistant."} ] st.session_state['number_tokens'] = [] st.session_state['model_name'] = [] st.session_state['cost'] = [] st.session_state['total_cost'] = 0.0 st.session_state['total_tokens'] = [] def evaluate(model, question, max_tokens=4096, top_p=0.7,temperature=0.1): data = { "model": model, "max_tokens": max_tokens, "messages": question, "temperature":temperature, "top_p":top_p } response = requests.post(url, headers=headers, json=data) response = response.json() sentence = response['choices'][0]['message']['content'] return sentence # generate a response def generate_response(prompt): st.session_state['messages'].append({"role": "user", "content": prompt}) question = st.session_state['messages'] sentence = evaluate(model, question) response = sentence st.session_state['messages'].append({"role": "assistant", "content": response}) # print(st.session_state['messages']) total_tokens = "153" prompt_tokens = "153" completion_tokens = "153" return response, total_tokens, prompt_tokens, completion_tokens # container for chat history response_container = st.container() # container for text box container = st.container() with container: with st.form(key='my_form', clear_on_submit=True): user_input = st.text_area("You:", key='input', height=2) submit_button = st.form_submit_button(label='✉') if submit_button and user_input: output, total_tokens, prompt_tokens, completion_tokens = generate_response(user_input) st.session_state['past'].append(user_input) st.session_state['generated'].append(output) st.session_state['model_name'].append(model_name) st.session_state['total_tokens'].append(total_tokens) # from https://openai.com/pricing#language-models if model_name == "30M_6.1K": cost = "1" else: cost = "2" if st.session_state['generated']: with response_container: for i in range(len(st.session_state['generated'])): message(st.session_state["past"][i], is_user=True, key=str(i) + '_user') message(st.session_state["generated"][i], key=str(i))