Geraldine commited on
Commit
fc8d5a7
1 Parent(s): 5ed8ff3

Update pages/app_langchain_completion.py

Browse files
Files changed (1) hide show
  1. pages/app_langchain_completion.py +135 -135
pages/app_langchain_completion.py CHANGED
@@ -1,135 +1,135 @@
1
- import requests
2
- import json
3
- import os
4
- import streamlit as st
5
- from langchain_community.llms import Ollama
6
- from langchain_nvidia_ai_endpoints import ChatNVIDIA
7
- from langchain_groq import ChatGroq
8
- from langchain.chains import ConversationChain
9
- from langchain.memory import ConversationBufferMemory
10
- from clients import OllamaClient, GroqClient
11
-
12
- st.set_page_config(
13
- page_title="QA Inference Streamlit App using Ollama, Nvidia and Groq with Langchain framework"
14
- )
15
-
16
-
17
- # Cache the header of the app to prevent re-rendering on each load
18
- @st.cache_resource
19
- def display_app_header():
20
- """Display the header of the Streamlit app."""
21
- st.title("QA Inference with Ollama & Nvidia & Groq as LLMs providers")
22
- st.subheader("ChatBot based on Langchain framework")
23
-
24
-
25
- # Display the header of the app
26
- display_app_header()
27
-
28
- # UI sidebar ##########################################
29
- st.sidebar.subheader("Models")
30
- # LLM
31
- llm_providers = {
32
- "Local Ollama": "ollama",
33
- "Cloud Nvidia": "nvidia",
34
- "Cloud Groq": "groq",
35
- }
36
- # hard coded because models returned by NvidiaClient().list_models() are not well formed for Langchain ChatNVIDIA class
37
- llms_from_nvidia = [
38
- "ai-llama3-70b",
39
- "ai-mistral-large",
40
- "ai-gemma-7b",
41
- "ai-codellama-70b",
42
- ]
43
- llm_provider = st.sidebar.radio(
44
- "Choose your LLM Provider", llm_providers.keys(), key="llm_provider"
45
- )
46
- if llm_provider == "Local Ollama":
47
- ollama_list_models = OllamaClient().list_models()
48
- ollama_models = [x["name"] for x in ollama_list_models["models"]]
49
- ollama_llm = st.sidebar.radio(
50
- "Select your Ollama model", ollama_models, key="ollama_llm"
51
- ) # retrive with st.session_state["ollama_llm"]
52
- elif llm_provider == "Cloud Nvidia":
53
- if nvidia_api_token := st.sidebar.text_input("Enter your Nvidia API Key"):
54
- os.environ["NVIDIA_API_KEY"] = nvidia_api_token
55
- st.sidebar.info("nvidia authentification ok")
56
- # nvidia_models = [model.model_name for model in list_nvidia_models() if (model.model_type == "chat") & (model.model_name is not None)] # list is false
57
- nvidia_models = llms_from_nvidia
58
- nvidia_llm = st.sidebar.radio(
59
- "Select your Nvidia LLM", nvidia_models, key="nvidia_llm"
60
- )
61
- else:
62
- st.sidebar.warning("You must enter your Nvidia API key")
63
- elif llm_provider == "Cloud Groq":
64
- if groq_api_token := st.sidebar.text_input("Enter your Groq API Key"):
65
- st.sidebar.info("Groq authentification ok")
66
- groq_list_models = GroqClient(api_key=groq_api_token).list_models()
67
- groq_models = [x["id"] for x in groq_list_models["data"]]
68
- groq_llm = st.sidebar.radio("Choose your Groq LLM", groq_models, key="groq_llm")
69
- else:
70
- st.sidebar.warning("You must enter your Groq API key")
71
-
72
- # LLM parameters
73
- st.sidebar.subheader("Parameters")
74
- max_tokens = st.sidebar.number_input("Token numbers", value=1024, key="max_tokens")
75
- temperature = st.sidebar.slider(
76
- "Temperature", min_value=0.0, max_value=1.0, value=0.5, step=0.1, key="temperature"
77
- )
78
- top_p = st.sidebar.slider(
79
- "Top P", min_value=0.0, max_value=1.0, value=0.7, step=0.1, key="top_p"
80
- )
81
-
82
-
83
- # LLM client #########################################
84
- class LlmProvider:
85
- def __init__(self, provider):
86
- if provider == "ollama":
87
- self.llm = Ollama(
88
- model=st.session_state["ollama_llm"],
89
- temperature=st.session_state["temperature"],
90
- max_tokens=st.session_state["max_tokens"],
91
- top_p=st.session_state["top_p"],
92
- )
93
- elif provider == "nvidia":
94
- self.llm = ChatNVIDIA(
95
- model=st.session_state["nvidia_llm"],
96
- temperature=st.session_state["temperature"],
97
- max_tokens=st.session_state["max_tokens"],
98
- top_p=st.session_state["top_p"],
99
- )
100
- elif provider == "groq":
101
- self.llm = ChatGroq(
102
- groq_api_key = groq_api_token,
103
- model_name=st.session_state["groq_llm"],
104
- temperature=st.session_state["temperature"],
105
- max_tokens=st.session_state["max_tokens"],
106
- top_p=st.session_state["top_p"],
107
- )
108
-
109
-
110
- # Initialize chat history
111
- if "messages" not in st.session_state:
112
- st.session_state.messages = []
113
-
114
- # Display chat messages from history on app rerun
115
- for message in st.session_state.messages:
116
- with st.chat_message(message["role"]):
117
- st.markdown(message["content"])
118
-
119
- # React to user input
120
- if prompt := st.chat_input("What is up?"):
121
- # Display user message in chat message container
122
- with st.chat_message("user"):
123
- st.markdown(prompt)
124
- conversation = ConversationChain(
125
- llm=LlmProvider(llm_providers[st.session_state["llm_provider"]]).llm,
126
- memory=ConversationBufferMemory(),
127
- )
128
- response = f"Echo: {prompt}"
129
- # Display assistant response in chat message container
130
- with st.chat_message("assistant"):
131
- # response = LlmProvider1(llm_providers[llm_provider], prompt=prompt).response
132
- response = conversation.invoke(prompt)["response"]
133
- st.markdown(response)
134
- # Add assistant response to chat history
135
- st.session_state.messages.append({"role": "assistant", "content": response})
 
