-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
67 lines (53 loc) · 2.12 KB
/
main.py
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
# ------------------------------------------------------------------------
# Endpoint for the LMS Assistant API.
#
# Copyright 2024 Pimenko <[email protected]><pimenko.com>
# Author Jordan Kesraoui
# License https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
# ------------------------------------------------------------------------
from typing import Optional
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from models.query import Query
from models.coursesupdated import CoursesUpdated
from chat_moodle import ChatMoodle
from moodle_store import MoodleStore
import os
from dotenv import load_dotenv
load_dotenv()
app = FastAPI()
@app.post("/api/v1/query")
async def query(query: Query):
""" Endpoint for the LMS Assistant API.
Args:
query(Query): The query object
Returns:
dict: The answer
"""
# Check token
if query.token != os.getenv("API_KEY"):
raise HTTPException(status_code=404, detail="Permission denied. Invalid token.")
# Initialize chat engine.
chat_moodle = ChatMoodle(token=os.getenv("WS_TOKEN"), llm_provider=query.llm_provider, model_name=query.model_name,
model_cache=str(os.getenv("MODEL_CACHE")), max_tokens=query.max_call_per_day, doc_language=query.doc_language,
courseid=query.courseid, instruction=query.instruction, history=query.history)
# Call chat engine.
answer = chat_moodle.call_chat(query.message)
# Answer
return {"answer": answer}
@app.post("/api/v1/coursesupdated")
async def coursesupdated(coursesupdated: CoursesUpdated):
""" Endpoint for the LMS Assistant API.
Args:
coursesupdated(CoursesUpdated): The coursesupdated object
Returns:
dict: The coursesupdated
"""
# Check token
if coursesupdated.token != os.getenv("API_KEY"):
raise HTTPException(status_code=404, detail="Permission denied. Invalid token.")
# Launch moodle-lmsassistant.py
moodle_store = MoodleStore(os.getenv("WS_TOKEN"), os.getenv("WS_ENDPOINT"), os.getenv("WS_STORAGE"))
moodle_store.store()
# Courses updated
return {"coursesupdated":"coursesupdated"}