Bigdata.com is designed specifically for business and finance
professionals, delivering real-time, AI-driven insights with precision
and speed.β Unmatched Data Access β Instantly analyze vast, high-quality datasets.
β Grounding with Bigdata.com β Generated responses reflect the accurate and real-time market conditions.
β Finance & Business Expertise β Designed for professionals, not generic use.
β Seamless Integration β Easily power your chatbot with cutting-edge AI.
Set up a chat-server.py backend (see expandable section below)
Node.js & npm β already set up
Follow the how-to guide Prerequisites to set up your Bigdata.com Python SDK environment
If you are on a Windows machine, please use your workspace in your windows machine instead of your Windows Subsystem for Linux (WSL). Otherwise you will run into issues when trying to execute the application.
Set Up chat-server.py
Create a directory for the chat server:
Copy
Ask AI
mkdir chat-servercd chat-server/
Place the files chat-server.py and requirements.txt in your project directory:
chat-server.py
Copy
Ask AI
from flask import Flask, request, jsonifyfrom flask_cors import CORSimport uuidfrom bigdata_client import Bigdatafrom bigdata_client.models.chat import ChatScopefrom flask import Response, stream_with_contextfrom dotenv import load_dotenvapp = Flask(__name__)from flask import Responseclass UnbufferedResponse(Response): direct_passthrough = Trueapp.response_class = UnbufferedResponseCORS(app)# Initialize Bigdata instanceload_dotenv()db = Bigdata()@app.route('/chats', methods=['POST', 'OPTIONS'])def create_chat(): if request.method == 'OPTIONS': return _build_cors_preflight_response() data = request.json chat_title = data.get("chat_title") chat = db.chat.new(chat_title) chat_id = chat.id # Extract chat ID from the chat object print(f"[INFO] Creating chat with ID: {chat_id}") print(f"[INFO] Created chat with ID: {chat_id}, Title: {chat_title}") return jsonify({"chat_id": chat_id})@app.route('/chats/ask-stream', methods=['GET'])def ask_question_stream(): chat_id = request.args.get("chat_id") question = request.args.get("question") scope = request.args.get("scope") if not chat_id or not question: return jsonify({"error": "Missing required parameters"}), 400 try: scope_enum = ChatScope[scope] if scope and scope != "ALL" else None except KeyError: return jsonify({"error": "Invalid scope value"}), 400 chat_instance = db.chat.get(chat_id) def generate(): if scope_enum: stream = chat_instance.ask(question, scope=scope_enum, streaming=True) else: stream = chat_instance.ask(question, streaming=True) for chunk in stream: # Force flush-friendly formatting (newline-separated, short) yield f"data: {chunk}\n\n" yield "event: done\ndata: [DONE]\n\n" return Response(stream_with_context(generate()), mimetype='text/event-stream')@app.route('/chats', methods=['DELETE', 'OPTIONS'])def delete_chat(): if request.method == 'OPTIONS': return _build_cors_preflight_response() data = request.json chat_id = data.get("chat_id") db.chat.delete(chat_id) print(f"[INFO] Deleted chat with ID: {chat_id}") return jsonify({"message": "Chat deleted successfully"})def _build_cors_preflight_response(): response = jsonify({}) response.headers.add("Access-Control-Allow-Origin", "*") response.headers.add("Access-Control-Allow-Methods", "GET, POST, OPTIONS, DELETE") response.headers.add("Access-Control-Allow-Headers", "Content-Type") return responseif __name__ == "__main__": app.run(debug=True, port=5000)