Update app.py
Browse filesAdded IBM endpoints
app.py
CHANGED
|
@@ -1,77 +1,147 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import
|
| 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 |
-
st.
|
| 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 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
def clear_chat():
|
| 7 |
+
st.session_state.messages = []
|
| 8 |
+
|
| 9 |
+
def initialize_provider_settings(provider_choice):
|
| 10 |
+
"""Configure API settings based on provider selection"""
|
| 11 |
+
provider_configs = {
|
| 12 |
+
"Denvr Dataworks": {
|
| 13 |
+
"api_key_source": st.secrets.get("openai_apikey", ""),
|
| 14 |
+
"base_url_source": os.environ.get("base_url", ""),
|
| 15 |
+
"fallback_model": "meta-llama/Llama-3.3-70B-Instruct"
|
| 16 |
+
},
|
| 17 |
+
"IBM": {
|
| 18 |
+
"api_key_source": os.environ.get("ibm_openai_apikey", ""),
|
| 19 |
+
"base_url_source": os.environ.get("ibm_base_url", ""),
|
| 20 |
+
"fallback_model": None
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
return provider_configs.get(provider_choice, {})
|
| 25 |
+
|
| 26 |
+
st.title("Intel® AI for Enterprise Inference")
|
| 27 |
+
st.header("LLM chatbot")
|
| 28 |
+
|
| 29 |
+
with st.sidebar:
|
| 30 |
+
# Provider selection dropdown
|
| 31 |
+
available_providers = ["Denvr Dataworks", "IBM"]
|
| 32 |
+
|
| 33 |
+
if "current_provider_choice" not in st.session_state:
|
| 34 |
+
st.session_state.current_provider_choice = available_providers[0]
|
| 35 |
+
|
| 36 |
+
provider_selection = st.selectbox(
|
| 37 |
+
"Choose AI Provider:",
|
| 38 |
+
available_providers,
|
| 39 |
+
key="current_provider_choice"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Get provider-specific settings
|
| 43 |
+
provider_settings = initialize_provider_settings(provider_selection)
|
| 44 |
+
|
| 45 |
+
# Validate required credentials
|
| 46 |
+
if not provider_settings.get("api_key_source") or not provider_settings.get("base_url_source"):
|
| 47 |
+
st.error(f"Configuration missing for {provider_selection}. Check environment variables.")
|
| 48 |
+
st.stop()
|
| 49 |
+
|
| 50 |
+
# Setup OpenAI client
|
| 51 |
+
try:
|
| 52 |
+
api_client = OpenAI(
|
| 53 |
+
api_key=provider_settings["api_key_source"],
|
| 54 |
+
base_url=provider_settings["base_url_source"]
|
| 55 |
+
)
|
| 56 |
+
available_models = api_client.models.list()
|
| 57 |
+
model_list = sorted([m.id for m in available_models])
|
| 58 |
+
|
| 59 |
+
# Handle model selection with provider switching
|
| 60 |
+
session_key = f"model_for_{provider_selection}"
|
| 61 |
+
if session_key not in st.session_state or st.session_state.get("last_provider") != provider_selection:
|
| 62 |
+
preferred_model = provider_settings.get("fallback_model")
|
| 63 |
+
if preferred_model and preferred_model in model_list:
|
| 64 |
+
st.session_state[session_key] = preferred_model
|
| 65 |
+
elif model_list:
|
| 66 |
+
st.session_state[session_key] = model_list[0]
|
| 67 |
+
st.session_state.last_provider = provider_selection
|
| 68 |
+
|
| 69 |
+
if not model_list:
|
| 70 |
+
st.error(f"No models found for {provider_selection}")
|
| 71 |
+
st.stop()
|
| 72 |
+
|
| 73 |
+
# Model selection interface
|
| 74 |
+
chosen_model = st.selectbox(
|
| 75 |
+
f"Available models from {provider_selection}:",
|
| 76 |
+
model_list,
|
| 77 |
+
key=session_key,
|
| 78 |
+
)
|
| 79 |
+
st.info(f"Active model: {chosen_model}")
|
| 80 |
+
|
| 81 |
+
except Exception as connection_error:
|
| 82 |
+
st.error(f"Connection failed for {provider_selection}: {connection_error}")
|
| 83 |
+
st.stop()
|
| 84 |
+
|
| 85 |
+
st.button("Reset Conversation", on_click=clear_chat)
|
| 86 |
+
|
| 87 |
+
st.markdown("---")
|
| 88 |
+
|
| 89 |
+
# Display provider-specific information
|
| 90 |
+
if provider_selection == "Denvr Dataworks":
|
| 91 |
+
st.markdown(
|
| 92 |
+
"""
|
| 93 |
+
**Denvr Dataworks Integration**
|
| 94 |
+
|
| 95 |
+
Visit [Denvr Dataworks](https://www.denvrdata.com/intel) for model information and API access.
|
| 96 |
+
|
| 97 |
+
Join the community: [Intel's DevHub Discord](https://discord.gg/kfJ3NKEw5t)
|
| 98 |
+
"""
|
| 99 |
+
)
|
| 100 |
+
elif provider_selection == "IBM":
|
| 101 |
+
st.markdown(
|
| 102 |
+
"""
|
| 103 |
+
**IBM AI Services**
|
| 104 |
+
|
| 105 |
+
Connected to IBM's AI infrastructure. Ensure your credentials are properly configured.
|
| 106 |
+
"""
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
# Main chat interface
|
| 110 |
+
try:
|
| 111 |
+
if "messages" not in st.session_state:
|
| 112 |
+
st.session_state.messages = []
|
| 113 |
+
|
| 114 |
+
# Display conversation history
|
| 115 |
+
for msg in st.session_state.messages:
|
| 116 |
+
with st.chat_message(msg["role"]):
|
| 117 |
+
st.markdown(msg["content"])
|
| 118 |
+
|
| 119 |
+
# Handle new user input
|
| 120 |
+
if user_input := st.chat_input("Enter your message..."):
|
| 121 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 122 |
+
with st.chat_message("user"):
|
| 123 |
+
st.markdown(user_input)
|
| 124 |
+
|
| 125 |
+
# Generate AI response
|
| 126 |
+
with st.chat_message("assistant"):
|
| 127 |
+
try:
|
| 128 |
+
response_stream = api_client.chat.completions.create(
|
| 129 |
+
model=chosen_model,
|
| 130 |
+
messages=[
|
| 131 |
+
{"role": msg["role"], "content": msg["content"]}
|
| 132 |
+
for msg in st.session_state.messages
|
| 133 |
+
],
|
| 134 |
+
max_tokens=4096,
|
| 135 |
+
stream=True,
|
| 136 |
+
)
|
| 137 |
+
ai_response = st.write_stream(response_stream)
|
| 138 |
+
except Exception as generation_error:
|
| 139 |
+
st.error(f"Response generation failed: {generation_error}")
|
| 140 |
+
ai_response = "Unable to generate response due to an error."
|
| 141 |
+
|
| 142 |
+
st.session_state.messages.append({"role": "assistant", "content": ai_response})
|
| 143 |
+
|
| 144 |
+
except KeyError as key_err:
|
| 145 |
+
st.error(f"Configuration key error: {key_err}")
|
| 146 |
+
except Exception as general_err:
|
| 147 |
+
st.error(f"Unexpected error occurred: {general_err}")
|