Skip to content

Commit

Permalink
feature/Make AISettings sparse overrides only (#1180)
Browse files Browse the repository at this point in the history
* Make AISettings sparse overrides only

* bug fix

* remove nulls

* set all values to null
  • Loading branch information
gecBurton authored Nov 12, 2024
1 parent 2cf2c0c commit 3d8812e
Show file tree
Hide file tree
Showing 3 changed files with 287 additions and 39 deletions.
3 changes: 3 additions & 0 deletions django_app/redbox_app/redbox_core/consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ def save_ai_message(
def get_ai_settings(chat: Chat) -> AISettings:
ai_settings = model_to_dict(chat.user.ai_settings, exclude=["label", "chat_backend"])
ai_settings["chat_backend"] = model_to_dict(chat.chat_backend)

# we remove null values so that AISettings can populate them with defaults
ai_settings = {k: v for k, v in ai_settings.items() if v is not None}
return AISettings.model_validate(ai_settings)

async def handle_text(self, response: str) -> str:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
# Generated by Django 5.1.2 on 2024-11-12 06:09
import django
from django.db import migrations, models

def set_ai_settings_prompt_value_to_null(apps, schema_editor):
AISettings = apps.get_model("redbox_core", "AISettings")
for ai_settings in AISettings.objects.all():
ai_settings.context_window_size = None
ai_settings.llm_max_tokens = None
ai_settings.max_document_tokens = None
ai_settings.self_route_enabled = None
ai_settings.map_max_concurrency = None
ai_settings.stuff_chunk_context_ratio = None
ai_settings.recursion_limit = None
ai_settings.chat_system_prompt = None
ai_settings.chat_question_prompt = None
ai_settings.chat_with_docs_system_prompt = None
ai_settings.chat_with_docs_question_prompt = None
ai_settings.chat_with_docs_reduce_system_prompt = None
ai_settings.retrieval_system_prompt = None
ai_settings.retrieval_question_prompt = None
ai_settings.agentic_retrieval_system_prompt = None
ai_settings.agentic_retrieval_question_prompt = None
ai_settings.agentic_give_up_system_prompt = None
ai_settings.agentic_give_up_question_prompt = None
ai_settings.condense_system_prompt = None
ai_settings.condense_question_prompt = None
ai_settings.chat_map_system_prompt = None
ai_settings.chat_map_question_prompt = None
ai_settings.reduce_system_prompt = None
ai_settings.rag_k = None
ai_settings.rag_num_candidates = None
ai_settings.rag_gauss_scale_size = None
ai_settings.rag_gauss_scale_decay = None
ai_settings.rag_gauss_scale_min = None
ai_settings.rag_gauss_scale_max = None
ai_settings.rag_desired_chunk_size = None
ai_settings.elbow_filter_enabled = None
ai_settings.match_boost = None
ai_settings.match_name_boost = None
ai_settings.match_description_boost = None
ai_settings.match_keywords_boost = None
ai_settings.knn_boost = None
ai_settings.similarity_threshold = None

ai_settings.save()

class Migration(migrations.Migration):

dependencies = [
('redbox_core', '0066_alter_aisettings_agentic_retrieval_system_prompt'),
]

operations = [
migrations.AlterField(
model_name='aisettings',
name='context_window_size',
field=models.PositiveIntegerField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='llm_max_tokens',
field=models.PositiveIntegerField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='map_max_concurrency',
field=models.PositiveIntegerField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='max_document_tokens',
field=models.PositiveIntegerField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='recursion_limit',
field=models.PositiveIntegerField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='self_route_enabled',
field=models.BooleanField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='stuff_chunk_context_ratio',
field=models.FloatField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='agentic_give_up_question_prompt',
field=models.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='agentic_give_up_system_prompt',
field=models.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='agentic_retrieval_question_prompt',
field=models.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='agentic_retrieval_system_prompt',
field=models.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='chat_map_question_prompt',
field=models.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='chat_map_system_prompt',
field=models.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='chat_question_prompt',
field=models.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='chat_system_prompt',
field=models.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='chat_with_docs_question_prompt',
field=models.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='chat_with_docs_reduce_system_prompt',
field=models.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='chat_with_docs_system_prompt',
field=models.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='condense_question_prompt',
field=models.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='condense_system_prompt',
field=models.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='reduce_system_prompt',
field=models.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='retrieval_question_prompt',
field=models.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='retrieval_system_prompt',
field=models.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='rag_gauss_scale_decay',
field=models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True, validators=[django.core.validators.MinValueValidator(0.0)]),
),
migrations.AlterField(
model_name='aisettings',
name='rag_gauss_scale_max',
field=models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True, validators=[django.core.validators.MinValueValidator(1.0)]),
),
migrations.AlterField(
model_name='aisettings',
name='rag_gauss_scale_min',
field=models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True, validators=[django.core.validators.MinValueValidator(1.0)]),
),
migrations.AlterField(
model_name='aisettings',
name='rag_gauss_scale_size',
field=models.PositiveIntegerField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='rag_k',
field=models.PositiveIntegerField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='rag_num_candidates',
field=models.PositiveIntegerField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='elbow_filter_enabled',
field=models.BooleanField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='knn_boost',
field=models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='match_boost',
field=models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='match_description_boost',
field=models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='match_keywords_boost',
field=models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='match_name_boost',
field=models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='rag_desired_chunk_size',
field=models.PositiveIntegerField(blank=True, null=True),
),
migrations.AlterField(
model_name='aisettings',
name='similarity_threshold',
field=models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True, validators=[django.core.validators.MinValueValidator(0.0), django.core.validators.MaxValueValidator(1.0)]),
),
migrations.RunPython(set_ai_settings_prompt_value_to_null, migrations.RunPython.noop),

]
81 changes: 42 additions & 39 deletions django_app/redbox_app/redbox_core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from django_use_email_as_username.models import BaseUser, BaseUserManager
from yarl import URL

