d221 commited on
Commit
cf93cda
·
verified ·
1 Parent(s): 53b74af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -69
app.py CHANGED
@@ -1,88 +1,110 @@
1
-
2
  import os
3
  import groq
4
- import phi
5
- import phi.api
6
-
7
  from dotenv import load_dotenv
8
- from phi.agent import Agent
9
- from phi.model.groq import Groq
10
- from phi.tools.yfinance import YFinanceTools
11
- from phi.tools.youtube_tools import YouTubeTools
12
- from phi.tools.googlesearch import GoogleSearch
13
- from phi.playground import Playground
 
 
 
 
14
 
15
- # 1. Load environment variables
16
- load_dotenv()
17
 
18
- phi.api = os.getenv("PHI_API_KEY")
19
- groq_api_key = os.getenv("GROQ_API_KEY")
20
 
21
- # 2. Create a groq client
22
- groq_client = groq.Client(api_key=groq_api_key)
23
 
24
- # 3. Define Agents
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- # Websearch Agent
27
  websearch_agent = Agent(
28
- name='websearch_agent',
29
- role="Search the web for financial information including all the latest news too.",
30
- model=Groq(id="llama-3.3-70b-versatile"),
31
- tools=[GoogleSearch()],
32
- instructions=["Always include the sources for the information in APA format."],
33
- markdown=True,
34
- # debug_mode=True,
35
  )
36
 
37
- # Youtube Agent
38
  youtube_agent = Agent(
39
- name="YouTube Agent",
40
- role="You are a YouTube agent that has the special skill of understanding YouTube videos and answering questions about them. You can only answer if the duration is less than 22 minutes.",
41
- model=Groq(id="llama-3.3-70b-versatile"),
42
- tools=[YouTubeTools()],
43
- instructions=[
44
- "1. When the user asks about a video, confirm that they have provided a valid YouTube URL. If not, ask them for it.",
45
- "2. Using a video URL, get the video data using the get_youtube_video_data tool. Using the video data, get the video captions using the get_youtube_video_captions tool.",
46
- "3. Using the data and captions, answer the user questions in an engaging and thoughtful manner and only focus on the important information.",
47
- "4. If you cannot find the information, let the user know by asking for more details, and don't hallucinate.",
48
- "5. Keep your answers concise and engaging."
49
- ],
50
- markdown=True,
51
- read_chat_history=True,
52
- # debug_mode=True,
53
  )
54
 
55
- # Financial Agent
56
  finance_agent = Agent(
57
- name="Finance AI Agent",
58
- model=Groq(id="llama-3.3-70b-versatile"),
59
- tools=[
60
- YFinanceTools(
61
- stock_price=True,
62
- analyst_recommendations=True,
63
- stock_fundamentals=True,
64
- company_news=True
65
- ),
66
- ],
67
- description="You are an investment analyst that researches stock prices, analyst recommendations, and stock fundamentals.",
68
- instructions=["Format your response using markdown and use tables to display data where possible."],
69
- markdown=True,
70
- # debug_mode=True,
71
  )
72
 
73
- # Multi-Model Agent
74
- MultiModelAgent = Agent(
75
- team=[websearch_agent, finance_agent, youtube_agent],
76
- model=Groq(id="llama-3.3-70b-versatile"),
77
- name="Multi-Model Agent",
78
- instructions=["Always include sources and use tables to display data where possible."],
79
- show_tool_calls=True,
80
- markdown=True,
81
- # debug_mode=True,
82
- )
83
 
84
- # 4. Create the Playground app with all agents
85
- playground = Playground(agents=[websearch_agent, finance_agent, MultiModelAgent, youtube_agent])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
- # 5. The Hugging Face Space will look for 'app' to serve
88
- app = playground.get_app()
 
 
1
  import os
2
  import groq
 
 
 
3
  from dotenv import load_dotenv
