Benjamin Consolvo
		
	commited on
		
		
					Commit 
							
							·
						
						0dc0782
	
1
								Parent(s):
							
							c274166
								
OpenAI key on sidebar
Browse files- .gitignore +1 -1
 - app.py +76 -75
 
    	
        .gitignore
    CHANGED
    
    | 
         @@ -1 +1 @@ 
     | 
|
| 1 | 
         
            -
             
     | 
| 
         | 
|
| 1 | 
         
            +
            app_backup.py
         
     | 
    	
        app.py
    CHANGED
    
    | 
         @@ -1,75 +1,76 @@ 
     | 
|
| 1 | 
         
            -
            #  
     | 
| 2 | 
         
            -
             
     | 
| 3 | 
         
            -
             
     | 
| 4 | 
         
            -
             
     | 
| 5 | 
         
            -
             
     | 
| 6 | 
         
            -
             
     | 
| 7 | 
         
            -
             
     | 
| 8 | 
         
            -
             
     | 
| 9 | 
         
            -
             
     | 
| 10 | 
         
            -
             
     | 
| 11 | 
         
            -
             
     | 
| 12 | 
         
            -
             
     | 
| 13 | 
         
            -
             
     | 
| 14 | 
         
            -
             
     | 
| 15 | 
         
            -
             
     | 
| 16 | 
         
            -
             
     | 
| 17 | 
         
            -
             
     | 
| 18 | 
         
            -
             
     | 
| 19 | 
         
            -
             
     | 
| 20 | 
         
            -
             
     | 
| 21 | 
         
            -
                st. 
     | 
| 22 | 
         
            -
             
     | 
| 23 | 
         
            -
             
     | 
| 24 | 
         
            -
             
     | 
| 25 | 
         
            -
             
     | 
| 26 | 
         
            -
             
     | 
| 27 | 
         
            -
             
     | 
| 28 | 
         
            -
             
     | 
| 29 | 
         
            -
             
     | 
| 30 | 
         
            -
             
     | 
| 31 | 
         
            -
                st. 
     | 
| 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 | 
         
            -
             
     | 
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            # Import necessary libraries
         
     | 
| 2 | 
         
            +
             
     | 
| 3 | 
         
            +
            import streamlit as st
         
     | 
| 4 | 
         
            +
            import os
         
     | 
| 5 | 
         
            +
            from openai import OpenAI
         
     | 
| 6 | 
         
            +
            import json
         
     | 
| 7 | 
         
            +
             
     | 
| 8 | 
         
            +
            working_dir = os.path.dirname(os.path.abspath(__file__))
         
     | 
| 9 | 
         
            +
            endpoint_data = json.load(open(f"{working_dir}/model_info.json"))
         
     | 
| 10 | 
         
            +
             
     | 
| 11 | 
         
            +
            def clear_chat():
         
     | 
| 12 | 
         
            +
                st.session_state.messages = []
         
     | 
| 13 | 
         
            +
             
     | 
| 14 | 
         
            +
            st.title("Intel® AI for Enterprise Inference \n Chatbot")
         
     | 
| 15 | 
         
            +
             
     | 
| 16 | 
         
            +
            # Extract the keys (model names) from the JSON data
         
     | 
| 17 | 
         
            +
            model_names = list(endpoint_data.keys())
         
     | 
| 18 | 
         
            +
             
     | 
| 19 | 
         
            +
            with st.sidebar:
         
     | 
| 20 | 
         
            +
                modelname = st.selectbox("Select a LLM model (Running on Intel® Gaudi®) ", model_names)
         
     | 
| 21 | 
         
            +
                st.write(f"You selected: {modelname}")
         
     | 
| 22 | 
         
            +
                st.button("Start New Chat", on_click=clear_chat)
         
     | 
| 23 | 
         
            +
             
     | 
| 24 | 
         
            +
                # Add a text input for the API key
         
     | 
| 25 | 
         
            +
                api_key = st.text_input("Enter your API Key", type="password")
         
     | 
