Skip to content

Commit

Permalink
added swagger to flask app
Browse files Browse the repository at this point in the history
  • Loading branch information
gurveervirk committed Sep 1, 2024
1 parent 5c16655 commit 4fa7350
Show file tree
Hide file tree
Showing 6 changed files with 590 additions and 5 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 27 additions & 3 deletions tok/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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:
Expand Down
21 changes: 21 additions & 0 deletions tok/prompts.json
Original file line number Diff line number Diff line change
@@ -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."
}
]
}
}
1 change: 1 addition & 0 deletions tok/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion tok/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"context_window": 3900,
"token_limit": 2048,
"chat_mode": "condense_plus_context"
}
}
Loading

0 comments on commit 4fa7350

Please sign in to comment.