4
+ import gradio as gr
5
+
6
+
7
+ # 1. Environment and Groq Client Setup
8
+
9
+ load_dotenv() # Load environment variables from .env
10
+
11
+ # Retrieve the API key and model identifier from environment variables
12
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
13
+ GROQ_MODEL_ID = os.getenv("GROQ_MODEL_ID", "llama-3.3-70b-versatile")
14
 
15
+ # Initialize the Groq client (adjust this based on your actual Groq API)
16
+ groq_client = groq.Client(api_key=GROQ_API_KEY)
17
 
 
 
18
 
19
+ # 2. Define a Simple Agent Class Using Groq
 
20
 
21
+ class Agent:
22
+ def __init__(self, name, role, instructions):
23
+ self.name = name
24
+ self.role = role
25
+ self.instructions = instructions
26
+
27
+ def build_prompt(self, query):
28
+ # Construct a prompt that provides context for the agent
29
+ prompt = (
30
+ f"Agent Name: {self.name}\n"
31
+ f"Role: {self.role}\n"
32
+ f"Instructions: {self.instructions}\n"
33
+ f"User Query: {query}\n"
34
+ "Response:"
35
+ )
36
+ return prompt
37
+
38
+ def respond(self, query):
39
+ prompt = self.build_prompt(query)
40
+ # Call Groq Cloud API to generate a response (adjust parameters as needed)
41
+ response = groq_client.generate(
42
+ model_id=GROQ_MODEL_ID,
43
+ prompt=prompt,
44
+ max_tokens=200
45
+ )
46
+ # Assume the API returns a dict with a 'text' field for the generated response
47
+ return response.get("text", "")
48
+
49
+
50
+ # 3. Define Specific Agents
51
 
 
52
  websearch_agent = Agent(
53
+ name="WebSearch Agent",
54
+ role="Searches the web for financial information and the latest news.",
55
+ instructions="Return results with sources in APA format."
 
 
 
 
56
  )
57
 
 
58
  youtube_agent = Agent(
59
+ name="YouTube Agent",
60
+ role="Analyzes YouTube videos that are less than 22 minutes in duration.",
61
+ instructions="Provide concise and engaging answers based on the video content."
 
 
 
 
 
 
 
 
 
 
 
62
  )
63
 
 
64
  finance_agent = Agent(
65
+ name="Finance Agent",
66
+ role="Analyzes stock prices, analyst recommendations, and fundamentals.",
67
+ instructions="Format your response using markdown tables where applicable."
 
 
 
 
 
 
 
 
 
 
 
68
  )
69
 
 
 
 
 
 
 
 
 
 
 
70
 
71
+ # 4. Build a Multi-Agent Coordinator
72
+
73
+ class MultiAgent:
74
+ def __init__(self, agents):
75
+ self.agents = agents
76
+
77
+ def handle_query(self, query):
78
+ # For the given query, ask each agent and collect their responses
79
+ responses = {}
80
+ for agent in self.agents:
81
+ responses[agent.name] = agent.respond(query)
82
+ return responses
83
+
84
+ # Create a multi-agent system with the defined agents
85
+ multi_agent = MultiAgent([websearch_agent, youtube_agent, finance_agent])
86
+
87
+
88
+ # 5. Gradio Chatbot Front End Function
89
+
90
+ def chat_response(user_query):
91
+ responses = multi_agent.handle_query(user_query)
92
+ # Format the responses into a single markdown string for display
93
+ result = ""
94
+ for agent_name, response in responses.items():
95
+ result += f"**{agent_name}**:\n{response}\n\n"
96
+ return result
97
+
98
+
99
+ # 6. Create the Gradio Interface
100
+
101
+ demo = gr.Interface(
102
+ fn=chat_response,
103
+ inputs=gr.Textbox(placeholder="Enter your message here...", lines=2),
104
+ outputs="markdown",
105
+ title="Groq Agent Chatbot",
106
+ description="A chatbot powered by Groq Cloud for GPT inference with multiple agents."
107
+ )
108
 
109
+ if __name__ == "__main__":
110
+ demo.launch()