-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_app.py
More file actions
71 lines (56 loc) · 2.23 KB
/
Copy pathfunction_app.py
File metadata and controls
71 lines (56 loc) · 2.23 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
import logging
import azure.functions as func
from function_service import FunctionService
import json
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Initialize your service once at module load
service = FunctionService()
@app.route(route="chat/conversation", methods=["POST"])
def func_conversation(req: func.HttpRequest) -> func.HttpResponse:
# 1. Parse body
try:
body = req.get_json() if req.get_body() else {}
except Exception as e:
logger.error("Error parsing JSON body: %s", e)
return func.HttpResponse("Invalid JSON body.", status_code=400)
thread_id = body.get("thread_id")
question = body.get("question")
if not thread_id or not question:
return func.HttpResponse(
"Please provide both 'thread_id' and 'question' parameters.",
status_code=400,
)
# 2. Load existing thread
thread = service.project_client.agents.threads.get(thread_id)
logger.info("📝 Using existing thread, ID: %s", thread.id)
# 3. Save user message: Cosmos DBS
service.save_message_to_cosmosdb(thread.id, question, "user")
# 4. Run agent
answer = service.run_agent_query(thread, question)
if answer:
logger.info("🤖 Assistant replied: %s", answer)
service.save_message_to_cosmosdb(thread.id, answer, "assistant")
return func.HttpResponse(
body=json.dumps({"question": question, "answer": answer}), status_code=200
)
@app.route(route="chat/newchat", methods=["POST"])
def func_newchat(req: func.HttpRequest) -> func.HttpResponse:
# 1. Fetch the agent
try:
agent = service.get_agent()
except Exception as e:
logger.error("Error fetching agent: %s", e)
return func.HttpResponse(
"Agent not found. Please check your configuration.", status_code=404
)
# 2. Create a new thread
if agent:
thread = service.project_client.agents.threads.create()
logger.info("📝 Created new thread, ID: %s", thread.id)
return func.HttpResponse(
json.dumps({"thread_id": thread.id}),
status_code=200,
mimetype="application/json",
)