-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
94 lines (65 loc) · 2.38 KB
/
app.py
File metadata and controls
94 lines (65 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from flask import Flask, request, jsonify, render_template
#from flask_cors import CORS
#imported flask related packages.
import os
import asyncio
from mcp_use import MCPClient, MCPAgent
from langchain_groq import ChatGroq # Changed from langchain_openai import ChatOpenAI
from flask_cors import CORS
# MCP Config
MCP_CONFIG = {
"mcpServers": {
"sqlite": {
"command": "npx",
"args": [
"-y",
"@executeautomation/database-server",
"C:\\Users\\Abc\\office.db"
]
}
}
}
# Load Agent once
agent = None
def setup_agent():
global agent
if agent is None:
client = MCPClient.from_dict(MCP_CONFIG)
# Changed from ChatOpenAI to ChatGroq
# Choose a Groq model. 'llama-3.3-70b-versatile' is a strong option for complex tasks,
# 'llama-3.1-8b-instant' is faster for simpler tasks.
# Ensure GROQ_API_KEY is set in your .env file or environment variables.
llm = ChatGroq(
model_name="llama-3.3-70b-versatile", # Using a powerful Groq model
# Groq API key is typically loaded from the environment variable GROQ_API_KEY
# You can explicitly pass it if needed, but using os.getenv is recommended.
groq_api_key = "your api key hear." #os.getenv("GROQ_API_KEY") # No need for a placeholder like "sk-..."
)
agent = MCPAgent(llm=llm, client=client, max_steps=10)
setup_agent()
# Chat function
def answer_query(user_input):
try:
if not user_input.strip():
return {"reply":"no input found"}
result = asyncio.run(agent.run(user_input))
return {"reply": result}
except Exception as e:
import traceback
return {"reply":e}
app = Flask(__name__)
#CORS(app) # ✅ Enables CORS for JS fetch requests
@app.route("/")
def home():
return render_template("index.html") # ✅ Loads HTML from templates/
@app.route("/chat", methods=["POST"])
def chat():
data = request.get_json()
app.logger.info(f'the received data is: {data}')
user_input = data.get("message", "")
app.logger.info(f'the parsed data: {user_input}')
print(f"[Received] User Input: {user_input}") # Logs input received
response = answer_query(user_input)
app.logger.info(f'the received data is: {response}')
return jsonify(response)
app.run(debug = True)