| | import os |
| | from langchain_huggingface import HuggingFaceEndpoint |
| | import streamlit as st |
| | from langchain_core.prompts import PromptTemplate |
| | from langchain_core.output_parsers import StrOutputParser |
| |
|
| | |
| | model_id="mistralai/Mistral-Nemo-Instruct-2407" |
| |
|
| | def get_llm_hf_inference(model_id=model_id, max_new_tokens=2580, temperature=0.1): |
| | """ |
| | Returns a language model for HuggingFace inference. |
| | |
| | Parameters: |
| | - model_id (str): The ID of the HuggingFace model repository. |
| | - max_new_tokens (int): The maximum number of new tokens to generate. |
| | - temperature (float): The temperature for sampling from the model. |
| | |
| | Returns: |
| | - llm (HuggingFaceEndpoint): The language model for HuggingFace inference. |
| | """ |
| | llm = HuggingFaceEndpoint( |
| | repo_id=model_id, |
| | max_new_tokens=max_new_tokens, |
| | temperature=temperature, |
| | huggingfacehub_api_token=os.getenv("HF_CHATBOT"), |
| | task="text-generation", |
| | ) |
| | return llm |
| |
|
| | |
| | st.set_page_config(page_title="AI Personal Assistant | Ary Setya P.", page_icon="π€",initial_sidebar_state="collapsed") |
| | |
| | |
| |
|
| | |
| | if "avatars" not in st.session_state: |
| | st.session_state.avatars = {'user': None, 'assistant': None} |
| |
|
| | |
| | if 'user_text' not in st.session_state: |
| | st.session_state.user_text = None |
| |
|
| | |
| | if "max_response_length" not in st.session_state: |
| | st.session_state.max_response_length = 256 |
| |
|
| | if "system_message" not in st.session_state: |
| | st.session_state.system_message = "You are a friendly AI conversing with a human user using original language, with default indonesia." |
| |
|
| | if "starter_message" not in st.session_state: |
| | st.session_state.starter_message = "Hello" |
| | |
| | |
| | |
| |
|
| | st.markdown( |
| | """ |
| | <style> |
| | [data-testid="collapsedControl"] { |
| | display: none |
| | } |
| | </style> |
| | """, |
| | unsafe_allow_html=True, |
| | ) |
| | with st.sidebar: |
| | st.header("System Settings") |
| |
|
| | |
| | st.session_state.system_message = st.text_area( |
| | "System Message", value="You are a friendly AI conversing with a human user using original language, with default indonesia." |
| | ) |
| | st.session_state.starter_message = st.text_area( |
| | 'First AI Message', value="Hello" |
| | ) |
| |
|
| | |
| | st.session_state.max_response_length = st.number_input( |
| | "Max Response Length", value=2000 , max_value=4000 |
| | ) |
| |
|
| | |
| | st.markdown("*Select Avatars:*") |
| | col1, col2 = st.columns(2) |
| | with col1: |
| | st.session_state.avatars['assistant'] = st.selectbox( |
| | "AI Avatar", options=["π€", "π€", "π¬"], index=0 |
| | ) |
| | with col2: |
| | st.session_state.avatars['user'] = st.selectbox( |
| | "User Avatar", options=["π€", "π±ββοΈ", "π¨πΎ", "π©", "π§πΎ"], index=0 |
| | ) |
| | |
| | reset_history = st.button("Reset Chat History") |
| | |
| |
|
| | |
| | |
| | if "chat_history" not in st.session_state or reset_history: |
| | st.session_state.chat_history = [{"role": "assistant", "content": st.session_state.starter_message}] |
| |
|
| | def get_response(system_message, chat_history, user_text, |
| | eos_token_id=['User'], max_new_tokens=256, get_llm_hf_kws={}): |
| | """ |
| | Generates a response from the chatbot model. |
| | |
| | Args: |
| | system_message (str): The system message for the conversation. |
| | chat_history (list): The list of previous chat messages. |
| | user_text (str): The user's input text. |
| | model_id (str, optional): The ID of the HuggingFace model to use. |
| | eos_token_id (list, optional): The list of end-of-sentence token IDs. |
| | max_new_tokens (int, optional): The maximum number of new tokens to generate. |
| | get_llm_hf_kws (dict, optional): Additional keyword arguments for the get_llm_hf function. |
| | |
| | Returns: |
| | tuple: A tuple containing the generated response and the updated chat history. |
| | """ |
| | |
| | hf = get_llm_hf_inference(max_new_tokens=max_new_tokens, temperature=0.1) |
| |
|
| | |
| | prompt = PromptTemplate.from_template( |
| | ( |
| | "[INST] {system_message}" |
| | "\nHistory:\n{chat_history}\n\n" |
| | "\nUser: {user_text}.\n [/INST]" |
| | "\nAI:" |
| | ) |
| | ) |
| | |
| | chat = prompt | hf.bind(skip_prompt=True) | StrOutputParser(output_key='content') |
| |
|
| | |
| | response = chat.invoke(input=dict(system_message=system_message, user_text=user_text, chat_history=chat_history)) |
| | response = response.split("AI:")[-1].strip() |
| |
|
| | |
| | chat_history.append({'role': 'user', 'content': user_text}) |
| | chat_history.append({'role': 'assistant', 'content': response}) |
| | return response, chat_history |
| |
|
| | |
| | chat_interface = st.container(border=True) |
| | with chat_interface: |
| | output_container = st.container() |
| | col1, col2 = st.columns([2,10]) |
| | with col1: |
| | reset_history = st.button(label="New Chat") |
| | with col2: |
| | st.session_state.user_text = st.chat_input(placeholder="Ask here ...") |
| | |
| | |
| | with output_container: |
| | |
| | for message in st.session_state.chat_history: |
| | |
| | if message['role'] == 'system': |
| | continue |
| | |
| | |
| | with st.chat_message(message['role'], |
| | avatar=st.session_state['avatars'][message['role']]): |
| | st.markdown(message['content']) |
| | |
| | |
| | if st.session_state.user_text: |
| | |
| | |
| | with st.chat_message("user", |
| | avatar=st.session_state.avatars['user']): |
| | st.markdown(st.session_state.user_text) |
| | |
| | |
| | with st.chat_message("assistant", |
| | avatar=st.session_state.avatars['assistant']): |
| |
|
| | with st.spinner("Thinking..."): |
| | |
| | response, st.session_state.chat_history = get_response( |
| | system_message=st.session_state.system_message, |
| | user_text=st.session_state.user_text, |
| | chat_history=st.session_state.chat_history, |
| | max_new_tokens=st.session_state.max_response_length, |
| | ) |
| | st.markdown(response) |