From 4fa7350400b4af9a5278f25558e23b1632ca120c Mon Sep 17 00:00:00 2001 From: gurveervirk Date: Sun, 1 Sep 2024 16:42:57 +0530 Subject: [PATCH] added swagger to flask app --- .gitignore | 1 - tok/main.py | 30 ++- tok/prompts.json | 21 ++ tok/requirements.txt | 1 + tok/settings.json | 2 +- tok/swagger.yaml | 540 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 590 insertions(+), 5 deletions(-) create mode 100644 tok/prompts.json create mode 100644 tok/swagger.yaml diff --git a/.gitignore b/.gitignore index 2bb8a43..8fd43ce 100644 --- a/.gitignore +++ b/.gitignore @@ -10,7 +10,6 @@ sub_optimal.txt /tok/web/build /tok/build /tok/prev_msgs -/tok/prompts.json /tok/tiktoken_cache /other_prev_msgs /tok/tok.zip diff --git a/tok/main.py b/tok/main.py index 251cb0b..076ad04 100644 --- a/tok/main.py +++ b/tok/main.py @@ -14,6 +14,7 @@ from flask import Flask, jsonify, request, send_from_directory from flask_cors import CORS from flaskwebgui import FlaskUI +from flask_swagger_ui import get_swaggerui_blueprint from llama_index.llms.ollama import Ollama from llama_index.embeddings.fastembed import FastEmbedEmbedding from llama_index.vector_stores.neo4jvector import Neo4jVectorStore @@ -34,6 +35,20 @@ ollama_process = None CORS(app) +# Path to your Swagger YAML file +SWAGGER_URL = '/api/docs' # URL for exposing Swagger UI +API_URL = '/swagger.yaml' # URL for your swagger.yaml file + +# Register the Swagger UI blueprint at /api/docs +swaggerui_blueprint = get_swaggerui_blueprint( + SWAGGER_URL, + API_URL, + config={ # Swagger UI config overrides + 'app_name': "ToK" + } +) +app.register_blueprint(swaggerui_blueprint, url_prefix=SWAGGER_URL) + def start_services(): global ollama_process try: @@ -64,11 +79,15 @@ def create_directory_if_not_exists(directory): def load_settings(): global settings try: - with open('settings.json', 'r+') as f: + with open('settings.json', 'r') as f: settings = json.load(f) - if type(settings["password"]) != str: + + if not isinstance(settings["password"], str): + print("Password not a string. Required for correct operation. Fixing...") + with open('settings.json', 'w') as f: settings["password"] = str(settings["password"]) - json.dump(settings, f) + json.dump(settings, f) + except FileNotFoundError: print("The settings file doesn't exist. Creating a new one...") settings = { @@ -174,6 +193,11 @@ def save_to_session(session, data): json.dump(session_data, session_file) session_file.truncate() +# Serve the Swagger YAML file directly +@app.route('/swagger.yaml') +def swagger_yaml(): + return send_from_directory('web/build/static', 'swagger.yaml') + @app.route('/api/query', methods=['POST']) def query(): try: diff --git a/tok/prompts.json b/tok/prompts.json new file mode 100644 index 0000000..6ac1eb3 --- /dev/null +++ b/tok/prompts.json @@ -0,0 +1,21 @@ +{ + "LLM": { + "default": 0, + "prompts": [ + { "label": "default_prompt", "value": "" }, + { + "label": "Bro", + "value": "Greet user by calling him 'Bro', only when user greets you. Treat other responses normally." + } + ] + }, + "Chat Engine": { + "default": 0, + "prompts": [ + { + "label": "default_prompt", + "value": "You are a chatbot, who needs to answer questions, preferably using the provided context.\nHere are the relevant documents for the context:\n{context_str}\nInstruction: Use the previous chat history, or the context above, to interact and help the user." + } + ] + } +} diff --git a/tok/requirements.txt b/tok/requirements.txt index f954084..76913e3 100644 --- a/tok/requirements.txt +++ b/tok/requirements.txt @@ -1,6 +1,7 @@ transformers== 4.40.1 fastembed==0.3.4 flaskwebgui==1.1.3 +flask-swagger-ui==4.11.1 flask==3.0.0 flask_cors==4.0.0 tqdm==4.66.4 diff --git a/tok/settings.json b/tok/settings.json index c29c109..b6e8639 100644 --- a/tok/settings.json +++ b/tok/settings.json @@ -8,4 +8,4 @@ "context_window": 3900, "token_limit": 2048, "chat_mode": "condense_plus_context" -} +} \ No newline at end of file diff --git a/tok/swagger.yaml b/tok/swagger.yaml new file mode 100644 index 0000000..abff4a9 --- /dev/null +++ b/tok/swagger.yaml @@ -0,0 +1,540 @@ +openapi: 3.0.0 +info: + title: ToK + version: 1.0.5 + description: API for interacting with a chatbot using LlamaIndex, Neo4j, and Ollama. +servers: + - url: http://localhost:5000 + description: Local development server + +paths: + /api/query: + post: + summary: Submit a query to the chatbot + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + query: + type: string + description: The query to ask the chatbot. + useQueryEngine: + type: boolean + description: Whether to use the chat engine or not. + responses: + '200': + description: Successful response + content: + text/plain: + schema: + type: string + description: The chatbot's response to the query. + '400': + description: Bad request (missing parameters) + content: + application/json: + schema: + type: object + properties: + error: + type: string + '500': + description: Internal server error + content: + application/json: + schema: + type: object + properties: + error: + type: string + + /api/history: + get: + summary: Retrieve chat history + responses: + '200': + description: Successful retrieval of chat history + content: + application/json: + schema: + type: object + properties: + Today: + type: array + items: + type: string + Last Week: + type: array + items: + type: string + Last Month: + type: array + items: + type: string + Older: + type: array + items: + type: string + '500': + description: Internal server error + content: + application/json: + schema: + type: object + properties: + error: + type: string + + /api/choose_chat_history: + post: + summary: Load a previous chat session + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + filename: + type: string + description: The name of the session file to load. + responses: + '200': + description: Successful loading of the session + content: + application/json: + schema: + type: array + items: + type: object + '400': + description: Bad request (missing filename parameter) + content: + application/json: + schema: + type: object + properties: + error: + type: string + '404': + description: Session file not found + content: + application/json: + schema: + type: object + properties: + error: + type: string + '500': + description: Internal server error + content: + application/json: + schema: + type: object + properties: + error: + type: string + + /api/add_new_documents: + post: + summary: Add new documents to the vector store + requestBody: + required: true + content: + multipart/form-data: + schema: + type: object + properties: + metadata: + type: string + description: Metadata in JSON format + files: + type: array + items: + type: string + format: binary + description: Files to be added. + responses: + '200': + description: Successful addition of documents + content: + application/json: + schema: + type: object + properties: + success: + type: string + '400': + description: Bad request (missing files or metadata) + content: + application/json: + schema: + type: object + properties: + error: + type: string + '500': + description: Internal server error + content: + application/json: + schema: + type: object + properties: + error: + type: string + + /api/new_chat: + get: + summary: Start a new chat session + responses: + '200': + description: New chat session started successfully + content: + application/json: + schema: + type: object + properties: + message: + type: string + '500': + description: Internal server error + content: + application/json: + schema: + type: object + properties: + error: + type: string + + /api/list_models: + get: + summary: List available models + responses: + '200': + description: List of models retrieved successfully + content: + application/json: + schema: + type: object + properties: + models: + type: array + items: + type: string + selectedModel: + type: string + '500': + description: Internal server error + content: + application/json: + schema: + type: object + properties: + error: + type: string + + /api/select_model: + post: + summary: Select a model to use + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + model: + type: string + description: The name of the model to select. + responses: + '200': + description: Model changed successfully + content: + application/json: + schema: + type: object + properties: + message: + type: string + '400': + description: Bad request (missing model parameter) + content: + application/json: + schema: + type: object + properties: + error: + type: string + '500': + description: Internal server error + content: + application/json: + schema: + type: object + properties: + error: + type: string + + /api/delete_model: + post: + summary: Delete a model + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + model: + type: string + description: The name of the model to delete. + responses: + '200': + description: Model deleted successfully + content: + application/json: + schema: + type: object + properties: + message: + type: string + '400': + description: Bad request (missing model parameter) + content: + application/json: + schema: + type: object + properties: + error: + type: string + '500': + description: Internal server error + content: + application/json: + schema: + type: object + properties: + error: + type: string + + /api/prompts: + get: + summary: List available prompts + responses: + '200': + description: List of prompts retrieved successfully + content: + application/json: + schema: + type: object + properties: + prompts: + type: object + description: Available prompts + example: + LLM: + - label: "Default" + content: "Respond as concisely as possible." + - label: "Verbose" + content: "Respond with detailed explanations." + Chat: + - label: "Default" + content: "Engage in a friendly and informal conversation." + - label: "Formal" + content: "Engage in a formal and professional conversation." + selectedLLMPrompt: + type: object + example: + label: "Verbose" + content: "Respond with detailed explanations." + selectedChatEnginePrompt: + type: object + example: + label: "Formal" + content: "Engage in a formal and professional conversation." + defaults: + type: object + properties: + LLM: + type: integer + Chat Engine: + type: integer + '500': + description: Internal server error + content: + application/json: + schema: + type: object + properties: + error: + type: string + + post: + summary: Update the prompts + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + LLM: + type: array + items: + type: object + Chat: + type: array + items: + type: object + defaults: + type: object + properties: + LLM: + type: integer + Chat Engine: + type: integer + selectedLLMPrompt: + type: object + selectedChatEnginePrompt: + type: object + responses: + '200': + description: Prompts updated successfully + content: + application/json: + schema: + type: object + properties: + message: + type: string + '500': + description: Internal server error + content: + application/json: + schema: + type: object + properties: + error: + type: string + + /api/delete_prompt: + post: + summary: Delete a prompt + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + type: + type: string + description: The type of the prompt (LLM or Chat). + label: + type: string + description: The label of the prompt to delete. + responses: + '200': + description: Prompt deleted successfully + content: + application/json: + schema: + type: object + properties: + message: + type: string + '400': + description: Bad request (missing type or label) + content: + application/json: + schema: + type: object + properties: + error: + type: string + '500': + description: Internal server error + content: + application/json: + schema: + type: object + properties: + error: + type: string + + /api/settings: + get: + summary: Retrieve current settings + responses: + '200': + description: Successfully retrieved settings + content: + application/json: + schema: + type: object + properties: + settings: + type: object + description: Current settings + example: + database: + uri: "bolt://localhost:7687" + username: "neo4j" + password: "password" + llamaindex: + index_path: "index.json" + model: + name: "GPT-3.5-turbo" + '500': + description: Internal server error + content: + application/json: + schema: + type: object + properties: + error: + type: string + + post: + summary: Update settings + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + settings: + type: object + description: Updated settings. + example: + database: + uri: "bolt://localhost:7687" + username: "neo4j" + password: "newpassword" + llamaindex: + index_path: "new_index.json" + model: + name: "GPT-4.0-turbo" + responses: + '200': + description: Successfully updated settings + content: + application/json: + schema: + type: object + properties: + message: + type: string + '500': + description: Internal server error + content: + application/json: + schema: + type: object + properties: + error: + type: string