-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
377 additions
and
169 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from werkzeug.local import LocalProxy | ||
from kairos.models.db import get_db | ||
|
||
db = LocalProxy(get_db) | ||
|
||
def get_messages(): | ||
return list(db.messages.find({'context': False},{'_id': False})) | ||
|
||
def get_messages_by_log_id(event_log_id): | ||
return list(db.messages.find({'context': False, 'event_log_id': int(event_log_id)},{'_id': False})) | ||
|
||
def get_messages_by_case_id(case_id): | ||
return list(db.messages.find({'context': False, 'case_id': case_id},{'_id': False})) | ||
|
||
def get_context(): | ||
return list(db.messages.find({},{'_id': False,'context': False, 'event_log_id': False, 'case_id': False})) | ||
|
||
def get_context_by_log_id(event_log_id): | ||
return list(db.messages.find({'event_log_id': int(event_log_id)},{'_id': False,'context': False, 'event_log_id': False, 'case_id': False})) | ||
|
||
def get_context_by_case_id(case_id): | ||
return list(db.messages.find({'case_id': case_id},{'_id': False,'context': False, 'event_log_id': False, 'case_id': False})) | ||
|
||
def save_message(role,content,context=False,event_log_id=None,case_id=None): | ||
new_message = { | ||
'role': role, | ||
'content': content, | ||
'context': context, | ||
'event_log_id': event_log_id, | ||
'case_id': case_id | ||
} | ||
return db.messages.insert_one(new_message) | ||
|
||
def count_messages(): | ||
return db.messages.count_documents({'context': False}) | ||
|
||
def delete_messages(): | ||
return db.messages.deleteMany({}) | ||
|
||
def get_system_messages(): | ||
return list(db.messages.find({'role': 'system'})) |
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,41 @@ | ||
from flask import request, jsonify, current_app | ||
|
||
import kairos.models.messages_model as messages_db | ||
import kairos.utils.openai as openai_utils | ||
|
||
def get_messages_for_log(event_log_id): | ||
try: | ||
messages = messages_db.get_messages_by_log_id(event_log_id=event_log_id) | ||
except Exception as e: | ||
current_app.logger.error(f'{request.method} {request.path} 500 - {e}') | ||
return jsonify(error=str(e)),500 | ||
current_app.logger.info(f'{request.method} {request.path} 200') | ||
return jsonify(memory = messages),200 | ||
|
||
def get_messages_for_case(event_log_id,case_id): | ||
try: | ||
messages = messages_db.get_messages_by_case_id(case_id=case_id) | ||
except Exception as e: | ||
current_app.logger.error(f'{request.method} {request.path} 500 - {e}') | ||
return jsonify(error=str(e)),500 | ||
current_app.logger.info(f'{request.method} {request.path} 200') | ||
return jsonify(memory = messages),200 | ||
|
||
def get_answer(event_log_id,case_id): | ||
if not event_log_id or not case_id: | ||
current_app.logger.error('Event log id and case id cannot be null.') | ||
return jsonify(error='Please specify event_log_id and case_id.'),403 | ||
|
||
question = request.get_json().get('question') | ||
if not question: | ||
current_app.logger.error('Question cannot be null.') | ||
return jsonify(error='Please specify a question.'),403 | ||
|
||
try: | ||
answer = openai_utils.ask_ai(content=question, event_log_id=event_log_id,case_id=case_id) | ||
except Exception as e: | ||
current_app.logger.error(f'{request.method} {request.path} 500 - {e}') | ||
return jsonify(error=str(e)),500 | ||
|
||
current_app.logger.info(f'{request.method} {request.path} 200') | ||
return jsonify(answer = answer),200 |
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
Oops, something went wrong.