Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,46 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
-
|
| 4 |
from langchain_groq import ChatGroq
|
| 5 |
from langchain_community.tools import ArxivQueryRun, WikipediaQueryRun, DuckDuckGoSearchRun
|
| 6 |
from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
|
| 7 |
from langchain import hub
|
| 8 |
-
from langchain.agents import create_openai_tools_agent
|
| 9 |
-
from langchain.agents import AgentExecutor
|
| 10 |
from langchain.prompts import PromptTemplate
|
| 11 |
from langchain_community.vectorstores import FAISS
|
| 12 |
from langchain_huggingface import HuggingFaceEmbeddings
|
| 13 |
from langchain.tools import Tool
|
| 14 |
from pydantic import BaseModel, Field
|
| 15 |
from typing import List, Dict
|
| 16 |
-
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 17 |
-
|
| 18 |
-
import logging
|
| 19 |
|
| 20 |
-
# Configure
|
| 21 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 22 |
-
|
| 23 |
-
# Example log messages
|
| 24 |
logging.info("Streamlit app started")
|
| 25 |
-
logging.error("This is an error log")
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
# Define a query condensing template
|
| 31 |
condense_prompt_template = PromptTemplate(
|
| 32 |
input_variables=["original_question", "conversation_history"],
|
| 33 |
template="""
|
| 34 |
Given the conversation history below, condense the user's query into a clear and specific question.
|
| 35 |
-
|
| 36 |
Conversation History:
|
| 37 |
{conversation_history}
|
| 38 |
-
|
| 39 |
Original Question:
|
| 40 |
{original_question}
|
| 41 |
-
|
| 42 |
Condensed Question:"""
|
| 43 |
)
|
| 44 |
|
| 45 |
def condense_query(llm_model, original_question, conversation_history):
|
| 46 |
-
# Format the prompt with conversation history and original question
|
| 47 |
prompt = condense_prompt_template.format(
|
| 48 |
original_question=original_question,
|
| 49 |
conversation_history=conversation_history
|
| 50 |
)
|
| 51 |
-
# Generate the condensed query
|
| 52 |
resp = llm_model.predict(prompt)
|
| 53 |
-
return resp.strip()
|
| 54 |
|
| 55 |
# Initialize HuggingFace embeddings and FAISS vectorstore
|
| 56 |
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
|
@@ -69,14 +59,14 @@ def retrieve_documents(query: str) -> List[Dict[str, str]]:
|
|
| 69 |
results = retriever_tool.get_relevant_documents(query)
|
| 70 |
return [{"content": doc.page_content} for doc in results]
|
| 71 |
|
| 72 |
-
# Define the
|
| 73 |
faiss_tool = Tool(
|
| 74 |
name="retrieve_documents",
|
| 75 |
description="Retrieve documents from FAISS vectorstore.",
|
| 76 |
func=retrieve_documents,
|
| 77 |
)
|
| 78 |
|
| 79 |
-
#
|
| 80 |
arxiv_wrapper = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=250)
|
| 81 |
arxiv_tool = ArxivQueryRun(api_wrapper=arxiv_wrapper)
|
| 82 |
|
|
@@ -84,16 +74,9 @@ wiki_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=250)
|
|
| 84 |
wiki_tool = WikipediaQueryRun(api_wrapper=wiki_wrapper)
|
| 85 |
|
| 86 |
search_tool = DuckDuckGoSearchRun(name="Search")
|
|
|
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
tools = [
|
| 90 |
-
arxiv_tool,
|
| 91 |
-
wiki_tool,
|
| 92 |
-
search_tool,
|
| 93 |
-
faiss_tool
|
| 94 |
-
]
|
| 95 |
-
|
| 96 |
-
# Set up the agent with prompt and LLM model
|
| 97 |
prompt = hub.pull("hwchase17/openai-functions-agent")
|
| 98 |
llm = ChatGroq(model="Gemma2-9B-It", api_key=groq_api_key, streaming=True)
|
| 99 |
agent = create_openai_tools_agent(llm, tools, prompt)
|
|
@@ -112,7 +95,7 @@ conversation_history = "\n".join(
|
|
| 112 |
[f"{msg['role']}: {msg['content']}" for msg in st.session_state['messages']]
|
| 113 |
)
|
| 114 |
|
| 115 |
-
# CSS
|
| 116 |
st.markdown("""
|
| 117 |
<style>
|
| 118 |
.fixed-bottom-input-container {
|
|
@@ -131,21 +114,20 @@ st.markdown("""
|
|
| 131 |
</style>
|
| 132 |
""", unsafe_allow_html=True)
|
| 133 |
|
| 134 |
-
#
|
| 135 |
user_input = st.text_input("Type your message here...", key="user_input", label_visibility="collapsed")
|
| 136 |
|
| 137 |
-
# If there's input, process it
|
| 138 |
if user_input:
|
| 139 |
condensed_question = condense_query(llm, user_input, conversation_history)
|
| 140 |
response = agent_executor.invoke({"input": condensed_question})
|
| 141 |
-
|
| 142 |
-
#
|
| 143 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 144 |
st.session_state.messages.append({"role": "assistant", "content": response.get("output", "")})
|
| 145 |
|
| 146 |
-
# Display the
|
| 147 |
st.chat_message("user").write(user_input)
|
| 148 |
st.chat_message("assistant").write(response.get("output", ""))
|
| 149 |
|
| 150 |
-
# Clear the input
|
| 151 |
-
st.
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
+
import logging
|
| 4 |
from langchain_groq import ChatGroq
|
| 5 |
from langchain_community.tools import ArxivQueryRun, WikipediaQueryRun, DuckDuckGoSearchRun
|
| 6 |
from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
|
| 7 |
from langchain import hub
|
| 8 |
+
from langchain.agents import create_openai_tools_agent, AgentExecutor
|
|
|
|
| 9 |
from langchain.prompts import PromptTemplate
|
| 10 |
from langchain_community.vectorstores import FAISS
|
| 11 |
from langchain_huggingface import HuggingFaceEmbeddings
|
| 12 |
from langchain.tools import Tool
|
| 13 |
from pydantic import BaseModel, Field
|
| 14 |
from typing import List, Dict
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Configure logging
|
| 17 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
|
|
|
| 18 |
logging.info("Streamlit app started")
|
|
|
|
| 19 |
|
| 20 |
+
# Load the API key
|
| 21 |
+
groq_api_key = "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" #os.getenv("GROQ_API_KEY")
|
| 22 |
+
if not groq_api_key:
|
| 23 |
+
logging.error("GROQ API key is missing!")
|
| 24 |
+
|
| 25 |
# Define a query condensing template
|
| 26 |
condense_prompt_template = PromptTemplate(
|
| 27 |
input_variables=["original_question", "conversation_history"],
|
| 28 |
template="""
|
| 29 |
Given the conversation history below, condense the user's query into a clear and specific question.
|
|
|
|
| 30 |
Conversation History:
|
| 31 |
{conversation_history}
|
|
|
|
| 32 |
Original Question:
|
| 33 |
{original_question}
|
|
|
|
| 34 |
Condensed Question:"""
|
| 35 |
)
|
| 36 |
|
| 37 |
def condense_query(llm_model, original_question, conversation_history):
|
|
|
|
| 38 |
prompt = condense_prompt_template.format(
|
| 39 |
original_question=original_question,
|
| 40 |
conversation_history=conversation_history
|
| 41 |
)
|
|
|
|
| 42 |
resp = llm_model.predict(prompt)
|
| 43 |
+
return resp.strip()
|
| 44 |
|
| 45 |
# Initialize HuggingFace embeddings and FAISS vectorstore
|
| 46 |
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
|
|
|
| 59 |
results = retriever_tool.get_relevant_documents(query)
|
| 60 |
return [{"content": doc.page_content} for doc in results]
|
| 61 |
|
| 62 |
+
# Define the FAISS tool
|
| 63 |
faiss_tool = Tool(
|
| 64 |
name="retrieve_documents",
|
| 65 |
description="Retrieve documents from FAISS vectorstore.",
|
| 66 |
func=retrieve_documents,
|
| 67 |
)
|
| 68 |
|
| 69 |
+
# Initialize tools
|
| 70 |
arxiv_wrapper = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=250)
|
| 71 |
arxiv_tool = ArxivQueryRun(api_wrapper=arxiv_wrapper)
|
| 72 |
|
|
|
|
| 74 |
wiki_tool = WikipediaQueryRun(api_wrapper=wiki_wrapper)
|
| 75 |
|
| 76 |
search_tool = DuckDuckGoSearchRun(name="Search")
|
| 77 |
+
tools = [arxiv_tool, wiki_tool, search_tool, faiss_tool]
|
| 78 |
|
| 79 |
+
# Set up the agent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
prompt = hub.pull("hwchase17/openai-functions-agent")
|
| 81 |
llm = ChatGroq(model="Gemma2-9B-It", api_key=groq_api_key, streaming=True)
|
| 82 |
agent = create_openai_tools_agent(llm, tools, prompt)
|
|
|
|
| 95 |
[f"{msg['role']}: {msg['content']}" for msg in st.session_state['messages']]
|
| 96 |
)
|
| 97 |
|
| 98 |
+
# CSS for input box at bottom of screen
|
| 99 |
st.markdown("""
|
| 100 |
<style>
|
| 101 |
.fixed-bottom-input-container {
|
|
|
|
| 114 |
</style>
|
| 115 |
""", unsafe_allow_html=True)
|
| 116 |
|
| 117 |
+
# Chat input and processing
|
| 118 |
user_input = st.text_input("Type your message here...", key="user_input", label_visibility="collapsed")
|
| 119 |
|
|
|
|
| 120 |
if user_input:
|
| 121 |
condensed_question = condense_query(llm, user_input, conversation_history)
|
| 122 |
response = agent_executor.invoke({"input": condensed_question})
|
| 123 |
+
|
| 124 |
+
# Update chat history
|
| 125 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 126 |
st.session_state.messages.append({"role": "assistant", "content": response.get("output", "")})
|
| 127 |
|
| 128 |
+
# Display the response
|
| 129 |
st.chat_message("user").write(user_input)
|
| 130 |
st.chat_message("assistant").write(response.get("output", ""))
|
| 131 |
|
| 132 |
+
# Clear the input field
|
| 133 |
+
st.session_state["user_input"] = ""
|