Skip to content

Commit

Permalink
- logging removed #26 (todo: implement logging #27)
Browse files Browse the repository at this point in the history
- fix project structure #25
- file naming #24
  • Loading branch information
SebastianJames55 committed Sep 4, 2023
1 parent 9726d5e commit 8d4aa77
Show file tree
Hide file tree
Showing 18 changed files with 132 additions and 233 deletions.
26 changes: 4 additions & 22 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,28 @@
import logging

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

import config
import constants
from connectors import MindsDBConnector
from connectors.mindsdb.project.gpt_model import GPTModel
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)


# 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()
return GPTModel()
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()
g.model = get_connector()


# Create a versioned API Blueprint
api_v1 = Blueprint('api_v1', __name__, url_prefix='/api/v1')
Expand All @@ -53,6 +37,4 @@ def before_request():
app.register_blueprint(api_v1)

if __name__ == '__main__':
app.logger.setLevel(logging.DEBUG)
app.logger.addHandler(logging.StreamHandler())
app.run(debug=True)
1 change: 0 additions & 1 deletion connectors/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
from .mindsdb_connector import MindsDBConnector
11 changes: 0 additions & 11 deletions connectors/base_connector.py

This file was deleted.

Empty file added connectors/mindsdb/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions connectors/mindsdb/connector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import mindsdb_sdk


class Connector:
@classmethod
def connect(cls):
return mindsdb_sdk.connect()
Empty file.
34 changes: 34 additions & 0 deletions connectors/mindsdb/database/chat_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os

from connectors.mindsdb.connector import Connector


class ChatDB:
DB_ENGINE = 'yugabyte'
DB_NAME_IN_SOURCE = 'demo'
SCHEMA_NAME_IN_SOURCE = 'public'
DB_PORT = 5433
DB_CONNECTION_ARGS = {
"user": os.environ.get('DB_USER'),
"password": os.environ.get('DB_PASSWORD'),
"host": os.environ.get('DB_HOST'),
"port": DB_PORT,
"database": DB_NAME_IN_SOURCE,
"schema": SCHEMA_NAME_IN_SOURCE
}

# Get the connection lazily
server = Connector.connect()

def create_database(self, db_name):
return self.server.create_database(
engine=self.DB_ENGINE,
name=db_name,
connection_args=self.DB_CONNECTION_ARGS
)

def get_database(self, db_name):
return self.server.get_database(db_name)

def drop_database(self, db_name):
self.server.drop_database(db_name)
7 changes: 7 additions & 0 deletions connectors/mindsdb/database/table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ChatInput:
def query_table(self):
pass

def insert_into_table(self):
pass

Empty file.
53 changes: 53 additions & 0 deletions connectors/mindsdb/project/gpt_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os

from connectors.mindsdb.project.mind_reader_project import MindReaderProject


class GPTModel:

MODEL_ENGINE = 'openai'
MODEL_NAME = 'text-davinci-003'
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
model_name = 'gpt_model'
project = MindReaderProject().get_project()

def __init__(self):
# self.create_model()
pass

def create_model(self):
return self.project.create_model(
name=self.model_name,
predict='response',
engine=self.MODEL_ENGINE,
options={
'model_name': self.MODEL_NAME,
'api_key': self.OPENAI_API_KEY,
'prompt_template': '''
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
}
)

def get_model(self):
return self.project.get_model(self.model_name)

def drop_model(self):
self.project.drop_model(self.model_name)

def predict(self, data):
prediction_query = f'''
SELECT response
FROM {MindReaderProject.PROJECT_NAME}.{self.model_name}
WHERE text = "{data}";
'''
# Query on the model in the project to make predictions based on data
query = self.project.query(prediction_query)
return query.fetch()
6 changes: 6 additions & 0 deletions connectors/mindsdb/project/job.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ChatJob:
def create_job(self):
pass

def get_job(self):
pass
20 changes: 20 additions & 0 deletions connectors/mindsdb/project/mind_reader_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from connectors.mindsdb.connector import Connector


class MindReaderProject:
PROJECT_NAME = 'mind_reader_project'
# Get the connection lazily
server = Connector.connect()

def __init__(self):
# self.create_project()
pass

def create_project(self):
return self.server.create_project(self.PROJECT_NAME)

def get_project(self):
return self.server.get_project(self.PROJECT_NAME)

def drop_project(self):
self.server.drop_project(self.PROJECT_NAME)
Empty file.
179 changes: 0 additions & 179 deletions connectors/mindsdb_connector.py

This file was deleted.

2 changes: 1 addition & 1 deletion endpoints/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def post(self):
request_message = data.get('request_message')

# Make predictions using the connector and model
prediction = g.connector.predict(request_message)
prediction = g.model.predict(request_message)
prediction_dict = prediction.to_dict(orient='records')
return prediction_dict, 200

Expand Down
1 change: 0 additions & 1 deletion models/__init__.py

This file was deleted.

Loading

0 comments on commit 8d4aa77

Please sign in to comment.