File size: 2,400 Bytes
7303dec
 
 
11f1ed0
7303dec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import streamlit as st
from dotenv import load_dotenv
from groq import Agent
from phi.model.groq import Groq
from phi.tools.duckduckgo import DuckDuckGo
from phi.tools.yfinance import YFinanceTools

# Load environment variables
load_dotenv()

# Retrieve API keys from the environment
deepseek_api_key = os.getenv("GROQ_DEEPSEEK_API_KEY")
qwen_api_key = os.getenv("GROQ_QWEN_API_KEY")

# Debugging API key loading
if not deepseek_api_key or not qwen_api_key:
    st.error("Missing API keys. Please set them in Hugging Face Secrets.")
    st.stop()

# Define Web Agent
web_agent = Agent(
    name="Web Agent",
    model=Groq(id="qwen-2.5-coder-32b", api_key=qwen_api_key),
    tools=[DuckDuckGo()],
    instructions=["Always include sources"],
    show_tool_calls=True,
    markdown=True,
)

# Define Finance Agent
finance_agent = Agent(
    name="Finance Agent",
    role="Get financial data",
    model=Groq(id="qwen-2.5-coder-32b", api_key=qwen_api_key),
    tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True)],
    instructions=["Use tables to display data"],
    show_tool_calls=True,
    markdown=True,
)

# Create agent team
agent_team = Agent(
    model=Groq(id="deepseek-r1-distill-llama-70b", api_key=deepseek_api_key),
    team=[web_agent, finance_agent],
    instructions=["Always include sources", "Use tables to display data"],
    show_tool_calls=True,
    markdown=True,
)

# Streamlit UI
st.set_page_config(page_title="AI Chat Assistant", layout="wide")
st.title("🤖 AI Chat Assistant")

# User input
if prompt := st.chat_input("Ask me anything..."):
    with st.chat_message("user"):
        st.markdown(prompt)

    with st.chat_message("assistant"):
        response_container = st.empty()
        response_text = ""

        # Check available response method
        if hasattr(agent_team, "respond") and callable(agent_team.respond):
            try:
                response_text = agent_team.respond(prompt) or "No response received."
                response_container.markdown(response_text)
            except Exception as e:
                error_message = f"Error: {str(e)}"
                st.error(error_message)
                response_text = error_message
        else:
            error_message = "Error: Agent does not support responses."
            st.error(error_message)
            response_text = error_message