Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,15 +2,17 @@ import logging
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
from dotenv import load_dotenv
|
|
|
|
| 5 |
import openai
|
| 6 |
-
import streamlit as st
|
| 7 |
from langchain_openai import ChatOpenAI
|
| 8 |
from langchain_community.vectorstores import FAISS
|
| 9 |
from langchain_openai import OpenAIEmbeddings
|
| 10 |
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 11 |
from langchain.agents import tool, AgentExecutor
|
| 12 |
from langchain.agents.output_parsers.openai_tools import OpenAIToolsAgentOutputParser
|
| 13 |
-
from langchain.agents.format_scratchpad.openai_tools import
|
|
|
|
|
|
|
| 14 |
from langchain_core.messages import AIMessage, HumanMessage
|
| 15 |
from langchain_community.document_loaders import TextLoader
|
| 16 |
from langchain_text_splitters import CharacterTextSplitter
|
|
@@ -41,6 +43,7 @@ except Exception as e:
|
|
| 41 |
logger.error(f"Error initializing OpenAI client: {e}")
|
| 42 |
raise e
|
| 43 |
|
|
|
|
| 44 |
# Load knowledge base
|
| 45 |
def load_knowledge_base():
|
| 46 |
try:
|
|
@@ -53,6 +56,7 @@ def load_knowledge_base():
|
|
| 53 |
logger.error(f"Error loading knowledge base: {e}")
|
| 54 |
raise e
|
| 55 |
|
|
|
|
| 56 |
knowledge_base = load_knowledge_base()
|
| 57 |
|
| 58 |
# Initialize embeddings and FAISS index
|
|
@@ -63,6 +67,7 @@ except Exception as e:
|
|
| 63 |
logger.error(f"Error initializing FAISS index: {e}")
|
| 64 |
raise e
|
| 65 |
|
|
|
|
| 66 |
# Define search function for knowledge base
|
| 67 |
def search_knowledge_base(query):
|
| 68 |
try:
|
|
@@ -72,6 +77,7 @@ def search_knowledge_base(query):
|
|
| 72 |
logger.error(f"Error searching knowledge base: {e}")
|
| 73 |
return ["Error occurred during knowledge base search"]
|
| 74 |
|
|
|
|
| 75 |
# SERPER API Google Search function
|
| 76 |
def google_search(query):
|
| 77 |
try:
|
|
@@ -91,6 +97,7 @@ def google_search(query):
|
|
| 91 |
logger.error(f"General Error: {e}")
|
| 92 |
return ["Error occurred during Google search"]
|
| 93 |
|
|
|
|
| 94 |
# RAG response function
|
| 95 |
def rag_response(query):
|
| 96 |
try:
|
|
@@ -104,16 +111,36 @@ def rag_response(query):
|
|
| 104 |
logger.error(f"Error generating RAG response: {e}")
|
| 105 |
return "Error occurred during RAG response generation"
|
| 106 |
|
|
|
|
| 107 |
# Define tools using LangChain's `tool` decorator
|
| 108 |
@tool
|
| 109 |
def knowledge_base_tool(query: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
return rag_response(query)
|
| 111 |
|
|
|
|
| 112 |
@tool
|
| 113 |
def google_search_tool(query: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
return google_search(query)
|
| 115 |
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
# Create the prompt template
|
| 119 |
prompt_message = """
|
|
@@ -133,7 +160,7 @@ prompt_template = ChatPromptTemplate.from_messages(
|
|
| 133 |
|
| 134 |
# Create Langchain Agent with specific model and temperature
|
| 135 |
try:
|
| 136 |
-
llm = ChatOpenAI(model="gpt-4o", temperature=0.5)
|
| 137 |
llm_with_tools = llm.bind_tools(tools)
|
| 138 |
except Exception as e:
|
| 139 |
logger.error(f"Error creating Langchain Agent: {e}")
|
|
@@ -161,6 +188,7 @@ except Exception as e:
|
|
| 161 |
# Initialize chat history
|
| 162 |
chat_history = []
|
| 163 |
|
|
|
|
| 164 |
def chatbot_response(message, history):
|
| 165 |
try:
|
| 166 |
# Generate response using the agent executor
|
|
@@ -179,12 +207,39 @@ def chatbot_response(message, history):
|
|
| 179 |
logger.error(f"Error generating chatbot response: {e}")
|
| 180 |
return "Error occurred during response generation"
|
| 181 |
|
| 182 |
-
# Streamlit application
|
| 183 |
|
| 184 |
-
#
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
+
import gradio as gr
|
| 6 |
import openai
|
|
|
|
| 7 |
from langchain_openai import ChatOpenAI
|
| 8 |
from langchain_community.vectorstores import FAISS
|
| 9 |
from langchain_openai import OpenAIEmbeddings
|
| 10 |
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 11 |
from langchain.agents import tool, AgentExecutor
|
| 12 |
from langchain.agents.output_parsers.openai_tools import OpenAIToolsAgentOutputParser
|
| 13 |
+
from langchain.agents.format_scratchpad.openai_tools import (
|
| 14 |
+
format_to_openai_tool_messages,
|
| 15 |
+
)
|
| 16 |
from langchain_core.messages import AIMessage, HumanMessage
|
| 17 |
from langchain_community.document_loaders import TextLoader
|
| 18 |
from langchain_text_splitters import CharacterTextSplitter
|
|
|
|
| 43 |
logger.error(f"Error initializing OpenAI client: {e}")
|
| 44 |
raise e
|
| 45 |
|
| 46 |
+
|
| 47 |
# Load knowledge base
|
| 48 |
def load_knowledge_base():
|
| 49 |
try:
|
|
|
|
| 56 |
logger.error(f"Error loading knowledge base: {e}")
|
| 57 |
raise e
|
| 58 |
|
| 59 |
+
|
| 60 |
knowledge_base = load_knowledge_base()
|
| 61 |
|
| 62 |
# Initialize embeddings and FAISS index
|
|
|
|
| 67 |
logger.error(f"Error initializing FAISS index: {e}")
|
| 68 |
raise e
|
| 69 |
|
| 70 |
+
|
| 71 |
# Define search function for knowledge base
|
| 72 |
def search_knowledge_base(query):
|
| 73 |
try:
|
|
|
|
| 77 |
logger.error(f"Error searching knowledge base: {e}")
|
| 78 |
return ["Error occurred during knowledge base search"]
|
| 79 |
|
| 80 |
+
|
| 81 |
# SERPER API Google Search function
|
| 82 |
def google_search(query):
|
| 83 |
try:
|
|
|
|
| 97 |
logger.error(f"General Error: {e}")
|
| 98 |
return ["Error occurred during Google search"]
|
| 99 |
|
| 100 |
+
|
| 101 |
# RAG response function
|
| 102 |
def rag_response(query):
|
| 103 |
try:
|
|
|
|
| 111 |
logger.error(f"Error generating RAG response: {e}")
|
| 112 |
return "Error occurred during RAG response generation"
|
| 113 |
|
| 114 |
+
|
| 115 |
# Define tools using LangChain's `tool` decorator
|
| 116 |
@tool
|
| 117 |
def knowledge_base_tool(query: str):
|
| 118 |
+
"""
|
| 119 |
+
Tool function to query the knowledge base and retrieve a response.
|
| 120 |
+
Args:
|
| 121 |
+
query (str): The query to search the knowledge base.
|
| 122 |
+
Returns:
|
| 123 |
+
str: The response retrieved from the knowledge base.
|
| 124 |
+
"""
|
| 125 |
return rag_response(query)
|
| 126 |
|
| 127 |
+
|
| 128 |
@tool
|
| 129 |
def google_search_tool(query: str):
|
| 130 |
+
"""
|
| 131 |
+
Tool function to perform a Google search using the SERPER API.
|
| 132 |
+
Args:
|
| 133 |
+
query (str): The query to search on Google.
|
| 134 |
+
Returns:
|
| 135 |
+
list: List of snippets extracted from search results.
|
| 136 |
+
"""
|
| 137 |
return google_search(query)
|
| 138 |
|
| 139 |
+
|
| 140 |
+
tools = [
|
| 141 |
+
knowledge_base_tool,
|
| 142 |
+
google_search_tool,
|
| 143 |
+
]
|
| 144 |
|
| 145 |
# Create the prompt template
|
| 146 |
prompt_message = """
|
|
|
|
| 160 |
|
| 161 |
# Create Langchain Agent with specific model and temperature
|
| 162 |
try:
|
| 163 |
+
llm = ChatOpenAI(model="gpt-4o", temperature=0.5) # Set temperature to 0.5
|
| 164 |
llm_with_tools = llm.bind_tools(tools)
|
| 165 |
except Exception as e:
|
| 166 |
logger.error(f"Error creating Langchain Agent: {e}")
|
|
|
|
| 188 |
# Initialize chat history
|
| 189 |
chat_history = []
|
| 190 |
|
| 191 |
+
|
| 192 |
def chatbot_response(message, history):
|
| 193 |
try:
|
| 194 |
# Generate response using the agent executor
|
|
|
|
| 207 |
logger.error(f"Error generating chatbot response: {e}")
|
| 208 |
return "Error occurred during response generation"
|
| 209 |
|
|
|
|
| 210 |
|
| 211 |
+
# # Define CSS for Gradio interface
|
| 212 |
+
# CSS = """
|
| 213 |
+
# .contain { display: flex; flex-direction: column; height: 100vh; }
|
| 214 |
+
# #component-0 { height: 90%; }
|
| 215 |
+
# """
|
| 216 |
|
| 217 |
+
# # Gradio interface
|
| 218 |
+
# with gr.Blocks(css=CSS) as demo:
|
| 219 |
+
|
| 220 |
+
submit_button = gr.Button("Submit")
|
| 221 |
+
|
| 222 |
+
bot = gr.Chatbot()
|
| 223 |
+
|
| 224 |
+
with gr.Blocks() as demo:
|
| 225 |
+
gr.Markdown(
|
| 226 |
+
"<span style='font-size:20px; font-weight:bold;'>Instant Insight-2-Action</span>",
|
| 227 |
+
visible=True,
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
chatbot = gr.ChatInterface(
|
| 231 |
+
fn=chatbot_response,
|
| 232 |
+
stop_btn=None,
|
| 233 |
+
retry_btn=None,
|
| 234 |
+
undo_btn=None,
|
| 235 |
+
clear_btn=None,
|
| 236 |
+
submit_btn=submit_button,
|
| 237 |
+
chatbot=bot,
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
+
# Launch the Gradio app
|
| 241 |
+
try:
|
| 242 |
+
demo.launch(server_name="0.0.0.0")
|
| 243 |
+
except Exception as e:
|
| 244 |
+
logger.error(f"Error launching Gradio app: {e}")
|
| 245 |
+
raise e
|