1
+ import requests
2
+ import json
3
+ import os
4
+ import streamlit as st
5
+ from langchain_community.llms import Ollama
6
+ from langchain_nvidia_ai_endpoints import ChatNVIDIA
7
+ from langchain_groq import ChatGroq
8
+ from langchain.chains import ConversationChain
9
+ from langchain.memory import ConversationBufferMemory
10
+ from clients import OllamaClient, GroqClient
11
+
12
+ st.set_page_config(
13
+ page_title="QA Inference Streamlit App using Ollama, Nvidia and Groq with Langchain framework"
14
+ )
15
+
16
+
17
+ # Cache the header of the app to prevent re-rendering on each load
18
+ @st.cache_resource
19
+ def display_app_header():
20
+ """Display the header of the Streamlit app."""
21
+ st.title("QA Inference with Ollama & Nvidia & Groq as LLMs providers")
22
+ st.subheader("ChatBot based on Langchain framework")
23
+
24
+
25
+ # Display the header of the app
26
+ display_app_header()
27
+
28
+ # UI sidebar ##########################################
29
+ st.sidebar.subheader("Models")
30
+ # LLM
31
+ llm_providers = {
32
+ "Local Ollama": "ollama",
33
+ "Cloud Nvidia": "nvidia",
34
+ "Cloud Groq": "groq",
35
+ }
36
+ # hard coded because models returned by NvidiaClient().list_models() are not well formed for Langchain ChatNVIDIA class
37
+ llms_from_nvidia = [
38
+ "ai-llama3-70b",
39
+ "ai-mistral-large",
40
+ "ai-gemma-7b",
41
+ "ai-codellama-70b",
42
+ ]
43
+ llm_provider = st.sidebar.radio(
44
+ "Choose your LLM Provider", llm_providers.keys(), key="llm_provider"
45
+ )
46
+ if llm_provider == "Local Ollama":
47
+ ollama_list_models = OllamaClient().list_models()
48
+ ollama_models = [x["name"] for x in ollama_list_models["models"]]
49
+ ollama_llm = st.sidebar.radio(
50
+ "Select your Ollama model", ollama_models, key="ollama_llm"
51
+ ) # retrive with st.session_state["ollama_llm"]
52
+ elif llm_provider == "Cloud Nvidia":
53
+ if nvidia_api_token := st.sidebar.text_input("Enter your Nvidia API Key", type="password"):
54
+ os.environ["NVIDIA_API_KEY"] = nvidia_api_token
55
+ st.sidebar.info("nvidia authentification ok")
56
+ # nvidia_models = [model.model_name for model in list_nvidia_models() if (model.model_type == "chat") & (model.model_name is not None)] # list is false
57
+ nvidia_models = llms_from_nvidia
58
+ nvidia_llm = st.sidebar.radio(
59
+ "Select your Nvidia LLM", nvidia_models, key="nvidia_llm"
60
+ )
61
+ else:
62
+ st.sidebar.warning("You must enter your Nvidia API key")
63
+ elif llm_provider == "Cloud Groq":
64
+ if groq_api_token := st.sidebar.text_input("Enter your Groq API Key", type="password"):
65
+ st.sidebar.info("Groq authentification ok")
66
+ groq_list_models = GroqClient(api_key=groq_api_token).list_models()
67
+ groq_models = [x["id"] for x in groq_list_models["data"]]
68
+ groq_llm = st.sidebar.radio("Choose your Groq LLM", groq_models, key="groq_llm")
69
+ else:
70
+ st.sidebar.warning("You must enter your Groq API key")
71
+
72
+ # LLM parameters
73
+ st.sidebar.subheader("Parameters")
74
+ max_tokens = st.sidebar.number_input("Token numbers", value=1024, key="max_tokens")
75
+ temperature = st.sidebar.slider(
76
+ "Temperature", min_value=0.0, max_value=1.0, value=0.5, step=0.1, key="temperature"
77
+ )
78
+ top_p = st.sidebar.slider(
79
+ "Top P", min_value=0.0, max_value=1.0, value=0.7, step=0.1, key="top_p"
80
+ )
81
+
82
+
83
+ # LLM client #########################################
84
+ class LlmProvider:
85
+ def __init__(self, provider):
86
+ if provider == "ollama":
87
+ self.llm = Ollama(
88
+ model=st.session_state["ollama_llm"],
89
+ temperature=st.session_state["temperature"],
90
+ max_tokens=st.session_state["max_tokens"],
91
+ top_p=st.session_state["top_p"],
92
+ )
93
+ elif provider == "nvidia":
94
+ self.llm = ChatNVIDIA(
95
+ model=st.session_state["nvidia_llm"],
96
+ temperature=st.session_state["temperature"],
97
+ max_tokens=st.session_state["max_tokens"],
98
+ top_p=st.session_state["top_p"],
99
+ )
100
+ elif provider == "groq":
101
+ self.llm = ChatGroq(
102
+ groq_api_key = groq_api_token,
103
+ model_name=st.session_state["groq_llm"],
104
+ temperature=st.session_state["temperature"],
105
+ max_tokens=st.session_state["max_tokens"],
106
+ top_p=st.session_state["top_p"],
107
+ )
108
+
109
+
110
+ # Initialize chat history
111
+ if "messages" not in st.session_state:
112
+ st.session_state.messages = []
113
+
114
+ # Display chat messages from history on app rerun
115
+ for message in st.session_state.messages:
116
+ with st.chat_message(message["role"]):
117
+ st.markdown(message["content"])
118
+
119
+ # React to user input
120
+ if prompt := st.chat_input("What is up?"):
121
+ # Display user message in chat message container
122
+ with st.chat_message("user"):
123
+ st.markdown(prompt)
124
+ conversation = ConversationChain(
125
+ llm=LlmProvider(llm_providers[st.session_state["llm_provider"]]).llm,
126
+ memory=ConversationBufferMemory(),
127
+ )
128
+ response = f"Echo: {prompt}"
129
+ # Display assistant response in chat message container
130
+ with st.chat_message("assistant"):
131
+ # response = LlmProvider1(llm_providers[llm_provider], prompt=prompt).response
132
+ response = conversation.invoke(prompt)["response"]
133
+ st.markdown(response)
134
+ # Add assistant response to chat history
135
+ st.session_state.messages.append({"role": "assistant", "content": response})