Update app.py
Browse files
app.py
CHANGED
@@ -1,134 +1,141 @@
|
|
1 |
import streamlit as st
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
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 |
-
"Gemini": "gemini-pro",
|
74 |
-
"DeepSeek": "deepseek-chat"
|
75 |
-
}
|
76 |
-
config["model_name"] = st.text_input(
|
77 |
-
"Model Name",
|
78 |
-
value=default_models.get(provider, "")
|
79 |
-
)
|
80 |
-
elif provider == "Ollama":
|
81 |
-
config["model_name"] = st.text_input(
|
82 |
-
"Model Name",
|
83 |
-
value="llama2",
|
84 |
-
help="Make sure the model is available in your Ollama instance"
|
85 |
-
)
|
86 |
-
config["base_url"] = st.text_input(
|
87 |
-
"Ollama Base URL",
|
88 |
-
"http://localhost:11434",
|
89 |
-
help="URL where your Ollama server is running"
|
90 |
-
)
|
91 |
-
|
92 |
-
# Main chat interface
|
93 |
-
st.title("💬 LLM Chat Interface")
|
94 |
-
|
95 |
-
# Display chat messages
|
96 |
-
for message in st.session_state.messages:
|
97 |
-
with st.chat_message(message["role"]):
|
98 |
-
st.markdown(message["content"])
|
99 |
-
|
100 |
-
# Handle user input
|
101 |
-
if prompt := st.chat_input("Type your message..."):
|
102 |
-
# Add user message to chat history
|
103 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
104 |
|
105 |
-
|
106 |
-
|
107 |
-
st.
|
108 |
-
|
109 |
-
#
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
if llm is None:
|
114 |
-
st.error("
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
except Exception as e:
|
134 |
-
st.error(f"Error generating response: {str(e)}")
|
|
|
1 |
import streamlit as st
|
2 |
+
from langchain.chat_models import ChatOpenAI
|
3 |
+
from langchain.schema import AIMessage, HumanMessage
|
4 |
+
|
5 |
+
# (Optional) If you're using Anthropic
|
6 |
+
# from langchain.chat_models import ChatAnthropic
|
7 |
+
|
8 |
+
# Placeholder functions for other LLMs (DeepSeek, Gemini, Ollama, etc.)
|
9 |
+
# Implement or import your own logic here.
|
10 |
+
def get_deepseek_llm(api_key: str):
|
11 |
+
"""
|
12 |
+
TODO: Implement your DeepSeek integration.
|
13 |
+
"""
|
14 |
+
# return your DeepSeek LLM client
|
15 |
+
pass
|
16 |
+
|
17 |
+
def get_gemini_llm(api_key: str):
|
18 |
+
"""
|
19 |
+
TODO: Implement your Gemini integration.
|
20 |
+
"""
|
21 |
+
# return your Gemini LLM client
|
22 |
+
pass
|
23 |
+
|
24 |
+
def get_ollama_llm():
|
25 |
+
"""
|
26 |
+
TODO: Implement your local Ollama integration.
|
27 |
+
Possibly specify a port, endpoint, etc.
|
28 |
+
"""
|
29 |
+
# return your Ollama LLM client
|
30 |
+
pass
|
31 |
+
|
32 |
+
def get_claude_llm(api_key: str):
|
33 |
+
"""
|
34 |
+
Example for Anthropic's Claude
|
35 |
+
"""
|
36 |
+
# If you installed anthropic: pip install anthropic
|
37 |
+
# from langchain.chat_models import ChatAnthropic
|
38 |
+
# llm = ChatAnthropic(anthropic_api_key=api_key)
|
39 |
+
# return llm
|
40 |
+
pass
|
41 |
+
|
42 |
+
def load_llm(selected_model: str, api_key: str):
|
43 |
+
"""
|
44 |
+
Returns the LLM object depending on user selection.
|
45 |
+
"""
|
46 |
+
if selected_model == "OpenAI":
|
47 |
+
# Use OpenAI ChatModel
|
48 |
+
# By default uses GPT-3.5. You can pass model_name="gpt-4" if you have access.
|
49 |
+
llm = ChatOpenAI(temperature=0.7, openai_api_key=api_key)
|
50 |
+
|
51 |
+
elif selected_model == "Claude":
|
52 |
+
# llm = get_claude_llm(api_key) # Uncomment once implemented
|
53 |
+
llm = None # Placeholder
|
54 |
+
st.warning("Claude is not implemented. Implement the get_claude_llm function.")
|
55 |
+
|
56 |
+
elif selected_model == "Gemini":
|
57 |
+
# llm = get_gemini_llm(api_key) # Uncomment once implemented
|
58 |
+
llm = None
|
59 |
+
st.warning("Gemini is not implemented. Implement the get_gemini_llm function.")
|
60 |
+
|
61 |
+
elif selected_model == "DeepSeek":
|
62 |
+
# llm = get_deepseek_llm(api_key) # Uncomment once implemented
|
63 |
+
llm = None
|
64 |
+
st.warning("DeepSeek is not implemented. Implement the get_deepseek_llm function.")
|
65 |
+
|
66 |
+
elif selected_model == "Ollama (local)":
|
67 |
+
# llm = get_ollama_llm() # Uncomment once implemented
|
68 |
+
llm = None
|
69 |
+
st.warning("Ollama is not implemented. Implement the get_ollama_llm function.")
|
70 |
+
|
71 |
+
else:
|
72 |
+
llm = None
|
73 |
+
|
74 |
+
return llm
|
75 |
|
76 |
+
def initialize_session_state():
|
77 |
+
"""
|
78 |
+
Initialize the session state for storing conversation history.
|
79 |
+
"""
|
80 |
+
if "messages" not in st.session_state:
|
81 |
+
st.session_state["messages"] = []
|
82 |
+
|
83 |
+
def main():
|
84 |
+
st.title("Multi-LLM Chat App")
|
85 |
+
|
86 |
+
# Sidebar for model selection and API key
|
87 |
+
st.sidebar.header("Configuration")
|
88 |
+
selected_model = st.sidebar.selectbox(
|
89 |
+
"Select an LLM",
|
90 |
+
["OpenAI", "Claude", "Gemini", "DeepSeek", "Ollama (local)"]
|
91 |
+
)
|
92 |
+
api_key = st.sidebar.text_input("API Key (if needed)", type="password")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
+
st.sidebar.write("---")
|
95 |
+
if st.sidebar.button("Clear Chat"):
|
96 |
+
st.session_state["messages"] = []
|
97 |
+
|
98 |
+
# Initialize conversation in session state
|
99 |
+
initialize_session_state()
|
100 |
+
|
101 |
+
# Load the chosen LLM
|
102 |
+
llm = load_llm(selected_model, api_key)
|
103 |
+
|
104 |
+
# Display existing conversation
|
105 |
+
for msg in st.session_state["messages"]:
|
106 |
+
if msg["role"] == "user":
|
107 |
+
st.markdown(f"**You:** {msg['content']}")
|
108 |
+
else:
|
109 |
+
st.markdown(f"**LLM:** {msg['content']}")
|
110 |
+
|
111 |
+
# User input
|
112 |
+
user_input = st.text_input("Type your message here...", "")
|
113 |
+
|
114 |
+
# On submit
|
115 |
+
if st.button("Send"):
|
116 |
+
if user_input.strip() == "":
|
117 |
+
st.warning("Please enter a message before sending.")
|
118 |
+
else:
|
119 |
+
# Add user message to conversation history
|
120 |
+
st.session_state["messages"].append({"role": "user", "content": user_input})
|
121 |
+
|
122 |
if llm is None:
|
123 |
+
st.error("LLM is not configured or implemented for this choice.")
|
124 |
+
else:
|
125 |
+
# Prepare messages in a LangChain format
|
126 |
+
lc_messages = []
|
127 |
+
for msg in st.session_state["messages"]:
|
128 |
+
if msg["role"] == "user":
|
129 |
+
lc_messages.append(HumanMessage(content=msg["content"]))
|
130 |
+
else:
|
131 |
+
lc_messages.append(AIMessage(content=msg["content"]))
|
132 |
+
|
133 |
+
# Call the LLM
|
134 |
+
response = llm(lc_messages)
|
135 |
+
# Add LLM response to conversation
|
136 |
+
st.session_state["messages"].append({"role": "assistant", "content": response.content})
|
137 |
+
|
138 |
+
# End
|
139 |
+
|
140 |
+
if __name__ == "__main__":
|
141 |
+
main()
|
|
|
|