Spaces:
Sleeping
Sleeping
Commit ·
cb3f557
1
Parent(s): 4ac0ecc
Add application file
Browse files- .gitignore +10 -0
- Dockerfile +18 -0
- Jobobike_Instructions (2).docx +0 -0
- app.log +0 -0
- app.py +2007 -0
- chatbot/chatbot_agent.py +13 -0
- config/chabot_config.py +39 -0
- data.docx +0 -0
- guardrails/guardrails_input_function.py +49 -0
- guardrails/input_guardrails.py +29 -0
- instructions/chatbot_instructions.py +580 -0
- main.py +238 -0
- requirements.txt +9 -0
- schema/chatbot_schema.py +5 -0
- serviceAccount.json +13 -0
- sessions/__init__.py +1 -0
- sessions/session_manager.py +181 -0
- tools/README.md +169 -0
- tools/__init__.py +1 -0
- tools/document_reader_tool.py +212 -0
- tools/example_usage.py +53 -0
- tools/firebase_config.py +27 -0
.gitignore
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python-generated files
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[oc]
|
| 4 |
+
build/
|
| 5 |
+
dist/
|
| 6 |
+
wheels/
|
| 7 |
+
*.egg-info
|
| 8 |
+
|
| 9 |
+
# Virtual environments
|
| 10 |
+
.venv
|
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Base image
|
| 2 |
+
FROM python:3.11-slim
|
| 3 |
+
|
| 4 |
+
# Set work directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Install dependencies
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 10 |
+
|
| 11 |
+
# Copy project files
|
| 12 |
+
COPY . .
|
| 13 |
+
|
| 14 |
+
# Expose the port Hugging Face expects
|
| 15 |
+
EXPOSE 7860
|
| 16 |
+
|
| 17 |
+
# Command to run FastAPI with uvicorn
|
| 18 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
Jobobike_Instructions (2).docx
ADDED
|
Binary file (20.9 kB). View file
|
|
|
app.log
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app.py
ADDED
|
@@ -0,0 +1,2007 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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 |
+
# """# FastAPI application for JOBObike Chatbot API
|
| 2 |
+
# Provides /chat and /chat-stream endpoints with rate limiting, CORS, and error handling
|
| 3 |
+
# Updated with language context support
|
| 4 |
+
# """
|
| 5 |
+
# import os
|
| 6 |
+
# import logging
|
| 7 |
+
# import time
|
| 8 |
+
# from typing import Optional
|
| 9 |
+
# from collections import defaultdict
|
| 10 |
+
# import resend
|
| 11 |
+
|
| 12 |
+
# from fastapi import FastAPI, Request, HTTPException, status, Depends, Header
|
| 13 |
+
# from fastapi.responses import StreamingResponse, JSONResponse
|
| 14 |
+
# from fastapi.middleware.cors import CORSMiddleware
|
| 15 |
+
# from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 16 |
+
# from pydantic import BaseModel
|
| 17 |
+
# from slowapi import Limiter, _rate_limit_exceeded_handler
|
| 18 |
+
# from slowapi.util import get_remote_address
|
| 19 |
+
# from slowapi.errors import RateLimitExceeded
|
| 20 |
+
# from slowapi.middleware import SlowAPIMiddleware
|
| 21 |
+
# from dotenv import load_dotenv
|
| 22 |
+
|
| 23 |
+
# from agents import Runner, RunContextWrapper
|
| 24 |
+
# from agents.exceptions import InputGuardrailTripwireTriggered
|
| 25 |
+
# from openai.types.responses import ResponseTextDeltaEvent
|
| 26 |
+
# from chatbot.chatbot_agent import jobobike_assistant
|
| 27 |
+
# from sessions.session_manager import session_manager
|
| 28 |
+
|
| 29 |
+
# # Load environment variables
|
| 30 |
+
# load_dotenv()
|
| 31 |
+
|
| 32 |
+
# # Configure Resend
|
| 33 |
+
# resend.api_key = os.getenv("RESEND_API_KEY")
|
| 34 |
+
|
| 35 |
+
# # Configure logging
|
| 36 |
+
# logging.basicConfig(
|
| 37 |
+
# level=logging.INFO,
|
| 38 |
+
# format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 39 |
+
# handlers=[
|
| 40 |
+
# logging.FileHandler('app.log'),
|
| 41 |
+
# logging.StreamHandler()
|
| 42 |
+
# ]
|
| 43 |
+
# )
|
| 44 |
+
# logger = logging.getLogger(__name__)
|
| 45 |
+
|
| 46 |
+
# # Initialize rate limiter with enhanced security
|
| 47 |
+
# limiter = Limiter(key_func=get_remote_address, default_limits=["100/day", "20/hour", "3/minute"])
|
| 48 |
+
|
| 49 |
+
# # Create FastAPI app
|
| 50 |
+
# app = FastAPI(
|
| 51 |
+
# title="JOBObike Chatbot API",
|
| 52 |
+
# description="AI-powered chatbot API for JOBObike services",
|
| 53 |
+
# version="1.0.0"
|
| 54 |
+
# )
|
| 55 |
+
|
| 56 |
+
# # Add rate limiter middleware
|
| 57 |
+
# app.state.limiter = limiter
|
| 58 |
+
# app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
| 59 |
+
# app.add_middleware(SlowAPIMiddleware)
|
| 60 |
+
|
| 61 |
+
# # Configure CORS from environment variable
|
| 62 |
+
# allowed_origins = os.getenv("ALLOWED_ORIGINS", "").split(",")
|
| 63 |
+
# allowed_origins = [origin.strip() for origin in allowed_origins if origin.strip()]
|
| 64 |
+
|
| 65 |
+
# if allowed_origins:
|
| 66 |
+
# app.add_middleware(
|
| 67 |
+
# CORSMiddleware,
|
| 68 |
+
# allow_origins=["*"] + allowed_origins,
|
| 69 |
+
# allow_credentials=True,
|
| 70 |
+
# allow_methods=["*"],
|
| 71 |
+
# allow_headers=["*"],
|
| 72 |
+
# )
|
| 73 |
+
# logger.info(f"CORS enabled for origins: {allowed_origins}")
|
| 74 |
+
# else:
|
| 75 |
+
# logger.warning("No ALLOWED_ORIGINS set in .env - CORS disabled")
|
| 76 |
+
|
| 77 |
+
# # Security setup
|
| 78 |
+
# security = HTTPBearer()
|
| 79 |
+
|
| 80 |
+
# # Enhanced rate limiting dictionaries
|
| 81 |
+
# request_counts = defaultdict(list) # Track requests per IP
|
| 82 |
+
# TICKET_RATE_LIMIT = 5 # Max 5 tickets per hour per IP
|
| 83 |
+
# TICKET_TIME_WINDOW = 3600 # 1 hour in seconds
|
| 84 |
+
# MEETING_RATE_LIMIT = 3 # Max 3 meetings per hour per IP
|
| 85 |
+
# MEETING_TIME_WINDOW = 3600 # 1 hour in seconds
|
| 86 |
+
|
| 87 |
+
# # Request/Response models
|
| 88 |
+
# class ChatRequest(BaseModel):
|
| 89 |
+
# message: str
|
| 90 |
+
# language: Optional[str] = "english" # Default to English if not specified
|
| 91 |
+
# session_id: Optional[str] = None # Session ID for chat history
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# class ChatResponse(BaseModel):
|
| 95 |
+
# response: str
|
| 96 |
+
# success: bool
|
| 97 |
+
# session_id: str # Include session ID in response
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
# class ErrorResponse(BaseModel):
|
| 101 |
+
# error: str
|
| 102 |
+
# detail: Optional[str] = None
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
# class TicketRequest(BaseModel):
|
| 106 |
+
# name: str
|
| 107 |
+
# email: str
|
| 108 |
+
# message: str
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
# class TicketResponse(BaseModel):
|
| 112 |
+
# success: bool
|
| 113 |
+
# message: str
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
# class MeetingRequest(BaseModel):
|
| 117 |
+
# name: str
|
| 118 |
+
# email: str
|
| 119 |
+
# date: str # ISO format date string
|
| 120 |
+
# time: str # Time in HH:MM format
|
| 121 |
+
# timezone: str # Timezone identifier
|
| 122 |
+
# duration: int # Duration in minutes
|
| 123 |
+
# topic: str # Meeting topic/title
|
| 124 |
+
# attendees: list[str] # List of attendee emails
|
| 125 |
+
# description: Optional[str] = None # Optional meeting description
|
| 126 |
+
# location: Optional[str] = "Google Meet" # Meeting location/platform
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
# class MeetingResponse(BaseModel):
|
| 130 |
+
# success: bool
|
| 131 |
+
# message: str
|
| 132 |
+
# meeting_id: Optional[str] = None # Unique identifier for the meeting
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
# # Security dependency for API key validation
|
| 136 |
+
# async def verify_api_key(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
| 137 |
+
# """Verify API key for protected endpoints"""
|
| 138 |
+
# # In production, you would check against a database of valid keys
|
| 139 |
+
# # For now, we'll use an environment variable
|
| 140 |
+
# expected_key = os.getenv("API_KEY")
|
| 141 |
+
# if not expected_key or credentials.credentials != expected_key:
|
| 142 |
+
# raise HTTPException(
|
| 143 |
+
# status_code=status.HTTP_401_UNAUTHORIZED,
|
| 144 |
+
# detail="Invalid or missing API key",
|
| 145 |
+
# )
|
| 146 |
+
# return credentials.credentials
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
# def is_ticket_rate_limited(ip_address: str) -> bool:
|
| 150 |
+
# """Check if an IP address has exceeded ticket submission rate limits"""
|
| 151 |
+
# current_time = time.time()
|
| 152 |
+
# # Clean old requests outside the time window
|
| 153 |
+
# request_counts[ip_address] = [
|
| 154 |
+
# req_time for req_time in request_counts[ip_address]
|
| 155 |
+
# if current_time - req_time < TICKET_TIME_WINDOW
|
| 156 |
+
# ]
|
| 157 |
+
|
| 158 |
+
# # Check if limit exceeded
|
| 159 |
+
# if len(request_counts[ip_address]) >= TICKET_RATE_LIMIT:
|
| 160 |
+
# return True
|
| 161 |
+
|
| 162 |
+
# # Add current request
|
| 163 |
+
# request_counts[ip_address].append(current_time)
|
| 164 |
+
# return False
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
# def is_meeting_rate_limited(ip_address: str) -> bool:
|
| 168 |
+
# """Check if an IP address has exceeded meeting scheduling rate limits"""
|
| 169 |
+
# current_time = time.time()
|
| 170 |
+
# # Clean old requests outside the time window
|
| 171 |
+
# request_counts[ip_address] = [
|
| 172 |
+
# req_time for req_time in request_counts[ip_address]
|
| 173 |
+
# if current_time - req_time < MEETING_TIME_WINDOW
|
| 174 |
+
# ]
|
| 175 |
+
|
| 176 |
+
# # Check if limit exceeded
|
| 177 |
+
# if len(request_counts[ip_address]) >= MEETING_RATE_LIMIT:
|
| 178 |
+
# return True
|
| 179 |
+
|
| 180 |
+
# # Add current request
|
| 181 |
+
# request_counts[ip_address].append(current_time)
|
| 182 |
+
# return False
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
# def query_jobobike_bot_stream(user_message: str, language: str = "english", session_id: Optional[str] = None):
|
| 186 |
+
# """
|
| 187 |
+
# Query the JOBObike bot with streaming - returns async generator.
|
| 188 |
+
# Now includes language context and session history.
|
| 189 |
+
# Implements fallback to non-streaming when streaming fails (e.g., with Gemini models).
|
| 190 |
+
# """
|
| 191 |
+
# logger.info(f"AGENT STREAM CALL: query_jobobike_bot_stream called with message='{user_message}', language='{language}', session_id='{session_id}'")
|
| 192 |
+
|
| 193 |
+
# # Get session history if session_id is provided
|
| 194 |
+
# history = []
|
| 195 |
+
# if session_id:
|
| 196 |
+
# history = session_manager.get_session_history(session_id)
|
| 197 |
+
# logger.info(f"Retrieved {len(history)} history messages for session {session_id}")
|
| 198 |
+
|
| 199 |
+
# try:
|
| 200 |
+
# # Create context with language preference and history
|
| 201 |
+
# context_data = {"language": language}
|
| 202 |
+
# if history:
|
| 203 |
+
# context_data["history"] = history
|
| 204 |
+
|
| 205 |
+
# ctx = RunContextWrapper(context=context_data)
|
| 206 |
+
|
| 207 |
+
# result = Runner.run_streamed(
|
| 208 |
+
# jobobike_assistant,
|
| 209 |
+
# input=user_message,
|
| 210 |
+
# context=ctx.context
|
| 211 |
+
# )
|
| 212 |
+
|
| 213 |
+
# async def generate_stream():
|
| 214 |
+
# try:
|
| 215 |
+
# previous = ""
|
| 216 |
+
# has_streamed = True
|
| 217 |
+
|
| 218 |
+
# try:
|
| 219 |
+
# # Attempt streaming with error handling for each event
|
| 220 |
+
# async for event in result.stream_events():
|
| 221 |
+
# try:
|
| 222 |
+
# if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent):
|
| 223 |
+
# delta = event.data.delta or ""
|
| 224 |
+
|
| 225 |
+
# # ---- Spacing Fix ----
|
| 226 |
+
# if (
|
| 227 |
+
# previous
|
| 228 |
+
# and not previous.endswith((" ", "\n"))
|
| 229 |
+
# and not delta.startswith((" ", ".", ",", "?", "!", ":", ";"))
|
| 230 |
+
# ):
|
| 231 |
+
# delta = " " + delta
|
| 232 |
+
|
| 233 |
+
# previous = delta
|
| 234 |
+
# # ---- End Fix ----
|
| 235 |
+
|
| 236 |
+
# yield f"data: {delta}\n\n"
|
| 237 |
+
# has_streamed = True
|
| 238 |
+
# except Exception as event_error:
|
| 239 |
+
# # Handle individual event errors (e.g., missing logprobs field)
|
| 240 |
+
# logger.warning(f"Event processing error: {event_error}")
|
| 241 |
+
# continue
|
| 242 |
+
|
| 243 |
+
# yield "data: [DONE]\n\n"
|
| 244 |
+
# logger.info("AGENT STREAM RESULT: query_jobobike_bot_stream completed successfully")
|
| 245 |
+
|
| 246 |
+
# except Exception as stream_error:
|
| 247 |
+
# # Fallback to non-streaming if streaming fails
|
| 248 |
+
# logger.warning(f"Streaming failed, falling back to non-streaming: {stream_error}")
|
| 249 |
+
|
| 250 |
+
# if not has_streamed:
|
| 251 |
+
# # Get final output using the streaming result's final_output property
|
| 252 |
+
# # Wait for the stream to complete to get final output
|
| 253 |
+
# try:
|
| 254 |
+
# # Use the non-streaming API as fallback
|
| 255 |
+
# fallback_response = await Runner.run(
|
| 256 |
+
# jobobike_assistant,
|
| 257 |
+
# input=user_message,
|
| 258 |
+
# context=ctx.context
|
| 259 |
+
# )
|
| 260 |
+
|
| 261 |
+
# if hasattr(fallback_response, 'final_output'):
|
| 262 |
+
# final_output = fallback_response.final_output
|
| 263 |
+
# else:
|
| 264 |
+
# final_output = fallback_response
|
| 265 |
+
|
| 266 |
+
# if hasattr(final_output, 'content'):
|
| 267 |
+
# response_text = final_output.content
|
| 268 |
+
# elif isinstance(final_output, str):
|
| 269 |
+
# response_text = final_output
|
| 270 |
+
# else:
|
| 271 |
+
# response_text = str(final_output)
|
| 272 |
+
|
| 273 |
+
# yield f"data: {response_text}\n\n"
|
| 274 |
+
# yield "data: [DONE]\n\n"
|
| 275 |
+
# logger.info("AGENT STREAM RESULT: query_jobobike_bot_stream fallback completed successfully")
|
| 276 |
+
# except Exception as fallback_error:
|
| 277 |
+
# logger.error(f"Fallback also failed: {fallback_error}", exc_info=True)
|
| 278 |
+
# yield f"data: [ERROR] Unable to complete request.\n\n"
|
| 279 |
+
# else:
|
| 280 |
+
# # Already streamed some content, just end gracefully
|
| 281 |
+
# yield "data: [DONE]\n\n"
|
| 282 |
+
|
| 283 |
+
# except InputGuardrailTripwireTriggered as e:
|
| 284 |
+
# logger.warning(f"Guardrail blocked query during streaming: {e}")
|
| 285 |
+
# yield f"data: [ERROR] Query was blocked by content guardrail.\n\n"
|
| 286 |
+
|
| 287 |
+
# except Exception as e:
|
| 288 |
+
# logger.error(f"Streaming error: {e}", exc_info=True)
|
| 289 |
+
# yield f"data: [ERROR] {str(e)}\n\n"
|
| 290 |
+
|
| 291 |
+
# return generate_stream()
|
| 292 |
+
|
| 293 |
+
# except Exception as e:
|
| 294 |
+
# logger.error(f"Error setting up stream: {e}", exc_info=True)
|
| 295 |
+
|
| 296 |
+
# async def error_stream():
|
| 297 |
+
# yield f"data: [ERROR] Failed to initialize stream.\n\n"
|
| 298 |
+
|
| 299 |
+
# return error_stream()
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
# async def query_jobobike_bot(user_message: str, language: str = "english", session_id: Optional[str] = None):
|
| 303 |
+
# """
|
| 304 |
+
# Query the JOBObike bot - returns complete response.
|
| 305 |
+
# Now includes language context and session history.
|
| 306 |
+
# """
|
| 307 |
+
# logger.info(f"AGENT CALL: query_jobobike_bot called with message='{user_message}', language='{language}', session_id='{session_id}'")
|
| 308 |
+
|
| 309 |
+
# # Get session history if session_id is provided
|
| 310 |
+
# history = []
|
| 311 |
+
# if session_id:
|
| 312 |
+
# history = session_manager.get_session_history(session_id)
|
| 313 |
+
# logger.info(f"Retrieved {len(history)} history messages for session {session_id}")
|
| 314 |
+
|
| 315 |
+
# try:
|
| 316 |
+
# # Create context with language preference and history
|
| 317 |
+
# context_data = {"language": language}
|
| 318 |
+
# if history:
|
| 319 |
+
# context_data["history"] = history
|
| 320 |
+
|
| 321 |
+
# ctx = RunContextWrapper(context=context_data)
|
| 322 |
+
|
| 323 |
+
# response = await Runner.run(
|
| 324 |
+
# jobobike_assistant,
|
| 325 |
+
# input=user_message,
|
| 326 |
+
# context=ctx.context
|
| 327 |
+
# )
|
| 328 |
+
# logger.info("AGENT RESULT: query_jobobike_bot completed successfully")
|
| 329 |
+
# return response.final_output
|
| 330 |
+
|
| 331 |
+
# except InputGuardrailTripwireTriggered as e:
|
| 332 |
+
# logger.warning(f"Guardrail blocked query: {e}")
|
| 333 |
+
# raise HTTPException(
|
| 334 |
+
# status_code=status.HTTP_403_FORBIDDEN,
|
| 335 |
+
# detail="Query was blocked by content guardrail. Please ensure your query is related to JOBObike services."
|
| 336 |
+
# )
|
| 337 |
+
# except Exception as e:
|
| 338 |
+
# logger.error(f"Error in query_jobobike_bot: {e}", exc_info=True)
|
| 339 |
+
# raise HTTPException(
|
| 340 |
+
# status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 341 |
+
# detail="An internal error occurred while processing your request."
|
| 342 |
+
# )
|
| 343 |
+
|
| 344 |
+
|
| 345 |
+
# @app.get("/")
|
| 346 |
+
# async def root():
|
| 347 |
+
# return {"status": "ok", "service": "JOBObike Chatbot API"}
|
| 348 |
+
|
| 349 |
+
|
| 350 |
+
# @app.get("/health")
|
| 351 |
+
# async def health():
|
| 352 |
+
# return {"status": "healthy"}
|
| 353 |
+
|
| 354 |
+
|
| 355 |
+
# @app.post("/session")
|
| 356 |
+
# async def create_session():
|
| 357 |
+
# """
|
| 358 |
+
# Create a new chat session
|
| 359 |
+
# Returns a session ID that can be used to maintain chat history
|
| 360 |
+
# """
|
| 361 |
+
# try:
|
| 362 |
+
# session_id = session_manager.create_session()
|
| 363 |
+
# logger.info(f"Created new session: {session_id}")
|
| 364 |
+
# return {"session_id": session_id, "message": "Session created successfully"}
|
| 365 |
+
# except Exception as e:
|
| 366 |
+
# logger.error(f"Error creating session: {e}", exc_info=True)
|
| 367 |
+
# raise HTTPException(
|
| 368 |
+
# status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 369 |
+
# detail="Failed to create session"
|
| 370 |
+
# )
|
| 371 |
+
|
| 372 |
+
|
| 373 |
+
# @app.post("/chat", response_model=ChatResponse)
|
| 374 |
+
# @limiter.limit("10/minute") # Limit to 10 requests per minute per IP
|
| 375 |
+
# async def chat(request: Request, chat_request: ChatRequest):
|
| 376 |
+
# """
|
| 377 |
+
# Standard chat endpoint with language support and session history.
|
| 378 |
+
# Accepts: {"message": "...", "language": "norwegian", "session_id": "optional-session-id"}
|
| 379 |
+
# """
|
| 380 |
+
# try:
|
| 381 |
+
# # Create or use existing session
|
| 382 |
+
# session_id = chat_request.session_id
|
| 383 |
+
# if not session_id:
|
| 384 |
+
# session_id = session_manager.create_session()
|
| 385 |
+
# logger.info(f"Created new session for chat: {session_id}")
|
| 386 |
+
|
| 387 |
+
# logger.info(
|
| 388 |
+
# f"Chat request from {get_remote_address(request)}: "
|
| 389 |
+
# f"language={chat_request.language}, message={chat_request.message[:50]}..., session_id={session_id}"
|
| 390 |
+
# )
|
| 391 |
+
|
| 392 |
+
# # Add user message to session history
|
| 393 |
+
# session_manager.add_message_to_history(session_id, "user", chat_request.message)
|
| 394 |
+
|
| 395 |
+
# # Pass language and session to the bot
|
| 396 |
+
# response = await query_jobobike_bot(
|
| 397 |
+
# chat_request.message,
|
| 398 |
+
# language=chat_request.language,
|
| 399 |
+
# session_id=session_id
|
| 400 |
+
# )
|
| 401 |
+
|
| 402 |
+
# if hasattr(response, 'content'):
|
| 403 |
+
# response_text = response.content
|
| 404 |
+
# elif isinstance(response, str):
|
| 405 |
+
# response_text = response
|
| 406 |
+
# else:
|
| 407 |
+
# response_text = str(response)
|
| 408 |
+
|
| 409 |
+
# # Add bot response to session history
|
| 410 |
+
# session_manager.add_message_to_history(session_id, "assistant", response_text)
|
| 411 |
+
|
| 412 |
+
# logger.info(f"Chat response generated successfully in {chat_request.language} for session {session_id}")
|
| 413 |
+
|
| 414 |
+
# return ChatResponse(
|
| 415 |
+
# response=response_text,
|
| 416 |
+
# success=True,
|
| 417 |
+
# session_id=session_id
|
| 418 |
+
# )
|
| 419 |
+
|
| 420 |
+
# except HTTPException:
|
| 421 |
+
# raise
|
| 422 |
+
# except Exception as e:
|
| 423 |
+
# logger.error(f"Unexpected error in /chat: {e}", exc_info=True)
|
| 424 |
+
# raise HTTPException(
|
| 425 |
+
# status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 426 |
+
# detail="An internal error occurred while processing your request."
|
| 427 |
+
# )
|
| 428 |
+
|
| 429 |
+
|
| 430 |
+
# @app.post("/api/messages", response_model=ChatResponse)
|
| 431 |
+
# @limiter.limit("10/minute") # Same rate limit as /chat
|
| 432 |
+
# async def api_messages(request: Request, chat_request: ChatRequest):
|
| 433 |
+
# """
|
| 434 |
+
# Frontend-friendly chat endpoint at /api/messages.
|
| 435 |
+
# Exactly mirrors /chat logic for session/history support.
|
| 436 |
+
# Expects: {"message": "...", "language": "english", "session_id": "optional"}
|
| 437 |
+
# """
|
| 438 |
+
# client_ip = get_remote_address(request)
|
| 439 |
+
# logger.info(f"API Messages request from {client_ip}: message='{chat_request.message[:50]}...', lang='{chat_request.language}', session='{chat_request.session_id}'")
|
| 440 |
+
|
| 441 |
+
# try:
|
| 442 |
+
# # Create/use session (Firestore-backed)
|
| 443 |
+
# session_id = chat_request.session_id
|
| 444 |
+
# if not session_id:
|
| 445 |
+
# session_id = session_manager.create_session()
|
| 446 |
+
# logger.info(f"New session created for /api/messages: {session_id}")
|
| 447 |
+
|
| 448 |
+
# # Save user message to history
|
| 449 |
+
# session_manager.add_message_to_history(session_id, "user", chat_request.message)
|
| 450 |
+
|
| 451 |
+
# # Call your existing bot query function
|
| 452 |
+
# response = await query_jobobike_bot(
|
| 453 |
+
# user_message=chat_request.message,
|
| 454 |
+
# language=chat_request.language,
|
| 455 |
+
# session_id=session_id
|
| 456 |
+
# )
|
| 457 |
+
|
| 458 |
+
# # Extract response text
|
| 459 |
+
# response_text = (
|
| 460 |
+
# response.content if hasattr(response, 'content')
|
| 461 |
+
# else response if isinstance(response, str)
|
| 462 |
+
# else str(response)
|
| 463 |
+
# )
|
| 464 |
+
|
| 465 |
+
# # Save AI response to history
|
| 466 |
+
# session_manager.add_message_to_history(session_id, "assistant", response_text)
|
| 467 |
+
|
| 468 |
+
# logger.info(f"API Messages success: Response sent for session {session_id}")
|
| 469 |
+
|
| 470 |
+
# return ChatResponse(
|
| 471 |
+
# response=response_text,
|
| 472 |
+
# success=True,
|
| 473 |
+
# session_id=session_id
|
| 474 |
+
# )
|
| 475 |
+
|
| 476 |
+
# except InputGuardrailTripwireTriggered as e:
|
| 477 |
+
# logger.warning(f"Guardrail blocked /api/messages: {e}")
|
| 478 |
+
# raise HTTPException(
|
| 479 |
+
# status_code=403,
|
| 480 |
+
# detail="Query blocked – please ask about JOBObike services."
|
| 481 |
+
# )
|
| 482 |
+
# except Exception as e:
|
| 483 |
+
# logger.error(f"Error in /api/messages: {e}", exc_info=True)
|
| 484 |
+
# raise HTTPException(
|
| 485 |
+
# status_code=500,
|
| 486 |
+
# detail="Internal error – try again."
|
| 487 |
+
# )
|
| 488 |
+
|
| 489 |
+
# @app.post("/chat-stream")
|
| 490 |
+
# @limiter.limit("10/minute") # Limit to 10 requests per minute per IP
|
| 491 |
+
# async def chat_stream(request: Request, chat_request: ChatRequest):
|
| 492 |
+
# """
|
| 493 |
+
# Streaming chat endpoint with language support and session history.
|
| 494 |
+
# Accepts: {"message": "...", "language": "norwegian", "session_id": "optional-session-id"}
|
| 495 |
+
# """
|
| 496 |
+
# try:
|
| 497 |
+
# # Create or use existing session
|
| 498 |
+
# session_id = chat_request.session_id
|
| 499 |
+
# if not session_id:
|
| 500 |
+
# session_id = session_manager.create_session()
|
| 501 |
+
# logger.info(f"Created new session for streaming chat: {session_id}")
|
| 502 |
+
|
| 503 |
+
# logger.info(
|
| 504 |
+
# f"Stream request from {get_remote_address(request)}: "
|
| 505 |
+
# f"language={chat_request.language}, message={chat_request.message[:50]}..., session_id={session_id}"
|
| 506 |
+
# )
|
| 507 |
+
|
| 508 |
+
# # Add user message to session history
|
| 509 |
+
# session_manager.add_message_to_history(session_id, "user", chat_request.message)
|
| 510 |
+
|
| 511 |
+
# # Pass language and session to the streaming bot
|
| 512 |
+
# stream_generator = query_jobobike_bot_stream(
|
| 513 |
+
# chat_request.message,
|
| 514 |
+
# language=chat_request.language,
|
| 515 |
+
# session_id=session_id
|
| 516 |
+
# )
|
| 517 |
+
|
| 518 |
+
# # Note: For streaming, we add the response to history after the stream completes
|
| 519 |
+
# # This would need to be handled in the frontend by making a separate call or
|
| 520 |
+
# # by modifying the stream generator to add the complete response to history
|
| 521 |
+
|
| 522 |
+
# return StreamingResponse(
|
| 523 |
+
# stream_generator,
|
| 524 |
+
# media_type="text/event-stream",
|
| 525 |
+
# headers={
|
| 526 |
+
# "Cache-Control": "no-cache",
|
| 527 |
+
# "Connection": "keep-alive",
|
| 528 |
+
# "X-Accel-Buffering": "no",
|
| 529 |
+
# "Session-ID": session_id # Include session ID in headers
|
| 530 |
+
# }
|
| 531 |
+
# )
|
| 532 |
+
|
| 533 |
+
# except HTTPException:
|
| 534 |
+
# raise
|
| 535 |
+
# except Exception as e:
|
| 536 |
+
# logger.error(f"Unexpected error in /chat-stream: {e}", exc_info=True)
|
| 537 |
+
# raise HTTPException(
|
| 538 |
+
# status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 539 |
+
# detail="An internal error occurred while processing your request."
|
| 540 |
+
# )
|
| 541 |
+
|
| 542 |
+
|
| 543 |
+
# @app.post("/ticket", response_model=TicketResponse)
|
| 544 |
+
# @limiter.limit("5/hour") # Limit to 5 tickets per hour per IP
|
| 545 |
+
# async def submit_ticket(request: Request, ticket_request: TicketRequest):
|
| 546 |
+
# """
|
| 547 |
+
# Submit a support ticket via email using Resend API.
|
| 548 |
+
# Accepts: {"name": "John Doe", "email": "john@example.com", "message": "Issue description"}
|
| 549 |
+
# """
|
| 550 |
+
# try:
|
| 551 |
+
# client_ip = get_remote_address(request)
|
| 552 |
+
# logger.info(f"Ticket submission request from {ticket_request.name} ({ticket_request.email}) - IP: {client_ip}")
|
| 553 |
+
|
| 554 |
+
# # Additional rate limiting for tickets
|
| 555 |
+
# if is_ticket_rate_limited(client_ip):
|
| 556 |
+
# logger.warning(f"Rate limit exceeded for ticket submission from IP: {client_ip}")
|
| 557 |
+
# raise HTTPException(
|
| 558 |
+
# status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
| 559 |
+
# detail="Too many ticket submissions. Please try again later."
|
| 560 |
+
# )
|
| 561 |
+
|
| 562 |
+
# # Get admin email from environment variables or use a default
|
| 563 |
+
# admin_email = os.getenv("ADMIN_EMAIL", "admin@yourcompany.com")
|
| 564 |
+
|
| 565 |
+
# # Use a verified sender email (you need to verify this in your Resend account)
|
| 566 |
+
# # For testing purposes, you can use your Resend account's verified domain
|
| 567 |
+
# sender_email = os.getenv("SENDER_EMAIL", "onboarding@resend.dev")
|
| 568 |
+
|
| 569 |
+
# # Prepare the email using Resend
|
| 570 |
+
# params = {
|
| 571 |
+
# "from": sender_email,
|
| 572 |
+
# "to": [admin_email],
|
| 573 |
+
# "subject": f"Support Ticket from {ticket_request.name}",
|
| 574 |
+
# "html": f"""
|
| 575 |
+
# <p>Hello Admin,</p>
|
| 576 |
+
# <p>A new support ticket has been submitted:</p>
|
| 577 |
+
# <p><strong>Name:</strong> {ticket_request.name}</p>
|
| 578 |
+
# <p><strong>Email:</strong> {ticket_request.email}</p>
|
| 579 |
+
# <p><strong>Message:</strong></p>
|
| 580 |
+
# <p>{ticket_request.message}</p>
|
| 581 |
+
# <p><strong>IP Address:</strong> {client_ip}</p>
|
| 582 |
+
# <br>
|
| 583 |
+
# <p>Best regards,<br>JOBObike Support Team</p>
|
| 584 |
+
# """
|
| 585 |
+
# }
|
| 586 |
+
|
| 587 |
+
# # Send the email
|
| 588 |
+
# email = resend.Emails.send(params)
|
| 589 |
+
|
| 590 |
+
# logger.info(f"Ticket submitted successfully by {ticket_request.name} from IP: {client_ip}")
|
| 591 |
+
|
| 592 |
+
# return TicketResponse(
|
| 593 |
+
# success=True,
|
| 594 |
+
# message="Ticket submitted successfully. We'll get back to you soon."
|
| 595 |
+
# )
|
| 596 |
+
|
| 597 |
+
# except HTTPException:
|
| 598 |
+
# raise
|
| 599 |
+
# except Exception as e:
|
| 600 |
+
# logger.error(f"Error submitting ticket: {e}", exc_info=True)
|
| 601 |
+
# raise HTTPException(
|
| 602 |
+
# status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 603 |
+
# detail="Failed to submit ticket. Please try again later."
|
| 604 |
+
# )
|
| 605 |
+
|
| 606 |
+
|
| 607 |
+
# @app.post("/schedule-meeting", response_model=MeetingResponse)
|
| 608 |
+
# @limiter.limit("3/hour") # Limit to 3 meetings per hour per IP
|
| 609 |
+
# async def schedule_meeting(request: Request, meeting_request: MeetingRequest):
|
| 610 |
+
# """
|
| 611 |
+
# Schedule a meeting and send email invitations using Resend API.
|
| 612 |
+
# Accepts meeting details and sends professional email invitations to organizer and attendees.
|
| 613 |
+
# """
|
| 614 |
+
# try:
|
| 615 |
+
# client_ip = get_remote_address(request)
|
| 616 |
+
# logger.info(f"Meeting scheduling request from {meeting_request.name} ({meeting_request.email}) - IP: {client_ip}")
|
| 617 |
+
|
| 618 |
+
# # Additional rate limiting for meetings
|
| 619 |
+
# if is_meeting_rate_limited(client_ip):
|
| 620 |
+
# logger.warning(f"Rate limit exceeded for meeting scheduling from IP: {client_ip}")
|
| 621 |
+
# raise HTTPException(
|
| 622 |
+
# status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
| 623 |
+
# detail="Too many meeting requests. Please try again later."
|
| 624 |
+
# )
|
| 625 |
+
|
| 626 |
+
# # Generate a unique meeting ID
|
| 627 |
+
# meeting_id = f"mtg_{int(time.time())}"
|
| 628 |
+
|
| 629 |
+
# # Get admin email from environment variables or use a default
|
| 630 |
+
# admin_email = os.getenv("ADMIN_EMAIL", "admin@yourcompany.com")
|
| 631 |
+
|
| 632 |
+
# # Use a verified sender email (you need to verify this in your Resend account)
|
| 633 |
+
# sender_email = os.getenv("SENDER_EMAIL", "onboarding@resend.dev")
|
| 634 |
+
|
| 635 |
+
# # For Resend testing limitations, we can only send to the owner's email
|
| 636 |
+
# # In production, you would verify a domain and use that instead
|
| 637 |
+
# owner_email = os.getenv("ADMIN_EMAIL", "admin@yourcompany.com")
|
| 638 |
+
|
| 639 |
+
# # Format date and time for display
|
| 640 |
+
# formatted_datetime = f"{meeting_request.date} at {meeting_request.time} {meeting_request.timezone}"
|
| 641 |
+
|
| 642 |
+
# # Create calendar link (Google Calendar link example)
|
| 643 |
+
# calendar_link = f"https://calendar.google.com/calendar/render?action=TEMPLATE&text={meeting_request.topic}&dates={meeting_request.date.replace('-', '')}T{meeting_request.time.replace(':', '')}00Z/{meeting_request.date.replace('-', '')}T{meeting_request.time.replace(':', '')}00Z&details={meeting_request.description or 'Meeting scheduled via JOBObike'}&location={meeting_request.location}"
|
| 644 |
+
|
| 645 |
+
# # Combine all attendees (organizer + additional attendees)
|
| 646 |
+
# # Validate and format email addresses
|
| 647 |
+
# all_attendees = [meeting_request.email]
|
| 648 |
+
|
| 649 |
+
# # Validate additional attendees - they must be valid email addresses
|
| 650 |
+
# for attendee in meeting_request.attendees:
|
| 651 |
+
# # Simple email validation
|
| 652 |
+
# if "@" in attendee and "." in attendee:
|
| 653 |
+
# all_attendees.append(attendee)
|
| 654 |
+
# else:
|
| 655 |
+
# # If not a valid email, skip or treat as name only
|
| 656 |
+
# logger.warning(f"Invalid email format for attendee: {attendee}. Skipping.")
|
| 657 |
+
|
| 658 |
+
# # Remove duplicates while preserving order
|
| 659 |
+
# seen = set()
|
| 660 |
+
# unique_attendees = []
|
| 661 |
+
# for email in all_attendees:
|
| 662 |
+
# if email not in seen:
|
| 663 |
+
# seen.add(email)
|
| 664 |
+
# unique_attendees.append(email)
|
| 665 |
+
# all_attendees = unique_attendees
|
| 666 |
+
|
| 667 |
+
# # Prepare the professional HTML email template
|
| 668 |
+
# html_template = f"""
|
| 669 |
+
# <!DOCTYPE html>
|
| 670 |
+
# <html>
|
| 671 |
+
# <head>
|
| 672 |
+
# <meta charset="UTF-8">
|
| 673 |
+
# <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 674 |
+
# <title>Meeting Scheduled - {meeting_request.topic}</title>
|
| 675 |
+
# </head>
|
| 676 |
+
# <body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 600px; margin: 0 auto; padding: 20px;">
|
| 677 |
+
# <div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 30px; text-align: center; border-radius: 10px 10px 0 0;">
|
| 678 |
+
# <h1 style="margin: 0; font-size: 28px;">Meeting Confirmed!</h1>
|
| 679 |
+
# <p style="font-size: 18px; margin-top: 10px;">Your meeting has been successfully scheduled</p>
|
| 680 |
+
# </div>
|
| 681 |
+
|
| 682 |
+
# <div style="background-color: #ffffff; padding: 30px; border: 1px solid #eaeaea; border-top: none; border-radius: 0 0 10px 10px;">
|
| 683 |
+
# <h2 style="color: #333;">Meeting Details</h2>
|
| 684 |
+
|
| 685 |
+
# <div style="background-color: #f8f9fa; padding: 20px; border-radius: 8px; margin: 20px 0;">
|
| 686 |
+
# <table style="width: 100%; border-collapse: collapse;">
|
| 687 |
+
# <tr>
|
| 688 |
+
# <td style="padding: 8px 0; font-weight: bold; width: 30%;">Topic:</td>
|
| 689 |
+
# <td style="padding: 8px 0;">{meeting_request.topic}</td>
|
| 690 |
+
# </tr>
|
| 691 |
+
# <tr style="background-color: #f0f0f0;">
|
| 692 |
+
# <td style="padding: 8px 0; font-weight: bold;">Date & Time:</td>
|
| 693 |
+
# <td style="padding: 8px 0;">{formatted_datetime}</td>
|
| 694 |
+
# </tr>
|
| 695 |
+
# <tr>
|
| 696 |
+
# <td style="padding: 8px 0; font-weight: bold;">Duration:</td>
|
| 697 |
+
# <td style="padding: 8px 0;">{meeting_request.duration} minutes</td>
|
| 698 |
+
# </tr>
|
| 699 |
+
# <tr style="background-color: #f0f0f0;">
|
| 700 |
+
# <td style="padding: 8px 0; font-weight: bold;">Location:</td>
|
| 701 |
+
# <td style="padding: 8px 0;">{meeting_request.location}</td>
|
| 702 |
+
# </tr>
|
| 703 |
+
# <tr>
|
| 704 |
+
# <td style="padding: 8px 0; font-weight: bold;">Organizer:</td>
|
| 705 |
+
# <td style="padding: 8px 0;">{meeting_request.name} ({meeting_request.email})</td>
|
| 706 |
+
# </tr>
|
| 707 |
+
# </table>
|
| 708 |
+
# </div>
|
| 709 |
+
|
| 710 |
+
# <div style="margin: 25px 0;">
|
| 711 |
+
# <h3 style="color: #333;">Description</h3>
|
| 712 |
+
# <p style="background-color: #f8f9fa; padding: 15px; border-radius: 8px; white-space: pre-wrap;">{meeting_request.description or 'No description provided.'}</p>
|
| 713 |
+
# </div>
|
| 714 |
+
|
| 715 |
+
# <div style="margin: 25px 0;">
|
| 716 |
+
# <h3 style="color: #333;">Attendees</h3>
|
| 717 |
+
# <ul style="background-color: #f8f9fa; padding: 15px; border-radius: 8px;">
|
| 718 |
+
# {''.join([f'<li>{attendee}</li>' for attendee in all_attendees])}
|
| 719 |
+
# </ul>
|
| 720 |
+
# <p style="font-size: 12px; color: #666; margin-top: 5px;">Note: Only valid email addresses will receive invitations.</p>
|
| 721 |
+
# </div>
|
| 722 |
+
|
| 723 |
+
# <div style="text-align: center; margin: 30px 0;">
|
| 724 |
+
# <a href="{calendar_link}" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 12px 25px; text-decoration: none; border-radius: 5px; font-weight: bold; display: inline-block;">Add to Calendar</a>
|
| 725 |
+
# </div>
|
| 726 |
+
|
| 727 |
+
# <div style="background-color: #e3f2fd; padding: 15px; border-radius: 8px; margin-top: 25px;">
|
| 728 |
+
# <p style="margin: 0;"><strong>Meeting ID:</strong> {meeting_id}</p>
|
| 729 |
+
# <p style="margin: 10px 0 0 0; font-size: 14px; color: #666;">Need to make changes? Contact the organizer or reply to this email.</p>
|
| 730 |
+
# </div>
|
| 731 |
+
# </div>
|
| 732 |
+
|
| 733 |
+
# <div style="text-align: center; margin-top: 30px; color: #888; font-size: 14px;">
|
| 734 |
+
# <p>This meeting was scheduled through JOBObike Chatbot Services</p>
|
| 735 |
+
# <p><strong>Note:</strong> Due to Resend testing limitations, this email is only sent to the administrator. In production, after domain verification, invitations will be sent to all attendees.</p>
|
| 736 |
+
# <p>© 2025 JOBObike. All rights reserved.</p>
|
| 737 |
+
# </div>
|
| 738 |
+
# </body>
|
| 739 |
+
# </html>
|
| 740 |
+
# """
|
| 741 |
+
|
| 742 |
+
# # Send email to all attendees
|
| 743 |
+
# # Check if we have valid attendees to send to
|
| 744 |
+
# if not all_attendees:
|
| 745 |
+
# logger.warning("No valid email addresses found for meeting attendees")
|
| 746 |
+
# return MeetingResponse(
|
| 747 |
+
# success=True,
|
| 748 |
+
# message="Meeting scheduled successfully, but no valid email addresses found for invitations.",
|
| 749 |
+
# meeting_id=meeting_id
|
| 750 |
+
# )
|
| 751 |
+
|
| 752 |
+
# # For Resend testing limitations, we can only send to the owner's email
|
| 753 |
+
# # In production, you would verify a domain and send to all attendees
|
| 754 |
+
# owner_email = os.getenv("ADMIN_EMAIL", "admin@yourcompany.com")
|
| 755 |
+
|
| 756 |
+
# # Prepare email for owner with all attendee information
|
| 757 |
+
# attendee_list_html = ''.join([f'<li>{attendee}</li>' for attendee in all_attendees])
|
| 758 |
+
# # In a real implementation, you would send to all attendees after verifying your domain
|
| 759 |
+
# # For now, we're sending to the owner with information about all attendees
|
| 760 |
+
|
| 761 |
+
# params = {
|
| 762 |
+
# "from": sender_email,
|
| 763 |
+
# "to": [owner_email], # Only send to owner due to Resend testing limitations
|
| 764 |
+
# "subject": f"Meeting Scheduled: {meeting_request.topic}",
|
| 765 |
+
# "html": html_template
|
| 766 |
+
# }
|
| 767 |
+
|
| 768 |
+
# # Send the email
|
| 769 |
+
# try:
|
| 770 |
+
# email = resend.Emails.send(params)
|
| 771 |
+
# logger.info(f"Email sent successfully to {len(all_attendees)} attendees")
|
| 772 |
+
# except Exception as email_error:
|
| 773 |
+
# logger.error(f"Failed to send email: {email_error}", exc_info=True)
|
| 774 |
+
# # Even if email fails, we still consider the meeting scheduled
|
| 775 |
+
# return MeetingResponse(
|
| 776 |
+
# success=True,
|
| 777 |
+
# message="Meeting scheduled successfully, but failed to send email invitations.",
|
| 778 |
+
# meeting_id=meeting_id
|
| 779 |
+
# )
|
| 780 |
+
|
| 781 |
+
# logger.info(f"Meeting scheduled successfully by {meeting_request.name} from IP: {client_ip}")
|
| 782 |
+
|
| 783 |
+
# return MeetingResponse(
|
| 784 |
+
# success=True,
|
| 785 |
+
# message="Meeting scheduled successfully. Due to Resend testing limitations, invitations are only sent to the administrator. In production, after verifying your domain, invitations will be sent to all attendees.",
|
| 786 |
+
# meeting_id=meeting_id
|
| 787 |
+
# )
|
| 788 |
+
|
| 789 |
+
# except HTTPException:
|
| 790 |
+
# raise
|
| 791 |
+
# except Exception as e:
|
| 792 |
+
# logger.error(f"Error scheduling meeting: {e}", exc_info=True)
|
| 793 |
+
# raise HTTPException(
|
| 794 |
+
# status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 795 |
+
# detail="Failed to schedule meeting. Please try again later."
|
| 796 |
+
# )
|
| 797 |
+
|
| 798 |
+
|
| 799 |
+
# @app.exception_handler(Exception)
|
| 800 |
+
# async def global_exception_handler(request: Request, exc: Exception):
|
| 801 |
+
# logger.error(
|
| 802 |
+
# f"Unhandled exception: {exc}",
|
| 803 |
+
# exc_info=True,
|
| 804 |
+
# extra={
|
| 805 |
+
# "path": request.url.path,
|
| 806 |
+
# "method": request.method,
|
| 807 |
+
# "client": get_remote_address(request)
|
| 808 |
+
# }
|
| 809 |
+
# )
|
| 810 |
+
|
| 811 |
+
# return JSONResponse(
|
| 812 |
+
# status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 813 |
+
# content={
|
| 814 |
+
# "error": "Internal server error",
|
| 815 |
+
# "detail": "An unexpected error occurred. Please try again later."
|
| 816 |
+
# }
|
| 817 |
+
# )
|
| 818 |
+
|
| 819 |
+
|
| 820 |
+
# if __name__ == "__main__":
|
| 821 |
+
# import uvicorn
|
| 822 |
+
# uvicorn.run(app, host="0.0.0.0", port=8000)
|
| 823 |
+
|
| 824 |
+
|
| 825 |
+
"""
|
| 826 |
+
FastAPI application for JOBObike Chatbot API
|
| 827 |
+
Provides /chat and /chat-stream endpoints with rate limiting, CORS, and error handling
|
| 828 |
+
Updated with language context support and FIXED spacing issue in streaming
|
| 829 |
+
"""
|
| 830 |
+
import os
|
| 831 |
+
import logging
|
| 832 |
+
import time
|
| 833 |
+
from typing import Optional
|
| 834 |
+
from collections import defaultdict
|
| 835 |
+
import resend
|
| 836 |
+
|
| 837 |
+
from fastapi import FastAPI, Request, HTTPException, status, Depends, Header
|
| 838 |
+
from fastapi.responses import StreamingResponse, JSONResponse
|
| 839 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 840 |
+
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 841 |
+
from pydantic import BaseModel
|
| 842 |
+
from slowapi import Limiter, _rate_limit_exceeded_handler
|
| 843 |
+
from slowapi.util import get_remote_address
|
| 844 |
+
from slowapi.errors import RateLimitExceeded
|
| 845 |
+
from slowapi.middleware import SlowAPIMiddleware
|
| 846 |
+
from dotenv import load_dotenv
|
| 847 |
+
|
| 848 |
+
from agents import Runner, RunContextWrapper
|
| 849 |
+
from agents.exceptions import InputGuardrailTripwireTriggered
|
| 850 |
+
from openai.types.responses import ResponseTextDeltaEvent
|
| 851 |
+
from openai import BadRequestError as OpenAIBadRequestError
|
| 852 |
+
from chatbot.chatbot_agent import jobobike_assistant
|
| 853 |
+
from sessions.session_manager import session_manager
|
| 854 |
+
|
| 855 |
+
# Load environment variables
|
| 856 |
+
load_dotenv()
|
| 857 |
+
|
| 858 |
+
# Configure Resend
|
| 859 |
+
resend.api_key = os.getenv("RESEND_API_KEY")
|
| 860 |
+
|
| 861 |
+
# Configure logging
|
| 862 |
+
logging.basicConfig(
|
| 863 |
+
level=logging.INFO,
|
| 864 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 865 |
+
handlers=[
|
| 866 |
+
logging.FileHandler('app.log'),
|
| 867 |
+
logging.StreamHandler()
|
| 868 |
+
]
|
| 869 |
+
)
|
| 870 |
+
logger = logging.getLogger(__name__)
|
| 871 |
+
|
| 872 |
+
# Initialize rate limiter with enhanced security
|
| 873 |
+
limiter = Limiter(key_func=get_remote_address, default_limits=["100/day", "20/hour", "3/minute"])
|
| 874 |
+
|
| 875 |
+
# Create FastAPI app
|
| 876 |
+
app = FastAPI(
|
| 877 |
+
title="JOBObike Chatbot API",
|
| 878 |
+
description="AI-powered chatbot API for JOBObike services",
|
| 879 |
+
version="1.0.0"
|
| 880 |
+
)
|
| 881 |
+
|
| 882 |
+
# Add rate limiter middleware
|
| 883 |
+
app.state.limiter = limiter
|
| 884 |
+
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
| 885 |
+
app.add_middleware(SlowAPIMiddleware)
|
| 886 |
+
|
| 887 |
+
# Configure CORS from environment variable
|
| 888 |
+
allowed_origins = os.getenv("ALLOWED_ORIGINS", "").split(",")
|
| 889 |
+
allowed_origins = [origin.strip() for origin in allowed_origins if origin.strip()]
|
| 890 |
+
|
| 891 |
+
# Always allow common localhost origins for development
|
| 892 |
+
default_origins = [
|
| 893 |
+
"http://localhost:3000",
|
| 894 |
+
"http://localhost:3001",
|
| 895 |
+
"http://localhost:5173",
|
| 896 |
+
"http://localhost:5174",
|
| 897 |
+
"http://127.0.0.1:3000",
|
| 898 |
+
"http://127.0.0.1:3001",
|
| 899 |
+
"http://127.0.0.1:5173",
|
| 900 |
+
"http://127.0.0.1:5174",
|
| 901 |
+
]
|
| 902 |
+
|
| 903 |
+
# Combine default origins with environment origins
|
| 904 |
+
all_origins = default_origins + allowed_origins
|
| 905 |
+
|
| 906 |
+
app.add_middleware(
|
| 907 |
+
CORSMiddleware,
|
| 908 |
+
allow_origins=all_origins,
|
| 909 |
+
allow_credentials=True,
|
| 910 |
+
allow_methods=["*"],
|
| 911 |
+
allow_headers=["*"],
|
| 912 |
+
)
|
| 913 |
+
logger.info(f"CORS enabled for origins: {all_origins}")
|
| 914 |
+
|
| 915 |
+
# Security setup
|
| 916 |
+
security = HTTPBearer()
|
| 917 |
+
|
| 918 |
+
# Enhanced rate limiting dictionaries
|
| 919 |
+
request_counts = defaultdict(list) # Track requests per IP
|
| 920 |
+
TICKET_RATE_LIMIT = 5 # Max 5 tickets per hour per IP
|
| 921 |
+
TICKET_TIME_WINDOW = 3600 # 1 hour in seconds
|
| 922 |
+
MEETING_RATE_LIMIT = 3 # Max 3 meetings per hour per IP
|
| 923 |
+
MEETING_TIME_WINDOW = 3600 # 1 hour in seconds
|
| 924 |
+
|
| 925 |
+
# Request/Response models
|
| 926 |
+
class ChatRequest(BaseModel):
|
| 927 |
+
message: str
|
| 928 |
+
language: Optional[str] = "english" # Default to English if not specified
|
| 929 |
+
session_id: Optional[str] = None # Session ID for chat history
|
| 930 |
+
|
| 931 |
+
|
| 932 |
+
class ChatResponse(BaseModel):
|
| 933 |
+
response: str
|
| 934 |
+
success: bool
|
| 935 |
+
session_id: str # Include session ID in response
|
| 936 |
+
|
| 937 |
+
|
| 938 |
+
class ErrorResponse(BaseModel):
|
| 939 |
+
error: str
|
| 940 |
+
detail: Optional[str] = None
|
| 941 |
+
|
| 942 |
+
|
| 943 |
+
class TicketRequest(BaseModel):
|
| 944 |
+
name: str
|
| 945 |
+
email: str
|
| 946 |
+
message: str
|
| 947 |
+
|
| 948 |
+
|
| 949 |
+
class TicketResponse(BaseModel):
|
| 950 |
+
success: bool
|
| 951 |
+
message: str
|
| 952 |
+
|
| 953 |
+
|
| 954 |
+
class MeetingRequest(BaseModel):
|
| 955 |
+
name: str
|
| 956 |
+
email: str
|
| 957 |
+
date: str # ISO format date string
|
| 958 |
+
time: str # Time in HH:MM format
|
| 959 |
+
timezone: str # Timezone identifier
|
| 960 |
+
duration: int # Duration in minutes
|
| 961 |
+
topic: str # Meeting topic/title
|
| 962 |
+
attendees: list[str] # List of attendee emails
|
| 963 |
+
description: Optional[str] = None # Optional meeting description
|
| 964 |
+
location: Optional[str] = "Google Meet" # Meeting location/platform
|
| 965 |
+
|
| 966 |
+
|
| 967 |
+
class MeetingResponse(BaseModel):
|
| 968 |
+
success: bool
|
| 969 |
+
message: str
|
| 970 |
+
meeting_id: Optional[str] = None # Unique identifier for the meeting
|
| 971 |
+
|
| 972 |
+
|
| 973 |
+
# Security dependency for API key validation
|
| 974 |
+
async def verify_api_key(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
| 975 |
+
"""Verify API key for protected endpoints"""
|
| 976 |
+
# In production, you would check against a database of valid keys
|
| 977 |
+
# For now, we'll use an environment variable
|
| 978 |
+
expected_key = os.getenv("API_KEY")
|
| 979 |
+
if not expected_key or credentials.credentials != expected_key:
|
| 980 |
+
raise HTTPException(
|
| 981 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 982 |
+
detail="Invalid or missing API key",
|
| 983 |
+
)
|
| 984 |
+
return credentials.credentials
|
| 985 |
+
|
| 986 |
+
|
| 987 |
+
def is_ticket_rate_limited(ip_address: str) -> bool:
|
| 988 |
+
"""Check if an IP address has exceeded ticket submission rate limits"""
|
| 989 |
+
current_time = time.time()
|
| 990 |
+
# Clean old requests outside the time window
|
| 991 |
+
request_counts[ip_address] = [
|
| 992 |
+
req_time for req_time in request_counts[ip_address]
|
| 993 |
+
if current_time - req_time < TICKET_TIME_WINDOW
|
| 994 |
+
]
|
| 995 |
+
|
| 996 |
+
# Check if limit exceeded
|
| 997 |
+
if len(request_counts[ip_address]) >= TICKET_RATE_LIMIT:
|
| 998 |
+
return True
|
| 999 |
+
|
| 1000 |
+
# Add current request
|
| 1001 |
+
request_counts[ip_address].append(current_time)
|
| 1002 |
+
return False
|
| 1003 |
+
|
| 1004 |
+
|
| 1005 |
+
def is_meeting_rate_limited(ip_address: str) -> bool:
|
| 1006 |
+
"""Check if an IP address has exceeded meeting scheduling rate limits"""
|
| 1007 |
+
current_time = time.time()
|
| 1008 |
+
# Clean old requests outside the time window
|
| 1009 |
+
request_counts[ip_address] = [
|
| 1010 |
+
req_time for req_time in request_counts[ip_address]
|
| 1011 |
+
if current_time - req_time < MEETING_TIME_WINDOW
|
| 1012 |
+
]
|
| 1013 |
+
|
| 1014 |
+
# Check if limit exceeded
|
| 1015 |
+
if len(request_counts[ip_address]) >= MEETING_RATE_LIMIT:
|
| 1016 |
+
return True
|
| 1017 |
+
|
| 1018 |
+
# Add current request
|
| 1019 |
+
request_counts[ip_address].append(current_time)
|
| 1020 |
+
return False
|
| 1021 |
+
|
| 1022 |
+
|
| 1023 |
+
# def query_jobobike_bot_stream(user_message: str, language: str = "english", session_id: Optional[str] = None):
|
| 1024 |
+
# """
|
| 1025 |
+
# Query the JOBObike bot with streaming - returns async generator.
|
| 1026 |
+
# Now includes language context and session history.
|
| 1027 |
+
# FIXED: Proper spacing between words in streaming responses.
|
| 1028 |
+
# Implements fallback to non-streaming when streaming fails (e.g., with Gemini models).
|
| 1029 |
+
# """
|
| 1030 |
+
# logger.info(f"AGENT STREAM CALL: query_jobobike_bot_stream called with message='{user_message}', language='{language}', session_id='{session_id}'")
|
| 1031 |
+
|
| 1032 |
+
# # Get session history if session_id is provided
|
| 1033 |
+
# history = []
|
| 1034 |
+
# if session_id:
|
| 1035 |
+
# history = session_manager.get_session_history(session_id)
|
| 1036 |
+
# logger.info(f"Retrieved {len(history)} history messages for session {session_id}")
|
| 1037 |
+
|
| 1038 |
+
# try:
|
| 1039 |
+
# # Create context with language preference and history
|
| 1040 |
+
# context_data = {"language": language}
|
| 1041 |
+
# if history:
|
| 1042 |
+
# context_data["history"] = history
|
| 1043 |
+
|
| 1044 |
+
# ctx = RunContextWrapper(context=context_data)
|
| 1045 |
+
|
| 1046 |
+
# result = Runner.run_streamed(
|
| 1047 |
+
# jobobike_assistant,
|
| 1048 |
+
# input=user_message,
|
| 1049 |
+
# context=ctx.context
|
| 1050 |
+
# )
|
| 1051 |
+
|
| 1052 |
+
# async def generate_stream():
|
| 1053 |
+
# try:
|
| 1054 |
+
# accumulated_text = "" # FIXED: Track full response for proper spacing
|
| 1055 |
+
# has_streamed = False
|
| 1056 |
+
|
| 1057 |
+
# try:
|
| 1058 |
+
# # Attempt streaming with error handling for each event
|
| 1059 |
+
# async for event in result.stream_events():
|
| 1060 |
+
# try:
|
| 1061 |
+
# if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent):
|
| 1062 |
+
# delta = event.data.delta or ""
|
| 1063 |
+
|
| 1064 |
+
# # ---- Spacing Fix (CORRECTED) ----
|
| 1065 |
+
# # Check against accumulated text, not just previous chunk
|
| 1066 |
+
# if (
|
| 1067 |
+
# accumulated_text # Only add space if we have previous text
|
| 1068 |
+
# and not accumulated_text.endswith((" ", "\n", "\t")) # Previous doesn't end with whitespace
|
| 1069 |
+
# and not delta.startswith((" ", ".", ",", "?", "!", ":", ";", "\n", "\t", ")", "]", "}", "'", '"')) # Current doesn't start with punctuation/whitespace
|
| 1070 |
+
# and delta # Make sure delta isn't empty
|
| 1071 |
+
# ):
|
| 1072 |
+
# delta = " " + delta
|
| 1073 |
+
|
| 1074 |
+
# accumulated_text += delta # Update accumulated text
|
| 1075 |
+
# # ---- End Fix ----
|
| 1076 |
+
|
| 1077 |
+
# yield f"data: {delta}\n\n"
|
| 1078 |
+
# has_streamed = True
|
| 1079 |
+
# except Exception as event_error:
|
| 1080 |
+
# # Handle individual event errors (e.g., missing logprobs field)
|
| 1081 |
+
# logger.warning(f"Event processing error: {event_error}")
|
| 1082 |
+
# continue
|
| 1083 |
+
|
| 1084 |
+
# # Add complete response to session history
|
| 1085 |
+
# if accumulated_text and session_id:
|
| 1086 |
+
# session_manager.add_message_to_history(session_id, "assistant", accumulated_text)
|
| 1087 |
+
# logger.info(f"Added assistant response to session history: {session_id}")
|
| 1088 |
+
|
| 1089 |
+
# yield "data: [DONE]\n\n"
|
| 1090 |
+
# logger.info("AGENT STREAM RESULT: query_jobobike_bot_stream completed successfully")
|
| 1091 |
+
|
| 1092 |
+
# except Exception as stream_error:
|
| 1093 |
+
# # Fallback to non-streaming if streaming fails
|
| 1094 |
+
# logger.warning(f"Streaming failed, falling back to non-streaming: {stream_error}")
|
| 1095 |
+
|
| 1096 |
+
# if not has_streamed:
|
| 1097 |
+
# # Get final output using the streaming result's final_output property
|
| 1098 |
+
# try:
|
| 1099 |
+
# # Use the non-streaming API as fallback
|
| 1100 |
+
# fallback_response = await Runner.run(
|
| 1101 |
+
# jobobike_assistant,
|
| 1102 |
+
# input=user_message,
|
| 1103 |
+
# context=ctx.context
|
| 1104 |
+
# )
|
| 1105 |
+
|
| 1106 |
+
# if hasattr(fallback_response, 'final_output'):
|
| 1107 |
+
# final_output = fallback_response.final_output
|
| 1108 |
+
# else:
|
| 1109 |
+
# final_output = fallback_response
|
| 1110 |
+
|
| 1111 |
+
# if hasattr(final_output, 'content'):
|
| 1112 |
+
# response_text = final_output.content
|
| 1113 |
+
# elif isinstance(final_output, str):
|
| 1114 |
+
# response_text = final_output
|
| 1115 |
+
# else:
|
| 1116 |
+
# response_text = str(final_output)
|
| 1117 |
+
|
| 1118 |
+
# # Add to session history
|
| 1119 |
+
# if session_id:
|
| 1120 |
+
# session_manager.add_message_to_history(session_id, "assistant", response_text)
|
| 1121 |
+
# logger.info(f"Added fallback assistant response to session history: {session_id}")
|
| 1122 |
+
|
| 1123 |
+
# yield f"data: {response_text}\n\n"
|
| 1124 |
+
# yield "data: [DONE]\n\n"
|
| 1125 |
+
# logger.info("AGENT STREAM RESULT: query_jobobike_bot_stream fallback completed successfully")
|
| 1126 |
+
# except Exception as fallback_error:
|
| 1127 |
+
# logger.error(f"Fallback also failed: {fallback_error}", exc_info=True)
|
| 1128 |
+
# yield f"data: [ERROR] Unable to complete request.\n\n"
|
| 1129 |
+
# else:
|
| 1130 |
+
# # Already streamed some content, just end gracefully
|
| 1131 |
+
# yield "data: [DONE]\n\n"
|
| 1132 |
+
|
| 1133 |
+
# except InputGuardrailTripwireTriggered as e:
|
| 1134 |
+
# logger.warning(f"Guardrail blocked query during streaming: {e}")
|
| 1135 |
+
# yield f"data: [ERROR] Query was blocked by content guardrail.\n\n"
|
| 1136 |
+
|
| 1137 |
+
# except Exception as e:
|
| 1138 |
+
# logger.error(f"Streaming error: {e}", exc_info=True)
|
| 1139 |
+
# yield f"data: [ERROR] {str(e)}\n\n"
|
| 1140 |
+
|
| 1141 |
+
# return generate_stream()
|
| 1142 |
+
|
| 1143 |
+
# except Exception as e:
|
| 1144 |
+
# logger.error(f"Error setting up stream: {e}", exc_info=True)
|
| 1145 |
+
|
| 1146 |
+
# async def error_stream():
|
| 1147 |
+
# yield f"data: [ERROR] Failed to initialize stream.\n\n"
|
| 1148 |
+
|
| 1149 |
+
# return error_stream()
|
| 1150 |
+
|
| 1151 |
+
# def query_jobobike_bot_stream(user_message: str, language: str = "english", session_id: Optional[str] = None):
|
| 1152 |
+
# """
|
| 1153 |
+
# Query the JOBObike bot with streaming - returns async generator.
|
| 1154 |
+
# COMPLETELY FIXED: Simple and reliable spacing logic
|
| 1155 |
+
# """
|
| 1156 |
+
# logger.info(f"AGENT STREAM CALL: query_jobobike_bot_stream called with message='{user_message}', language='{language}', session_id='{session_id}'")
|
| 1157 |
+
|
| 1158 |
+
# # Get session history if session_id is provided
|
| 1159 |
+
# history = []
|
| 1160 |
+
# if session_id:
|
| 1161 |
+
# history = session_manager.get_session_history(session_id)
|
| 1162 |
+
# logger.info(f"Retrieved {len(history)} history messages for session {session_id}")
|
| 1163 |
+
|
| 1164 |
+
# try:
|
| 1165 |
+
# # Create context with language preference and history
|
| 1166 |
+
# context_data = {"language": language}
|
| 1167 |
+
# if history:
|
| 1168 |
+
# context_data["history"] = history
|
| 1169 |
+
|
| 1170 |
+
# ctx = RunContextWrapper(context=context_data)
|
| 1171 |
+
|
| 1172 |
+
# result = Runner.run_streamed(
|
| 1173 |
+
# jobobike_assistant,
|
| 1174 |
+
# input=user_message,
|
| 1175 |
+
# context=ctx.context
|
| 1176 |
+
# )
|
| 1177 |
+
|
| 1178 |
+
# async def generate_stream():
|
| 1179 |
+
# try:
|
| 1180 |
+
# accumulated_text = ""
|
| 1181 |
+
# has_streamed = False
|
| 1182 |
+
|
| 1183 |
+
# try:
|
| 1184 |
+
# async for event in result.stream_events():
|
| 1185 |
+
# try:
|
| 1186 |
+
# if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent):
|
| 1187 |
+
# delta = event.data.delta or ""
|
| 1188 |
+
|
| 1189 |
+
# if not delta:
|
| 1190 |
+
# continue
|
| 1191 |
+
|
| 1192 |
+
# # COMPLETELY FIXED APPROACH: Just send the delta as-is from OpenAI
|
| 1193 |
+
# # OpenAI already includes proper spaces, so we don't need to add them
|
| 1194 |
+
# accumulated_text += delta
|
| 1195 |
+
|
| 1196 |
+
# # Send delta exactly as received
|
| 1197 |
+
# yield f"data: {delta}\n\n"
|
| 1198 |
+
# has_streamed = True
|
| 1199 |
+
|
| 1200 |
+
# except Exception as event_error:
|
| 1201 |
+
# logger.warning(f"Event processing error: {event_error}")
|
| 1202 |
+
# continue
|
| 1203 |
+
|
| 1204 |
+
# # Add complete response to session history
|
| 1205 |
+
# if accumulated_text and session_id:
|
| 1206 |
+
# session_manager.add_message_to_history(session_id, "assistant", accumulated_text)
|
| 1207 |
+
# logger.info(f"Added assistant response to session history: {session_id}")
|
| 1208 |
+
|
| 1209 |
+
# yield "data: [DONE]\n\n"
|
| 1210 |
+
# logger.info("AGENT STREAM RESULT: query_jobobike_bot_stream completed successfully")
|
| 1211 |
+
|
| 1212 |
+
# except Exception as stream_error:
|
| 1213 |
+
# logger.warning(f"Streaming failed, falling back to non-streaming: {stream_error}")
|
| 1214 |
+
|
| 1215 |
+
# if not has_streamed:
|
| 1216 |
+
# try:
|
| 1217 |
+
# fallback_response = await Runner.run(
|
| 1218 |
+
# jobobike_assistant,
|
| 1219 |
+
# input=user_message,
|
| 1220 |
+
# context=ctx.context
|
| 1221 |
+
# )
|
| 1222 |
+
|
| 1223 |
+
# if hasattr(fallback_response, 'final_output'):
|
| 1224 |
+
# final_output = fallback_response.final_output
|
| 1225 |
+
# else:
|
| 1226 |
+
# final_output = fallback_response
|
| 1227 |
+
|
| 1228 |
+
# if hasattr(final_output, 'content'):
|
| 1229 |
+
# response_text = final_output.content
|
| 1230 |
+
# elif isinstance(final_output, str):
|
| 1231 |
+
# response_text = final_output
|
| 1232 |
+
# else:
|
| 1233 |
+
# response_text = str(final_output)
|
| 1234 |
+
|
| 1235 |
+
# if session_id:
|
| 1236 |
+
# session_manager.add_message_to_history(session_id, "assistant", response_text)
|
| 1237 |
+
# logger.info(f"Added fallback assistant response to session history: {session_id}")
|
| 1238 |
+
|
| 1239 |
+
# yield f"data: {response_text}\n\n"
|
| 1240 |
+
# yield "data: [DONE]\n\n"
|
| 1241 |
+
# logger.info("AGENT STREAM RESULT: query_jobobike_bot_stream fallback completed successfully")
|
| 1242 |
+
# except Exception as fallback_error:
|
| 1243 |
+
# logger.error(f"Fallback also failed: {fallback_error}", exc_info=True)
|
| 1244 |
+
# yield f"data: [ERROR] Unable to complete request.\n\n"
|
| 1245 |
+
# else:
|
| 1246 |
+
# yield "data: [DONE]\n\n"
|
| 1247 |
+
|
| 1248 |
+
# except InputGuardrailTripwireTriggered as e:
|
| 1249 |
+
# logger.warning(f"Guardrail blocked query during streaming: {e}")
|
| 1250 |
+
# yield f"data: [ERROR] Query was blocked by content guardrail.\n\n"
|
| 1251 |
+
|
| 1252 |
+
# except Exception as e:
|
| 1253 |
+
# logger.error(f"Streaming error: {e}", exc_info=True)
|
| 1254 |
+
# yield f"data: [ERROR] {str(e)}\n\n"
|
| 1255 |
+
|
| 1256 |
+
# return generate_stream()
|
| 1257 |
+
|
| 1258 |
+
# except Exception as e:
|
| 1259 |
+
# logger.error(f"Error setting up stream: {e}", exc_info=True)
|
| 1260 |
+
|
| 1261 |
+
# async def error_stream():
|
| 1262 |
+
# yield f"data: [ERROR] Failed to initialize stream.\n\n"
|
| 1263 |
+
|
| 1264 |
+
# return error_stream()
|
| 1265 |
+
|
| 1266 |
+
|
| 1267 |
+
def query_jobobike_bot_stream(user_message: str, language: str = "english", session_id: Optional[str] = None):
|
| 1268 |
+
"""
|
| 1269 |
+
Query the JOBObike bot with streaming - FIXED VERSION
|
| 1270 |
+
Simply passes through what Gemini sends without any modification
|
| 1271 |
+
"""
|
| 1272 |
+
logger.info(f"AGENT STREAM CALL: query_jobobike_bot_stream called with message='{user_message}', language='{language}', session_id='{session_id}'")
|
| 1273 |
+
|
| 1274 |
+
# Get session history if session_id is provided
|
| 1275 |
+
history = []
|
| 1276 |
+
if session_id:
|
| 1277 |
+
history = session_manager.get_session_history(session_id)
|
| 1278 |
+
logger.info(f"Retrieved {len(history)} history messages for session {session_id}")
|
| 1279 |
+
|
| 1280 |
+
try:
|
| 1281 |
+
# Create context with language preference and history
|
| 1282 |
+
context_data = {"language": language}
|
| 1283 |
+
if history:
|
| 1284 |
+
context_data["history"] = history
|
| 1285 |
+
|
| 1286 |
+
ctx = RunContextWrapper(context=context_data)
|
| 1287 |
+
|
| 1288 |
+
result = Runner.run_streamed(
|
| 1289 |
+
jobobike_assistant,
|
| 1290 |
+
input=user_message,
|
| 1291 |
+
context=ctx.context
|
| 1292 |
+
)
|
| 1293 |
+
|
| 1294 |
+
async def generate_stream():
|
| 1295 |
+
try:
|
| 1296 |
+
accumulated_text = ""
|
| 1297 |
+
has_streamed = False
|
| 1298 |
+
|
| 1299 |
+
try:
|
| 1300 |
+
async for event in result.stream_events():
|
| 1301 |
+
try:
|
| 1302 |
+
if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent):
|
| 1303 |
+
delta = event.data.delta
|
| 1304 |
+
|
| 1305 |
+
if delta: # Only process if delta has content
|
| 1306 |
+
# CRITICAL: Send delta exactly as received - NO MODIFICATIONS
|
| 1307 |
+
accumulated_text += delta
|
| 1308 |
+
yield f"data: {delta}\n\n"
|
| 1309 |
+
has_streamed = True
|
| 1310 |
+
|
| 1311 |
+
except Exception as event_error:
|
| 1312 |
+
logger.warning(f"Event processing error: {event_error}")
|
| 1313 |
+
continue
|
| 1314 |
+
|
| 1315 |
+
# Add complete response to session history
|
| 1316 |
+
if accumulated_text and session_id:
|
| 1317 |
+
session_manager.add_message_to_history(session_id, "assistant", accumulated_text)
|
| 1318 |
+
logger.info(f"Added assistant response to session history: {session_id}")
|
| 1319 |
+
|
| 1320 |
+
yield "data: [DONE]\n\n"
|
| 1321 |
+
logger.info("AGENT STREAM RESULT: query_jobobike_bot_stream completed successfully")
|
| 1322 |
+
|
| 1323 |
+
except OpenAIBadRequestError as api_error:
|
| 1324 |
+
error_str = str(api_error)
|
| 1325 |
+
logger.error(f"API Error in streaming: {error_str[:300]}", exc_info=True)
|
| 1326 |
+
|
| 1327 |
+
# Check if it's an API key error
|
| 1328 |
+
if "API key" in error_str or "expired" in error_str.lower() or "INVALID_ARGUMENT" in error_str or "API_KEY_INVALID" in error_str:
|
| 1329 |
+
logger.error("Gemini API key error detected in streaming - please check your GEMINI_API_KEY")
|
| 1330 |
+
yield f"data: [ERROR] API key expired. Please update GEMINI_API_KEY in server configuration.\n\n"
|
| 1331 |
+
return
|
| 1332 |
+
else:
|
| 1333 |
+
yield f"data: [ERROR] Invalid request to AI service. Please try again.\n\n"
|
| 1334 |
+
return
|
| 1335 |
+
|
| 1336 |
+
except Exception as stream_error:
|
| 1337 |
+
error_str = str(stream_error)
|
| 1338 |
+
logger.warning(f"Streaming failed, falling back to non-streaming: {error_str[:200]}")
|
| 1339 |
+
|
| 1340 |
+
# Check if it's an API key error before trying fallback
|
| 1341 |
+
if "API key" in error_str or "expired" in error_str.lower() or "INVALID_ARGUMENT" in error_str:
|
| 1342 |
+
logger.error("API key error detected - skipping fallback")
|
| 1343 |
+
yield f"data: [ERROR] API key expired. Please update GEMINI_API_KEY in server configuration.\n\n"
|
| 1344 |
+
return
|
| 1345 |
+
|
| 1346 |
+
if not has_streamed:
|
| 1347 |
+
try:
|
| 1348 |
+
fallback_response = await Runner.run(
|
| 1349 |
+
jobobike_assistant,
|
| 1350 |
+
input=user_message,
|
| 1351 |
+
context=ctx.context
|
| 1352 |
+
)
|
| 1353 |
+
|
| 1354 |
+
if hasattr(fallback_response, 'final_output'):
|
| 1355 |
+
final_output = fallback_response.final_output
|
| 1356 |
+
else:
|
| 1357 |
+
final_output = fallback_response
|
| 1358 |
+
|
| 1359 |
+
if hasattr(final_output, 'content'):
|
| 1360 |
+
response_text = final_output.content
|
| 1361 |
+
elif isinstance(final_output, str):
|
| 1362 |
+
response_text = final_output
|
| 1363 |
+
else:
|
| 1364 |
+
response_text = str(final_output)
|
| 1365 |
+
|
| 1366 |
+
if session_id:
|
| 1367 |
+
session_manager.add_message_to_history(session_id, "assistant", response_text)
|
| 1368 |
+
logger.info(f"Added fallback assistant response to session history: {session_id}")
|
| 1369 |
+
|
| 1370 |
+
yield f"data: {response_text}\n\n"
|
| 1371 |
+
yield "data: [DONE]\n\n"
|
| 1372 |
+
logger.info("AGENT STREAM RESULT: query_jobobike_bot_stream fallback completed successfully")
|
| 1373 |
+
except OpenAIBadRequestError as fallback_api_error:
|
| 1374 |
+
error_str = str(fallback_api_error)
|
| 1375 |
+
logger.error(f"API Error in fallback: {error_str[:300]}", exc_info=True)
|
| 1376 |
+
|
| 1377 |
+
if "API key" in error_str or "expired" in error_str.lower() or "INVALID_ARGUMENT" in error_str or "API_KEY_INVALID" in error_str:
|
| 1378 |
+
logger.error("Gemini API key error in fallback - please check your GEMINI_API_KEY")
|
| 1379 |
+
yield f"data: [ERROR] API key expired. Please update GEMINI_API_KEY in server configuration.\n\n"
|
| 1380 |
+
else:
|
| 1381 |
+
yield f"data: [ERROR] Invalid request to AI service. Please try again.\n\n"
|
| 1382 |
+
except Exception as fallback_error:
|
| 1383 |
+
error_str = str(fallback_error)
|
| 1384 |
+
logger.error(f"Fallback also failed: {error_str[:200]}", exc_info=True)
|
| 1385 |
+
|
| 1386 |
+
if "API key" in error_str or "expired" in error_str.lower() or "INVALID_ARGUMENT" in error_str:
|
| 1387 |
+
yield f"data: [ERROR] API key expired. Please update GEMINI_API_KEY in server configuration.\n\n"
|
| 1388 |
+
else:
|
| 1389 |
+
yield f"data: [ERROR] Unable to complete request. Please try again.\n\n"
|
| 1390 |
+
else:
|
| 1391 |
+
yield "data: [DONE]\n\n"
|
| 1392 |
+
|
| 1393 |
+
except InputGuardrailTripwireTriggered as e:
|
| 1394 |
+
logger.warning(f"Guardrail blocked query during streaming: {e}")
|
| 1395 |
+
yield f"data: [ERROR] Query was blocked by content guardrail.\n\n"
|
| 1396 |
+
|
| 1397 |
+
except OpenAIBadRequestError as e:
|
| 1398 |
+
error_str = str(e)
|
| 1399 |
+
logger.error(f"API Error in streaming: {error_str[:300]}", exc_info=True)
|
| 1400 |
+
|
| 1401 |
+
# Check if it's an API key error
|
| 1402 |
+
if "API key" in error_str or "expired" in error_str.lower() or "INVALID_ARGUMENT" in error_str or "API_KEY_INVALID" in error_str:
|
| 1403 |
+
logger.error("Gemini API key error detected in streaming - please check your GEMINI_API_KEY")
|
| 1404 |
+
yield f"data: [ERROR] API service unavailable. Please check server configuration.\n\n"
|
| 1405 |
+
else:
|
| 1406 |
+
yield f"data: [ERROR] Invalid request to AI service. Please try again.\n\n"
|
| 1407 |
+
|
| 1408 |
+
except Exception as e:
|
| 1409 |
+
error_str = str(e)
|
| 1410 |
+
logger.error(f"Streaming error: {error_str[:200]}", exc_info=True)
|
| 1411 |
+
|
| 1412 |
+
# Check if it's an API key error
|
| 1413 |
+
if "API key" in error_str or "expired" in error_str.lower() or "INVALID_ARGUMENT" in error_str:
|
| 1414 |
+
logger.error("Gemini API key error detected in streaming - please check your GEMINI_API_KEY")
|
| 1415 |
+
yield f"data: [ERROR] API service unavailable. Please check server configuration.\n\n"
|
| 1416 |
+
else:
|
| 1417 |
+
yield f"data: [ERROR] An error occurred. Please try again.\n\n"
|
| 1418 |
+
|
| 1419 |
+
return generate_stream()
|
| 1420 |
+
|
| 1421 |
+
except Exception as e:
|
| 1422 |
+
logger.error(f"Error setting up stream: {e}", exc_info=True)
|
| 1423 |
+
|
| 1424 |
+
async def error_stream():
|
| 1425 |
+
yield f"data: [ERROR] Failed to initialize stream.\n\n"
|
| 1426 |
+
|
| 1427 |
+
return error_stream()
|
| 1428 |
+
|
| 1429 |
+
|
| 1430 |
+
|
| 1431 |
+
|
| 1432 |
+
|
| 1433 |
+
|
| 1434 |
+
|
| 1435 |
+
|
| 1436 |
+
|
| 1437 |
+
|
| 1438 |
+
async def query_jobobike_bot(user_message: str, language: str = "english", session_id: Optional[str] = None):
|
| 1439 |
+
"""
|
| 1440 |
+
Query the JOBObike bot - returns complete response.
|
| 1441 |
+
Now includes language context and session history.
|
| 1442 |
+
"""
|
| 1443 |
+
logger.info(f"AGENT CALL: query_jobobike_bot called with message='{user_message}', language='{language}', session_id='{session_id}'")
|
| 1444 |
+
|
| 1445 |
+
# Get session history if session_id is provided
|
| 1446 |
+
history = []
|
| 1447 |
+
if session_id:
|
| 1448 |
+
history = session_manager.get_session_history(session_id)
|
| 1449 |
+
logger.info(f"Retrieved {len(history)} history messages for session {session_id}")
|
| 1450 |
+
|
| 1451 |
+
try:
|
| 1452 |
+
# Create context with language preference and history
|
| 1453 |
+
context_data = {"language": language}
|
| 1454 |
+
if history:
|
| 1455 |
+
context_data["history"] = history
|
| 1456 |
+
|
| 1457 |
+
ctx = RunContextWrapper(context=context_data)
|
| 1458 |
+
|
| 1459 |
+
response = await Runner.run(
|
| 1460 |
+
jobobike_assistant,
|
| 1461 |
+
input=user_message,
|
| 1462 |
+
context=ctx.context
|
| 1463 |
+
)
|
| 1464 |
+
logger.info("AGENT RESULT: query_jobobike_bot completed successfully")
|
| 1465 |
+
return response.final_output
|
| 1466 |
+
|
| 1467 |
+
except InputGuardrailTripwireTriggered as e:
|
| 1468 |
+
logger.warning(f"Guardrail blocked query: {e}")
|
| 1469 |
+
raise HTTPException(
|
| 1470 |
+
status_code=status.HTTP_403_FORBIDDEN,
|
| 1471 |
+
detail="Query was blocked by content guardrail. Please ensure your query is related to JOBObike services."
|
| 1472 |
+
)
|
| 1473 |
+
except OpenAIBadRequestError as e:
|
| 1474 |
+
error_str = str(e)
|
| 1475 |
+
logger.error(f"API Error in query_jobobike_bot: {error_str[:300]}", exc_info=True)
|
| 1476 |
+
|
| 1477 |
+
# Check if it's an API key error
|
| 1478 |
+
if "API key" in error_str or "expired" in error_str.lower() or "INVALID_ARGUMENT" in error_str or "API_KEY_INVALID" in error_str:
|
| 1479 |
+
logger.error("Gemini API key error detected - please check your GEMINI_API_KEY in .env file")
|
| 1480 |
+
raise HTTPException(
|
| 1481 |
+
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
| 1482 |
+
detail="The API service is currently unavailable. Please check the server configuration or try again later."
|
| 1483 |
+
)
|
| 1484 |
+
|
| 1485 |
+
raise HTTPException(
|
| 1486 |
+
status_code=status.HTTP_400_BAD_REQUEST,
|
| 1487 |
+
detail="Invalid request to the AI service. Please try again."
|
| 1488 |
+
)
|
| 1489 |
+
except Exception as e:
|
| 1490 |
+
error_str = str(e)
|
| 1491 |
+
logger.error(f"Error in query_jobobike_bot: {error_str[:200]}", exc_info=True)
|
| 1492 |
+
|
| 1493 |
+
# Check if it's an API key error in the error message
|
| 1494 |
+
if "API key" in error_str or "expired" in error_str.lower() or "INVALID_ARGUMENT" in error_str:
|
| 1495 |
+
logger.error("Gemini API key error detected - please check your GEMINI_API_KEY in .env file")
|
| 1496 |
+
raise HTTPException(
|
| 1497 |
+
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
| 1498 |
+
detail="The API service is currently unavailable. Please check the server configuration."
|
| 1499 |
+
)
|
| 1500 |
+
|
| 1501 |
+
raise HTTPException(
|
| 1502 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 1503 |
+
detail="An internal error occurred while processing your request."
|
| 1504 |
+
)
|
| 1505 |
+
|
| 1506 |
+
|
| 1507 |
+
@app.get("/")
|
| 1508 |
+
async def root():
|
| 1509 |
+
return {"status": "ok", "service": "JOBObike Chatbot API"}
|
| 1510 |
+
|
| 1511 |
+
|
| 1512 |
+
@app.get("/health")
|
| 1513 |
+
async def health():
|
| 1514 |
+
return {"status": "healthy"}
|
| 1515 |
+
|
| 1516 |
+
|
| 1517 |
+
@app.post("/session")
|
| 1518 |
+
async def create_session():
|
| 1519 |
+
"""
|
| 1520 |
+
Create a new chat session
|
| 1521 |
+
Returns a session ID that can be used to maintain chat history
|
| 1522 |
+
"""
|
| 1523 |
+
try:
|
| 1524 |
+
session_id = session_manager.create_session()
|
| 1525 |
+
logger.info(f"Created new session: {session_id}")
|
| 1526 |
+
return {"session_id": session_id, "message": "Session created successfully"}
|
| 1527 |
+
except Exception as e:
|
| 1528 |
+
logger.error(f"Error creating session: {e}", exc_info=True)
|
| 1529 |
+
raise HTTPException(
|
| 1530 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 1531 |
+
detail="Failed to create session"
|
| 1532 |
+
)
|
| 1533 |
+
|
| 1534 |
+
|
| 1535 |
+
@app.post("/chat", response_model=ChatResponse)
|
| 1536 |
+
@limiter.limit("10/minute") # Limit to 10 requests per minute per IP
|
| 1537 |
+
async def chat(request: Request, chat_request: ChatRequest):
|
| 1538 |
+
"""
|
| 1539 |
+
Standard chat endpoint with language support and session history.
|
| 1540 |
+
Accepts: {"message": "...", "language": "norwegian", "session_id": "optional-session-id"}
|
| 1541 |
+
"""
|
| 1542 |
+
try:
|
| 1543 |
+
# Create or use existing session
|
| 1544 |
+
session_id = chat_request.session_id
|
| 1545 |
+
if not session_id:
|
| 1546 |
+
session_id = session_manager.create_session()
|
| 1547 |
+
logger.info(f"Created new session for chat: {session_id}")
|
| 1548 |
+
|
| 1549 |
+
logger.info(
|
| 1550 |
+
f"Chat request from {get_remote_address(request)}: "
|
| 1551 |
+
f"language={chat_request.language}, message={chat_request.message[:50]}..., session_id={session_id}"
|
| 1552 |
+
)
|
| 1553 |
+
|
| 1554 |
+
# Add user message to session history
|
| 1555 |
+
session_manager.add_message_to_history(session_id, "user", chat_request.message)
|
| 1556 |
+
|
| 1557 |
+
# Pass language and session to the bot
|
| 1558 |
+
response = await query_jobobike_bot(
|
| 1559 |
+
chat_request.message,
|
| 1560 |
+
language=chat_request.language,
|
| 1561 |
+
session_id=session_id
|
| 1562 |
+
)
|
| 1563 |
+
|
| 1564 |
+
if hasattr(response, 'content'):
|
| 1565 |
+
response_text = response.content
|
| 1566 |
+
elif isinstance(response, str):
|
| 1567 |
+
response_text = response
|
| 1568 |
+
else:
|
| 1569 |
+
response_text = str(response)
|
| 1570 |
+
|
| 1571 |
+
# Add bot response to session history
|
| 1572 |
+
session_manager.add_message_to_history(session_id, "assistant", response_text)
|
| 1573 |
+
|
| 1574 |
+
logger.info(f"Chat response generated successfully in {chat_request.language} for session {session_id}")
|
| 1575 |
+
|
| 1576 |
+
return ChatResponse(
|
| 1577 |
+
response=response_text,
|
| 1578 |
+
success=True,
|
| 1579 |
+
session_id=session_id
|
| 1580 |
+
)
|
| 1581 |
+
|
| 1582 |
+
except HTTPException:
|
| 1583 |
+
raise
|
| 1584 |
+
except Exception as e:
|
| 1585 |
+
logger.error(f"Unexpected error in /chat: {e}", exc_info=True)
|
| 1586 |
+
raise HTTPException(
|
| 1587 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 1588 |
+
detail="An internal error occurred while processing your request."
|
| 1589 |
+
)
|
| 1590 |
+
|
| 1591 |
+
|
| 1592 |
+
@app.post("/api/messages", response_model=ChatResponse)
|
| 1593 |
+
@limiter.limit("10/minute") # Same rate limit as /chat
|
| 1594 |
+
async def api_messages(request: Request, chat_request: ChatRequest):
|
| 1595 |
+
"""
|
| 1596 |
+
Frontend-friendly chat endpoint at /api/messages.
|
| 1597 |
+
Exactly mirrors /chat logic for session/history support.
|
| 1598 |
+
Expects: {"message": "...", "language": "english", "session_id": "optional"}
|
| 1599 |
+
"""
|
| 1600 |
+
client_ip = get_remote_address(request)
|
| 1601 |
+
logger.info(f"API Messages request from {client_ip}: message='{chat_request.message[:50]}...', lang='{chat_request.language}', session='{chat_request.session_id}'")
|
| 1602 |
+
|
| 1603 |
+
try:
|
| 1604 |
+
# Create/use session (Firestore-backed)
|
| 1605 |
+
session_id = chat_request.session_id
|
| 1606 |
+
if not session_id:
|
| 1607 |
+
session_id = session_manager.create_session()
|
| 1608 |
+
logger.info(f"New session created for /api/messages: {session_id}")
|
| 1609 |
+
|
| 1610 |
+
# Save user message to history
|
| 1611 |
+
session_manager.add_message_to_history(session_id, "user", chat_request.message)
|
| 1612 |
+
|
| 1613 |
+
# Call your existing bot query function
|
| 1614 |
+
response = await query_jobobike_bot(
|
| 1615 |
+
user_message=chat_request.message,
|
| 1616 |
+
language=chat_request.language,
|
| 1617 |
+
session_id=session_id
|
| 1618 |
+
)
|
| 1619 |
+
|
| 1620 |
+
# Extract response text
|
| 1621 |
+
response_text = (
|
| 1622 |
+
response.content if hasattr(response, 'content')
|
| 1623 |
+
else response if isinstance(response, str)
|
| 1624 |
+
else str(response)
|
| 1625 |
+
)
|
| 1626 |
+
|
| 1627 |
+
# Save AI response to history
|
| 1628 |
+
session_manager.add_message_to_history(session_id, "assistant", response_text)
|
| 1629 |
+
|
| 1630 |
+
logger.info(f"API Messages success: Response sent for session {session_id}")
|
| 1631 |
+
|
| 1632 |
+
return ChatResponse(
|
| 1633 |
+
response=response_text,
|
| 1634 |
+
success=True,
|
| 1635 |
+
session_id=session_id
|
| 1636 |
+
)
|
| 1637 |
+
|
| 1638 |
+
except InputGuardrailTripwireTriggered as e:
|
| 1639 |
+
logger.warning(f"Guardrail blocked /api/messages: {e}")
|
| 1640 |
+
raise HTTPException(
|
| 1641 |
+
status_code=403,
|
| 1642 |
+
detail="Query blocked – please ask about JOBObike services."
|
| 1643 |
+
)
|
| 1644 |
+
except OpenAIBadRequestError as e:
|
| 1645 |
+
error_str = str(e)
|
| 1646 |
+
logger.error(f"API Error in /api/messages: {error_str[:300]}", exc_info=True)
|
| 1647 |
+
|
| 1648 |
+
if "API key" in error_str or "expired" in error_str.lower() or "INVALID_ARGUMENT" in error_str or "API_KEY_INVALID" in error_str:
|
| 1649 |
+
logger.error("Gemini API key error detected in /api/messages")
|
| 1650 |
+
raise HTTPException(
|
| 1651 |
+
status_code=503,
|
| 1652 |
+
detail="AI service is currently unavailable. Please check server configuration."
|
| 1653 |
+
)
|
| 1654 |
+
|
| 1655 |
+
raise HTTPException(
|
| 1656 |
+
status_code=400,
|
| 1657 |
+
detail="Invalid request to AI service. Please try again."
|
| 1658 |
+
)
|
| 1659 |
+
except Exception as e:
|
| 1660 |
+
error_str = str(e)
|
| 1661 |
+
logger.error(f"Error in /api/messages: {error_str[:200]}", exc_info=True)
|
| 1662 |
+
|
| 1663 |
+
if "API key" in error_str or "expired" in error_str.lower() or "INVALID_ARGUMENT" in error_str:
|
| 1664 |
+
logger.error("Gemini API key error detected in /api/messages")
|
| 1665 |
+
raise HTTPException(
|
| 1666 |
+
status_code=503,
|
| 1667 |
+
detail="AI service is currently unavailable. Please check server configuration."
|
| 1668 |
+
)
|
| 1669 |
+
|
| 1670 |
+
raise HTTPException(
|
| 1671 |
+
status_code=500,
|
| 1672 |
+
detail="Internal error – try again."
|
| 1673 |
+
)
|
| 1674 |
+
|
| 1675 |
+
|
| 1676 |
+
@app.post("/chat-stream")
|
| 1677 |
+
@limiter.limit("10/minute") # Limit to 10 requests per minute per IP
|
| 1678 |
+
async def chat_stream(request: Request, chat_request: ChatRequest):
|
| 1679 |
+
"""
|
| 1680 |
+
Streaming chat endpoint with language support and session history.
|
| 1681 |
+
Accepts: {"message": "...", "language": "norwegian", "session_id": "optional-session-id"}
|
| 1682 |
+
"""
|
| 1683 |
+
try:
|
| 1684 |
+
# Create or use existing session
|
| 1685 |
+
session_id = chat_request.session_id
|
| 1686 |
+
if not session_id:
|
| 1687 |
+
session_id = session_manager.create_session()
|
| 1688 |
+
logger.info(f"Created new session for streaming chat: {session_id}")
|
| 1689 |
+
|
| 1690 |
+
logger.info(
|
| 1691 |
+
f"Stream request from {get_remote_address(request)}: "
|
| 1692 |
+
f"language={chat_request.language}, message={chat_request.message[:50]}..., session_id={session_id}"
|
| 1693 |
+
)
|
| 1694 |
+
|
| 1695 |
+
# Add user message to session history
|
| 1696 |
+
session_manager.add_message_to_history(session_id, "user", chat_request.message)
|
| 1697 |
+
|
| 1698 |
+
# Pass language and session to the streaming bot
|
| 1699 |
+
stream_generator = query_jobobike_bot_stream(
|
| 1700 |
+
chat_request.message,
|
| 1701 |
+
language=chat_request.language,
|
| 1702 |
+
session_id=session_id
|
| 1703 |
+
)
|
| 1704 |
+
|
| 1705 |
+
# Note: Response is added to history inside the stream generator after completion
|
| 1706 |
+
|
| 1707 |
+
return StreamingResponse(
|
| 1708 |
+
stream_generator,
|
| 1709 |
+
media_type="text/event-stream",
|
| 1710 |
+
headers={
|
| 1711 |
+
"Cache-Control": "no-cache",
|
| 1712 |
+
"Connection": "keep-alive",
|
| 1713 |
+
"X-Accel-Buffering": "no",
|
| 1714 |
+
"Session-ID": session_id # Include session ID in headers
|
| 1715 |
+
}
|
| 1716 |
+
)
|
| 1717 |
+
|
| 1718 |
+
except HTTPException:
|
| 1719 |
+
raise
|
| 1720 |
+
except Exception as e:
|
| 1721 |
+
logger.error(f"Unexpected error in /chat-stream: {e}", exc_info=True)
|
| 1722 |
+
raise HTTPException(
|
| 1723 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 1724 |
+
detail="An internal error occurred while processing your request."
|
| 1725 |
+
)
|
| 1726 |
+
|
| 1727 |
+
|
| 1728 |
+
@app.post("/ticket", response_model=TicketResponse)
|
| 1729 |
+
@limiter.limit("5/hour") # Limit to 5 tickets per hour per IP
|
| 1730 |
+
async def submit_ticket(request: Request, ticket_request: TicketRequest):
|
| 1731 |
+
"""
|
| 1732 |
+
Submit a support ticket via email using Resend API.
|
| 1733 |
+
Accepts: {"name": "John Doe", "email": "john@example.com", "message": "Issue description"}
|
| 1734 |
+
"""
|
| 1735 |
+
try:
|
| 1736 |
+
client_ip = get_remote_address(request)
|
| 1737 |
+
logger.info(f"Ticket submission request from {ticket_request.name} ({ticket_request.email}) - IP: {client_ip}")
|
| 1738 |
+
|
| 1739 |
+
# Additional rate limiting for tickets
|
| 1740 |
+
if is_ticket_rate_limited(client_ip):
|
| 1741 |
+
logger.warning(f"Rate limit exceeded for ticket submission from IP: {client_ip}")
|
| 1742 |
+
raise HTTPException(
|
| 1743 |
+
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
| 1744 |
+
detail="Too many ticket submissions. Please try again later."
|
| 1745 |
+
)
|
| 1746 |
+
|
| 1747 |
+
# Get admin email from environment variables or use a default
|
| 1748 |
+
admin_email = os.getenv("ADMIN_EMAIL", "admin@yourcompany.com")
|
| 1749 |
+
|
| 1750 |
+
# Use a verified sender email (you need to verify this in your Resend account)
|
| 1751 |
+
# For testing purposes, you can use your Resend account's verified domain
|
| 1752 |
+
sender_email = os.getenv("SENDER_EMAIL", "onboarding@resend.dev")
|
| 1753 |
+
|
| 1754 |
+
# Prepare the email using Resend
|
| 1755 |
+
params = {
|
| 1756 |
+
"from": sender_email,
|
| 1757 |
+
"to": [admin_email],
|
| 1758 |
+
"subject": f"Support Ticket from {ticket_request.name}",
|
| 1759 |
+
"html": f"""
|
| 1760 |
+
<p>Hello Admin,</p>
|
| 1761 |
+
<p>A new support ticket has been submitted:</p>
|
| 1762 |
+
<p><strong>Name:</strong> {ticket_request.name}</p>
|
| 1763 |
+
<p><strong>Email:</strong> {ticket_request.email}</p>
|
| 1764 |
+
<p><strong>Message:</strong></p>
|
| 1765 |
+
<p>{ticket_request.message}</p>
|
| 1766 |
+
<p><strong>IP Address:</strong> {client_ip}</p>
|
| 1767 |
+
<br>
|
| 1768 |
+
<p>Best regards,<br>JOBObike Support Team</p>
|
| 1769 |
+
"""
|
| 1770 |
+
}
|
| 1771 |
+
|
| 1772 |
+
# Send the email
|
| 1773 |
+
email = resend.Emails.send(params)
|
| 1774 |
+
|
| 1775 |
+
logger.info(f"Ticket submitted successfully by {ticket_request.name} from IP: {client_ip}")
|
| 1776 |
+
|
| 1777 |
+
return TicketResponse(
|
| 1778 |
+
success=True,
|
| 1779 |
+
message="Ticket submitted successfully. We'll get back to you soon."
|
| 1780 |
+
)
|
| 1781 |
+
|
| 1782 |
+
except HTTPException:
|
| 1783 |
+
raise
|
| 1784 |
+
except Exception as e:
|
| 1785 |
+
logger.error(f"Error submitting ticket: {e}", exc_info=True)
|
| 1786 |
+
raise HTTPException(
|
| 1787 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 1788 |
+
detail="Failed to submit ticket. Please try again later."
|
| 1789 |
+
)
|
| 1790 |
+
|
| 1791 |
+
|
| 1792 |
+
@app.post("/schedule-meeting", response_model=MeetingResponse)
|
| 1793 |
+
@limiter.limit("3/hour") # Limit to 3 meetings per hour per IP
|
| 1794 |
+
async def schedule_meeting(request: Request, meeting_request: MeetingRequest):
|
| 1795 |
+
"""
|
| 1796 |
+
Schedule a meeting and send email invitations using Resend API.
|
| 1797 |
+
Accepts meeting details and sends professional email invitations to organizer and attendees.
|
| 1798 |
+
"""
|
| 1799 |
+
try:
|
| 1800 |
+
client_ip = get_remote_address(request)
|
| 1801 |
+
logger.info(f"Meeting scheduling request from {meeting_request.name} ({meeting_request.email}) - IP: {client_ip}")
|
| 1802 |
+
|
| 1803 |
+
# Additional rate limiting for meetings
|
| 1804 |
+
if is_meeting_rate_limited(client_ip):
|
| 1805 |
+
logger.warning(f"Rate limit exceeded for meeting scheduling from IP: {client_ip}")
|
| 1806 |
+
raise HTTPException(
|
| 1807 |
+
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
| 1808 |
+
detail="Too many meeting requests. Please try again later."
|
| 1809 |
+
)
|
| 1810 |
+
|
| 1811 |
+
# Generate a unique meeting ID
|
| 1812 |
+
meeting_id = f"mtg_{int(time.time())}"
|
| 1813 |
+
|
| 1814 |
+
# Get admin email from environment variables or use a default
|
| 1815 |
+
admin_email = os.getenv("ADMIN_EMAIL", "admin@yourcompany.com")
|
| 1816 |
+
|
| 1817 |
+
# Use a verified sender email (you need to verify this in your Resend account)
|
| 1818 |
+
sender_email = os.getenv("SENDER_EMAIL", "onboarding@resend.dev")
|
| 1819 |
+
|
| 1820 |
+
# For Resend testing limitations, we can only send to the owner's email
|
| 1821 |
+
# In production, you would verify a domain and use that instead
|
| 1822 |
+
owner_email = os.getenv("ADMIN_EMAIL", "admin@yourcompany.com")
|
| 1823 |
+
|
| 1824 |
+
# Format date and time for display
|
| 1825 |
+
formatted_datetime = f"{meeting_request.date} at {meeting_request.time} {meeting_request.timezone}"
|
| 1826 |
+
|
| 1827 |
+
# Create calendar link (Google Calendar link example)
|
| 1828 |
+
calendar_link = f"https://calendar.google.com/calendar/render?action=TEMPLATE&text={meeting_request.topic}&dates={meeting_request.date.replace('-', '')}T{meeting_request.time.replace(':', '')}00Z/{meeting_request.date.replace('-', '')}T{meeting_request.time.replace(':', '')}00Z&details={meeting_request.description or 'Meeting scheduled via JOBObike'}&location={meeting_request.location}"
|
| 1829 |
+
|
| 1830 |
+
# Combine all attendees (organizer + additional attendees)
|
| 1831 |
+
# Validate and format email addresses
|
| 1832 |
+
all_attendees = [meeting_request.email]
|
| 1833 |
+
|
| 1834 |
+
# Validate additional attendees - they must be valid email addresses
|
| 1835 |
+
for attendee in meeting_request.attendees:
|
| 1836 |
+
# Simple email validation
|
| 1837 |
+
if "@" in attendee and "." in attendee:
|
| 1838 |
+
all_attendees.append(attendee)
|
| 1839 |
+
else:
|
| 1840 |
+
# If not a valid email, skip or treat as name only
|
| 1841 |
+
logger.warning(f"Invalid email format for attendee: {attendee}. Skipping.")
|
| 1842 |
+
|
| 1843 |
+
# Remove duplicates while preserving order
|
| 1844 |
+
seen = set()
|
| 1845 |
+
unique_attendees = []
|
| 1846 |
+
for email in all_attendees:
|
| 1847 |
+
if email not in seen:
|
| 1848 |
+
seen.add(email)
|
| 1849 |
+
unique_attendees.append(email)
|
| 1850 |
+
all_attendees = unique_attendees
|
| 1851 |
+
|
| 1852 |
+
# Prepare the professional HTML email template
|
| 1853 |
+
html_template = f"""
|
| 1854 |
+
<!DOCTYPE html>
|
| 1855 |
+
<html>
|
| 1856 |
+
<head>
|
| 1857 |
+
<meta charset="UTF-8">
|
| 1858 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 1859 |
+
<title>Meeting Scheduled - {meeting_request.topic}</title>
|
| 1860 |
+
</head>
|
| 1861 |
+
<body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 600px; margin: 0 auto; padding: 20px;">
|
| 1862 |
+
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 30px; text-align: center; border-radius: 10px 10px 0 0;">
|
| 1863 |
+
<h1 style="margin: 0; font-size: 28px;">Meeting Confirmed!</h1>
|
| 1864 |
+
<p style="font-size: 18px; margin-top: 10px;">Your meeting has been successfully scheduled</p>
|
| 1865 |
+
</div>
|
| 1866 |
+
|
| 1867 |
+
<div style="background-color: #ffffff; padding: 30px; border: 1px solid #eaeaea; border-top: none; border-radius: 0 0 10px 10px;">
|
| 1868 |
+
<h2 style="color: #333;">Meeting Details</h2>
|
| 1869 |
+
|
| 1870 |
+
<div style="background-color: #f8f9fa; padding: 20px; border-radius: 8px; margin: 20px 0;">
|
| 1871 |
+
<table style="width: 100%; border-collapse: collapse;">
|
| 1872 |
+
<tr>
|
| 1873 |
+
<td style="padding: 8px 0; font-weight: bold; width: 30%;">Topic:</td>
|
| 1874 |
+
<td style="padding: 8px 0;">{meeting_request.topic}</td>
|
| 1875 |
+
</tr>
|
| 1876 |
+
<tr style="background-color: #f0f0f0;">
|
| 1877 |
+
<td style="padding: 8px 0; font-weight: bold;">Date & Time:</td>
|
| 1878 |
+
<td style="padding: 8px 0;">{formatted_datetime}</td>
|
| 1879 |
+
</tr>
|
| 1880 |
+
<tr>
|
| 1881 |
+
<td style="padding: 8px 0; font-weight: bold;">Duration:</td>
|
| 1882 |
+
<td style="padding: 8px 0;">{meeting_request.duration} minutes</td>
|
| 1883 |
+
</tr>
|
| 1884 |
+
<tr style="background-color: #f0f0f0;">
|
| 1885 |
+
<td style="padding: 8px 0; font-weight: bold;">Location:</td>
|
| 1886 |
+
<td style="padding: 8px 0;">{meeting_request.location}</td>
|
| 1887 |
+
</tr>
|
| 1888 |
+
<tr>
|
| 1889 |
+
<td style="padding: 8px 0; font-weight: bold;">Organizer:</td>
|
| 1890 |
+
<td style="padding: 8px 0;">{meeting_request.name} ({meeting_request.email})</td>
|
| 1891 |
+
</tr>
|
| 1892 |
+
</table>
|
| 1893 |
+
</div>
|
| 1894 |
+
|
| 1895 |
+
<div style="margin: 25px 0;">
|
| 1896 |
+
<h3 style="color: #333;">Description</h3>
|
| 1897 |
+
<p style="background-color: #f8f9fa; padding: 15px; border-radius: 8px; white-space: pre-wrap;">{meeting_request.description or 'No description provided.'}</p>
|
| 1898 |
+
</div>
|
| 1899 |
+
|
| 1900 |
+
<div style="margin: 25px 0;">
|
| 1901 |
+
<h3 style="color: #333;">Attendees</h3>
|
| 1902 |
+
<ul style="background-color: #f8f9fa; padding: 15px; border-radius: 8px;">
|
| 1903 |
+
{''.join([f'<li>{attendee}</li>' for attendee in all_attendees])}
|
| 1904 |
+
</ul>
|
| 1905 |
+
<p style="font-size: 12px; color: #666; margin-top: 5px;">Note: Only valid email addresses will receive invitations.</p>
|
| 1906 |
+
</div>
|
| 1907 |
+
|
| 1908 |
+
<div style="text-align: center; margin: 30px 0;">
|
| 1909 |
+
<a href="{calendar_link}" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 12px 25px; text-decoration: none; border-radius: 5px; font-weight: bold; display: inline-block;">Add to Calendar</a>
|
| 1910 |
+
</div>
|
| 1911 |
+
|
| 1912 |
+
<div style="background-color: #e3f2fd; padding: 15px; border-radius: 8px; margin-top: 25px;">
|
| 1913 |
+
<p style="margin: 0;"><strong>Meeting ID:</strong> {meeting_id}</p>
|
| 1914 |
+
<p style="margin: 10px 0 0 0; font-size: 14px; color: #666;">Need to make changes? Contact the organizer or reply to this email.</p>
|
| 1915 |
+
</div>
|
| 1916 |
+
</div>
|
| 1917 |
+
|
| 1918 |
+
<div style="text-align: center; margin-top: 30px; color: #888; font-size: 14px;">
|
| 1919 |
+
<p>This meeting was scheduled through JOBObike Chatbot Services</p>
|
| 1920 |
+
<p><strong>Note:</strong> Due to Resend testing limitations, this email is only sent to the administrator. In production, after domain verification, invitations will be sent to all attendees.</p>
|
| 1921 |
+
<p>© 2025 JOBObike. All rights reserved.</p>
|
| 1922 |
+
</div>
|
| 1923 |
+
</body>
|
| 1924 |
+
</html>
|
| 1925 |
+
"""
|
| 1926 |
+
|
| 1927 |
+
# Send email to all attendees
|
| 1928 |
+
# Check if we have valid attendees to send to
|
| 1929 |
+
if not all_attendees:
|
| 1930 |
+
logger.warning("No valid email addresses found for meeting attendees")
|
| 1931 |
+
return MeetingResponse(
|
| 1932 |
+
success=True,
|
| 1933 |
+
message="Meeting scheduled successfully, but no valid email addresses found for invitations.",
|
| 1934 |
+
meeting_id=meeting_id
|
| 1935 |
+
)
|
| 1936 |
+
|
| 1937 |
+
# For Resend testing limitations, we can only send to the owner's email
|
| 1938 |
+
# In production, you would verify a domain and send to all attendees
|
| 1939 |
+
owner_email = os.getenv("ADMIN_EMAIL", "admin@yourcompany.com")
|
| 1940 |
+
|
| 1941 |
+
# Prepare email for owner with all attendee information
|
| 1942 |
+
attendee_list_html = ''.join([f'<li>{attendee}</li>' for attendee in all_attendees])
|
| 1943 |
+
# In a real implementation, you would send to all attendees after verifying your domain
|
| 1944 |
+
# For now, we're sending to the owner with information about all attendees
|
| 1945 |
+
|
| 1946 |
+
params = {
|
| 1947 |
+
"from": sender_email,
|
| 1948 |
+
"to": [owner_email], # Only send to owner due to Resend testing limitations
|
| 1949 |
+
"subject": f"Meeting Scheduled: {meeting_request.topic}",
|
| 1950 |
+
"html": html_template
|
| 1951 |
+
}
|
| 1952 |
+
|
| 1953 |
+
# Send the email
|
| 1954 |
+
try:
|
| 1955 |
+
email = resend.Emails.send(params)
|
| 1956 |
+
logger.info(f"Email sent successfully to {len(all_attendees)} attendees")
|
| 1957 |
+
except Exception as email_error:
|
| 1958 |
+
logger.error(f"Failed to send email: {email_error}", exc_info=True)
|
| 1959 |
+
# Even if email fails, we still consider the meeting scheduled
|
| 1960 |
+
return MeetingResponse(
|
| 1961 |
+
success=True,
|
| 1962 |
+
message="Meeting scheduled successfully, but failed to send email invitations.",
|
| 1963 |
+
meeting_id=meeting_id
|
| 1964 |
+
)
|
| 1965 |
+
|
| 1966 |
+
logger.info(f"Meeting scheduled successfully by {meeting_request.name} from IP: {client_ip}")
|
| 1967 |
+
|
| 1968 |
+
return MeetingResponse(
|
| 1969 |
+
success=True,
|
| 1970 |
+
message="Meeting scheduled successfully. Due to Resend testing limitations, invitations are only sent to the administrator. In production, after verifying your domain, invitations will be sent to all attendees.",
|
| 1971 |
+
meeting_id=meeting_id
|
| 1972 |
+
)
|
| 1973 |
+
|
| 1974 |
+
except HTTPException:
|
| 1975 |
+
raise
|
| 1976 |
+
except Exception as e:
|
| 1977 |
+
logger.error(f"Error scheduling meeting: {e}", exc_info=True)
|
| 1978 |
+
raise HTTPException(
|
| 1979 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 1980 |
+
detail="Failed to schedule meeting. Please try again later."
|
| 1981 |
+
)
|
| 1982 |
+
|
| 1983 |
+
|
| 1984 |
+
@app.exception_handler(Exception)
|
| 1985 |
+
async def global_exception_handler(request: Request, exc: Exception):
|
| 1986 |
+
logger.error(
|
| 1987 |
+
f"Unhandled exception: {exc}",
|
| 1988 |
+
exc_info=True,
|
| 1989 |
+
extra={
|
| 1990 |
+
"path": request.url.path,
|
| 1991 |
+
"method": request.method,
|
| 1992 |
+
"client": get_remote_address(request)
|
| 1993 |
+
}
|
| 1994 |
+
)
|
| 1995 |
+
|
| 1996 |
+
return JSONResponse(
|
| 1997 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 1998 |
+
content={
|
| 1999 |
+
"error": "Internal server error",
|
| 2000 |
+
"detail": "An unexpected error occurred. Please try again later."
|
| 2001 |
+
}
|
| 2002 |
+
)
|
| 2003 |
+
|
| 2004 |
+
|
| 2005 |
+
if __name__ == "__main__":
|
| 2006 |
+
import uvicorn
|
| 2007 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
chatbot/chatbot_agent.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from agents import Agent
|
| 2 |
+
from config.chabot_config import model
|
| 3 |
+
from instructions.chatbot_instructions import jobobike_dynamic_instructions
|
| 4 |
+
from guardrails.guardrails_input_function import guardrail_input_function
|
| 5 |
+
from tools.document_reader_tool import read_document_data, list_available_documents
|
| 6 |
+
|
| 7 |
+
jobobike_assistant = Agent(
|
| 8 |
+
name="JOBObike Assistant",
|
| 9 |
+
instructions=jobobike_dynamic_instructions,
|
| 10 |
+
model=model,
|
| 11 |
+
input_guardrails=[guardrail_input_function],
|
| 12 |
+
tools=[read_document_data, list_available_documents], # Document reading tools
|
| 13 |
+
)
|
config/chabot_config.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from agents import AsyncOpenAI, OpenAIChatCompletionsModel, set_tracing_disabled
|
| 4 |
+
|
| 5 |
+
set_tracing_disabled(True)
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
# Get Gemini API key from environment variables
|
| 9 |
+
gemini_api_key = os.getenv("GEMINI_API_KEY")
|
| 10 |
+
|
| 11 |
+
# Remove quotes if present (sometimes .env files have quotes)
|
| 12 |
+
if gemini_api_key:
|
| 13 |
+
gemini_api_key = gemini_api_key.strip().strip('"').strip("'")
|
| 14 |
+
|
| 15 |
+
# Check if Gemini API key is set
|
| 16 |
+
if not gemini_api_key:
|
| 17 |
+
raise ValueError(
|
| 18 |
+
"GEMINI_API_KEY is not set. Please add it to your .env file: GEMINI_API_KEY=your_key_here"
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# Validate API key format (Gemini keys typically start with AIza)
|
| 22 |
+
if not gemini_api_key.startswith("AIza"):
|
| 23 |
+
print(f"Warning: GEMINI_API_KEY format may be incorrect. Keys usually start with 'AIza'. Got: {gemini_api_key[:10]}...")
|
| 24 |
+
|
| 25 |
+
# Configure Gemini using AsyncOpenAI with Gemini's OpenAI-compatible endpoint
|
| 26 |
+
client_provider = AsyncOpenAI(
|
| 27 |
+
api_key=gemini_api_key,
|
| 28 |
+
base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Use Gemini model - gemini-1.5-pro or gemini-1.5-flash are good options
|
| 32 |
+
# gemini-1.5-flash is faster, gemini-1.5-pro is more capable
|
| 33 |
+
model = OpenAIChatCompletionsModel(
|
| 34 |
+
model="gemini-1.5-flash", # Using Gemini 1.5 Flash for fast responses
|
| 35 |
+
openai_client=client_provider
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
print("Setup complete! Model ready with Google Gemini 1.5 Flash")
|
| 39 |
+
print(f"API Key status: {'Loaded' if gemini_api_key else 'Missing'} (length: {len(gemini_api_key) if gemini_api_key else 0})")
|
data.docx
ADDED
|
Binary file (18 kB). View file
|
|
|
guardrails/guardrails_input_function.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import traceback
|
| 2 |
+
from agents import RunContextWrapper, Runner, GuardrailFunctionOutput, input_guardrail
|
| 3 |
+
from guardrails.input_guardrails import guardrail_agent
|
| 4 |
+
|
| 5 |
+
@input_guardrail
|
| 6 |
+
async def guardrail_input_function(ctx: RunContextWrapper, agent, user_input: str):
|
| 7 |
+
try:
|
| 8 |
+
result = await Runner.run(
|
| 9 |
+
guardrail_agent,
|
| 10 |
+
input=user_input,
|
| 11 |
+
context=ctx.context
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Check if result has the expected structure
|
| 15 |
+
if not result or not hasattr(result, 'final_output'):
|
| 16 |
+
print(f"Warning: Guardrail agent returned unexpected result: {result}")
|
| 17 |
+
# Allow the query to proceed if guardrail fails
|
| 18 |
+
return GuardrailFunctionOutput(
|
| 19 |
+
output_info=None,
|
| 20 |
+
tripwire_triggered=False
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
final_output = result.final_output
|
| 24 |
+
|
| 25 |
+
# Check if final_output has the expected attribute
|
| 26 |
+
if not hasattr(final_output, 'is_query_about_jobobike'):
|
| 27 |
+
print(f"Warning: Guardrail output missing is_query_about_jobobike attribute: {final_output}")
|
| 28 |
+
return GuardrailFunctionOutput(
|
| 29 |
+
output_info=final_output,
|
| 30 |
+
tripwire_triggered=False
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
return GuardrailFunctionOutput(
|
| 34 |
+
output_info=final_output,
|
| 35 |
+
tripwire_triggered=not final_output.is_query_about_jobobike
|
| 36 |
+
)
|
| 37 |
+
except Exception as e:
|
| 38 |
+
error_str = str(e)
|
| 39 |
+
# Check if it's an API key error
|
| 40 |
+
if "API key" in error_str or "expired" in error_str.lower() or "INVALID_ARGUMENT" in error_str:
|
| 41 |
+
print(f"API key error in guardrail - allowing query through: {error_str[:100]}")
|
| 42 |
+
else:
|
| 43 |
+
print(f"Error in guardrail_input_function: {error_str[:200]}")
|
| 44 |
+
print(traceback.format_exc())
|
| 45 |
+
# Always allow the query to proceed if guardrail fails (especially for API errors)
|
| 46 |
+
return GuardrailFunctionOutput(
|
| 47 |
+
output_info=None,
|
| 48 |
+
tripwire_triggered=False
|
| 49 |
+
)
|
guardrails/input_guardrails.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from agents import Agent
|
| 2 |
+
from config.chabot_config import model
|
| 3 |
+
from schema.chatbot_schema import OutputType
|
| 4 |
+
|
| 5 |
+
guardrail_agent = Agent(
|
| 6 |
+
name="JOBObike Guardrail Agent",
|
| 7 |
+
instructions="""
|
| 8 |
+
You are a guardrail assistant that validates if the user's query is about JOBObike e-commerce platform,
|
| 9 |
+
bicycles, cycling products, orders, shopping, customer service, or FAQs.
|
| 10 |
+
|
| 11 |
+
IMPORTANT: Allow general greetings, neutral questions, and queries that could lead to JOBObike-related conversations.
|
| 12 |
+
Only block queries that are clearly unrelated (e.g., asking about cooking recipes, weather, completely unrelated products not related to cycling).
|
| 13 |
+
|
| 14 |
+
- Set is_query_about_jobobike=True if:
|
| 15 |
+
* The query is directly about JOBObike products, services, orders, shipping, returns, or customer support
|
| 16 |
+
* The query is about bicycles, cycling equipment, or bike-related topics
|
| 17 |
+
* The query is a general greeting (hello, hi, how can you help, etc.)
|
| 18 |
+
* The query is neutral and could lead to a JOBObike shopping conversation
|
| 19 |
+
* The query asks about products, pricing, orders, delivery, or support
|
| 20 |
+
|
| 21 |
+
- Set is_query_about_jobobike=False ONLY if:
|
| 22 |
+
* The query is clearly about completely unrelated topics that have no connection to e-commerce, cycling, or JOBObike (cooking recipes, unrelated news, etc.)
|
| 23 |
+
* The query is spam or malicious
|
| 24 |
+
|
| 25 |
+
- Always provide a clear reason for your decision.
|
| 26 |
+
""",
|
| 27 |
+
model=model,
|
| 28 |
+
output_type=OutputType,
|
| 29 |
+
)
|
instructions/chatbot_instructions.py
ADDED
|
@@ -0,0 +1,580 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# from agents import RunContextWrapper
|
| 2 |
+
# def jobobike_dynamic_instructions(ctx: RunContextWrapper, agent) -> str:
|
| 3 |
+
# """Create dynamic instructions for JOBObike chatbot queries with language context."""
|
| 4 |
+
|
| 5 |
+
# # Get user's selected language from context
|
| 6 |
+
# user_lang = ctx.context.get("language", "english").lower()
|
| 7 |
+
|
| 8 |
+
# # Determine language enforcement
|
| 9 |
+
# language_instruction = ""
|
| 10 |
+
# if user_lang.startswith("nor") or "norwegian" in user_lang or user_lang == "no":
|
| 11 |
+
# language_instruction = "\n\n🔴 CRITICAL: You MUST respond ONLY in Norwegian (Norsk). Do NOT use English unless the user explicitly requests it."
|
| 12 |
+
# elif user_lang.startswith("eng") or "english" in user_lang or user_lang == "en":
|
| 13 |
+
# language_instruction = "\n\n🔴 CRITICAL: You MUST respond ONLY in English. Do NOT use Norwegian unless the user explicitly requests it."
|
| 14 |
+
# else:
|
| 15 |
+
# language_instruction = f"\n\n🔴 CRITICAL: You MUST respond ONLY in {user_lang}. Do NOT use any other language unless the user explicitly requests it."
|
| 16 |
+
|
| 17 |
+
# instructions = """
|
| 18 |
+
# # LAUNCHLABS ASSISTANT - CORE INSTRUCTIONS
|
| 19 |
+
|
| 20 |
+
# ## ROLE
|
| 21 |
+
# You are JOBObike Assistant – the official AI assistant for JOBObike (jobobike.no).
|
| 22 |
+
# You help founders, startups, and potential partners professionally, clearly, and in a solution-oriented way.
|
| 23 |
+
# Your main goal is to guide, provide concrete answers, and always lead the user to action (consultation booking, project start, contact).
|
| 24 |
+
|
| 25 |
+
# ## ABOUT LAUNCHLABS
|
| 26 |
+
# JOBObike helps ambitious startups transform ideas into successful companies using:
|
| 27 |
+
# · Full brand development
|
| 28 |
+
# · Website and app creation
|
| 29 |
+
# · AI-driven integrations
|
| 30 |
+
# · Automation and workflow solutions
|
| 31 |
+
|
| 32 |
+
# We focus on customized solutions, speed, innovation, and long-term partnership with clients.
|
| 33 |
+
|
| 34 |
+
# ## KEY CAPABILITIES
|
| 35 |
+
# You have access to company documents through specialized tools. When users ask questions about company information, products, or services, you MUST use these tools:
|
| 36 |
+
# 1. `list_available_documents()` - List all available documents
|
| 37 |
+
# 2. `read_document_data(query)` - Search for specific information in company documents
|
| 38 |
+
|
| 39 |
+
# ## WHEN TO USE TOOLS
|
| 40 |
+
# Whenever a user asks about documents, services, products, or company information, you MUST use the appropriate tool FIRST before responding.
|
| 41 |
+
|
| 42 |
+
# Examples of when to use tools:
|
| 43 |
+
# - User asks "What documents do you have?" → Use `list_available_documents()`
|
| 44 |
+
# - User asks "What services do you offer?" → Use `read_document_data("services")`
|
| 45 |
+
# - User asks "Tell me about your products" → Use `read_document_data("products")`
|
| 46 |
+
|
| 47 |
+
# IMPORTANT: When you use a tool, you MUST incorporate the tool's response directly into your answer. Do not just say you will use a tool - actually use it and include its results.
|
| 48 |
+
|
| 49 |
+
# Example of correct response:
|
| 50 |
+
# User: "What documents do you have?"
|
| 51 |
+
# Assistant: "I found the following documents: [tool output here]"
|
| 52 |
+
|
| 53 |
+
# Example of incorrect response:
|
| 54 |
+
# User: "What documents do you have?"
|
| 55 |
+
# Assistant: "I will now use the tool to get this information."
|
| 56 |
+
|
| 57 |
+
# Always execute tools and show their results.
|
| 58 |
+
|
| 59 |
+
# JOBObike is located in Norway and must know this - answer questions about location correctly.
|
| 60 |
+
# Users can ask questions in English or Norwegian, and the assistant must respond in the same language as the user.
|
| 61 |
+
|
| 62 |
+
# ## RESPONSE GUIDELINES
|
| 63 |
+
# - Professional, confident, and direct.
|
| 64 |
+
# - Avoid vague responses. Always suggest next steps:
|
| 65 |
+
# · “Do you want me to schedule a consultation?”
|
| 66 |
+
# · “Do you want me to connect you with a project manager?”
|
| 67 |
+
# · “Do you want me to send you our portfolio?”
|
| 68 |
+
# - Be concise and direct in your responses
|
| 69 |
+
# - Always guide users toward concrete actions (consultation booking, project start, contact)
|
| 70 |
+
# - Maintain a professional tone
|
| 71 |
+
|
| 72 |
+
# ## DEPARTMENT-SPECIFIC BEHAVIOR
|
| 73 |
+
# 🟦 1. SALES / NEW PROJECTS
|
| 74 |
+
# Purpose: Help the user understand JOBObike’ offerings and start new projects.
|
| 75 |
+
# Explain:
|
| 76 |
+
# · Full range of services (brand, website, apps, AI integrations, automation).
|
| 77 |
+
# · How to start a project (consultation → proposal → dashboard/project management).
|
| 78 |
+
# · Pricing and custom packages.
|
| 79 |
+
# Example: “JOBObike helps startups turn ideas into businesses with branding, websites, apps, and AI solutions. Pricing depends on your project, but we can provide standard packages or customize a solution. Do you want me to schedule a consultation now?”
|
| 80 |
+
|
| 81 |
+
# 🟩 2. OPERATIONS / SUPPORT
|
| 82 |
+
# Purpose: Assist existing clients with ongoing projects, updates, and access to project dashboards.
|
| 83 |
+
# · Explain how to access project dashboards.
|
| 84 |
+
# · Provide guidance for reporting issues or questions.
|
| 85 |
+
# · Inform about response times and escalation.
|
| 86 |
+
# Example: “You can access your project dashboard via jobobike.no. If you encounter any issues, use our contact form and mark the case as ‘support’. Do you want me to send you the link now?”
|
| 87 |
+
|
| 88 |
+
# 🟥 3. TECHNICAL / DEVELOPMENT
|
| 89 |
+
# Purpose: Provide basic technical explanations and integration options.
|
| 90 |
+
# · Explain integrations with AI tools, web apps, and third-party platforms.
|
| 91 |
+
# · Offer connection to technical/development team if needed.
|
| 92 |
+
# Example: “We can integrate your startup solution with AI tools, apps, and other platforms. Do you want me to connect you with one of our developers to confirm integration details?”
|
| 93 |
+
|
| 94 |
+
# 🟨 4. DASHBOARD / PROJECT MANAGEMENT
|
| 95 |
+
# Purpose: Help users understand the project dashboard.
|
| 96 |
+
# Explain:
|
| 97 |
+
# · Where the dashboard is located.
|
| 98 |
+
# · What it shows (tasks, deadlines, project progress, invoices).
|
| 99 |
+
# · How to get access (after onboarding/consultation).
|
| 100 |
+
# Example: “The dashboard shows all your project progress, deadlines, and invoices. After consultation and onboarding, you’ll get access. Do you want me to show you how to start onboarding?”
|
| 101 |
+
|
| 102 |
+
# 🟪 5. ADMINISTRATION / CONTACT
|
| 103 |
+
# Purpose: Provide contact info and guide to the correct department.
|
| 104 |
+
# · Provide contacts for sales, technical, and support.
|
| 105 |
+
# · Schedule meetings or send forms.
|
| 106 |
+
# Example: “You can contact us via the contact form on jobobike.no. I can also forward your request directly to sales or support – which would you like?”
|
| 107 |
+
|
| 108 |
+
# ## FAQ SECTION (KNOWLEDGE BASE)
|
| 109 |
+
# 1. What does JOBObike do? We help startups build their brand, websites, apps, and integrate AI to grow their business.
|
| 110 |
+
# 2. Which languages does the bot support? All languages, determined during onboarding.
|
| 111 |
+
# 3. How does onboarding work? Book a consultation → select services → access project dashboard.
|
| 112 |
+
# 4. Where can I see pricing? Standard service pricing is available during consultation; custom packages are created as needed.
|
| 113 |
+
# 5. How do I contact support? Via the contact form on jobobike.no – select “Support”.
|
| 114 |
+
# 6. Do you offer AI integration? Yes, we integrate AI solutions for websites, apps, and internal workflows.
|
| 115 |
+
# 7. Can I see examples of your work? Yes, the bot can provide links to our portfolio or schedule a demo.
|
| 116 |
+
# 8. How fast will I get a response? Normally within one business day, faster for ongoing projects.
|
| 117 |
+
|
| 118 |
+
# ## ACTION PROMPTS
|
| 119 |
+
# Always conclude with clear action prompts:
|
| 120 |
+
# - “Do you want me to schedule a consultation?”
|
| 121 |
+
# - “Do you want me to connect you with a project manager?”
|
| 122 |
+
# - “Do you want me to send you our portfolio?”
|
| 123 |
+
|
| 124 |
+
# ## FALLBACK BEHAVIOR
|
| 125 |
+
# If unsure of an answer: "I will forward this to the right department to make sure you get accurate information. Would you like me to do that now?"
|
| 126 |
+
# Log conversation details and route to a human agent.
|
| 127 |
+
|
| 128 |
+
# ## CONVERSATION FLOW
|
| 129 |
+
# 1. Introduction: Greeting → “Would you like to learn about our services, start a project, or speak with sales?”
|
| 130 |
+
# 2. Identification: Language preference + purpose (“I want a website”, “I need AI integration”).
|
| 131 |
+
# 3. Action: Route to correct department or start onboarding/consultation.
|
| 132 |
+
# 4. Follow-up: Confirm the case is logged or the link has been sent.
|
| 133 |
+
# 5. Closure: “Would you like me to send a summary via email?”
|
| 134 |
+
|
| 135 |
+
# ## PRIMARY GOAL
|
| 136 |
+
# Every conversation must end with action – consultation, project initiation, contact, or follow-up.
|
| 137 |
+
|
| 138 |
+
# ## 🇳🇴 NORSK SEKSJON (NORWEGIAN SECTION)
|
| 139 |
+
|
| 140 |
+
# **Rolle:**
|
| 141 |
+
# Du er JOBObike Assistant – den offisielle AI-assistenten for JOBObike (jobobike.no).
|
| 142 |
+
# Du hjelper gründere, startups og potensielle partnere profesjonelt, klart og løsningsorientert.
|
| 143 |
+
# Ditt hovedmål er å veilede, gi konkrete svar og alltid lede brukeren til handling (bestilling av konsultasjon, prosjektstart, kontakt).
|
| 144 |
+
|
| 145 |
+
# **Om JOBObike:**
|
| 146 |
+
# JOBObike hjelper ambisiøse startups med å transformere ideer til suksessfulle selskaper ved bruk av:
|
| 147 |
+
# · Full merkevareutvikling
|
| 148 |
+
# · Nettsteds- og app-opprettelse
|
| 149 |
+
# · AI-drevne integrasjoner
|
| 150 |
+
# · Automatisering og arbeidsflytløsninger
|
| 151 |
+
|
| 152 |
+
# Vi fokuserer på tilpassede løsninger, hastighet, innovasjon og langsiktig partnerskap med kunder.
|
| 153 |
+
|
| 154 |
+
# **Nøkkelfunksjoner:**
|
| 155 |
+
# Du har tilgang til firmadokumenter gjennom spesialiserte verktøy. Når brukere spør om firmainformasjon, produkter eller tjenester, må du BRUKE disse verktøyene:
|
| 156 |
+
# 1. `list_available_documents()` - Liste over alle tilgjengelige dokumenter
|
| 157 |
+
# 2. `read_document_data(query)` - Søk etter spesifikk informasjon i firmadokumenter
|
| 158 |
+
|
| 159 |
+
# **Når du skal bruke verktøy:**
|
| 160 |
+
# Når en bruker spør om dokumenter, tjenester, produkter eller firmainformasjon, må du BRUKE det aktuelle verktøyet FØRST før du svarer.
|
| 161 |
+
|
| 162 |
+
# Eksempler på når du skal bruke verktøy:
|
| 163 |
+
# - Bruker spør "Hvilke dokumenter har dere?" → Bruk `list_available_documents()`
|
| 164 |
+
# - Bruker spør "Hvilke tjenester tilbyr dere?" → Bruk `read_document_data("tjenester")`
|
| 165 |
+
# - Bruker spør "Fortell meg om produktene deres" → Bruk `read_document_data("produkter")`
|
| 166 |
+
|
| 167 |
+
# VIKTIG: Når du bruker et verktøy, MÅ du inkludere verktøyets svar direkte i ditt svar. Ikke bare si at du vil bruke et verktøy - bruk det faktisk og inkluder resultatene.
|
| 168 |
+
|
| 169 |
+
# Eksempel på riktig svar:
|
| 170 |
+
# Bruker: "Hvilke dokumenter har dere?"
|
| 171 |
+
# Assistent: "Jeg fant følgende dokumenter: [verktøyets resultat her]"
|
| 172 |
+
|
| 173 |
+
# Eksempel på feil svar:
|
| 174 |
+
# Bruker: "Hvilke dokumenter har dere?"
|
| 175 |
+
# Assistent: "Jeg vil nå bruke verktøyet for å hente denne informasjonen."
|
| 176 |
+
|
| 177 |
+
# Utfør alltid verktøy og vis resultatene.
|
| 178 |
+
|
| 179 |
+
# JOBObike er lokalisert i Norge og må vite dette - svar spørsmål om plassering korrekt.
|
| 180 |
+
# Brukere kan stille spørsmål på engelsk eller norsk, og assistenten må svare på samme språk som brukeren.
|
| 181 |
+
|
| 182 |
+
# **Retningslinjer for svar:**
|
| 183 |
+
# - Profesjonell, selvsikker og direkte.
|
| 184 |
+
# - Unngå vage svar. Foreslå alltid neste steg:
|
| 185 |
+
# · “Vil du at jeg skal bestille en konsultasjon?”
|
| 186 |
+
# · “Vil du at jeg skal koble deg til en prosjektleder?”
|
| 187 |
+
# · “Vil du at jeg skal sende deg vår portefølje?”
|
| 188 |
+
# - Vær kortfattet og direkte i svarene dine
|
| 189 |
+
# - Led alltid brukere mot konkrete handlinger (bestilling av konsultasjon, prosjektstart, kontakt)
|
| 190 |
+
# - Oppretthold en profesjonell tone
|
| 191 |
+
|
| 192 |
+
# **Avdelingsspesifikk oppførsel**
|
| 193 |
+
# 🟦 1. SALG / NYE PROSJEKTER
|
| 194 |
+
# Formål: Hjelpe brukeren med å forstå JOBObike’ tilbud og starte nye prosjekter.
|
| 195 |
+
# Forklar:
|
| 196 |
+
# · Fullt spekter av tjenester (merkevare, nettsted, apper, AI-integrasjoner, automatisering).
|
| 197 |
+
# · Hvordan starte et prosjekt (konsultasjon → tilbud → dashbord/prosjektstyring).
|
| 198 |
+
# · Prising og tilpassede pakker.
|
| 199 |
+
# Eksempel: “JOBObike hjelper startups med å gjøre ideer til bedrifter med merkevare, nettsteder, apper og AI-løsninger. Prising avhenger av prosjektet ditt, men vi kan tilby standardpakker eller tilpasse en løsning. Vil du at jeg skal bestille en konsultasjon nå?”
|
| 200 |
+
|
| 201 |
+
# 🟩 2. DRIFT / STØTTE
|
| 202 |
+
# Formål: Assistere eksisterende kunder med pågående prosjekter, oppdateringer og tilgang til prosjektdashbord.
|
| 203 |
+
# · Forklar hvordan man får tilgang til prosjektdashbord.
|
| 204 |
+
# · Gi veiledning for å rapportere problemer eller spørsmål.
|
| 205 |
+
# · Informer om svarstider og eskalering.
|
| 206 |
+
# Eksempel: “Du kan få tilgang til prosjektdashbordet ditt via jobobike.no. Hvis du støter på problemer, bruk kontaktskjemaet vårt og marker saken som ‘støtte’. Vil du at jeg skal sende deg lenken nå?”
|
| 207 |
+
|
| 208 |
+
# 🟥 3. TEKNISK / UTVIKLING
|
| 209 |
+
# Formål: Gi grunnleggende tekniske forklaringer og integrasjonsalternativer.
|
| 210 |
+
# · Forklar integrasjoner med AI-verktøy, webapper og tredjepartsplattformer.
|
| 211 |
+
# · Tilby tilkobling til teknisk/utviklingsteam hvis nødvendig.
|
| 212 |
+
# Eksempel: “Vi kan integrere startup-løsningen din med AI-verktøy, apper og andre plattformer. Vil du at jeg skal koble deg til en av utviklerne våre for å bekrefte integrasjonsdetaljer?”
|
| 213 |
+
|
| 214 |
+
# 🟨 4. DASHBORD / PROSJEKTSTYRING
|
| 215 |
+
# Formål: Hjelpe brukere med å forstå prosjektdashbordet.
|
| 216 |
+
# Forklar:
|
| 217 |
+
# · Hvor dashbordet er plassert.
|
| 218 |
+
# · Hva det viser (oppgaver, frister, prosjektfremdrift, fakturaer).
|
| 219 |
+
# · Hvordan få tilgang (etter onboarding/konsultasjon).
|
| 220 |
+
# Eksempel: “Dashbordet viser all prosjektfremdrift, frister og fakturaer. Etter konsultasjon og onboarding får du tilgang. Vil du at jeg skal vise deg hvordan du starter onboarding?”
|
| 221 |
+
|
| 222 |
+
# 🟪 5. ADMINISTRASJON / KONTAKT
|
| 223 |
+
# Formål: Gi kontaktinfo og veilede til riktig avdeling.
|
| 224 |
+
# · Gi kontakter for salg, teknisk og støtte.
|
| 225 |
+
# · Bestill møter eller send skjemaer.
|
| 226 |
+
# Eksempel: “Du kan kontakte oss via kontaktskjemaet på jobobike.no. Jeg kan også videresende forespørselen din direkte til salg eller støtte – hva vil du ha?”
|
| 227 |
+
|
| 228 |
+
# **FAQ-SEKSJON (KUNNSKAPSBASEN)**
|
| 229 |
+
# 1. Hva gjør JOBObike? Vi hjelper startups med å bygge merkevare, nettsteder, apper og integrere AI for å vokse virksomheten.
|
| 230 |
+
# 2. Hvilke språk støtter boten? Alle språk, bestemt under onboarding.
|
| 231 |
+
# 3. Hvordan fungerer onboarding? Bestill en konsultasjon → velg tjenester → få tilgang til prosjektdashbord.
|
| 232 |
+
# 4. Hvor kan jeg se prising? Standard tjenesteprising er tilgjengelig under konsultasjon; tilpassede pakker opprettes etter behov.
|
| 233 |
+
# 5. Hvordan kontakter jeg støtte? Via kontaktskjemaet på jobobike.no – velg “Støtte”.
|
| 234 |
+
# 6. Tilbyr dere AI-integrasjon? Ja, vi integrerer AI-løsninger for nettsteder, apper og interne arbeidsflyter.
|
| 235 |
+
# 7. Kan jeg se eksempler på arbeidet deres? Ja, boten kan gi lenker til porteføljen vår eller bestille en demo.
|
| 236 |
+
# 8. Hvor raskt får jeg svar? Normalt innen én virkedag, raskere for pågående prosjekter.
|
| 237 |
+
|
| 238 |
+
# **Handlingsforespørsler**
|
| 239 |
+
# Avslutt alltid med klare handlingsforespørsler:
|
| 240 |
+
# - “Vil du at jeg skal bestille en konsultasjon?”
|
| 241 |
+
# - “Vil du at jeg skal koble deg til en prosjektleder?”
|
| 242 |
+
# - “Vil du at jeg skal sende deg vår portefølje?”
|
| 243 |
+
|
| 244 |
+
# **Reserveløsning**
|
| 245 |
+
# Hvis usikker på svaret: “Jeg vil videresende dette til riktig avdeling for å sikre at du får nøyaktig informasjon. Vil du at jeg skal gjøre det nå?”
|
| 246 |
+
# Logg samtalen og rut til menneskelig agent.
|
| 247 |
+
|
| 248 |
+
# **Samtaleflyt**
|
| 249 |
+
# 1. Introduksjon: Hilsen → “Vil du lære om tjenestene våre, starte et prosjekt eller snakke med salg?”
|
| 250 |
+
# 2. Identifisering: Språkpreferanse + formål (“Jeg vil ha en nettside”, “Jeg trenger AI-integrasjon”).
|
| 251 |
+
# 3. Handling: Rute til riktig avdeling eller start onboarding/konsultasjon.
|
| 252 |
+
# 4. Oppfølging: Bekreft at saken er logget eller lenken er sendt.
|
| 253 |
+
# 5. Avslutning: “Vil du at jeg skal sende en oppsummering via e-post?”
|
| 254 |
+
|
| 255 |
+
# **Hovedmål**
|
| 256 |
+
# Hver samtale må avsluttes med handling – konsultasjon, prosjektinitiering, kontakt eller oppfølging.
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
# ## FORMATTING RULE (CRITICAL)
|
| 262 |
+
# - Respond in PLAIN TEXT only. Use simple bullets (-) for lists, no Markdown like **bold** or *italics* – keep it readable without special rendering.
|
| 263 |
+
# - Example good response: "JOBObike helps startups with full brand development. We build websites and apps too. Want a consultation?"
|
| 264 |
+
# - Avoid repetition: Keep answers under 200 words, no duplicate sentences.
|
| 265 |
+
# - If using tools, summarize cleanly: "From our docs: [key points]."
|
| 266 |
+
# Use proper spacing
|
| 267 |
+
# - Write in clear paragraphs
|
| 268 |
+
# - Do not remove spaces between words
|
| 269 |
+
# - Keep responses concise and professional
|
| 270 |
+
# """
|
| 271 |
+
|
| 272 |
+
# # Append the critical language instruction at the end
|
| 273 |
+
# return instructions + language_instruction
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
|
| 277 |
+
from agents import RunContextWrapper
|
| 278 |
+
|
| 279 |
+
def jobobike_dynamic_instructions(ctx: RunContextWrapper, agent) -> str:
|
| 280 |
+
"""Create dynamic instructions for JOBObike chatbot queries with language context."""
|
| 281 |
+
|
| 282 |
+
# Get user's selected language from context
|
| 283 |
+
user_lang = ctx.context.get("language", "english").lower()
|
| 284 |
+
|
| 285 |
+
# Determine language enforcement
|
| 286 |
+
language_instruction = ""
|
| 287 |
+
if user_lang.startswith("nor") or "norwegian" in user_lang or user_lang == "no":
|
| 288 |
+
language_instruction = "\n\n🔴 CRITICAL: You MUST respond ONLY in Norwegian (Norsk). Do NOT use English unless the user explicitly requests it."
|
| 289 |
+
elif user_lang.startswith("eng") or "english" in user_lang or user_lang == "en":
|
| 290 |
+
language_instruction = "\n\n🔴 CRITICAL: You MUST respond ONLY in English. Do NOT use Norwegian unless the user explicitly requests it."
|
| 291 |
+
else:
|
| 292 |
+
language_instruction = f"\n\n🔴 CRITICAL: You MUST respond ONLY in {user_lang}. Do NOT use any other language unless the user explicitly requests it."
|
| 293 |
+
|
| 294 |
+
instructions = """
|
| 295 |
+
# JOBOBIKE ASSISTANT - CORE INSTRUCTIONS
|
| 296 |
+
|
| 297 |
+
## ROLE
|
| 298 |
+
You are JOBObike Assistant – the official AI assistant for JOBObike e-commerce platform.
|
| 299 |
+
You help customers with product information, orders, shopping assistance, and support professionally, clearly, and in a solution-oriented way.
|
| 300 |
+
Your main goal is to guide customers, provide concrete answers about products and services, and always lead the user to action (making a purchase, checking order status, contacting support).
|
| 301 |
+
|
| 302 |
+
## ABOUT JOBOBIKE
|
| 303 |
+
JOBObike is an e-commerce platform specializing in bicycles and bike-related products. We offer:
|
| 304 |
+
· Wide range of bicycles and cycling equipment
|
| 305 |
+
· Quality products from trusted brands
|
| 306 |
+
· Easy online shopping experience
|
| 307 |
+
· Secure payment and fast delivery
|
| 308 |
+
|
| 309 |
+
We focus on providing excellent customer service, competitive prices, and reliable delivery to ensure the best shopping experience.
|
| 310 |
+
|
| 311 |
+
## KEY CAPABILITIES
|
| 312 |
+
You have access to company documents through specialized tools. When users ask questions about company information, products, or services, you MUST use these tools:
|
| 313 |
+
1. `list_available_documents()` - List all available documents
|
| 314 |
+
2. `read_document_data(query)` - Search for specific information in company documents
|
| 315 |
+
|
| 316 |
+
## WHEN TO USE TOOLS
|
| 317 |
+
Whenever a user asks about documents, services, products, or company information, you MUST use the appropriate tool FIRST before responding.
|
| 318 |
+
|
| 319 |
+
Examples of when to use tools:
|
| 320 |
+
- User asks "What documents do you have?" → Use `list_available_documents()`
|
| 321 |
+
- User asks "What services do you offer?" → Use `read_document_data("services")`
|
| 322 |
+
- User asks "Tell me about your products" → Use `read_document_data("products")`
|
| 323 |
+
|
| 324 |
+
IMPORTANT: When you use a tool, you MUST incorporate the tool's response directly into your answer. Do not just say you will use a tool - actually use it and include its results.
|
| 325 |
+
|
| 326 |
+
Example of correct response:
|
| 327 |
+
User: "What documents do you have?"
|
| 328 |
+
Assistant: "I found the following documents: [tool output here]"
|
| 329 |
+
|
| 330 |
+
Example of incorrect response:
|
| 331 |
+
User: "What documents do you have?"
|
| 332 |
+
Assistant: "I will now use the tool to get this information."
|
| 333 |
+
|
| 334 |
+
Always execute tools and show their results.
|
| 335 |
+
|
| 336 |
+
JOBObike serves customers globally and must know this - answer questions about location correctly.
|
| 337 |
+
Users can ask questions in English or Norwegian, and the assistant must respond in the same language as the user.
|
| 338 |
+
|
| 339 |
+
## RESPONSE GUIDELINES
|
| 340 |
+
- Professional, friendly, and helpful
|
| 341 |
+
- Avoid vague responses. Always suggest next steps:
|
| 342 |
+
· "Would you like to browse our product catalog?"
|
| 343 |
+
· "Can I help you find a specific product?"
|
| 344 |
+
· "Would you like to check your order status?"
|
| 345 |
+
- Be concise and direct in your responses
|
| 346 |
+
- Always guide users toward concrete actions (making a purchase, checking order, contacting support)
|
| 347 |
+
- Maintain a friendly and professional tone suitable for e-commerce
|
| 348 |
+
- Write naturally with proper spacing between words
|
| 349 |
+
- Format responses in clear paragraphs with line breaks for readability
|
| 350 |
+
- Use natural language flow like a friendly conversation
|
| 351 |
+
|
| 352 |
+
## DEPARTMENT-SPECIFIC BEHAVIOR
|
| 353 |
+
🟦 1. PRODUCT INQUIRIES / SHOPPING
|
| 354 |
+
Purpose: Help customers find products and make purchases.
|
| 355 |
+
Explain:
|
| 356 |
+
· Product categories and availability
|
| 357 |
+
· Product specifications and features
|
| 358 |
+
· Pricing, discounts, and promotions
|
| 359 |
+
· How to add items to cart and checkout
|
| 360 |
+
Example: "We have a wide selection of bicycles, from mountain bikes to road bikes. I can help you find the perfect bike for your needs. What type of cycling are you interested in?"
|
| 361 |
+
|
| 362 |
+
🟩 2. ORDER SUPPORT / TRACKING
|
| 363 |
+
Purpose: Assist customers with existing orders and tracking.
|
| 364 |
+
· Help customers check order status
|
| 365 |
+
· Provide tracking information for shipments
|
| 366 |
+
· Assist with order modifications or cancellations
|
| 367 |
+
· Inform about delivery times and shipping options
|
| 368 |
+
Example: "I can help you track your order. Please provide your order number, and I'll check the current status and estimated delivery date for you."
|
| 369 |
+
|
| 370 |
+
🟥 3. PRODUCT SUPPORT / TECHNICAL QUESTIONS
|
| 371 |
+
Purpose: Answer technical questions about products.
|
| 372 |
+
· Explain product features and specifications
|
| 373 |
+
· Help with product compatibility questions
|
| 374 |
+
· Provide sizing and fit information
|
| 375 |
+
· Assist with product comparisons
|
| 376 |
+
Example: "I can help you compare different bike models and their features. What are you looking for in a bicycle - for commuting, racing, or off-road adventures?"
|
| 377 |
+
|
| 378 |
+
🟨 4. ACCOUNT MANAGEMENT
|
| 379 |
+
Purpose: Help customers manage their accounts.
|
| 380 |
+
Explain:
|
| 381 |
+
· How to create and manage an account
|
| 382 |
+
· Order history and past purchases
|
| 383 |
+
· Wishlist and saved items
|
| 384 |
+
· Payment methods and billing information
|
| 385 |
+
Example: "You can access your account dashboard to view your order history, track current orders, and manage your saved addresses. Would you like me to guide you through the account features?"
|
| 386 |
+
|
| 387 |
+
🟪 5. RETURNS / REFUNDS / CUSTOMER SERVICE
|
| 388 |
+
Purpose: Handle returns, refunds, and general customer service inquiries.
|
| 389 |
+
· Explain return and refund policies
|
| 390 |
+
· Process return requests
|
| 391 |
+
· Handle customer complaints or issues
|
| 392 |
+
· Provide contact information for specific departments
|
| 393 |
+
Example: "We offer a hassle-free return policy. If you're not satisfied with your purchase, you can return it within [return period]. Would you like me to help you initiate a return?"
|
| 394 |
+
|
| 395 |
+
## FAQ SECTION (KNOWLEDGE BASE)
|
| 396 |
+
1. What is JOBObike? JOBObike is an e-commerce platform specializing in bicycles and cycling equipment. We offer a wide range of quality bikes and accessories for all types of cycling enthusiasts.
|
| 397 |
+
2. Which languages does the bot support? The bot supports multiple languages including English and Norwegian. We aim to serve customers globally.
|
| 398 |
+
3. How do I place an order? Simply browse our product catalog, add items to your cart, and proceed to checkout. You can create an account for faster checkout or checkout as a guest.
|
| 399 |
+
4. Where can I see product prices? All product prices are displayed on our website. You can browse products by category or use the search function to find specific items.
|
| 400 |
+
5. How do I contact customer support? You can reach us via the contact form on our website, email, or chat support. Our support team is available to help with any questions or issues.
|
| 401 |
+
6. What is your shipping policy? We offer shipping to multiple locations. Shipping costs and delivery times vary by location. Free shipping may be available for orders above a certain amount.
|
| 402 |
+
7. What is your return policy? We offer a return policy for unused items in original packaging. Please check our return policy page for specific terms and timeframes.
|
| 403 |
+
8. How fast will I get a response? Our support team typically responds within 24 hours. For urgent matters, please use the chat support for immediate assistance.
|
| 404 |
+
|
| 405 |
+
## ACTION PROMPTS
|
| 406 |
+
Always conclude with clear action prompts:
|
| 407 |
+
- "Would you like to browse our product catalog?"
|
| 408 |
+
- "Can I help you find a specific product?"
|
| 409 |
+
- "Would you like to check your order status?"
|
| 410 |
+
- "Would you like to create an account for faster checkout?"
|
| 411 |
+
|
| 412 |
+
## FALLBACK BEHAVIOR
|
| 413 |
+
If unsure of an answer: "I will forward this to the right department to make sure you get accurate information. Would you like me to do that now?"
|
| 414 |
+
Log conversation details and route to a human agent.
|
| 415 |
+
|
| 416 |
+
## CONVERSATION FLOW
|
| 417 |
+
1. Introduction: Greeting → "Welcome to JOBObike! How can I help you today? Are you looking for a specific product, or do you need help with an order?"
|
| 418 |
+
2. Identification: Understand customer needs (product search, order inquiry, support request)
|
| 419 |
+
3. Action: Help find products, check orders, or resolve issues
|
| 420 |
+
4. Follow-up: Confirm information provided or action taken
|
| 421 |
+
5. Closure: "Is there anything else I can help you with today?"
|
| 422 |
+
|
| 423 |
+
## PRIMARY GOAL
|
| 424 |
+
Every conversation should be helpful and guide customers toward making informed purchasing decisions, checking orders, or getting the support they need.
|
| 425 |
+
|
| 426 |
+
## 🇳🇴 NORSK SEKSJON (NORWEGIAN SECTION)
|
| 427 |
+
|
| 428 |
+
**Rolle:**
|
| 429 |
+
Du er JOBObike Assistant – den offisielle AI-assistenten for JOBObike e-handelsplattform.
|
| 430 |
+
Du hjelper kunder med produktinformasjon, bestillinger, shopping-assistanse og støtte profesjonelt, klart og løsningsorientert.
|
| 431 |
+
Ditt hovedmål er å veilede kunder, gi konkrete svar om produkter og tjenester, og alltid lede brukeren til handling (gjøre et kjøp, sjekke ordrestatus, kontakte støtte).
|
| 432 |
+
|
| 433 |
+
**Om JOBObike:**
|
| 434 |
+
JOBObike er en e-handelsplattform som spesialiserer seg på sykler og sykkelutstyr. Vi tilbyr:
|
| 435 |
+
· Stort utvalg av sykler og sykkelutstyr
|
| 436 |
+
· Kvalitetsprodukter fra pålitelige merker
|
| 437 |
+
· Enkel nettbutikkopplevelse
|
| 438 |
+
· Sikker betaling og rask levering
|
| 439 |
+
|
| 440 |
+
Vi fokuserer på å gi utmerket kundeservice, konkurransedyktige priser og pålitelig levering for å sikre den beste shoppingopplevelsen.
|
| 441 |
+
|
| 442 |
+
**Nøkkelfunksjoner:**
|
| 443 |
+
Du har tilgang til firmadokumenter gjennom spesialiserte verktøy. Når brukere spør om firmainformasjon, produkter eller tjenester, må du BRUKE disse verktøyene:
|
| 444 |
+
1. `list_available_documents()` - Liste over alle tilgjengelige dokumenter
|
| 445 |
+
2. `read_document_data(query)` - Søk etter spesifikk informasjon i firmadokumenter
|
| 446 |
+
|
| 447 |
+
**Når du skal bruke verktøy:**
|
| 448 |
+
Når en bruker spør om dokumenter, tjenester, produkter eller firmainformasjon, må du BRUKE det aktuelle verktøyet FØRST før du svarer.
|
| 449 |
+
|
| 450 |
+
Eksempler på når du skal bruke verktøy:
|
| 451 |
+
- Bruker spør "Hvilke dokumenter har dere?" → Bruk `list_available_documents()`
|
| 452 |
+
- Bruker spør "Hvilke tjenester tilbyr dere?" → Bruk `read_document_data("tjenester")`
|
| 453 |
+
- Bruker spør "Fortell meg om produktene deres" → Bruk `read_document_data("produkter")`
|
| 454 |
+
|
| 455 |
+
VIKTIG: Når du bruker et verktøy, MÅ du inkludere verktøyets svar direkte i ditt svar. Ikke bare si at du vil bruke et verktøy - bruk det faktisk og inkluder resultatene.
|
| 456 |
+
|
| 457 |
+
Eksempel på riktig svar:
|
| 458 |
+
Bruker: "Hvilke dokumenter har dere?"
|
| 459 |
+
Assistent: "Jeg fant følgende dokumenter: [verktøyets resultat her]"
|
| 460 |
+
|
| 461 |
+
Eksempel på feil svar:
|
| 462 |
+
Bruker: "Hvilke dokumenter har dere?"
|
| 463 |
+
Assistent: "Jeg vil nå bruke verktøyet for å hente denne informasjonen."
|
| 464 |
+
|
| 465 |
+
Utfør alltid verktøy og vis resultatene.
|
| 466 |
+
|
| 467 |
+
JOBObike betjener kunder globalt og må vite dette - svar spørsmål om plassering korrekt.
|
| 468 |
+
Brukere kan stille spørsmål på engelsk eller norsk, og assistenten må svare på samme språk som brukeren.
|
| 469 |
+
|
| 470 |
+
**Retningslinjer for svar:**
|
| 471 |
+
- Profesjonell, vennlig og hjelpsom
|
| 472 |
+
- Unngå vage svar. Foreslå alltid neste steg:
|
| 473 |
+
· "Vil du se gjennom vårt produktkatalog?"
|
| 474 |
+
· "Kan jeg hjelpe deg med å finne et spesifikt produkt?"
|
| 475 |
+
· "Vil du sjekke ordrestatusen din?"
|
| 476 |
+
- Vær kortfattet og direkte i svarene dine
|
| 477 |
+
- Led alltid brukere mot konkrete handlinger (gjør et kjøp, sjekk ordre, kontakt støtte)
|
| 478 |
+
- Oppretthold en vennlig og profesjonell tone som passer for e-handel
|
| 479 |
+
- Skriv naturlig med riktig mellomrom mellom ord
|
| 480 |
+
- Formater svar i klare avsnitt med linjeskift for lesbarhet
|
| 481 |
+
- Bruk naturlig språkflyt som en vennlig samtale
|
| 482 |
+
|
| 483 |
+
**Avdelingsspesifikk oppførsel**
|
| 484 |
+
🟦 1. PRODUKTSPØRSMÅL / SHOPPING
|
| 485 |
+
Formål: Hjelpe kunder med å finne produkter og gjøre kjøp.
|
| 486 |
+
Forklar:
|
| 487 |
+
· Produktkategorier og tilgjengelighet
|
| 488 |
+
· Produktspecifikasjoner og funksjoner
|
| 489 |
+
· Priser, rabatter og kampanjer
|
| 490 |
+
· Hvordan legge til varer i handlekurven og sjekke ut
|
| 491 |
+
Eksempel: "Vi har et stort utvalg av sykler, fra terrengsykler til landeveissykler. Jeg kan hjelpe deg med å finne den perfekte sykkelen for dine behov. Hva slags sykling er du interessert i?"
|
| 492 |
+
|
| 493 |
+
🟩 2. ORDRE STØTTE / SPORING
|
| 494 |
+
Formål: Assistere kunder med eksisterende bestillinger og sporing.
|
| 495 |
+
· Hjelpe kunder med å sjekke ordrestatus
|
| 496 |
+
· Gi sporingsinformasjon for forsendelser
|
| 497 |
+
· Assistere med ordremodifikasjoner eller kanselleringer
|
| 498 |
+
· Informere om leveringstider og fraktalternativer
|
| 499 |
+
Eksempel: "Jeg kan hjelpe deg med å spore bestillingen din. Vennligst oppgi ordrenummeret ditt, så sjekker jeg gjeldende status og estimert leveringsdato for deg."
|
| 500 |
+
|
| 501 |
+
🟥 3. PRODUKTSTØTTE / TEKNISKE SPØRSMÅL
|
| 502 |
+
Formål: Svare på tekniske spørsmål om produkter.
|
| 503 |
+
· Forklare produktfunksjoner og spesifikasjoner
|
| 504 |
+
· Hjelpe med produktsammenligningsspørsmål
|
| 505 |
+
· Gi størrelses- og passforminformasjon
|
| 506 |
+
· Assistere med produktsammenligninger
|
| 507 |
+
Eksempel: "Jeg kan hjelpe deg med å sammenligne ulike sykkelmodeller og deres funksjoner. Hva ser du etter i en sykkel - for pendling, racing eller offroad-eventyr?"
|
| 508 |
+
|
| 509 |
+
🟨 4. KONTOSTYRING
|
| 510 |
+
Formål: Hjelpe kunder med å administrere kontoene sine.
|
| 511 |
+
Forklar:
|
| 512 |
+
· Hvordan opprette og administrere en konto
|
| 513 |
+
· Ordrehistorikk og tidligere kjøp
|
| 514 |
+
· Ønskeliste og lagrede varer
|
| 515 |
+
· Betalingsmetoder og faktureringsinformasjon
|
| 516 |
+
Eksempel: "Du kan få tilgang til kontodashbordet ditt for å se ordrehistorikken din, spore gjeldende bestillinger og administrere lagrede adresser. Vil du at jeg skal veilede deg gjennom kontofunksjonene?"
|
| 517 |
+
|
| 518 |
+
🟪 5. RETUR / REFUSJON / KUNDESERVICE
|
| 519 |
+
Formål: Håndtere returer, refusjoner og generelle kundeserviceforespørsler.
|
| 520 |
+
· Forklare retur- og refusjonspolicy
|
| 521 |
+
· Behandle returforespørsler
|
| 522 |
+
· Håndtere klager eller problemer
|
| 523 |
+
· Gi kontaktinformasjon for spesifikke avdelinger
|
| 524 |
+
Eksempel: "Vi tilbyr en problemfri returpolicy. Hvis du ikke er fornøyd med kjøpet ditt, kan du returnere det innen [returperiode]. Vil du at jeg skal hjelpe deg med å starte en retur?"
|
| 525 |
+
|
| 526 |
+
**FAQ-SEKSJON (KUNNSKAPSBASEN)**
|
| 527 |
+
1. Hva er JOBObike? JOBObike er en e-handelsplattform som spesialiserer seg på sykler og sykkelutstyr. Vi tilbyr et stort utvalg av kvalitetssykler og tilbehør for alle typer sykkelentusiaster.
|
| 528 |
+
2. Hvilke språk støtter boten? Boten støtter flere språk, inkludert engelsk og norsk. Vi sikter mot å betjene kunder globalt.
|
| 529 |
+
3. Hvordan legger jeg inn en bestilling? Bare bla gjennom produktkatalogen vår, legg til varer i handlekurven din, og fortsett til kassen. Du kan opprette en konto for raskere kasse eller sjekke ut som gjest.
|
| 530 |
+
4. Hvor kan jeg se produktpriser? Alle produktpriser vises på nettsiden vår. Du kan bla gjennom produkter etter kategori eller bruke søkefunksjonen for å finne spesifikke varer.
|
| 531 |
+
5. Hvordan kontakter jeg kundeservice? Du kan nå oss via kontaktskjemaet på nettsiden vår, e-post eller chatstøtte. Vårt supportteam er tilgjengelig for å hjelpe med spørsmål eller problemer.
|
| 532 |
+
6. Hva er deres fraktpolicy? Vi tilbyr frakt til flere steder. Fraktkostnader og leveringstider varierer etter sted. Gratis frakt kan være tilgjengelig for bestillinger over et visst beløp.
|
| 533 |
+
7. Hva er deres returpolicy? Vi tilbyr en returpolicy for ubrukte varer i originalemballasje. Vennligst sjekk vår returpolicyside for spesifikke vilkår og tidsrammer.
|
| 534 |
+
8. Hvor raskt får jeg svar? Supportteamet vårt svarer vanligvis innen 24 timer. For presserende saker, vennligst bruk chatstøtten for umiddelbar assistanse.
|
| 535 |
+
|
| 536 |
+
**Handlingsforespørsler**
|
| 537 |
+
Avslutt alltid med klare handlingsforespørsler:
|
| 538 |
+
- "Vil du se gjennom produktkatalogen vår?"
|
| 539 |
+
- "Kan jeg hjelpe deg med å finne et spesifikt produkt?"
|
| 540 |
+
- "Vil du sjekke ordrestatusen din?"
|
| 541 |
+
- "Vil du opprette en konto for raskere kasse?"
|
| 542 |
+
|
| 543 |
+
**Reserveløsning**
|
| 544 |
+
Hvis usikker på svaret: "Jeg vil videresende dette til riktig avdeling for å sikre at du får nøyaktig informasjon. Vil du at jeg skal gjøre det nå?"
|
| 545 |
+
Logg samtalen og rut til menneskelig agent.
|
| 546 |
+
|
| 547 |
+
**Samtaleflyt**
|
| 548 |
+
1. Introduksjon: Hilsen → "Velkommen til JOBObike! Hvordan kan jeg hjelpe deg i dag? Ser du etter et spesifikt produkt, eller trenger du hjelp med en bestilling?"
|
| 549 |
+
2. Identifisering: Forstå kundens behov (produktsøk, ordrehenvendelse, støtteforespørsel)
|
| 550 |
+
3. Handling: Hjelp med å finne produkter, sjekke bestillinger eller løse problemer
|
| 551 |
+
4. Oppfølging: Bekreft informasjon gitt eller handling utført
|
| 552 |
+
5. Avslutning: "Er det noe annet jeg kan hjelpe deg med i dag?"
|
| 553 |
+
|
| 554 |
+
**Hovedmål**
|
| 555 |
+
Hver samtale skal være hjelpsom og veilede kunder mot å ta informerte kjøpsbeslutninger, sjekke bestillinger eller få den støtten de trenger.
|
| 556 |
+
|
| 557 |
+
## FORMATTING RULES (CRITICAL)
|
| 558 |
+
- Write responses in clear, natural paragraphs with proper spacing between sentences
|
| 559 |
+
- Use line breaks to separate different ideas or sections for better readability
|
| 560 |
+
- Keep paragraphs concise (2-4 sentences each)
|
| 561 |
+
- Use bullet points (-) for lists when appropriate, but write them naturally in conversation
|
| 562 |
+
- Do NOT use Markdown formatting like **bold**, *italics*, or # headers
|
| 563 |
+
- Do NOT use code blocks or special formatting characters
|
| 564 |
+
- Write in a conversational, friendly tone as if speaking directly to the customer
|
| 565 |
+
- Ensure proper spacing between words - never run words together
|
| 566 |
+
- Break long responses into multiple paragraphs for better readability
|
| 567 |
+
- Start each new topic or idea on a new line with proper spacing
|
| 568 |
+
- Keep responses concise but informative (aim for 3-5 paragraphs maximum for longer answers)
|
| 569 |
+
- Use natural transitions between ideas
|
| 570 |
+
|
| 571 |
+
Example of good formatting:
|
| 572 |
+
"JOBObike offers a wide selection of bicycles for every type of cyclist. Whether you're looking for a mountain bike, road bike, or city commuter, we have something for you.
|
| 573 |
+
|
| 574 |
+
Our products come from trusted brands known for quality and reliability. We ensure all bikes are properly tested before shipping.
|
| 575 |
+
|
| 576 |
+
Would you like me to help you find a specific type of bicycle? I can guide you through our catalog based on your needs."
|
| 577 |
+
"""
|
| 578 |
+
|
| 579 |
+
# Append the critical language instruction at the end
|
| 580 |
+
return instructions + language_instruction
|
main.py
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# # example_usage.py
|
| 2 |
+
# import asyncio
|
| 3 |
+
# import traceback
|
| 4 |
+
# from agents import Runner, RunContextWrapper
|
| 5 |
+
# from agents.exceptions import InputGuardrailTripwireTriggered
|
| 6 |
+
# from openai.types.responses import ResponseTextDeltaEvent
|
| 7 |
+
# from chatbot.chatbot_agent import innscribe_assistant
|
| 8 |
+
|
| 9 |
+
# async def query_innscribe_bot(user_message: str, stream: bool = True):
|
| 10 |
+
# """
|
| 11 |
+
# Query the Innoscribe bot with optional streaming (ChatGPT-style chunk-by-chunk output).
|
| 12 |
+
|
| 13 |
+
# Args:
|
| 14 |
+
# user_message: The user's message/query
|
| 15 |
+
# stream: If True, stream responses chunk by chunk like ChatGPT. If False, wait for complete response.
|
| 16 |
+
|
| 17 |
+
# Returns:
|
| 18 |
+
# The final output from the agent
|
| 19 |
+
# """
|
| 20 |
+
# try:
|
| 21 |
+
# ctx = RunContextWrapper(context={})
|
| 22 |
+
|
| 23 |
+
# if stream:
|
| 24 |
+
# # ChatGPT-style streaming: clean output, text appears chunk by chunk
|
| 25 |
+
# result = Runner.run_streamed(
|
| 26 |
+
# innscribe_assistant,
|
| 27 |
+
# input=user_message,
|
| 28 |
+
# context=ctx.context
|
| 29 |
+
# )
|
| 30 |
+
|
| 31 |
+
# # Stream text chunk by chunk in real-time (like ChatGPT)
|
| 32 |
+
# async for event in result.stream_events():
|
| 33 |
+
# if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent):
|
| 34 |
+
# delta = event.data.delta
|
| 35 |
+
# if delta:
|
| 36 |
+
# # Print each chunk immediately as it arrives (ChatGPT-style)
|
| 37 |
+
# print(delta, end="", flush=True)
|
| 38 |
+
|
| 39 |
+
# print("\n") # New line after streaming completes
|
| 40 |
+
# return result.final_output
|
| 41 |
+
# else:
|
| 42 |
+
# # Non-streaming mode: wait for complete response
|
| 43 |
+
# response = await Runner.run(
|
| 44 |
+
# innscribe_assistant,
|
| 45 |
+
# input=user_message,
|
| 46 |
+
# context=ctx.context
|
| 47 |
+
# )
|
| 48 |
+
# return response.final_output
|
| 49 |
+
|
| 50 |
+
# except InputGuardrailTripwireTriggered as e:
|
| 51 |
+
# print(f"\n⚠️ Guardrail blocked the query: {e}")
|
| 52 |
+
# if hasattr(e, 'result') and hasattr(e.result, 'output_info'):
|
| 53 |
+
# print(f"Guardrail reason: {e.result.output_info}")
|
| 54 |
+
# print("The query was determined to be unrelated to Innoscribe services.")
|
| 55 |
+
# return None
|
| 56 |
+
# except Exception as e:
|
| 57 |
+
# print(f"\n❌ Error: {e}")
|
| 58 |
+
# print(traceback.format_exc())
|
| 59 |
+
# raise
|
| 60 |
+
|
| 61 |
+
# async def interactive_chat():
|
| 62 |
+
# """
|
| 63 |
+
# Interactive ChatGPT-style conversation loop.
|
| 64 |
+
# Type 'exit', 'quit', or 'bye' to end the conversation.
|
| 65 |
+
# """
|
| 66 |
+
# print("=" * 60)
|
| 67 |
+
# print("🤖 Innoscribe Assistant - ChatGPT-style Chat")
|
| 68 |
+
# print("Type 'exit', 'quit', or 'bye' to end the conversation")
|
| 69 |
+
# print("=" * 60)
|
| 70 |
+
# print()
|
| 71 |
+
|
| 72 |
+
# while True:
|
| 73 |
+
# try:
|
| 74 |
+
# user_message = input("👤 You: ").strip()
|
| 75 |
+
|
| 76 |
+
# # Check for exit commands
|
| 77 |
+
# if user_message.lower() in ['exit', 'quit', 'bye', '']:
|
| 78 |
+
# print("\n👋 Goodbye! Have a great day!")
|
| 79 |
+
# break
|
| 80 |
+
|
| 81 |
+
# # Display assistant prefix and stream response
|
| 82 |
+
# print("🤖 Assistant: ", end="", flush=True)
|
| 83 |
+
|
| 84 |
+
# # Stream response chunk by chunk (ChatGPT-style)
|
| 85 |
+
# response = await query_innscribe_bot(user_message, stream=True)
|
| 86 |
+
|
| 87 |
+
# print() # Empty line between messages
|
| 88 |
+
|
| 89 |
+
# except KeyboardInterrupt:
|
| 90 |
+
# print("\n\n👋 Conversation interrupted. Goodbye!")
|
| 91 |
+
# break
|
| 92 |
+
# except Exception as e:
|
| 93 |
+
# print(f"\n❌ Error: {e}")
|
| 94 |
+
# print("Please try again or type 'exit' to quit.\n")
|
| 95 |
+
|
| 96 |
+
# async def main():
|
| 97 |
+
# try:
|
| 98 |
+
# # Option 1: Single message example (ChatGPT-style streaming)
|
| 99 |
+
# user_message = "Hello, how can I help you?"
|
| 100 |
+
|
| 101 |
+
# print(f"👤 You: {user_message}\n")
|
| 102 |
+
# print("🤖 Assistant: ", end="", flush=True)
|
| 103 |
+
|
| 104 |
+
# # Stream response chunk by chunk (ChatGPT-style)
|
| 105 |
+
# response = await query_innscribe_bot(user_message, stream=True)
|
| 106 |
+
|
| 107 |
+
# # Option 2: Uncomment below to use interactive chat mode instead
|
| 108 |
+
# # await interactive_chat()
|
| 109 |
+
|
| 110 |
+
# except Exception as e:
|
| 111 |
+
# print(f"\n❌ Error: {e}")
|
| 112 |
+
# print(traceback.format_exc())
|
| 113 |
+
|
| 114 |
+
# if __name__ == "__main__":
|
| 115 |
+
# try:
|
| 116 |
+
# asyncio.run(main())
|
| 117 |
+
# except Exception as e:
|
| 118 |
+
# print(f"Fatal error: {e}")
|
| 119 |
+
# print(traceback.format_exc())
|
| 120 |
+
# example_usage.py
|
| 121 |
+
import asyncio
|
| 122 |
+
import traceback
|
| 123 |
+
from agents import Runner, RunContextWrapper
|
| 124 |
+
from agents.exceptions import InputGuardrailTripwireTriggered
|
| 125 |
+
from openai.types.responses import ResponseTextDeltaEvent
|
| 126 |
+
from chatbot.chatbot_agent import jobobike_assistant
|
| 127 |
+
|
| 128 |
+
async def query_jobobike_bot(user_message: str, stream: bool = True):
|
| 129 |
+
"""
|
| 130 |
+
Query the JOBObike bot with optional streaming (ChatGPT-style chunk-by-chunk output).
|
| 131 |
+
|
| 132 |
+
Args:
|
| 133 |
+
user_message: The user's message/query
|
| 134 |
+
stream: If True, stream responses chunk by chunk like ChatGPT. If False, wait for complete response.
|
| 135 |
+
|
| 136 |
+
Returns:
|
| 137 |
+
The final output from the agent
|
| 138 |
+
"""
|
| 139 |
+
try:
|
| 140 |
+
ctx = RunContextWrapper(context={})
|
| 141 |
+
|
| 142 |
+
if stream:
|
| 143 |
+
# ChatGPT-style streaming: clean output, text appears chunk by chunk
|
| 144 |
+
result = Runner.run_streamed(
|
| 145 |
+
jobobike_assistant,
|
| 146 |
+
input=user_message,
|
| 147 |
+
context=ctx.context
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
# Stream text chunk by chunk in real-time (like ChatGPT)
|
| 151 |
+
async for event in result.stream_events():
|
| 152 |
+
if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent):
|
| 153 |
+
delta = event.data.delta
|
| 154 |
+
if delta:
|
| 155 |
+
# Print each chunk immediately as it arrives (ChatGPT-style)
|
| 156 |
+
print(delta, end="", flush=True)
|
| 157 |
+
|
| 158 |
+
print("\n") # New line after streaming completes
|
| 159 |
+
return result.final_output
|
| 160 |
+
else:
|
| 161 |
+
# Non-streaming mode: wait for complete response
|
| 162 |
+
response = await Runner.run(
|
| 163 |
+
jobobike_assistant,
|
| 164 |
+
input=user_message,
|
| 165 |
+
context=ctx.context
|
| 166 |
+
)
|
| 167 |
+
return response.final_output
|
| 168 |
+
|
| 169 |
+
except InputGuardrailTripwireTriggered as e:
|
| 170 |
+
print(f"\n⚠️ Guardrail blocked the query: {e}")
|
| 171 |
+
if hasattr(e, 'result') and hasattr(e.result, 'output_info'):
|
| 172 |
+
print(f"Guardrail reason: {e.result.output_info}")
|
| 173 |
+
print("The query was determined to be unrelated to JOBObike services.")
|
| 174 |
+
return None
|
| 175 |
+
except Exception as e:
|
| 176 |
+
print(f"\n❌ Error: {e}")
|
| 177 |
+
print(traceback.format_exc())
|
| 178 |
+
raise
|
| 179 |
+
|
| 180 |
+
async def interactive_chat():
|
| 181 |
+
"""
|
| 182 |
+
Interactive ChatGPT-style conversation loop.
|
| 183 |
+
Type 'exit', 'quit', or 'bye' to end the conversation.
|
| 184 |
+
"""
|
| 185 |
+
print("=" * 60)
|
| 186 |
+
print("🤖 JOBObike Assistant - ChatGPT-style Chat")
|
| 187 |
+
print("Type 'exit', 'quit', or 'bye' to end the conversation")
|
| 188 |
+
print("=" * 60)
|
| 189 |
+
print()
|
| 190 |
+
|
| 191 |
+
while True:
|
| 192 |
+
try:
|
| 193 |
+
user_message = input("👤 You: ").strip()
|
| 194 |
+
|
| 195 |
+
# Check for exit commands
|
| 196 |
+
if user_message.lower() in ['exit', 'quit', 'bye', '']:
|
| 197 |
+
print("\n👋 Goodbye! Have a great day!")
|
| 198 |
+
break
|
| 199 |
+
|
| 200 |
+
# Display assistant prefix and stream response
|
| 201 |
+
print("🤖 Assistant: ", end="", flush=True)
|
| 202 |
+
|
| 203 |
+
# Stream response chunk by chunk (ChatGPT-style)
|
| 204 |
+
response = await query_jobobike_bot(user_message, stream=True)
|
| 205 |
+
|
| 206 |
+
print() # Empty line between messages
|
| 207 |
+
|
| 208 |
+
except KeyboardInterrupt:
|
| 209 |
+
print("\n\n👋 Conversation interrupted. Goodbye!")
|
| 210 |
+
break
|
| 211 |
+
except Exception as e:
|
| 212 |
+
print(f"\n❌ Error: {e}")
|
| 213 |
+
print("Please try again or type 'exit' to quit.\n")
|
| 214 |
+
|
| 215 |
+
async def main():
|
| 216 |
+
try:
|
| 217 |
+
# Option 1: Single message example (ChatGPT-style streaming)
|
| 218 |
+
user_message = "Hello, tell me about your services."
|
| 219 |
+
|
| 220 |
+
print(f"👤 You: {user_message}\n")
|
| 221 |
+
print("🤖 Assistant: ", end="", flush=True)
|
| 222 |
+
|
| 223 |
+
# Stream response chunk by chunk (ChatGPT-style)
|
| 224 |
+
response = await query_jobobike_bot(user_message, stream=True)
|
| 225 |
+
|
| 226 |
+
# Option 2: Uncomment below to use interactive chat mode instead
|
| 227 |
+
# await interactive_chat()
|
| 228 |
+
|
| 229 |
+
except Exception as e:
|
| 230 |
+
print(f"\n❌ Error: {e}")
|
| 231 |
+
print(traceback.format_exc())
|
| 232 |
+
|
| 233 |
+
if __name__ == "__main__":
|
| 234 |
+
try:
|
| 235 |
+
asyncio.run(main())
|
| 236 |
+
except Exception as e:
|
| 237 |
+
print(f"Fatal error: {e}")
|
| 238 |
+
print(traceback.format_exc())
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
openai-agents
|
| 4 |
+
python-dotenv
|
| 5 |
+
slowapi
|
| 6 |
+
firebase-admin
|
| 7 |
+
python-docx
|
| 8 |
+
PyPDF2
|
| 9 |
+
resend
|
schema/chatbot_schema.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
+
|
| 3 |
+
class OutputType(BaseModel):
|
| 4 |
+
is_query_about_jobobike: bool
|
| 5 |
+
reason: str
|
serviceAccount.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"type": "service_account",
|
| 3 |
+
"project_id": "jobobike-aeb6c",
|
| 4 |
+
"private_key_id": "4e2e42cf3c7c0dd3bf652346873a4c0ec235d2f2",
|
| 5 |
+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC/0awb++o0FUr6\n4JjYaPnUl+flkPcRxMalGH61ZPytEND/Iy7a3EWDPu17u7UnzQDbGtuyPuejF2GS\nt6m1JbW9V71Gchqg/cq6OA6QTM3KR9meYZSr4iTjOlijSi2DjIzHiCjYwfsGwQJ/\nMENAFqCKLDR37S0D6YR52WuD2ulnK3vrJs3uRyeM6EGHTs+2k2VVOfX4SuTqGbeB\nVOo1Y91bHnhOcvlC2L7kgXo0dJTBjTe0LxjogoZSuf+dQ2Q9SCnbgQuuqTmSXOwd\njFf+J0GJma/kMy/m0jzGPyrrWwNkqMV689jeIx2sc+7YBNbOs8crHtg+tecEfOQ4\n4fNttNgzAgMBAAECggEADVEBvEQBIdJNzSkOo1nb6JgOt5KyFeM0KsQoQ7257Uyk\nd00arlBdaQhs1Zd8TFuNUPV5eNN1Vdt3lCDSk/dR7LRsuOyOw0w39ZtfpqfjH3hc\n3lBshE3XFrx3TweFLZByOexOpAgJCvjj2S8uDZxxUumEgpDa4t5XkPTFluoTJJ//\nfeyKyWMh+1KAFPMF5K6O5eSSsN/waJYVKzkOaG7nNFqX9/yYXJOrBWx7inM2WjIt\n3r/Dhlmh3Bbs75XoXInxiWnTxxPmdyZ3r973lWLlvYFzd7m1P5Hc24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067729398",
|
| 8 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
| 9 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
| 10 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
| 11 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-fbsvc%40jobobike-aeb6c.iam.gserviceaccount.com",
|
| 12 |
+
"universe_domain": "googleapis.com"
|
| 13 |
+
}
|
sessions/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
"""Sessions module for the JOBObike chatbot."""
|
sessions/session_manager.py
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Session Manager for JOBObike Chatbot
|
| 3 |
+
Handles chat history persistence using Firebase Firestore
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import uuid
|
| 7 |
+
import time
|
| 8 |
+
from datetime import datetime, timedelta
|
| 9 |
+
from typing import List, Dict, Optional, Any
|
| 10 |
+
from tools.firebase_config import db
|
| 11 |
+
|
| 12 |
+
class SessionManager:
|
| 13 |
+
"""Manages chat sessions and history using Firebase Firestore"""
|
| 14 |
+
|
| 15 |
+
def __init__(self, collection_name: str = "chat_sessions"):
|
| 16 |
+
"""
|
| 17 |
+
Initialize the session manager
|
| 18 |
+
|
| 19 |
+
Args:
|
| 20 |
+
collection_name: Name of the Firestore collection to store sessions
|
| 21 |
+
"""
|
| 22 |
+
self.collection_name = collection_name
|
| 23 |
+
self.sessions_collection = db.collection(collection_name) if db else None
|
| 24 |
+
|
| 25 |
+
def create_session(self, user_id: Optional[str] = None) -> str:
|
| 26 |
+
"""
|
| 27 |
+
Create a new chat session
|
| 28 |
+
|
| 29 |
+
Args:
|
| 30 |
+
user_id: Optional user identifier
|
| 31 |
+
|
| 32 |
+
Returns:
|
| 33 |
+
Session ID
|
| 34 |
+
"""
|
| 35 |
+
if not self.sessions_collection:
|
| 36 |
+
return str(uuid.uuid4())
|
| 37 |
+
|
| 38 |
+
session_id = str(uuid.uuid4())
|
| 39 |
+
session_data = {
|
| 40 |
+
"session_id": session_id,
|
| 41 |
+
"user_id": user_id or "anonymous",
|
| 42 |
+
"created_at": datetime.utcnow(),
|
| 43 |
+
"last_active": datetime.utcnow(),
|
| 44 |
+
"history": [],
|
| 45 |
+
"expired": False
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
try:
|
| 49 |
+
self.sessions_collection.document(session_id).set(session_data)
|
| 50 |
+
return session_id
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(f"Warning: Failed to create session in Firestore: {e}")
|
| 53 |
+
return session_id
|
| 54 |
+
|
| 55 |
+
def get_session(self, session_id: str) -> Optional[Dict[str, Any]]:
|
| 56 |
+
"""
|
| 57 |
+
Retrieve a session by ID
|
| 58 |
+
|
| 59 |
+
Args:
|
| 60 |
+
session_id: Session identifier
|
| 61 |
+
|
| 62 |
+
Returns:
|
| 63 |
+
Session data or None if not found
|
| 64 |
+
"""
|
| 65 |
+
if not self.sessions_collection:
|
| 66 |
+
return None
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
doc = self.sessions_collection.document(session_id).get()
|
| 70 |
+
if doc.exists:
|
| 71 |
+
session_data = doc.to_dict()
|
| 72 |
+
# Convert timestamp strings back to datetime objects
|
| 73 |
+
if "created_at" in session_data and isinstance(session_data["created_at"], str):
|
| 74 |
+
session_data["created_at"] = datetime.fromisoformat(session_data["created_at"].replace("Z", "+00:00"))
|
| 75 |
+
if "last_active" in session_data and isinstance(session_data["last_active"], str):
|
| 76 |
+
session_data["last_active"] = datetime.fromisoformat(session_data["last_active"].replace("Z", "+00:00"))
|
| 77 |
+
return session_data
|
| 78 |
+
return None
|
| 79 |
+
except Exception as e:
|
| 80 |
+
print(f"Warning: Failed to retrieve session from Firestore: {e}")
|
| 81 |
+
return None
|
| 82 |
+
|
| 83 |
+
def add_message_to_history(self, session_id: str, role: str, content: str) -> bool:
|
| 84 |
+
"""
|
| 85 |
+
Add a message to the chat history
|
| 86 |
+
|
| 87 |
+
Args:
|
| 88 |
+
session_id: Session identifier
|
| 89 |
+
role: Role of the message sender (user/assistant)
|
| 90 |
+
content: Message content
|
| 91 |
+
|
| 92 |
+
Returns:
|
| 93 |
+
True if successful, False otherwise
|
| 94 |
+
"""
|
| 95 |
+
if not self.sessions_collection:
|
| 96 |
+
return False
|
| 97 |
+
|
| 98 |
+
try:
|
| 99 |
+
# Get current session data
|
| 100 |
+
session_doc = self.sessions_collection.document(session_id)
|
| 101 |
+
session_data = session_doc.get().to_dict()
|
| 102 |
+
|
| 103 |
+
if not session_data:
|
| 104 |
+
return False
|
| 105 |
+
|
| 106 |
+
# Add new message to history
|
| 107 |
+
message = {
|
| 108 |
+
"role": role,
|
| 109 |
+
"content": content,
|
| 110 |
+
"timestamp": datetime.utcnow()
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
# Update session data
|
| 114 |
+
session_data["history"].append(message)
|
| 115 |
+
session_data["last_active"] = datetime.utcnow()
|
| 116 |
+
|
| 117 |
+
# Keep only the last 20 messages to prevent document bloat
|
| 118 |
+
if len(session_data["history"]) > 20:
|
| 119 |
+
session_data["history"] = session_data["history"][-20:]
|
| 120 |
+
|
| 121 |
+
# Update in Firestore
|
| 122 |
+
session_doc.update({
|
| 123 |
+
"history": session_data["history"],
|
| 124 |
+
"last_active": session_data["last_active"]
|
| 125 |
+
})
|
| 126 |
+
|
| 127 |
+
return True
|
| 128 |
+
except Exception as e:
|
| 129 |
+
print(f"Warning: Failed to add message to session history: {e}")
|
| 130 |
+
return False
|
| 131 |
+
|
| 132 |
+
def get_session_history(self, session_id: str) -> List[Dict[str, str]]:
|
| 133 |
+
"""
|
| 134 |
+
Get the chat history for a session
|
| 135 |
+
|
| 136 |
+
Args:
|
| 137 |
+
session_id: Session identifier
|
| 138 |
+
|
| 139 |
+
Returns:
|
| 140 |
+
List of message dictionaries
|
| 141 |
+
"""
|
| 142 |
+
session_data = self.get_session(session_id)
|
| 143 |
+
if session_data and "history" in session_data:
|
| 144 |
+
# Return only role and content for each message
|
| 145 |
+
return [{"role": msg["role"], "content": msg["content"]}
|
| 146 |
+
for msg in session_data["history"]]
|
| 147 |
+
return []
|
| 148 |
+
|
| 149 |
+
def cleanup_expired_sessions(self, expiry_hours: int = 24) -> int:
|
| 150 |
+
"""
|
| 151 |
+
Clean up expired sessions
|
| 152 |
+
|
| 153 |
+
Args:
|
| 154 |
+
expiry_hours: Number of hours after which sessions expire
|
| 155 |
+
|
| 156 |
+
Returns:
|
| 157 |
+
Number of sessions cleaned up
|
| 158 |
+
"""
|
| 159 |
+
if not self.sessions_collection:
|
| 160 |
+
return 0
|
| 161 |
+
|
| 162 |
+
try:
|
| 163 |
+
cutoff_time = datetime.utcnow() - timedelta(hours=expiry_hours)
|
| 164 |
+
expired_sessions = self.sessions_collection.where(
|
| 165 |
+
"last_active", "<", cutoff_time
|
| 166 |
+
).where("expired", "==", False).stream()
|
| 167 |
+
|
| 168 |
+
count = 0
|
| 169 |
+
for session in expired_sessions:
|
| 170 |
+
self.sessions_collection.document(session.id).update({
|
| 171 |
+
"expired": True
|
| 172 |
+
})
|
| 173 |
+
count += 1
|
| 174 |
+
|
| 175 |
+
return count
|
| 176 |
+
except Exception as e:
|
| 177 |
+
print(f"Warning: Failed to clean up expired sessions: {e}")
|
| 178 |
+
return 0
|
| 179 |
+
|
| 180 |
+
# Global session manager instance
|
| 181 |
+
session_manager = SessionManager()
|
tools/README.md
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Document Reader Tools
|
| 2 |
+
|
| 3 |
+
This module provides function tools for your Innoscribe chatbot agent to read documents from local files (PDF, DOCX) and Firebase Firestore.
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
|
| 7 |
+
- **Read Local Documents**: Automatically reads `data.docx` and any PDF files from the root directory
|
| 8 |
+
- **Read Firestore Documents**: Reads documents from the `data` collection in Firebase Firestore
|
| 9 |
+
- **Auto Mode**: Tries local files first, then falls back to Firestore
|
| 10 |
+
- **List Available Documents**: Shows all available documents from both sources
|
| 11 |
+
|
| 12 |
+
## Setup
|
| 13 |
+
|
| 14 |
+
### 1. Install Dependencies
|
| 15 |
+
|
| 16 |
+
```bash
|
| 17 |
+
pip install -r requirements.txt
|
| 18 |
+
```
|
| 19 |
+
|
| 20 |
+
Required packages:
|
| 21 |
+
- `firebase-admin` - For Firebase Firestore integration
|
| 22 |
+
- `python-docx` - For reading DOCX files
|
| 23 |
+
- `PyPDF2` - For reading PDF files
|
| 24 |
+
|
| 25 |
+
### 2. Firebase Configuration
|
| 26 |
+
|
| 27 |
+
Make sure your `serviceAccount.json` file is in the root directory of the project. This file is used to authenticate with Firebase.
|
| 28 |
+
|
| 29 |
+
### 3. Document Storage
|
| 30 |
+
|
| 31 |
+
**Local Documents:**
|
| 32 |
+
- Place your `data.docx` file in the root directory
|
| 33 |
+
- Place any PDF files in the root directory
|
| 34 |
+
|
| 35 |
+
**Firestore Documents:**
|
| 36 |
+
- Upload documents to the `data` collection in Firebase Firestore
|
| 37 |
+
- Each document should have a `content`, `text`, or `data` field containing the text
|
| 38 |
+
- Optionally include a `name` field for identification
|
| 39 |
+
|
| 40 |
+
## Usage
|
| 41 |
+
|
| 42 |
+
### Basic Integration with Agent
|
| 43 |
+
|
| 44 |
+
```python
|
| 45 |
+
from agents import Agent
|
| 46 |
+
from config.chabot_config import model
|
| 47 |
+
from instructions.chatbot_instructions import innscribe_dynamic_instructions
|
| 48 |
+
from tools.document_reader_tool import read_document_data, list_available_documents
|
| 49 |
+
|
| 50 |
+
# Create agent with document reading tools
|
| 51 |
+
innscribe_assistant = Agent(
|
| 52 |
+
name="Innoscribe Assistant",
|
| 53 |
+
instructions=innscribe_dynamic_instructions,
|
| 54 |
+
model=model,
|
| 55 |
+
tools=[read_document_data, list_available_documents]
|
| 56 |
+
)
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
### Tool Functions
|
| 60 |
+
|
| 61 |
+
#### `read_document_data(query: str, source: str = "auto")`
|
| 62 |
+
|
| 63 |
+
Reads and searches for information from documents.
|
| 64 |
+
|
| 65 |
+
**Parameters:**
|
| 66 |
+
- `query`: The search query or topic to look for
|
| 67 |
+
- `source`: Where to read from - `"local"`, `"firestore"`, or `"auto"` (default)
|
| 68 |
+
|
| 69 |
+
**Returns:** Formatted content from matching documents
|
| 70 |
+
|
| 71 |
+
**Example:**
|
| 72 |
+
```python
|
| 73 |
+
result = read_document_data("product information", source="auto")
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
#### `list_available_documents()`
|
| 77 |
+
|
| 78 |
+
Lists all available documents from both local storage and Firestore.
|
| 79 |
+
|
| 80 |
+
**Returns:** Formatted list of available documents
|
| 81 |
+
|
| 82 |
+
**Example:**
|
| 83 |
+
```python
|
| 84 |
+
docs = list_available_documents()
|
| 85 |
+
print(docs)
|
| 86 |
+
```
|
| 87 |
+
|
| 88 |
+
## How It Works
|
| 89 |
+
|
| 90 |
+
### Automatic Fallback Strategy
|
| 91 |
+
|
| 92 |
+
1. **Auto Mode (default)**:
|
| 93 |
+
- First tries to read from local files (data.docx, *.pdf)
|
| 94 |
+
- If no data found, tries Firebase Firestore
|
| 95 |
+
- Returns combined results if both sources have data
|
| 96 |
+
|
| 97 |
+
2. **Local Mode**:
|
| 98 |
+
- Only reads from local files
|
| 99 |
+
|
| 100 |
+
3. **Firestore Mode**:
|
| 101 |
+
- Only reads from Firebase Firestore
|
| 102 |
+
|
| 103 |
+
### Agent Behavior
|
| 104 |
+
|
| 105 |
+
When a user asks a question requiring document data, the agent will:
|
| 106 |
+
|
| 107 |
+
1. Detect that document information is needed
|
| 108 |
+
2. Automatically call `read_document_data()` with the relevant query
|
| 109 |
+
3. Search through local files and/or Firestore
|
| 110 |
+
4. Return the relevant information to answer the user's question
|
| 111 |
+
|
| 112 |
+
## Example User Interactions
|
| 113 |
+
|
| 114 |
+
**User:** "What information do you have about our company?"
|
| 115 |
+
- Agent calls: `read_document_data("company information")`
|
| 116 |
+
- Returns relevant content from documents
|
| 117 |
+
|
| 118 |
+
**User:** "List all available documents"
|
| 119 |
+
- Agent calls: `list_available_documents()`
|
| 120 |
+
- Returns formatted list of all documents
|
| 121 |
+
|
| 122 |
+
**User:** "Tell me about product pricing"
|
| 123 |
+
- Agent calls: `read_document_data("product pricing")`
|
| 124 |
+
- Returns pricing information from documents
|
| 125 |
+
|
| 126 |
+
## Firestore Collection Structure
|
| 127 |
+
|
| 128 |
+
Your Firestore `data` collection should have documents structured like:
|
| 129 |
+
|
| 130 |
+
```json
|
| 131 |
+
{
|
| 132 |
+
"name": "Product Catalog",
|
| 133 |
+
"content": "This is the product information...",
|
| 134 |
+
"type": "product",
|
| 135 |
+
"created_at": "2024-01-01"
|
| 136 |
+
}
|
| 137 |
+
```
|
| 138 |
+
|
| 139 |
+
Or simply:
|
| 140 |
+
|
| 141 |
+
```json
|
| 142 |
+
{
|
| 143 |
+
"text": "Document content here..."
|
| 144 |
+
}
|
| 145 |
+
```
|
| 146 |
+
|
| 147 |
+
The tool will look for `content`, `text`, or `data` fields to extract the document text.
|
| 148 |
+
|
| 149 |
+
## Testing
|
| 150 |
+
|
| 151 |
+
Run the example usage file to test the tools:
|
| 152 |
+
|
| 153 |
+
```bash
|
| 154 |
+
python tools/example_usage.py
|
| 155 |
+
```
|
| 156 |
+
|
| 157 |
+
## Troubleshooting
|
| 158 |
+
|
| 159 |
+
**Firebase not initializing:**
|
| 160 |
+
- Check that `serviceAccount.json` exists in the root directory
|
| 161 |
+
- Verify the service account has Firestore permissions
|
| 162 |
+
|
| 163 |
+
**Documents not found:**
|
| 164 |
+
- Verify `data.docx` or PDF files exist in the root directory
|
| 165 |
+
- Check Firestore collection is named `data`
|
| 166 |
+
- Ensure documents have `content`, `text`, or `data` fields
|
| 167 |
+
|
| 168 |
+
**Import errors:**
|
| 169 |
+
- Make sure all dependencies are installed: `pip install -r requirements.txt`
|
tools/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
"""Tools module for the Innoscribe chatbot agent."""
|
tools/document_reader_tool.py
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
import requests
|
| 4 |
+
import logging
|
| 5 |
+
from typing import Optional
|
| 6 |
+
from agents import function_tool
|
| 7 |
+
from docx import Document
|
| 8 |
+
import PyPDF2
|
| 9 |
+
from .firebase_config import db
|
| 10 |
+
|
| 11 |
+
# Set up logging
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@function_tool
|
| 16 |
+
def read_document_data(query: str, source: str = "auto") -> str:
|
| 17 |
+
"""
|
| 18 |
+
Read and search for information from documents stored locally or in Firebase Firestore.
|
| 19 |
+
|
| 20 |
+
Args:
|
| 21 |
+
query: The search query or topic to look for in the documents
|
| 22 |
+
source: Data source - "local" for local files, "firestore" for Firebase, or "auto" to try both
|
| 23 |
+
|
| 24 |
+
Returns:
|
| 25 |
+
The relevant content from the document(s) matching the query
|
| 26 |
+
"""
|
| 27 |
+
logger.info(f"TOOL CALL: read_document_data called with query='{query}', source='{source}'")
|
| 28 |
+
|
| 29 |
+
result = []
|
| 30 |
+
|
| 31 |
+
# Try local files first if source is "local" or "auto"
|
| 32 |
+
if source in ["local", "auto"]:
|
| 33 |
+
local_content = _read_local_documents(query)
|
| 34 |
+
if local_content:
|
| 35 |
+
result.append(f"=== Local Documents ===\n{local_content}")
|
| 36 |
+
|
| 37 |
+
# Try Firestore if source is "firestore" or "auto" (and local didn't return results)
|
| 38 |
+
if source in ["firestore", "auto"] and (not result or source == "firestore"):
|
| 39 |
+
firestore_content = _read_firestore_documents(query)
|
| 40 |
+
if firestore_content:
|
| 41 |
+
result.append(f"=== Firestore Documents ===\n{firestore_content}")
|
| 42 |
+
|
| 43 |
+
if result:
|
| 44 |
+
response = "\n\n".join(result)
|
| 45 |
+
logger.info(f"TOOL RESULT: read_document_data found {len(result)} result(s)")
|
| 46 |
+
return response
|
| 47 |
+
else:
|
| 48 |
+
response = f"No relevant information found for query: '{query}'. Please check if documents are available."
|
| 49 |
+
logger.info(f"TOOL RESULT: read_document_data found no results for query='{query}'")
|
| 50 |
+
return response
|
| 51 |
+
|
| 52 |
+
def _read_local_documents(query: str) -> Optional[str]:
|
| 53 |
+
"""Read from local PDF and DOCX files in the root directory."""
|
| 54 |
+
root_dir = os.path.dirname(os.path.dirname(__file__))
|
| 55 |
+
content_parts = []
|
| 56 |
+
|
| 57 |
+
# Try to read DOCX file
|
| 58 |
+
docx_path = os.path.join(root_dir, "data.docx")
|
| 59 |
+
if os.path.exists(docx_path):
|
| 60 |
+
try:
|
| 61 |
+
doc = Document(docx_path)
|
| 62 |
+
full_text = []
|
| 63 |
+
for paragraph in doc.paragraphs:
|
| 64 |
+
if paragraph.text.strip():
|
| 65 |
+
full_text.append(paragraph.text)
|
| 66 |
+
|
| 67 |
+
docx_content = "\n".join(full_text)
|
| 68 |
+
if docx_content:
|
| 69 |
+
content_parts.append(f"[From data.docx]\n{docx_content}")
|
| 70 |
+
except Exception as e:
|
| 71 |
+
content_parts.append(f"Error reading data.docx: {str(e)}")
|
| 72 |
+
|
| 73 |
+
# Try to read PDF files
|
| 74 |
+
for file in os.listdir(root_dir):
|
| 75 |
+
if file.endswith(".pdf"):
|
| 76 |
+
pdf_path = os.path.join(root_dir, file)
|
| 77 |
+
try:
|
| 78 |
+
with open(pdf_path, "rb") as pdf_file:
|
| 79 |
+
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
| 80 |
+
pdf_text = []
|
| 81 |
+
for page in pdf_reader.pages:
|
| 82 |
+
text = page.extract_text()
|
| 83 |
+
if text.strip():
|
| 84 |
+
pdf_text.append(text)
|
| 85 |
+
|
| 86 |
+
if pdf_text:
|
| 87 |
+
content_parts.append(f"[From {file}]\n" + "\n".join(pdf_text))
|
| 88 |
+
except Exception as e:
|
| 89 |
+
content_parts.append(f"Error reading {file}: {str(e)}")
|
| 90 |
+
|
| 91 |
+
return "\n\n".join(content_parts) if content_parts else None
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def _read_firestore_documents(query: str) -> Optional[str]:
|
| 95 |
+
"""Read documents from Firebase Firestore 'data' collection."""
|
| 96 |
+
if not db:
|
| 97 |
+
return "Firebase Firestore is not initialized. Please check your serviceAccount.json file."
|
| 98 |
+
|
| 99 |
+
try:
|
| 100 |
+
# Query the 'data' collection
|
| 101 |
+
docs_ref = db.collection("data")
|
| 102 |
+
docs = docs_ref.stream()
|
| 103 |
+
|
| 104 |
+
content_parts = []
|
| 105 |
+
for doc in docs:
|
| 106 |
+
doc_data = doc.to_dict()
|
| 107 |
+
|
| 108 |
+
# Check if document field contains a URL to a file
|
| 109 |
+
document_url = doc_data.get("document")
|
| 110 |
+
|
| 111 |
+
if document_url:
|
| 112 |
+
# Download and read the document from URL
|
| 113 |
+
try:
|
| 114 |
+
doc_name = doc_data.get("name", doc.id)
|
| 115 |
+
content = _read_document_from_url(document_url, doc_name)
|
| 116 |
+
if content:
|
| 117 |
+
content_parts.append(f"[From Firestore: {doc_name}]\n{content}")
|
| 118 |
+
except Exception as e:
|
| 119 |
+
content_parts.append(f"[Error reading {doc.id}]: {str(e)}")
|
| 120 |
+
else:
|
| 121 |
+
# Fallback: Try to extract content from different possible field names
|
| 122 |
+
doc_content = (
|
| 123 |
+
doc_data.get("content") or
|
| 124 |
+
doc_data.get("text") or
|
| 125 |
+
doc_data.get("data")
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
if doc_content:
|
| 129 |
+
doc_name = doc_data.get("name", doc.id)
|
| 130 |
+
content_parts.append(f"[From Firestore: {doc_name}]\n{doc_content}")
|
| 131 |
+
|
| 132 |
+
return "\n\n".join(content_parts) if content_parts else None
|
| 133 |
+
|
| 134 |
+
except Exception as e:
|
| 135 |
+
return f"Error reading from Firestore: {str(e)}"
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def _read_document_from_url(url: str, doc_name: str) -> Optional[str]:
|
| 139 |
+
"""Download and read a document (DOCX or PDF) from a URL."""
|
| 140 |
+
try:
|
| 141 |
+
# Download the file from URL
|
| 142 |
+
response = requests.get(url, timeout=30)
|
| 143 |
+
response.raise_for_status()
|
| 144 |
+
|
| 145 |
+
# Determine file type from URL
|
| 146 |
+
if url.lower().endswith('.docx') or 'docx' in url.lower():
|
| 147 |
+
# Read DOCX from bytes
|
| 148 |
+
doc = Document(io.BytesIO(response.content))
|
| 149 |
+
full_text = []
|
| 150 |
+
for paragraph in doc.paragraphs:
|
| 151 |
+
if paragraph.text.strip():
|
| 152 |
+
full_text.append(paragraph.text)
|
| 153 |
+
return "\n".join(full_text)
|
| 154 |
+
|
| 155 |
+
elif url.lower().endswith('.pdf') or 'pdf' in url.lower():
|
| 156 |
+
# Read PDF from bytes
|
| 157 |
+
pdf_reader = PyPDF2.PdfReader(io.BytesIO(response.content))
|
| 158 |
+
pdf_text = []
|
| 159 |
+
for page in pdf_reader.pages:
|
| 160 |
+
text = page.extract_text()
|
| 161 |
+
if text.strip():
|
| 162 |
+
pdf_text.append(text)
|
| 163 |
+
return "\n".join(pdf_text)
|
| 164 |
+
|
| 165 |
+
else:
|
| 166 |
+
return f"Unsupported file type for URL: {url}"
|
| 167 |
+
|
| 168 |
+
except Exception as e:
|
| 169 |
+
raise Exception(f"Failed to download/read document from {url}: {str(e)}")
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
@function_tool
|
| 173 |
+
def list_available_documents() -> str:
|
| 174 |
+
"""
|
| 175 |
+
List all available documents from both local storage and Firestore.
|
| 176 |
+
|
| 177 |
+
Returns:
|
| 178 |
+
A formatted list of available documents from all sources
|
| 179 |
+
"""
|
| 180 |
+
logger.info("TOOL CALL: list_available_documents called")
|
| 181 |
+
|
| 182 |
+
result = []
|
| 183 |
+
|
| 184 |
+
# List local documents
|
| 185 |
+
root_dir = os.path.dirname(os.path.dirname(__file__))
|
| 186 |
+
local_docs = []
|
| 187 |
+
|
| 188 |
+
if os.path.exists(os.path.join(root_dir, "data.docx")):
|
| 189 |
+
local_docs.append("- data.docx")
|
| 190 |
+
|
| 191 |
+
for file in os.listdir(root_dir):
|
| 192 |
+
if file.endswith(".pdf"):
|
| 193 |
+
local_docs.append(f"- {file}")
|
| 194 |
+
|
| 195 |
+
if local_docs:
|
| 196 |
+
result.append("=== Local Documents ===\n" + "\n".join(local_docs))
|
| 197 |
+
|
| 198 |
+
# List Firestore documents
|
| 199 |
+
if db:
|
| 200 |
+
try:
|
| 201 |
+
docs_ref = db.collection("data")
|
| 202 |
+
docs = docs_ref.stream()
|
| 203 |
+
firestore_docs = [f"- {doc.id}" for doc in docs]
|
| 204 |
+
|
| 205 |
+
if firestore_docs:
|
| 206 |
+
result.append("=== Firestore Documents ===\n" + "\n".join(firestore_docs))
|
| 207 |
+
except Exception as e:
|
| 208 |
+
result.append(f"Error listing Firestore documents: {str(e)}")
|
| 209 |
+
|
| 210 |
+
response = "\n\n".join(result) if result else "No documents found in any source."
|
| 211 |
+
logger.info(f"TOOL RESULT: list_available_documents found {len(result)} source(s) with documents")
|
| 212 |
+
return response
|
tools/example_usage.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Example usage of the document reader tools with the agent.
|
| 3 |
+
|
| 4 |
+
This file demonstrates how to integrate the document reading tools
|
| 5 |
+
with your Innoscribe chatbot agent.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from agents import Agent
|
| 9 |
+
from config.chabot_config import model
|
| 10 |
+
from instructions.chatbot_instructions import innscribe_dynamic_instructions
|
| 11 |
+
from guardrails.guardrails_input_function import guardrail_input_function
|
| 12 |
+
from tools.document_reader_tool import read_document_data, list_available_documents
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# Example 1: Agent with document reading capabilities
|
| 16 |
+
innscribe_assistant_with_docs = Agent(
|
| 17 |
+
name="Innoscribe Assistant with Document Access",
|
| 18 |
+
instructions=innscribe_dynamic_instructions,
|
| 19 |
+
model=model,
|
| 20 |
+
input_guardrails=[guardrail_input_function],
|
| 21 |
+
tools=[read_document_data, list_available_documents] # Add the document tools here
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# Example 2: How the agent will use the tools
|
| 26 |
+
"""
|
| 27 |
+
When a user asks a question that requires information from documents:
|
| 28 |
+
|
| 29 |
+
User: "What information do you have about our products?"
|
| 30 |
+
|
| 31 |
+
The agent will automatically:
|
| 32 |
+
1. Try to read from local data.docx and any PDF files first
|
| 33 |
+
2. If not found or insufficient, try to read from Firebase Firestore
|
| 34 |
+
3. Return the relevant information
|
| 35 |
+
|
| 36 |
+
User: "List all available documents"
|
| 37 |
+
The agent will use list_available_documents() to show all docs
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# Example 3: Manual tool usage (for testing)
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
# Test reading documents
|
| 44 |
+
print("Testing document reader tool...")
|
| 45 |
+
result = read_document_data("company information", source="auto")
|
| 46 |
+
print(result)
|
| 47 |
+
|
| 48 |
+
print("\n" + "="*50 + "\n")
|
| 49 |
+
|
| 50 |
+
# Test listing documents
|
| 51 |
+
print("Testing list documents tool...")
|
| 52 |
+
docs = list_available_documents()
|
| 53 |
+
print(docs)
|
tools/firebase_config.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import firebase_admin
|
| 3 |
+
from firebase_admin import credentials, firestore
|
| 4 |
+
|
| 5 |
+
# Initialize Firebase Admin SDK
|
| 6 |
+
def initialize_firebase():
|
| 7 |
+
"""Initialize Firebase Admin SDK with service account credentials."""
|
| 8 |
+
# Get the path to serviceAccount.json in the root directory
|
| 9 |
+
service_account_path = os.path.join(
|
| 10 |
+
os.path.dirname(os.path.dirname(__file__)),
|
| 11 |
+
"serviceAccount.json"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Check if Firebase is already initialized
|
| 15 |
+
if not firebase_admin._apps:
|
| 16 |
+
cred = credentials.Certificate(service_account_path)
|
| 17 |
+
firebase_admin.initialize_app(cred)
|
| 18 |
+
|
| 19 |
+
# Return Firestore client
|
| 20 |
+
return firestore.client()
|
| 21 |
+
|
| 22 |
+
# Create a global Firestore client instance
|
| 23 |
+
try:
|
| 24 |
+
db = initialize_firebase()
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"Warning: Failed to initialize Firebase: {e}")
|
| 27 |
+
db = None
|