File size: 7,950 Bytes
6b50ab8
 
 
28c7f90
 
6b50ab8
 
ee08cfb
6b50ab8
ee08cfb
8e5962b
 
 
 
ee08cfb
6b50ab8
 
 
 
d4d9713
6b50ab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee08cfb
6b50ab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee08cfb
6b50ab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee08cfb
 
6b50ab8
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
---
title: AI Chatbot
emoji: πŸ€–
colorFrom: pink
colorTo: yellow
sdk: docker
sdk_version: "0.0.1"
app_file: app.py
pinned: false
license: mit
tags:
  - text-generation
  - agentic-ai
  - openai-sdk
short_description: An Experimental Agentic Chatbot (uses OpenAI Agent SDK)
---

# AI Chatbot

This is an experimental chatbot for chatting with AI. It is equipped with agents & tools to give you realtime data from the web. It uses **OpenAI - SDK** and **OpenAI - Agents**...

## Features
- Predefined prompts for quick analysis
- Chat interface with AI responses
- Enter key support and responsive design
- Latest messages appear on top

## Usage
1. Type a message or select a predefined prompt
2. Press **Enter** or click **Send**
3. AI responses appear instantly in the chat interface

## Supported APIs
- OpenAI
- Google
- GROQ
- SERPER
- News API

## Notes
- Make sure your API keys are configured in the Space secrets
- Built using Streamlit and deployed as a Docker Space

## References

https://openai.github.io/openai-agents-python/

https://github.com/openai/openai-agents-python/tree/main/examples/mcp

## Project Folder Structure

```
chatbot/
β”œβ”€β”€ app.py                    # Main Streamlit chatbot interface
β”œβ”€β”€ appagents/
β”‚   β”œβ”€β”€ __init__.py               # Package initialization
β”‚   β”œβ”€β”€ OrchestratorAgent.py      # Main orchestrator - coordinates all agents
β”‚   β”œβ”€β”€ FinancialAgent.py         # Financial data and analysis agent
β”‚   β”œβ”€β”€ NewsAgent.py              # News retrieval and summarization agent
β”‚   β”œβ”€β”€ SearchAgent.py            # General web search agent
β”‚   └── InputValidationAgent.py   # Input validation and sanitization agent
β”œβ”€β”€ core/
β”‚   β”œβ”€β”€ __init__.py               # Package initialization
β”‚   └── logger.py                 # Centralized logging configuration
β”œβ”€β”€ tools/
β”‚   β”œβ”€β”€ __init__.py               # Package initialization
β”‚   β”œβ”€β”€ google_tools.py           # Google search API wrapper
β”‚   β”œβ”€β”€ yahoo_tools.py            # Yahoo Finance API wrapper
β”‚   β”œβ”€β”€ news_tools.py             # News API wrapper
β”‚   └── time_tools.py             # Time-related utility functions
β”œβ”€β”€ prompts/
β”‚   β”œβ”€β”€ economic_news.txt         # Prompt for economic news analysis
β”‚   β”œβ”€β”€ market_sentiment.txt      # Prompt for market sentiment analysis
β”‚   β”œβ”€β”€ news_headlines.txt        # Prompt for news headline summarization
β”‚   β”œβ”€β”€ trade_recommendation.txt  # Prompt for trade recommendations
β”‚   └── upcoming_earnings.txt     # Prompt for upcoming earnings alerts
β”œβ”€β”€ Dockerfile                     # Docker configuration for container deployment
β”œβ”€β”€ pyproject.toml                 # Project metadata and dependencies (copied from root)
β”œβ”€β”€ uv.lock                        # Locked dependency versions (copied from root)
β”œβ”€β”€ README.md                      # Project documentation
└── run.py                         # Script to run the application locally
```

## File Descriptions

### UI Layer
- **app.py** - Main Streamlit chatbot interface that provides:
  - Chat message display with user and AI messages
  - Text input for user queries
  - Predefined prompt buttons for quick analysis
  - Real-time AI responses
  - Support for Enter key submission
  - Responsive design with latest messages appearing first

### Agents (`appagents/`)
- **OrchestratorAgent.py** - Main orchestrator that:
  - Coordinates communication between all specialized agents
  - Routes user queries to appropriate agents
  - Manages conversation flow and context
  - Integrates tool responses

