File size: 1,119 Bytes
98651d2
 
 
894e5af
98651d2
 
 
 
 
 
 
894e5af
98651d2
 
 
 
 
 
894e5af
98651d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
894e5af
98651d2
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from flask import Flask, request, jsonify
from duckai import DuckAI
from flask_cors import CORS

app = Flask(__name__)
CORS(app, resources={r"/*": {
    "origins": "*",
    "allow_headers": "*",
    "methods": ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
    "support_credentials": True
}})

def chat_with_model(query: str, model: str):
    try:
        results = DuckAI().chat(query, model=model)
        return jsonify({"results": results})
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route("/chat/", methods=["GET"])
def chat():
    query = request.args.get("query")
    if not query:
        return jsonify({"error": "Query parameter is required"}), 400
    
    try:
        results = DuckAI().chat(query, model='gpt-4o-mini')
        return jsonify({"results": results})
    except Exception as e:
        try:
            results = DuckAI().chat(query, model='claude-3-haiku')
            return jsonify({"results": results})
        except Exception as e:
            return jsonify({"error": str(e)}), 500

if __name__ == "__main__":
    app.run(host="localhost", port=3000)