Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from langchain.agents import create_react_agent, AgentExecutor
|
| 3 |
+
from langchain.tools.tavily_search import TavilySearchResults
|
| 4 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
| 5 |
+
from langchain.memory import ConversationBufferMemory
|
| 6 |
+
from langchain.prompts import PromptTemplate
|
| 7 |
+
from langchain.chains import LLMChain
|
| 8 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# Set page config
|
| 12 |
+
st.set_page_config(page_title="Company Info Agent", layout="wide")
|
| 13 |
+
|
| 14 |
+
st.title("π Business Intelligence Assistant")
|
| 15 |
+
st.markdown(
|
| 16 |
+
"Ask anything about companies: *stock, revenue, growth, acquisitions, news*, etc."
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# --- Your custom prompt ---
|
| 21 |
+
prompt = PromptTemplate(
|
| 22 |
+
input_variables=["input", "agent_scratchpad", "tool_names", "tools"],
|
| 23 |
+
template="""You are a smart and reliable business assistant.
|
| 24 |
+
|
| 25 |
+
You help users by answering questions about *any company* (public or private), including their:
|
| 26 |
+
- Stock performance
|
| 27 |
+
- Revenue
|
| 28 |
+
- Funding
|
| 29 |
+
- Acquisitions
|
| 30 |
+
- Market valuation
|
| 31 |
+
- Recent news
|
| 32 |
+
- Business activities
|
| 33 |
+
|
| 34 |
+
You can search online to gather accurate and current data. You must cite sources (like URLs or page titles) in your final answer.
|
| 35 |
+
|
| 36 |
+
You have access to the following tools:
|
| 37 |
+
|
| 38 |
+
{tools}
|
| 39 |
+
|
| 40 |
+
Use the following format:
|
| 41 |
+
|
| 42 |
+
Question: the input question you must answer
|
| 43 |
+
Thought: think step-by-step about what to do
|
| 44 |
+
Action: the action to take, should be one of [{tool_names}]
|
| 45 |
+
Action Input: the input to the action
|
| 46 |
+
Observation: the result of the action
|
| 47 |
+
|
| 48 |
+
(Repeat the above Thought/Action/Action Input/Observation format only *2 times*)
|
| 49 |
+
|
| 50 |
+
Then:
|
| 51 |
+
|
| 52 |
+
Thought: I now know the final answer
|
| 53 |
+
Final Answer: provide a detailed answer, *include helpful insights and links to your sources*.
|
| 54 |
+
If no specific info is available, say so politely and try to offer related context.
|
| 55 |
+
|
| 56 |
+
---
|
| 57 |
+
|
| 58 |
+
Begin!
|
| 59 |
+
|
| 60 |
+
Question: {input}
|
| 61 |
+
Thought:{agent_scratchpad}
|
| 62 |
+
"""
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
from re import search
|
| 66 |
+
# search_tool = TavilySearchResults(
|
| 67 |
+
# tavily_api_key = "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",
|
| 68 |
+
# depth = 'advanced',
|
| 69 |
+
# )
|
| 70 |
+
search_tool = DuckDuckGoSearchRun()
|
| 71 |
+
|
| 72 |
+
tools = [search_tool]
|
| 73 |
+
|
| 74 |
+
# --- Initializing the LLM
|
| 75 |
+
llm = ChatGoogleGenerativeAI(
|
| 76 |
+
api_key = "AIzaSyBYBRKYkgZm5OYM1XQYWlrz9psaS3t65Cg",
|
| 77 |
+
model = "gemini-2.0-flash"
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# User input
|
| 81 |
+
user_input = st.text_input("Enter your question:")
|
| 82 |
+
|
| 83 |
+
agent = create_react_agent(llm=llm, tools=tools, prompt=prompt)
|
| 84 |
+
|
| 85 |
+
# Create executor
|
| 86 |
+
agent_executor = AgentExecutor(
|
| 87 |
+
agent=agent,
|
| 88 |
+
tools=tools,
|
| 89 |
+
verbose=True,
|
| 90 |
+
handle_parsing_errors=True,
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
# Run on user input
|
| 94 |
+
if user_input:
|
| 95 |
+
with st.spinner("π Thinking..."):
|
| 96 |
+
response = agent_executor.invoke({"input": user_input})
|
| 97 |
+
st.markdown("### π§ Answer:")
|
| 98 |
+
st.write(response["output"])
|