from redbox.models import prompts
from redbox.models.settings import get_settings
from redbox_app.redbox_core.utils import get_date_group

Expand Down Expand Up @@ -110,66 +109,70 @@ class AISettings(UUIDPrimaryKeyBase, TimeStampedModel, AbstractAISettings):
label = models.CharField(max_length=50, unique=True)

# LLM settings
context_window_size = models.PositiveIntegerField(default=128_000)
llm_max_tokens = models.PositiveIntegerField(default=1024)
context_window_size = models.PositiveIntegerField(null=True, blank=True)
llm_max_tokens = models.PositiveIntegerField(null=True, blank=True)

# Prompts and LangGraph settings
max_document_tokens = models.PositiveIntegerField(default=1_000_000, null=True, blank=True)
self_route_enabled = models.BooleanField(default=False)
map_max_concurrency = models.PositiveIntegerField(default=128)
stuff_chunk_context_ratio = models.FloatField(default=0.75)
recursion_limit = models.PositiveIntegerField(default=50)

chat_system_prompt = models.TextField(default=prompts.CHAT_SYSTEM_PROMPT)
chat_question_prompt = models.TextField(default=prompts.CHAT_QUESTION_PROMPT)
chat_with_docs_system_prompt = models.TextField(default=prompts.CHAT_WITH_DOCS_SYSTEM_PROMPT)
chat_with_docs_question_prompt = models.TextField(default=prompts.CHAT_WITH_DOCS_QUESTION_PROMPT)
chat_with_docs_reduce_system_prompt = models.TextField(default=prompts.CHAT_WITH_DOCS_REDUCE_SYSTEM_PROMPT)
retrieval_system_prompt = models.TextField(default=prompts.RETRIEVAL_SYSTEM_PROMPT)
retrieval_question_prompt = models.TextField(default=prompts.RETRIEVAL_QUESTION_PROMPT)
agentic_retrieval_system_prompt = models.TextField(default=prompts.AGENTIC_RETRIEVAL_SYSTEM_PROMPT)
agentic_retrieval_question_prompt = models.TextField(default=prompts.AGENTIC_RETRIEVAL_QUESTION_PROMPT)
agentic_give_up_system_prompt = models.TextField(default=prompts.AGENTIC_GIVE_UP_SYSTEM_PROMPT)
agentic_give_up_question_prompt = models.TextField(default=prompts.AGENTIC_GIVE_UP_QUESTION_PROMPT)
condense_system_prompt = models.TextField(default=prompts.CONDENSE_SYSTEM_PROMPT)
condense_question_prompt = models.TextField(default=prompts.CONDENSE_QUESTION_PROMPT)
chat_map_system_prompt = models.TextField(default=prompts.CHAT_MAP_SYSTEM_PROMPT)
chat_map_question_prompt = models.TextField(default=prompts.CHAT_MAP_QUESTION_PROMPT)
reduce_system_prompt = models.TextField(default=prompts.REDUCE_SYSTEM_PROMPT)
max_document_tokens = models.PositiveIntegerField(null=True, blank=True)
self_route_enabled = models.BooleanField(null=True, blank=True)
map_max_concurrency = models.PositiveIntegerField(null=True, blank=True)
stuff_chunk_context_ratio = models.FloatField(null=True, blank=True)
recursion_limit = models.PositiveIntegerField(null=True, blank=True)