- **FinancialAgent.py** - Financial data and analysis:
  - Retrieves stock prices and financial metrics
  - Performs financial analysis using Yahoo Finance API
  - Provides market insights and recommendations
  - Integrates with yahoo_tools for data fetching

- **NewsAgent.py** - News retrieval and summarization:
  - Fetches latest news articles
  - Summarizes news content
  - Integrates with News API for real-time updates
  - Provides news-based market insights

- **SearchAgent.py** - General web search:
  - Performs web searches for general queries
  - Integrates with Google Search / Serper API
  - Returns relevant search results
  - Supports multi-source data gathering

- **InputValidationAgent.py** - Input validation:
  - Sanitizes user input
  - Validates query format and content
  - Prevents malicious inputs
  - Ensures appropriate content

### Core Utilities (`core/`)
- **logger.py** - Centralized logging configuration:
  - Provides consistent logging across agents
  - Handles different log levels
  - Formats log messages for clarity

### Tools (`tools/`)
- **google_tools.py** - Google Search API wrapper:
  - Executes web searches via Google Search / Serper API
  - Parses and returns search results
  - Handles API authentication

- **yahoo_tools.py** - Yahoo Finance API integration:
  - Retrieves stock price data
  - Fetches financial metrics and indicators
  - Provides historical price data
  - Returns earnings information

- **news_tools.py** - News API integration:
  - Fetches latest news articles
  - Filters news by category and keywords
  - Returns news headlines and summaries
  - Provides market-related news feeds

- **time_tools.py** - Time utility functions:
  - Provides current time information
  - Formats timestamps
  - Handles timezone conversions

### Prompts (`prompts/`)
Predefined prompts for specialized analysis:
- **economic_news.txt** - Analyzes economic news and implications
- **market_sentiment.txt** - Analyzes market sentiment trends
- **news_headlines.txt** - Summarizes and explains news headlines
- **trade_recommendation.txt** - Provides trading recommendations
- **upcoming_earnings.txt** - Alerts about upcoming earnings reports

### Configuration Files
- **Dockerfile** - Container deployment:
  - Builds Docker image with Python 3.12
  - Installs dependencies using `uv`
  - Sets up Streamlit server on port 8501
  - Configures PYTHONPATH for module imports

- **pyproject.toml** - Project metadata:
  - Package name: "agents"
  - Python version requirement: 3.12
  - Lists all dependencies (OpenAI, LangChain, Streamlit, etc.)

- **uv.lock** - Dependency lock file:
  - Ensures reproducible builds
  - Pins exact versions of all dependencies

## Key Technologies

| Component | Technology | Purpose |
|-----------|-----------|---------|
| LLM Framework | OpenAI Agents | Multi-agent orchestration |
| Chat Interface | Streamlit | User interaction and display |
| Web Search | Google Search / Serper API | Web search results |
| Financial Data | Yahoo Finance API | Stock prices and metrics |
| News Data | News API | Latest news articles |
| Async Operations | AsyncIO | Parallel agent execution |
| Dependencies | UV | Fast Python package management |
| Containerization | Docker | Cloud deployment |

## Predefined Prompts

The chatbot includes quick-access buttons for common analysis:

1. **Economic News** - Analyzes current economic trends and news
2. **Market Sentiment** - Provides market sentiment analysis
3. **News Headlines** - Summarizes latest news headlines
4. **Trade Recommendation** - Suggests trading strategies
5. **Upcoming Earnings** - Lists upcoming company earnings

## Running Locally

```bash
# Install dependencies
uv sync

# Set environment variables defined in .env.name file
export OPENAI_API_KEY="your-key"
export SERPER_API_KEY="your-key"
export NEWS_API_KEY="your-key"

# Run the Streamlit app (from the root)
python run.py chatbot
```

## Deployment

The project is deployed on Hugging Face Spaces as a Docker container:
- **Space**: https://huggingface.co/spaces/mishrabp/chatbot-app
- **URL**: https://mishrabp-chatbot-app.hf.space
- **Trigger**: Automatic deployment on push to `main` branch
- **Configuration**: `.github/workflows/chatbot-app-hf.yml`