| 26 | 
         
            +
                if api_key:
         
     | 
| 27 | 
         
            +
                    st.session_state.api_key = api_key    
         
     | 
| 28 | 
         
            +
             
     | 
| 29 | 
         
            +
            # Check if the API key is provided
         
     | 
| 30 | 
         
            +
            if "api_key" not in st.session_state or not st.session_state.api_key:
         
     | 
| 31 | 
         
            +
                st.error("Please enter your API Key in the sidebar.")
         
     | 
| 32 | 
         
            +
            else:
         
     | 
| 33 | 
         
            +
                try:
         
     | 
| 34 | 
         
            +
                    endpoint = endpoint_data[modelname]
         
     | 
| 35 | 
         
            +
                    
         
     | 
| 36 | 
         
            +
                    api_key = st.session_state.api_key
         
     | 
| 37 | 
         
            +
                    base_url = endpoint
         
     | 
| 38 | 
         
            +
                    client = OpenAI(api_key=api_key, base_url=base_url)
         
     | 
| 39 | 
         
            +
             
     | 
| 40 | 
         
            +
                    # Extract the model name
         
     | 
| 41 | 
         
            +
                    models = client.models.list()
         
     | 
| 42 | 
         
            +
                    modelname = models.data[0].id
         
     | 
| 43 | 
         
            +
             
     | 
| 44 | 
         
            +
                    if "messages" not in st.session_state:
         
     | 
| 45 | 
         
            +
                        st.session_state.messages = []
         
     | 
| 46 | 
         
            +
             
     | 
| 47 | 
         
            +
                    for message in st.session_state.messages:
         
     | 
| 48 | 
         
            +
                        with st.chat_message(message["role"]):
         
     | 
| 49 | 
         
            +
                            st.markdown(message["content"])
         
     | 
| 50 | 
         
            +
             
     | 
| 51 | 
         
            +
                    if prompt := st.chat_input("What is up?"):
         
     | 
| 52 | 
         
            +
                        st.session_state.messages.append({"role": "user", "content": prompt})
         
     | 
| 53 | 
         
            +
                        with st.chat_message("user"):
         
     | 
| 54 | 
         
            +
                            st.markdown(prompt)
         
     | 
| 55 | 
         
            +
             
     | 
| 56 | 
         
            +
                        with st.chat_message("assistant"):
         
     | 
| 57 | 
         
            +
                            try:
         
     | 
| 58 | 
         
            +
                                stream = client.chat.completions.create(
         
     | 
| 59 | 
         
            +
                                    model=modelname,
         
     | 
| 60 | 
         
            +
                                    messages=[
         
     | 
| 61 | 
         
            +
                                        {"role": m["role"], "content": m["content"]}
         
     | 
| 62 | 
         
            +
                                        for m in st.session_state.messages
         
     | 
| 63 | 
         
            +
                                    ],
         
     | 
| 64 | 
         
            +
                                    max_tokens=1024,
         
     | 
| 65 | 
         
            +
                                    stream=True,
         
     | 
| 66 | 
         
            +
                                )
         
     | 
| 67 | 
         
            +
                                response = st.write_stream(stream)
         
     | 
| 68 | 
         
            +
                            except Exception as e:
         
     | 
| 69 | 
         
            +
                                st.error(f"An error occurred while generating the response: {e}")
         
     | 
| 70 | 
         
            +
                                response = "An error occurred while generating the response."
         
     | 
| 71 | 
         
            +
             
     | 
| 72 | 
         
            +
                        st.session_state.messages.append({"role": "assistant", "content": response})
         
     | 
| 73 | 
         
            +
                except KeyError as e:
         
     | 
| 74 | 
         
            +
                    st.error(f"Key error: {e}")
         
     | 
| 75 | 
         
            +
                except Exception as e:
         
     | 
| 76 | 
         
            +
                    st.error(f"An unexpected error occurred: {e}")
         
     |