chat_system_prompt = models.TextField(null=True, blank=True)
chat_question_prompt = models.TextField(null=True, blank=True)
chat_with_docs_system_prompt = models.TextField(null=True, blank=True)
chat_with_docs_question_prompt = models.TextField(null=True, blank=True)
chat_with_docs_reduce_system_prompt = models.TextField(null=True, blank=True)
retrieval_system_prompt = models.TextField(null=True, blank=True)
retrieval_question_prompt = models.TextField(null=True, blank=True)
agentic_retrieval_system_prompt = models.TextField(null=True, blank=True)
agentic_retrieval_question_prompt = models.TextField(null=True, blank=True)
agentic_give_up_system_prompt = models.TextField(null=True, blank=True)
agentic_give_up_question_prompt = models.TextField(null=True, blank=True)
condense_system_prompt = models.TextField(null=True, blank=True)
condense_question_prompt = models.TextField(null=True, blank=True)
chat_map_system_prompt = models.TextField(null=True, blank=True)
chat_map_question_prompt = models.TextField(null=True, blank=True)
reduce_system_prompt = models.TextField(null=True, blank=True)

# Elsticsearch RAG and boost values
rag_k = models.PositiveIntegerField(default=30)
rag_num_candidates = models.PositiveIntegerField(default=10)
rag_gauss_scale_size = models.PositiveIntegerField(default=3)
rag_k = models.PositiveIntegerField(null=True, blank=True)
rag_num_candidates = models.PositiveIntegerField(null=True, blank=True)
rag_gauss_scale_size = models.PositiveIntegerField(null=True, blank=True)
rag_gauss_scale_decay = models.DecimalField(
max_digits=5,
decimal_places=2,
default=0.5,
null=True,
blank=True,
validators=[validators.MinValueValidator(0.0)],
)
rag_gauss_scale_min = models.DecimalField(
max_digits=5,
decimal_places=2,
default=1.1,
null=True,
blank=True,
validators=[validators.MinValueValidator(1.0)],
)
rag_gauss_scale_max = models.DecimalField(
max_digits=5,
decimal_places=2,
default=2.0,
null=True,
blank=True,
validators=[validators.MinValueValidator(1.0)],
)
rag_desired_chunk_size = models.PositiveIntegerField(default=300)
elbow_filter_enabled = models.BooleanField(default=False)
match_boost = models.DecimalField(max_digits=5, decimal_places=2, default=1.0)
match_name_boost = models.DecimalField(max_digits=5, decimal_places=2, default=2.0)
match_description_boost = models.DecimalField(max_digits=5, decimal_places=2, default=0.5)
match_keywords_boost = models.DecimalField(max_digits=5, decimal_places=2, default=0.5)
knn_boost = models.DecimalField(max_digits=5, decimal_places=2, default=2.0)
rag_desired_chunk_size = models.PositiveIntegerField(null=True, blank=True)
elbow_filter_enabled = models.BooleanField(null=True, blank=True)
match_boost = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
match_name_boost = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
match_description_boost = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
match_keywords_boost = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
knn_boost = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
similarity_threshold = models.DecimalField(
max_digits=5,
decimal_places=2,
default=0.7,
null=True,
blank=True,
validators=[
validators.MinValueValidator(0.0),
validators.MaxValueValidator(1.0),
Expand Down

0 comments on commit 3d8812e

Please sign in to comment.