diff --git a/examples/README.md b/examples/README.md index 960daba4..7f6dccb9 100644 --- a/examples/README.md +++ b/examples/README.md @@ -159,14 +159,24 @@ Allows to run any LocalAI-compatible model as a backend on the servers of https: ### Continue -Screenshot - _by [@gruberdev](https://github.com/gruberdev)_ +Screenshot + Demonstrates how to integrate an open-source copilot alternative that enhances code analysis, completion, and improvements. This approach seamlessly integrates with any LocalAI model, offering a more user-friendly experience. [Check it out here](https://github.com/go-skynet/LocalAI/tree/master/examples/continue/) +### Streamlit bot + +_by [@majoshi1](https://github.com/majoshi1)_ + +![Screenshot](streamlit-bot/streamlit-bot.png) + +A chat bot made using `Streamlit` & LocalAI. + +[Check it out here](https://github.com/go-skynet/LocalAI/tree/master/examples/streamlit-bot/) + ## Want to contribute? Create an issue, and put `Example: ` in the title! We will post your examples here. diff --git a/examples/streamlit-bot/.gitignore b/examples/streamlit-bot/.gitignore new file mode 100644 index 00000000..0fb83220 --- /dev/null +++ b/examples/streamlit-bot/.gitignore @@ -0,0 +1 @@ +installer_files \ No newline at end of file diff --git a/examples/streamlit-bot/LICENSE b/examples/streamlit-bot/LICENSE new file mode 100644 index 00000000..e6200f0f --- /dev/null +++ b/examples/streamlit-bot/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Manohar Joshi + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/examples/streamlit-bot/Main.py b/examples/streamlit-bot/Main.py new file mode 100644 index 00000000..0063289a --- /dev/null +++ b/examples/streamlit-bot/Main.py @@ -0,0 +1,70 @@ +import streamlit as st +import time +import requests +import json + +def ask(prompt): + url = 'http://localhost:8080/v1/chat/completions' + myobj = { + "model": "ggml-gpt4all-j.bin", + "messages": [{"role": "user", "content": prompt}], + "temperature": 0.9 + } + myheaders = { "Content-Type" : "application/json" } + + x = requests.post(url, json = myobj, headers=myheaders) + + print(x.text) + + json_data = json.loads(x.text) + + return json_data["choices"][0]["message"]["content"] + + +def main(): + # Page setup + st.set_page_config(page_title="Ask your LLM") + st.header("Ask your Question 💬") + + # Initialize chat history + if "messages" not in st.session_state: + st.session_state.messages = [] + + # Display chat messages from history on app rerun + for message in st.session_state.messages: + with st.chat_message(message["role"]): + st.markdown(message["content"]) + + # Scroll to bottom + st.markdown( + """ + + """, + unsafe_allow_html=True, + ) + + # React to user input + if prompt := st.chat_input("What is up?"): + # Display user message in chat message container + st.chat_message("user").markdown(prompt) + # Add user message to chat history + st.session_state.messages.append({"role": "user", "content": prompt}) + print(f"User has asked the following question: {prompt}") + + # Process + response = "" + with st.spinner('Processing...'): + response = ask(prompt) + + #response = f"Echo: {prompt}" + # Display assistant response in chat message container + with st.chat_message("assistant"): + st.markdown(response) + # Add assistant response to chat history + st.session_state.messages.append({"role": "assistant", "content": response}) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/examples/streamlit-bot/README.md b/examples/streamlit-bot/README.md new file mode 100644 index 00000000..6588cbde --- /dev/null +++ b/examples/streamlit-bot/README.md @@ -0,0 +1,54 @@ +## Streamlit bot + +![Screenshot](streamlit-bot.png) + +This is an example to deploy a Streamlit bot with LocalAI instead of OpenAI. Instructions are for Windows. + +```bash +# Install & run Git Bash + +# Clone LocalAI +git clone https://github.com/go-skynet/LocalAI.git +cd LocalAI + +# (optional) Checkout a specific LocalAI tag +# git checkout -b build + +# Use a template from the examples +cp -rf prompt-templates/ggml-gpt4all-j.tmpl models/ + +# (optional) Edit the .env file to set things like context size and threads +# vim .env +# Download model +curl --progress-bar -C - -O https://gpt4all.io/models/ggml-gpt4all-j.bin > models/ggml-gpt4all-j.bin + +# Install & Run Docker Desktop for Windows +https://www.docker.com/products/docker-desktop/ + +# start with docker-compose +docker-compose up -d --pull always +# or you can build the images with: +# docker-compose up -d --build +# Now API is accessible at localhost:8080 +curl http://localhost:8080/v1/models +# {"object":"list","data":[{"id":"ggml-gpt4all-j","object":"model"}]} + +curl http://localhost:8080/v1/chat/completions -H "Content-Type: application/json" -d '{ + "model": "ggml-gpt4all-j", + "messages": [{"role": "user", "content": "How are you?"}], + "temperature": 0.9 + }' + +# {"model":"ggml-gpt4all-j","choices":[{"message":{"role":"assistant","content":"I'm doing well, thanks. How about you?"}}]} + +cd examples/streamlit-bot + +install_requirements.bat + +# run the bot +start_windows.bat + +# UI will be launched automatically (http://localhost:8501/) in browser. + +``` + diff --git a/examples/streamlit-bot/cmd_windows.bat b/examples/streamlit-bot/cmd_windows.bat new file mode 100644 index 00000000..606ff485 --- /dev/null +++ b/examples/streamlit-bot/cmd_windows.bat @@ -0,0 +1,31 @@ +@echo off + +cd /D "%~dp0" + +set PATH=%PATH%;%SystemRoot%\system32 + +echo "%CD%"| findstr /C:" " >nul && echo This script relies on Miniconda which can not be silently installed under a path with spaces. && goto end + +@rem fix failed install when installing to a separate drive +set TMP=%cd%\installer_files +set TEMP=%cd%\installer_files + +@rem config +set CONDA_ROOT_PREFIX=%cd%\installer_files\conda +set INSTALL_ENV_DIR=%cd%\installer_files\env + +@rem environment isolation +set PYTHONNOUSERSITE=1 +set PYTHONPATH= +set PYTHONHOME= +set "CUDA_PATH=%INSTALL_ENV_DIR%" +set "CUDA_HOME=%CUDA_PATH%" + +@rem activate installer env +call "%CONDA_ROOT_PREFIX%\condabin\conda.bat" activate "%INSTALL_ENV_DIR%" || ( echo. && echo Miniconda hook not found. && goto end ) + +@rem enter commands +cmd /k "%*" + +:end +pause diff --git a/examples/streamlit-bot/install_requirements.bat b/examples/streamlit-bot/install_requirements.bat new file mode 100644 index 00000000..534091ee --- /dev/null +++ b/examples/streamlit-bot/install_requirements.bat @@ -0,0 +1,81 @@ +@echo off + +cd /D "%~dp0" + +set PATH=%PATH%;%SystemRoot%\system32 + +echo "%CD%"| findstr /C:" " >nul && echo This script relies on Miniconda which can not be silently installed under a path with spaces. && goto end + +@rem Check for special characters in installation path +set "SPCHARMESSAGE="WARNING: Special characters were detected in the installation path!" " This can cause the installation to fail!"" +echo "%CD%"| findstr /R /C:"[!#\$%&()\*+,;<=>?@\[\]\^`{|}~]" >nul && ( + call :PrintBigMessage %SPCHARMESSAGE% +) +set SPCHARMESSAGE= + +@rem fix failed install when installing to a separate drive +set TMP=%cd%\installer_files +set TEMP=%cd%\installer_files + +@rem config +set INSTALL_DIR=%cd%\installer_files +set CONDA_ROOT_PREFIX=%cd%\installer_files\conda +set INSTALL_ENV_DIR=%cd%\installer_files\env +set MINICONDA_DOWNLOAD_URL=https://repo.anaconda.com/miniconda/Miniconda3-py310_23.3.1-0-Windows-x86_64.exe +set conda_exists=F + +@rem figure out whether git and conda needs to be installed +call "%CONDA_ROOT_PREFIX%\_conda.exe" --version >nul 2>&1 +if "%ERRORLEVEL%" EQU "0" set conda_exists=T + +@rem (if necessary) install git and conda into a contained environment +@rem download conda +if "%conda_exists%" == "F" ( + echo Downloading Miniconda from %MINICONDA_DOWNLOAD_URL% to %INSTALL_DIR%\miniconda_installer.exe + + mkdir "%INSTALL_DIR%" + call curl -Lk "%MINICONDA_DOWNLOAD_URL%" > "%INSTALL_DIR%\miniconda_installer.exe" || ( echo. && echo Miniconda failed to download. && goto end ) + + echo Installing Miniconda to %CONDA_ROOT_PREFIX% + start /wait "" "%INSTALL_DIR%\miniconda_installer.exe" /InstallationType=JustMe /NoShortcuts=1 /AddToPath=0 /RegisterPython=0 /NoRegistry=1 /S /D=%CONDA_ROOT_PREFIX% + + @rem test the conda binary + echo Miniconda version: + call "%CONDA_ROOT_PREFIX%\_conda.exe" --version || ( echo. && echo Miniconda not found. && goto end ) +) + +@rem create the installer env +if not exist "%INSTALL_ENV_DIR%" ( + echo Packages to install: %PACKAGES_TO_INSTALL% + call "%CONDA_ROOT_PREFIX%\_conda.exe" create --no-shortcuts -y -k --prefix "%INSTALL_ENV_DIR%" python=3.10 || ( echo. && echo Conda environment creation failed. && goto end ) +) + +@rem check if conda environment was actually created +if not exist "%INSTALL_ENV_DIR%\python.exe" ( echo. && echo Conda environment is empty. && goto end ) + +@rem environment isolation +set PYTHONNOUSERSITE=1 +set PYTHONPATH= +set PYTHONHOME= +set "CUDA_PATH=%INSTALL_ENV_DIR%" +set "CUDA_HOME=%CUDA_PATH%" + +@rem activate installer env +call "%CONDA_ROOT_PREFIX%\condabin\conda.bat" activate "%INSTALL_ENV_DIR%" || ( echo. && echo Miniconda hook not found. && goto end ) + +@rem setup installer env +call pip install -r requirements.txt + +@rem below are functions for the script next line skips these during normal execution +goto end + +:PrintBigMessage +echo. && echo. +echo ******************************************************************* +for %%M in (%*) do echo * %%~M +echo ******************************************************************* +echo. && echo. +exit /b + +:end +pause \ No newline at end of file diff --git a/examples/streamlit-bot/requirements.txt b/examples/streamlit-bot/requirements.txt new file mode 100644 index 00000000..ae527c76 --- /dev/null +++ b/examples/streamlit-bot/requirements.txt @@ -0,0 +1,2 @@ +streamlit==1.26.0 +requests \ No newline at end of file diff --git a/examples/streamlit-bot/start_windows.bat b/examples/streamlit-bot/start_windows.bat new file mode 100644 index 00000000..fd76ab15 --- /dev/null +++ b/examples/streamlit-bot/start_windows.bat @@ -0,0 +1,81 @@ +@echo off + +cd /D "%~dp0" + +set PATH=%PATH%;%SystemRoot%\system32 + +echo "%CD%"| findstr /C:" " >nul && echo This script relies on Miniconda which can not be silently installed under a path with spaces. && goto end + +@rem Check for special characters in installation path +set "SPCHARMESSAGE="WARNING: Special characters were detected in the installation path!" " This can cause the installation to fail!"" +echo "%CD%"| findstr /R /C:"[!#\$%&()\*+,;<=>?@\[\]\^`{|}~]" >nul && ( + call :PrintBigMessage %SPCHARMESSAGE% +) +set SPCHARMESSAGE= + +@rem fix failed install when installing to a separate drive +set TMP=%cd%\installer_files +set TEMP=%cd%\installer_files + +@rem config +set INSTALL_DIR=%cd%\installer_files +set CONDA_ROOT_PREFIX=%cd%\installer_files\conda +set INSTALL_ENV_DIR=%cd%\installer_files\env +set MINICONDA_DOWNLOAD_URL=https://repo.anaconda.com/miniconda/Miniconda3-py310_23.3.1-0-Windows-x86_64.exe +set conda_exists=F + +@rem figure out whether git and conda needs to be installed +call "%CONDA_ROOT_PREFIX%\_conda.exe" --version >nul 2>&1 +if "%ERRORLEVEL%" EQU "0" set conda_exists=T + +@rem (if necessary) install git and conda into a contained environment +@rem download conda +if "%conda_exists%" == "F" ( + echo Downloading Miniconda from %MINICONDA_DOWNLOAD_URL% to %INSTALL_DIR%\miniconda_installer.exe + + mkdir "%INSTALL_DIR%" + call curl -Lk "%MINICONDA_DOWNLOAD_URL%" > "%INSTALL_DIR%\miniconda_installer.exe" || ( echo. && echo Miniconda failed to download. && goto end ) + + echo Installing Miniconda to %CONDA_ROOT_PREFIX% + start /wait "" "%INSTALL_DIR%\miniconda_installer.exe" /InstallationType=JustMe /NoShortcuts=1 /AddToPath=0 /RegisterPython=0 /NoRegistry=1 /S /D=%CONDA_ROOT_PREFIX% + + @rem test the conda binary + echo Miniconda version: + call "%CONDA_ROOT_PREFIX%\_conda.exe" --version || ( echo. && echo Miniconda not found. && goto end ) +) + +@rem create the installer env +if not exist "%INSTALL_ENV_DIR%" ( + echo Packages to install: %PACKAGES_TO_INSTALL% + call "%CONDA_ROOT_PREFIX%\_conda.exe" create --no-shortcuts -y -k --prefix "%INSTALL_ENV_DIR%" python=3.10 || ( echo. && echo Conda environment creation failed. && goto end ) +) + +@rem check if conda environment was actually created +if not exist "%INSTALL_ENV_DIR%\python.exe" ( echo. && echo Conda environment is empty. && goto end ) + +@rem environment isolation +set PYTHONNOUSERSITE=1 +set PYTHONPATH= +set PYTHONHOME= +set "CUDA_PATH=%INSTALL_ENV_DIR%" +set "CUDA_HOME=%CUDA_PATH%" + +@rem activate installer env +call "%CONDA_ROOT_PREFIX%\condabin\conda.bat" activate "%INSTALL_ENV_DIR%" || ( echo. && echo Miniconda hook not found. && goto end ) + +@rem setup installer env +streamlit run Main.py + +@rem below are functions for the script next line skips these during normal execution +goto end + +:PrintBigMessage +echo. && echo. +echo ******************************************************************* +for %%M in (%*) do echo * %%~M +echo ******************************************************************* +echo. && echo. +exit /b + +:end +pause \ No newline at end of file diff --git a/examples/streamlit-bot/streamlit-bot.png b/examples/streamlit-bot/streamlit-bot.png new file mode 100644 index 00000000..7b69ba99 Binary files /dev/null and b/examples/streamlit-bot/streamlit-bot.png differ