From 512a73f7184518fc12394577bb1b5b333232d4f9 Mon Sep 17 00:00:00 2001 From: ShubhamSaboo Date: Tue, 20 Aug 2024 20:46:47 -0500 Subject: [PATCH] Added new demo --- llm_router_app/README.md | 52 +++++++++++++++++++++++++++++++++ llm_router_app/llm_router.py | 52 +++++++++++++++++++++++++++++++++ llm_router_app/requirements.txt | 2 ++ 3 files changed, 106 insertions(+) create mode 100644 llm_router_app/README.md create mode 100644 llm_router_app/llm_router.py create mode 100644 llm_router_app/requirements.txt diff --git a/llm_router_app/README.md b/llm_router_app/README.md new file mode 100644 index 0000000..d2169ab --- /dev/null +++ b/llm_router_app/README.md @@ -0,0 +1,52 @@ +## 📡 RouteLLM Chat App + +> Note: This project is inspired by the opensource [RouteLLM library](https://github.com/lm-sys/RouteLLM/tree/main), which provides intelligent routing between different language models. + +This Streamlit application demonstrates the use of RouteLLM, a system that intelligently routes queries between different language models based on the complexity of the task. It provides a chat interface where users can interact with AI models, and the app automatically selects the most appropriate model for each query. + +### Features +- Chat interface for interacting with AI models +- Automatic model selection using RouteLLM +- Utilizes both GPT-4 and Meta-Llama 3.1 models +- Displays chat history with model information + +### How to get Started? + +1. Clone the GitHub repository + +```bash +git clone https://github.com/Shubhamsaboo/awesome-llm-apps.git +``` +2. Install the required dependencies: + +```bash +pip install -r requirements.txt +``` +3. Set up your API keys: + +```bash +os.environ["OPENAI_API_KEY"] = "your_openai_api_key" +os.environ['TOGETHERAI_API_KEY'] = "your_togetherai_api_key" +``` +Note: In a production environment, it's recommended to use environment variables or a secure configuration management system instead of hardcoding API keys. + +4. Run the Streamlit App +```bash +streamlit run llm_router.py +``` + +### How it Works? + +1. RouteLLM Initialization: The app initializes the RouteLLM controller with two models: + - Strong model: GPT-4 (mini) + - Weak model: Meta-Llama 3.1 70B Instruct Turbo + +2. Chat Interface: Users can input messages through a chat interface. + +3. Model Selection: RouteLLM automatically selects the appropriate model based on the complexity of the user's query. + +4. Response Generation: The selected model generates a response to the user's input. + +5. Display: The app displays the response along with information about which model was used. + +6. History: The chat history is maintained and displayed, including model information for each response. \ No newline at end of file diff --git a/llm_router_app/llm_router.py b/llm_router_app/llm_router.py new file mode 100644 index 0000000..93c4da1 --- /dev/null +++ b/llm_router_app/llm_router.py @@ -0,0 +1,52 @@ +import os + +os.environ["OPENAI_API_KEY"] = "your_openai_api_key" +os.environ['TOGETHERAI_API_KEY'] = "your_togetherai_api_key" + +import streamlit as st +from routellm.controller import Controller + +# Initialize RouteLLM client +client = Controller( + routers=["mf"], + strong_model="gpt-4o-mini", + weak_model="together_ai/meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo", +) + +# Set up Streamlit app +st.title("RouteLLM Chat App") + +# Initialize chat history +if "messages" not in st.session_state: + st.session_state.messages = [] + +# Display chat messages +for message in st.session_state.messages: + with st.chat_message(message["role"]): + st.markdown(message["content"]) + if "model" in message: + st.caption(f"Model used: {message['model']}") + +# Chat input +if prompt := st.chat_input("What is your message?"): + # Add user message to chat history + st.session_state.messages.append({"role": "user", "content": prompt}) + with st.chat_message("user"): + st.markdown(prompt) + + # Get RouteLLM response + with st.chat_message("assistant"): + message_placeholder = st.empty() + response = client.chat.completions.create( + model="router-mf-0.11593", + messages=[{"role": "user", "content": prompt}] + ) + message_content = response['choices'][0]['message']['content'] + model_name = response['model'] + + # Display assistant's response + message_placeholder.markdown(message_content) + st.caption(f"Model used: {model_name}") + + # Add assistant's response to chat history + st.session_state.messages.append({"role": "assistant", "content": message_content, "model": model_name}) \ No newline at end of file diff --git a/llm_router_app/requirements.txt b/llm_router_app/requirements.txt new file mode 100644 index 0000000..a8f8745 --- /dev/null +++ b/llm_router_app/requirements.txt @@ -0,0 +1,2 @@ +streamlit +"routellm[serve,eval]" \ No newline at end of file