Skip to content

Commit

Permalink
prompt logs
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisclark committed Jan 11, 2024
1 parent 945626a commit 4c73262
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 5 deletions.
21 changes: 21 additions & 0 deletions explorer/assistant/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.db import models

Check failure on line 1 in explorer/assistant/models.py

View workflow job for this annotation

GitHub Actions / isort

Imports are incorrectly sorted and/or formatted.

Check failure on line 1 in explorer/assistant/models.py

View workflow job for this annotation

GitHub Actions / isort

Imports are incorrectly sorted and/or formatted.
from django.conf import settings


class PromptLog(models.Model):

class Meta:
app_label = 'explorer'

prompt = models.TextField(blank=True)
response = models.TextField(blank=True)
run_by_user = models.ForeignKey(
settings.AUTH_USER_MODEL,
null=True,
blank=True,
on_delete=models.CASCADE
)
run_at = models.DateTimeField(auto_now_add=True)
duration = models.FloatField(blank=True, null=True) # seconds
model = models.CharField(blank=True, max_length=128, default='')
error = models.TextField(blank=True, null=True)
1 change: 0 additions & 1 deletion explorer/assistant/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def do_req(prompt, history=None):
{"role": "system", "content": prompt['system']},
{"role": "user", "content": prompt['user']},
]
print(messages)
resp = openai_client.chat.completions.create(
model=OPENAI_MODEL,
messages=messages
Expand Down
27 changes: 23 additions & 4 deletions explorer/assistant/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.http import JsonResponse

Check failure on line 1 in explorer/assistant/views.py

View workflow job for this annotation

GitHub Actions / isort

Imports are incorrectly sorted and/or formatted.

Check failure on line 1 in explorer/assistant/views.py

View workflow job for this annotation

GitHub Actions / isort

Imports are incorrectly sorted and/or formatted.
from django.utils import timezone
import json

from sql_metadata import Parser
from .models import PromptLog
from explorer.schema import schema_info
from explorer.assistant.prompts import PROMPT_MAP
from explorer.assistant.utils import format_prompt, do_req, extract_response
Expand All @@ -12,7 +14,7 @@ def tables_from_schema_info(connection, table_names):
return [table for table in schema if table[0] in table_names]


def do_modify_query(request_data):
def do_modify_query(request_data, user):
sql = request_data['sql']
connection = request_data['connection']
assistant_request = request_data['assistant_request']
Expand All @@ -24,8 +26,25 @@ def do_modify_query(request_data):
'description': assistant_request,
'table_structure': table_struct
})
resp = do_req(prompt)
return extract_response(resp)
start = timezone.now()
pl = PromptLog(
prompt=prompt,
run_by_user=user,
run_at=timezone.now(),
)
response_text = None
try:
resp = do_req(prompt)
response_text = extract_response(resp)
pl.response = response_text
except Exception as e:
pl.error = str(e)
finally:
stop = timezone.now()
pl.duration = (stop - start).total_seconds()
pl.save()
return response_text



def assistant_help(request):

Check failure on line 50 in explorer/assistant/views.py

View workflow job for this annotation

GitHub Actions / flake8

too many blank lines (3)

Check failure on line 50 in explorer/assistant/views.py

View workflow job for this annotation

GitHub Actions / flake8

too many blank lines (3)
Expand All @@ -35,7 +54,7 @@ def assistant_help(request):
assistant_function = data['assistant_function']

if assistant_function == 'modify_query':
resp = do_modify_query(data)
resp = do_modify_query(data, request.user)
else:
resp = None

Expand Down
29 changes: 29 additions & 0 deletions explorer/migrations/0014_promptlog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 4.2.8 on 2024-01-11 08:22

Check failure on line 1 in explorer/migrations/0014_promptlog.py

View workflow job for this annotation

GitHub Actions / isort

Imports are incorrectly sorted and/or formatted.

Check failure on line 1 in explorer/migrations/0014_promptlog.py

View workflow job for this annotation

GitHub Actions / isort

Imports are incorrectly sorted and/or formatted.

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('explorer', '0013_querylog_error_querylog_success'),
]

operations = [
migrations.CreateModel(
name='PromptLog',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('prompt', models.TextField(blank=True)),
('response', models.TextField(blank=True)),
('run_at', models.DateTimeField(auto_now_add=True)),
('duration', models.FloatField(blank=True, null=True)),
('model', models.CharField(blank=True, default='', max_length=128)),
('error', models.TextField(blank=True, null=True)),
('run_by_user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
1 change: 1 addition & 0 deletions explorer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.urls import reverse
from django.utils.translation import gettext_lazy as _

from assistant.models import PromptLog

Check failure on line 10 in explorer/models.py

View workflow job for this annotation

GitHub Actions / flake8

'assistant.models.PromptLog' imported but unused

Check failure on line 10 in explorer/models.py

View workflow job for this annotation

GitHub Actions / flake8

'assistant.models.PromptLog' imported but unused
from explorer import app_settings
from explorer.utils import (
extract_params, get_params_for_url, get_s3_bucket, get_valid_connection, passes_blacklist, s3_url,
Expand Down

0 comments on commit 4c73262

Please sign in to comment.