GroqChatbot / app.py
wop's picture
Update app.py
5f03bac verified
raw
history blame
5.17 kB
import os
from dotenv import find_dotenv, load_dotenv
import streamlit as st
from typing import Generator
from groq import Groq
import requests
from bs4 import BeautifulSoup
_ = load_dotenv(find_dotenv())
st.set_page_config(page_icon="๐Ÿ’ฌ", layout="wide", page_title="Groq Chat Bot...")
def icon(emoji: str):
"""Shows an emoji as a Notion-style page icon."""
st.write(
f'<span style="font-size: 78px; line-height: 1">{emoji}</span>',
unsafe_allow_html=True,
)
icon("โšก")
st.subheader("GroqChatbot", divider="rainbow", anchor=False)
client = Groq(api_key=os.environ['GROQ_API_KEY'])
if "messages" not in st.session_state:
st.session_state.messages = []
if "selected_model" not in st.session_state:
st.session_state.selected_model = None
models = {
"mixtral-8x7b-32768": {"name": "Mixtral-8x7b-Instruct-v0.1", "tokens": 32768, "developer": "Mistral"},
"gemma-7b-it": {"name": "Gemma-7b-it", "tokens": 8192, "developer": "Google"},
"llama2-70b-4096": {"name": "LLaMA2-70b-chat", "tokens": 4096, "developer": "Meta"},
"llama3-70b-8192": {"name": "LLaMA3-70b-8192", "tokens": 8192, "developer": "Meta"},
"llama3-8b-8192": {"name": "LLaMA3-8b-8192", "tokens": 8192, "developer": "Meta"},
}
col1, col2 = st.columns(2)
with col1:
model_option = st.selectbox(
"Choose a model:",
options=list(models.keys()),
format_func=lambda x: models[x]["name"],
index=0,
)
if st.session_state.selected_model != model_option:
st.session_state.messages = []
st.session_state.selected_model = model_option
max_tokens_range = models[model_option]["tokens"]
with col2:
max_tokens = st.slider(
"Max Tokens:",
min_value=512,
max_value=max_tokens_range,
value=min(32768, max_tokens_range),
step=512,
help=f"Adjust the maximum number of tokens (words) for the model's response. Max for selected model: {max_tokens_range}",
)
for message in st.session_state.messages:
avatar = "๐Ÿค–" if message["role"] == "assistant" else "๐Ÿ•บ"
with st.chat_message(message["role"], avatar=avatar):
st.markdown(message["content"])
def generate_chat_responses(chat_completion) -> Generator[str, None, None]:
"""Yield chat response content from the Groq API response."""
for chunk in chat_completion:
if chunk.choices[0].delta.content:
yield chunk.choices[0].delta.content
def search_web(query):
try:
search_url = f"https://www.google.com/search?q={query}"
response = requests.get(search_url)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
search_results = soup.find_all('div', class_='tF2Cxc')
results = []
for result in search_results:
title = result.find('h3').text
url = result.find('a')['href']
snippet = result.find('span', class_='aCOpRe').text
results.append({"title": title, "url": url, "snippet": snippet})
return results
else:
return "Failed to retrieve search results"
except Exception as e:
return f"An error occurred: {e}"
def run_conversation(user_prompt):
# Step 1: send the conversation and available functions to the model
messages = [
{
"role": "system",
"content": "You are a function calling LLM that uses the data extracted from the get_game_score function to answer questions around NBA game scores. Include the team and their opponent in your response."
},
{
"role": "user",
"content": user_prompt,
}
]
tools = [
{
"type": "internet",
"internet": {
"allow": ["search_web"],
"description": "Search the web for information.",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query.",
}
},
"required": ["query"],
},
},
}
]
response = client.chat.completions.create(
model=model_option,
messages=messages,
tools=tools,
tool_choice="auto",
max_tokens=max_tokens
)
for chunk in response:
if chunk.choices[0].delta.content:
yield chunk.choices[0].delta.content
if prompt := st.text_input("Enter your prompt here..."):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user", avatar="๐Ÿ•บ"):
st.markdown(prompt)
try:
chat_completion = run_conversation(prompt)
with st.chat_message("assistant", avatar="๐Ÿค–"):
chat_responses_generator = generate_chat_responses(chat_completion)
for response in chat_responses_generator:
st.markdown(response)
except Exception as e:
st.error(e, icon="๐Ÿšจ")