Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,11 +3,12 @@ from dotenv import find_dotenv, load_dotenv
|
|
3 |
import streamlit as st
|
4 |
from typing import Generator
|
5 |
from groq import Groq
|
|
|
|
|
6 |
|
7 |
_ = load_dotenv(find_dotenv())
|
8 |
st.set_page_config(page_icon="💬", layout="wide", page_title="Groq Chat Bot...")
|
9 |
|
10 |
-
|
11 |
def icon(emoji: str):
|
12 |
"""Shows an emoji as a Notion-style page icon."""
|
13 |
st.write(
|
@@ -15,34 +16,26 @@ def icon(emoji: str):
|
|
15 |
unsafe_allow_html=True,
|
16 |
)
|
17 |
|
18 |
-
|
19 |
icon("⚡")
|
20 |
|
21 |
st.subheader("GroqChatbot", divider="rainbow", anchor=False)
|
22 |
|
23 |
-
client = Groq(
|
24 |
-
api_key=os.environ['GROQ_API_KEY'],
|
25 |
-
)
|
26 |
|
27 |
-
# Initialize chat history and selected model
|
28 |
if "messages" not in st.session_state:
|
29 |
st.session_state.messages = []
|
30 |
|
31 |
if "selected_model" not in st.session_state:
|
32 |
st.session_state.selected_model = None
|
33 |
|
34 |
-
# Define model details
|
35 |
models = {
|
36 |
-
"mixtral-8x7b-32768": {
|
37 |
-
"name": "Mixtral-8x7b-Instruct-v0.1",
|
38 |
-
"tokens": 32768,
|
39 |
-
"developer": "Mistral",
|
40 |
-
},
|
41 |
-
"llama2-70b-4096": {"name": "LLaMA2-70b-chat", "tokens": 4096, "developer": "Meta"},
|
42 |
"gemma-7b-it": {"name": "Gemma-7b-it", "tokens": 8192, "developer": "Google"},
|
|
|
|
|
|
|
43 |
}
|
44 |
|
45 |
-
# Layout for model selection and max_tokens slider
|
46 |
col1, col2 = st.columns(2)
|
47 |
|
48 |
with col1:
|
@@ -50,10 +43,9 @@ with col1:
|
|
50 |
"Choose a model:",
|
51 |
options=list(models.keys()),
|
52 |
format_func=lambda x: models[x]["name"],
|
53 |
-
index=0,
|
54 |
)
|
55 |
|
56 |
-
# Detect model change and clear chat history if model has changed
|
57 |
if st.session_state.selected_model != model_option:
|
58 |
st.session_state.messages = []
|
59 |
st.session_state.selected_model = model_option
|
@@ -61,30 +53,44 @@ if st.session_state.selected_model != model_option:
|
|
61 |
max_tokens_range = models[model_option]["tokens"]
|
62 |
|
63 |
with col2:
|
64 |
-
# Adjust max_tokens slider dynamically based on the selected model
|
65 |
max_tokens = st.slider(
|
66 |
"Max Tokens:",
|
67 |
-
min_value=512,
|
68 |
max_value=max_tokens_range,
|
69 |
-
# Default value or max allowed if less
|
70 |
value=min(32768, max_tokens_range),
|
71 |
step=512,
|
72 |
help=f"Adjust the maximum number of tokens (words) for the model's response. Max for selected model: {max_tokens_range}",
|
73 |
)
|
74 |
|
75 |
-
# Display chat messages from history on app rerun
|
76 |
for message in st.session_state.messages:
|
77 |
avatar = "🤖" if message["role"] == "assistant" else "🕺"
|
78 |
with st.chat_message(message["role"], avatar=avatar):
|
79 |
st.markdown(message["content"])
|
80 |
|
81 |
-
|
82 |
def generate_chat_responses(chat_completion) -> Generator[str, None, None]:
|
83 |
"""Yield chat response content from the Groq API response."""
|
84 |
for chunk in chat_completion:
|
85 |
if chunk.choices[0].delta.content:
|
86 |
yield chunk.choices[0].delta.content
|
87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
if prompt := st.chat_input("Enter your prompt here..."):
|
90 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
@@ -92,33 +98,35 @@ if prompt := st.chat_input("Enter your prompt here..."):
|
|
92 |
with st.chat_message("user", avatar="🕺"):
|
93 |
st.markdown(prompt)
|
94 |
|
95 |
-
# Fetch response from Groq API
|
96 |
try:
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
111 |
except Exception as e:
|
112 |
st.error(e, icon="🚨")
|
113 |
|
114 |
-
# Append the full response to session_state.messages
|
115 |
if isinstance(full_response, str):
|
116 |
st.session_state.messages.append(
|
117 |
{"role": "assistant", "content": full_response}
|
118 |
)
|
119 |
else:
|
120 |
-
# Handle the case where full_response is not a string
|
121 |
combined_response = "\n".join(str(item) for item in full_response)
|
122 |
st.session_state.messages.append(
|
123 |
{"role": "assistant", "content": combined_response}
|
124 |
-
)
|
|
|
3 |
import streamlit as st
|
4 |
from typing import Generator
|
5 |
from groq import Groq
|
6 |
+
import requests
|
7 |
+
from bs4 import BeautifulSoup
|
8 |
|
9 |
_ = load_dotenv(find_dotenv())
|
10 |
st.set_page_config(page_icon="💬", layout="wide", page_title="Groq Chat Bot...")
|
11 |
|
|
|
12 |
def icon(emoji: str):
|
13 |
"""Shows an emoji as a Notion-style page icon."""
|
14 |
st.write(
|
|
|
16 |
unsafe_allow_html=True,
|
17 |
)
|
18 |
|
|
|
19 |
icon("⚡")
|
20 |
|
21 |
st.subheader("GroqChatbot", divider="rainbow", anchor=False)
|
22 |
|
23 |
+
client = Groq(api_key=os.environ['GROQ_API_KEY'])
|
|
|
|
|
24 |
|
|
|
25 |
if "messages" not in st.session_state:
|
26 |
st.session_state.messages = []
|
27 |
|
28 |
if "selected_model" not in st.session_state:
|
29 |
st.session_state.selected_model = None
|
30 |
|
|
|
31 |
models = {
|
32 |
+
"mixtral-8x7b-32768": {"name": "Mixtral-8x7b-Instruct-v0.1", "tokens": 32768, "developer": "Mistral"},
|
|
|
|
|
|
|
|
|
|
|
33 |
"gemma-7b-it": {"name": "Gemma-7b-it", "tokens": 8192, "developer": "Google"},
|
34 |
+
"llama2-70b-4096": {"name": "LLaMA2-70b-chat", "tokens": 4096, "developer": "Meta"},
|
35 |
+
"llama3-70b-8192": {"name": "LLaMA3-70b-8192", "tokens": 8192, "developer": "Meta"},
|
36 |
+
"llama3-8b-8192": {"name": "LLaMA3-8b-8192", "tokens": 8192, "developer": "Meta"},
|
37 |
}
|
38 |
|
|
|
39 |
col1, col2 = st.columns(2)
|
40 |
|
41 |
with col1:
|
|
|
43 |
"Choose a model:",
|
44 |
options=list(models.keys()),
|
45 |
format_func=lambda x: models[x]["name"],
|
46 |
+
index=0,
|
47 |
)
|
48 |
|
|
|
49 |
if st.session_state.selected_model != model_option:
|
50 |
st.session_state.messages = []
|
51 |
st.session_state.selected_model = model_option
|
|
|
53 |
max_tokens_range = models[model_option]["tokens"]
|
54 |
|
55 |
with col2:
|
|
|
56 |
max_tokens = st.slider(
|
57 |
"Max Tokens:",
|
58 |
+
min_value=512,
|
59 |
max_value=max_tokens_range,
|
|
|
60 |
value=min(32768, max_tokens_range),
|
61 |
step=512,
|
62 |
help=f"Adjust the maximum number of tokens (words) for the model's response. Max for selected model: {max_tokens_range}",
|
63 |
)
|
64 |
|
|
|
65 |
for message in st.session_state.messages:
|
66 |
avatar = "🤖" if message["role"] == "assistant" else "🕺"
|
67 |
with st.chat_message(message["role"], avatar=avatar):
|
68 |
st.markdown(message["content"])
|
69 |
|
|
|
70 |
def generate_chat_responses(chat_completion) -> Generator[str, None, None]:
|
71 |
"""Yield chat response content from the Groq API response."""
|
72 |
for chunk in chat_completion:
|
73 |
if chunk.choices[0].delta.content:
|
74 |
yield chunk.choices[0].delta.content
|
75 |
|
76 |
+
def search_web(query):
|
77 |
+
try:
|
78 |
+
search_url = f"https://www.google.com/search?q={query}"
|
79 |
+
response = requests.get(search_url)
|
80 |
+
if response.status_code == 200:
|
81 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
82 |
+
search_results = soup.find_all('div', class_='tF2Cxc')
|
83 |
+
results = []
|
84 |
+
for result in search_results:
|
85 |
+
title = result.find('h3').text
|
86 |
+
url = result.find('a')['href']
|
87 |
+
snippet = result.find('span', class_='aCOpRe').text
|
88 |
+
results.append({"title": title, "url": url, "snippet": snippet})
|
89 |
+
return results
|
90 |
+
else:
|
91 |
+
return "Failed to retrieve search results"
|
92 |
+
except Exception as e:
|
93 |
+
return f"An error occurred: {e}"
|
94 |
|
95 |
if prompt := st.chat_input("Enter your prompt here..."):
|
96 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
|
98 |
with st.chat_message("user", avatar="🕺"):
|
99 |
st.markdown(prompt)
|
100 |
|
|
|
101 |
try:
|
102 |
+
if "search for" in prompt.lower():
|
103 |
+
query = prompt.lower().replace("search for", "").strip()
|
104 |
+
search_results = search_web(query)
|
105 |
+
formatted_results = "\n\n".join([f"Title: {result['title']}\nURL: {result['url']}\nSnippet: {result['snippet']}" for result in search_results])
|
106 |
+
st.session_state.messages.append({"role": "assistant", "content": formatted_results})
|
107 |
+
else:
|
108 |
+
chat_completion = client.chat.completions.create(
|
109 |
+
model=model_option,
|
110 |
+
messages=[
|
111 |
+
{"role": m["role"], "content": m["content"]}
|
112 |
+
for m in st.session_state.messages
|
113 |
+
],
|
114 |
+
max_tokens=max_tokens,
|
115 |
+
stream=True,
|
116 |
+
)
|
117 |
+
|
118 |
+
with st.chat_message("assistant", avatar="🤖"):
|
119 |
+
chat_responses_generator = generate_chat_responses(chat_completion)
|
120 |
+
full_response = st.write_stream(chat_responses_generator)
|
121 |
except Exception as e:
|
122 |
st.error(e, icon="🚨")
|
123 |
|
|
|
124 |
if isinstance(full_response, str):
|
125 |
st.session_state.messages.append(
|
126 |
{"role": "assistant", "content": full_response}
|
127 |
)
|
128 |
else:
|
|
|
129 |
combined_response = "\n".join(str(item) for item in full_response)
|
130 |
st.session_state.messages.append(
|
131 |
{"role": "assistant", "content": combined_response}
|
132 |
+
)
|