Skip to content

Commit

Permalink
Merge pull request #28 from SebastianJames55/feature/file_naming_#24
Browse files Browse the repository at this point in the history
- logging removed #26 (todo: implement logging #27)
  • Loading branch information
SebastianJames55 authored Sep 23, 2023
2 parents 52a4b4d + ae239d8 commit b7d9c98
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 285 deletions.
46 changes: 6 additions & 40 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,24 @@
import logging

from flask import Flask, g, Blueprint
from flask import Blueprint
from flask_restx import Api

import config
import constants
from connectors import MindsDBConnector
from endpoints.predict import predict_ns
from models import MindsDBModel

app = Flask(__name__)
# Set up logging configuration
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Create a logger for your module
logger = logging.getLogger(__name__)


# Function to get the connector based on the specified app in config.py
def get_connector():
# Create a connector based on the specified app in config.py
if config.APP_TO_CONNECT == constants.MINDSDB:
logger.debug('Choosing mindsdb app')
return MindsDBConnector()
else:
raise ValueError(constants.INVALID_APP_MESSAGE)

from mindsdb.setup import initialize, app

# Function to get the model based on the specified app in config.py
def get_model():
if config.APP_TO_CONNECT == constants.MINDSDB:
return MindsDBModel()
else:
raise ValueError(constants.INVALID_APP_MESSAGE)


@app.before_request
def before_request():
# Set the connector and model instances as application context variables
g.connector = get_connector()
g.model = get_model()

# Create a versioned API Blueprint
api_v1 = Blueprint('api_v1', __name__, url_prefix='/api/v1')

# Initialize Flask-RESTx Api object
api = Api(api_v1, version='0.1.0', title='Mind Reader API', description='API for the Mind Reader backend')


# Register the predict_ns namespace
api.add_namespace(predict_ns)

# Register the API Blueprint
app.register_blueprint(api_v1)


if __name__ == '__main__':
app.logger.setLevel(logging.DEBUG)
app.logger.addHandler(logging.StreamHandler())
app.run(debug=True)
initialize()
app.run(debug=False)
3 changes: 0 additions & 3 deletions config.py

This file was deleted.

File renamed without changes.
30 changes: 30 additions & 0 deletions config/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os

# Data source to connect to
DB_ENGINE = 'yugabyte'
# Name of the database in source
DB_NAME_IN_SOURCE = 'demo'
# Name of the schema in source database
SCHEMA_NAME_IN_SOURCE = 'public'
# Port at which database is available
DB_PORT = 5433
# Database connection arguments
DB_CONNECTION_ARGS = {
# User name
"user": os.environ.get('DB_USER'),
# Password for the user
"password": os.environ.get('DB_PASSWORD'),
# Url where database is available
"host": os.environ.get('DB_HOST'),
# Port to connect to database
"port": DB_PORT,
# Database name
"database": DB_NAME_IN_SOURCE,
# Schema name
"schema": SCHEMA_NAME_IN_SOURCE
}
# Name to be given to DB in the connector
DB_NAME_IN_CONNECTOR = 'yugabyte_demo'

# Name of the chat input table in DB
CHAT_INPUT_TABLE = 'chat_input_table'
3 changes: 3 additions & 0 deletions config/general.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Configuration settings
# Project name
PROJECT_NAME = 'mind_reader_project'
1 change: 0 additions & 1 deletion connectors/__init__.py

This file was deleted.

11 changes: 0 additions & 11 deletions connectors/base_connector.py

This file was deleted.

179 changes: 0 additions & 179 deletions connectors/mindsdb_connector.py

This file was deleted.

2 changes: 1 addition & 1 deletion constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
MINDSDB = 'mindsdb'

# Messages
INVALID_APP_MESSAGE = 'Invalid app specified in config.py'
INVALID_APP_MESSAGE = 'Invalid app specified in general.py'
17 changes: 14 additions & 3 deletions endpoints/predict.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from flask import jsonify, g
from flask_restx import Resource, fields, Namespace

from config.general import PROJECT_NAME
from config.model import MODEL_NAME_IN_CONNECTOR
from mindsdb.utils import get_project

predict_ns = Namespace('predict', description='Prediction operations')

resource_model = predict_ns.model('RequestModel', {
request_model = predict_ns.model('RequestModel', {
'request_message': fields.String(description='Message to get reply to', required=True),
})

Expand All @@ -15,7 +19,7 @@

class Predict(Resource):
@predict_ns.doc('predict')
@predict_ns.expect(resource_model, validate=True)
@predict_ns.expect(request_model, validate=True)
@predict_ns.response(200, 'Success', response_model)
def post(self):
"""
Expand All @@ -27,7 +31,14 @@ def post(self):
request_message = data.get('request_message')

# Make predictions using the connector and model
prediction = g.connector.predict(request_message)
prediction_query = f'''
SELECT response
FROM {PROJECT_NAME}.{MODEL_NAME_IN_CONNECTOR}
WHERE text = "{request_message}";
'''
# Query on the model in the project to make predictions based on data
query = get_project().query(prediction_query)
prediction = query.fetch()
prediction_dict = prediction.to_dict(orient='records')
return prediction_dict, 200

Expand Down
Empty file added mindsdb/__init__.py
Empty file.
45 changes: 45 additions & 0 deletions mindsdb/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from flask import Flask

from config.model import *
from config.general import PROJECT_NAME
from config.database import *
from mindsdb.utils import server, is_project_created, is_model_created, is_db_created

app = Flask(__name__)


def initialize():
project = server.get_project(PROJECT_NAME) if is_project_created(PROJECT_NAME) else server \
.create_project(PROJECT_NAME)
project.get_model(MODEL_NAME_IN_CONNECTOR) if is_model_created(MODEL_NAME_IN_CONNECTOR) \
else project.create_model(
name=MODEL_NAME_IN_CONNECTOR,
predict='response',
engine=MODEL_ENGINE,
options={
'model_name': MODEL_NAME,
'api_key': OPENAI_API_KEY,
'prompt_template': '''
Hello there, I'm here to provide support and information during tough times.
Whether you have questions, need encouragement, or seek resources, I'm here for you.
Please share what's on your mind, and I'll do my best to assist you. Remember, you're not alone,
and together, we can navigate through challenges with strength and resilience.
If you're feeling down, just let me know, and I'll provide uplifting words of encouragement.
If you have specific questions or need information, feel free to ask.
Reply like a friend who cares and wants to help.
Input message: {{text}}
In less than 550 characters, when there's some sign of distress in the input share healthy habits,
motivational quotes, inspirational real-life stories.
Provide options to seek out in-person help if you aren't able to satisfy.
Keep the conversation going by asking them to share more. Be a good listener and conversationalist.
In case there's no signs of distress then reply casually like how you would engage in conversation.
''',
'max_tokens': 300
}
)
server.get_database(DB_NAME_IN_CONNECTOR) if is_db_created(DB_NAME_IN_CONNECTOR) else server \
.create_database(
engine=DB_ENGINE,
name=DB_NAME_IN_CONNECTOR,
connection_args=DB_CONNECTION_ARGS
)
Loading

0 comments on commit b7d9c98

Please sign in to comment.