This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement the conversational api
- Loading branch information
Showing
7 changed files
with
185 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,14 @@ | |
from starlette.status import HTTP_201_CREATED | ||
|
||
from src.models.main_model import MainModel | ||
from src.models.create_api_model import CreateAPIModel | ||
from src.models.chatting_model import ChattingModel | ||
|
||
from utils.total_process import total_process | ||
from utils.create_api import create_api_key | ||
from utils.delete_api_key import delete_api_key | ||
from utils.check_api_key import check_api_key | ||
from utils.chatting import chatting | ||
|
||
# Create a FastAPI application | ||
app = FastAPI(swagger_ui_parameters={"tryItOutEnabled": True}) | ||
|
@@ -39,12 +46,31 @@ | |
allow_headers=["*"], | ||
) | ||
|
||
# Current directory | ||
current_dir = os.path.dirname(os.path.abspath(__file__)) | ||
|
||
# Get the parent directory | ||
parent_dir = os.path.dirname(current_dir) | ||
|
||
# Define a route to handle the root endpoint and redirect to the API documentation | ||
@app.get("/") | ||
async def root(): | ||
return RedirectResponse(app.docs_url) | ||
|
||
def get_payload_dir(data_id: str): | ||
payload_name = "payload.json" | ||
if data_id == "": | ||
test_name = "regression_test013" | ||
else: | ||
test_name = data_id | ||
|
||
payload_dir = os.path.join(parent_dir, "test", "regression", test_name, "payload", payload_name) | ||
|
||
return payload_dir | ||
|
||
@app.post("/finetuning") | ||
async def finetuning(request_body: MainModel): | ||
payload_dir = get_payload_dir(request_body.data_id) | ||
|
||
if request_body.user == "": | ||
user = "[email protected]" | ||
|
@@ -63,3 +89,92 @@ async def root(): | |
} | ||
|
||
total_process(args) | ||
|
||
|
||
@app.post("/create_api") | ||
async def create_api(request_body: CreateAPIModel): | ||
payload_dir = get_payload_dir(request_body.data_id) | ||
|
||
if request_body.user == "": | ||
user = "[email protected]" | ||
else: | ||
user = request_body.user | ||
|
||
args = { | ||
'payload_dir' : payload_dir, | ||
'user' : user, | ||
'title' : request_body.title, | ||
'description' : request_body.description | ||
} | ||
|
||
create_api_key(args) | ||
|
||
|
||
@app.post("/delete_api") | ||
async def delete_api(request_body: MainModel): | ||
payload_dir = get_payload_dir(request_body.data_id) | ||
|
||
if request_body.user == "": | ||
user = "[email protected]" | ||
else: | ||
user = request_body.user | ||
|
||
if request_body.api_key == "": | ||
api_key = "AMEYbpdcmrUxNu_Fb80qutukUZdlsmYiH4g7As5LzNA1" | ||
else: | ||
api_key = request_body.api_key | ||
|
||
args = { | ||
'payload_dir' : payload_dir, | ||
'user' : user, | ||
'api_key' : api_key | ||
} | ||
|
||
delete_api_key(args) | ||
|
||
|
||
@app.post("/check_api") | ||
async def check_api(request_body: MainModel): | ||
payload_dir = get_payload_dir(request_body.data_id) | ||
|
||
if request_body.user == "": | ||
user = "[email protected]" | ||
else: | ||
user = request_body.user | ||
|
||
if request_body.api_key == "": | ||
api_key = "AMEYbpdcmrUxNu_Fb80qutukUZdlsmYiH4g7As5LzNA1" | ||
else: | ||
api_key = request_body.api_key | ||
|
||
args = { | ||
'payload_dir' : payload_dir, | ||
'user' : user, | ||
'api_key' : api_key | ||
} | ||
|
||
check_api_key(args) | ||
|
||
|
||
@app.post("/conversation") | ||
async def conversation(request_body: ChattingModel): | ||
payload_dir = get_payload_dir(request_body.data_id) | ||
|
||
if request_body.user == "": | ||
user = "[email protected]" | ||
else: | ||
user = request_body.user | ||
|
||
if request_body.api_key == "": | ||
api_key = "AMEYbpdcmrUxNu_Fb80qutukUZdlsmYiH4g7As5LzNA1" | ||
else: | ||
api_key = request_body.api_key | ||
|
||
args = { | ||
'payload_dir' : payload_dir, | ||
'user' : user, | ||
'api_key' : api_key, | ||
'question' : request_body.question | ||
} | ||
|
||
chatting(args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import gc | ||
from pathlib import Path | ||
from urllib.parse import quote_plus | ||
|
||
from src.utils.read_json import read_json | ||
from src.chatting.ChattingClass import ChattingClass | ||
from src.mongodb.MongoDBClass import MongoDBClass | ||
|
||
def chatting(args): | ||
""" | ||
main entry point | ||
""" | ||
|
||
# Payload | ||
payload_data = read_json(args['payload_dir']) | ||
|
||
# Your MongoDB Atlas connection details | ||
mongodb_username = payload_data["mongodb_username"] | ||
mongodb_password = payload_data["mongodb_password"] | ||
mongodb_cluster_name = payload_data["mongodb_cluster_name"] | ||
mongodb_database_name = payload_data["mongodb_database_name"] | ||
|
||
# Escape the mongodb_username and mongodb_password | ||
mongodb_escaped_username = quote_plus(mongodb_username) | ||
mongodb_escaped_password = quote_plus(mongodb_password) | ||
|
||
# Construct the MongoDB Atlas URI | ||
mongo_uri = f"mongodb+srv://{mongodb_escaped_username}:{mongodb_escaped_password}@{mongodb_cluster_name}.mongodb.net/{mongodb_database_name}" | ||
|
||
# Call class instance | ||
mongodb = MongoDBClass( | ||
db_name=payload_data["db_name"], | ||
collection_name=payload_data["collection_name"], | ||
mongo_uri=mongo_uri) | ||
|
||
is_available = mongodb.check_validation_api(api_key=str(Path(args['api_key'])), user=str(Path(args['user']))) | ||
|
||
if is_available: | ||
print("valid api key") | ||
# Call class instance | ||
chatting = ChattingClass( | ||
data_path=payload_data["data_path"], | ||
api_key=payload_data["api_key"], | ||
model_id=payload_data["model_id"], | ||
temperature=payload_data["temperature"]) | ||
|
||
response = chatting.ask_question(args['question']) | ||
print(response) | ||
else: | ||
print("invalide api key") | ||
|
||
gc.collect() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
|
||
import os | ||
import gc | ||
import argparse | ||
from pathlib import Path | ||
from urllib.parse import quote_plus | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
|
||
import os | ||
import gc | ||
import argparse | ||
from pathlib import Path | ||
from urllib.parse import quote_plus | ||
|
||
|