-
-
Notifications
You must be signed in to change notification settings - Fork 269
Chunk model and embeddings of message #1651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9627d1e
chunk model and embeddings
Dishant1804 00ed69d
Merge remote-tracking branch 'upstream/main' into chunk_model
Dishant1804 79627a7
draft
Dishant1804 ef999c8
Merge remote-tracking branch 'upstream/main' into chunk_model
Dishant1804 1e55c56
spelling and fixes
Dishant1804 cbb255f
code rabbit suggestions
Dishant1804 9733fee
fixes
Dishant1804 a9b1be9
open ai error handling changes
Dishant1804 e2abafa
suggestions implemented
Dishant1804 6798ad6
Merge remote-tracking branch 'upstream/main' into chunk_model
Dishant1804 d93208c
Update code
arkid15r e131566
Merge remote-tracking branch 'upstream/main' into chunk_model
Dishant1804 258f79d
Merge branch 'main' into chunk_model
arkid15r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Empty file.
This file contains hidden or 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,20 @@ | ||
| """AI app admin.""" | ||
|
|
||
| from django.contrib import admin | ||
|
|
||
| from apps.ai.models.chunk import Chunk | ||
|
|
||
|
|
||
| class ChunkAdmin(admin.ModelAdmin): | ||
| list_display = ( | ||
| "id", | ||
| "message", | ||
| "text", | ||
| ) | ||
| search_fields = ( | ||
| "message__slack_message_id", | ||
| "text", | ||
| ) | ||
|
|
||
|
|
||
| admin.site.register(Chunk, ChunkAdmin) |
Empty file.
92 changes: 92 additions & 0 deletions
92
backend/apps/ai/management/commands/ai_create_slack_message_chunks.py
This file contains hidden or 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,92 @@ | ||
| """A command to create chunks of Slack messages.""" | ||
|
|
||
| import os | ||
| import time | ||
| from datetime import UTC, datetime, timedelta | ||
|
|
||
| import openai | ||
| from django.core.management.base import BaseCommand | ||
|
|
||
| from apps.ai.models.chunk import Chunk | ||
| from apps.slack.models.message import Message | ||
|
|
||
| MIN_REQUEST_INTERVAL_SECONDS = 1.2 | ||
| DEFAULT_LAST_REQUEST_OFFSET_SECONDS = 2 | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = "Create chunks for Slack messages" | ||
|
|
||
| def handle(self, *args, **options): | ||
| if not (openai_api_key := os.getenv("DJANGO_OPEN_AI_SECRET_KEY")): | ||
| self.stdout.write( | ||
| self.style.ERROR("DJANGO_OPEN_AI_SECRET_KEY environment variable not set") | ||
| ) | ||
| return | ||
|
|
||
| self.openai_client = openai.OpenAI(api_key=openai_api_key) | ||
|
|
||
| total_messages = Message.objects.count() | ||
| self.stdout.write(f"Found {total_messages} messages to process") | ||
|
|
||
| batch_size = 100 | ||
| for offset in range(0, total_messages, batch_size): | ||
| Chunk.bulk_save( | ||
| [ | ||
| chunk | ||
| for message in Message.objects.all()[offset : offset + batch_size] | ||
| for chunk in self.create_chunks(message) | ||
| ] | ||
| ) | ||
|
|
||
| self.stdout.write(f"Completed processing all {total_messages} messages") | ||
|
|
||
| def create_chunks(self, message: Message) -> list[Chunk]: | ||
| """Create chunks from a message.""" | ||
| if message.subtype in {"channel_join", "channel_leave"}: | ||
| return [] | ||
|
|
||
| if not (chunk_text := Chunk.split_text(message.cleaned_text)): | ||
arkid15r marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.stdout.write( | ||
| f"No chunks created for message {message.slack_message_id}: " | ||
| f"`{message.cleaned_text}`" | ||
| ) | ||
| return [] | ||
|
|
||
| try: | ||
| time_since_last_request = datetime.now(UTC) - getattr( | ||
| self, | ||
| "last_request_time", | ||
| datetime.now(UTC) - timedelta(seconds=DEFAULT_LAST_REQUEST_OFFSET_SECONDS), | ||
| ) | ||
|
|
||
| if time_since_last_request < timedelta(seconds=MIN_REQUEST_INTERVAL_SECONDS): | ||
| time.sleep(MIN_REQUEST_INTERVAL_SECONDS - time_since_last_request.total_seconds()) | ||
|
|
||
arkid15r marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| response = self.openai_client.embeddings.create( | ||
| input=chunk_text, | ||
| model="text-embedding-3-small", | ||
| ) | ||
| self.last_request_time = datetime.now(UTC) | ||
|
|
||
| return [ | ||
| chunk | ||
| for text, embedding in zip( | ||
| chunk_text, | ||
| [d.embedding for d in response.data], # Embedding data from OpenAI response. | ||
| strict=True, | ||
| ) | ||
| if ( | ||
| chunk := Chunk.update_data( | ||
| embedding=embedding, | ||
| message=message, | ||
| save=False, | ||
| text=text, | ||
| ) | ||
| ) | ||
| ] | ||
| except openai.OpenAIError as e: | ||
| self.stdout.write( | ||
| self.style.ERROR(f"OpenAI API error for message {message.slack_message_id}: {e}") | ||
| ) | ||
| return [] | ||
This file contains hidden or 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,51 @@ | ||
| # Generated by Django 5.2.3 on 2025-06-22 06:17 | ||
|
|
||
| import django.db.models.deletion | ||
| import pgvector.django.vector | ||
| from django.db import migrations, models | ||
| from pgvector.django import VectorExtension | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| ("slack", "0018_conversation_sync_messages"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| VectorExtension(), | ||
| migrations.CreateModel( | ||
| name="Chunk", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.BigAutoField( | ||
| auto_created=True, primary_key=True, serialize=False, verbose_name="ID" | ||
| ), | ||
| ), | ||
| ("nest_created_at", models.DateTimeField(auto_now_add=True)), | ||
| ("nest_updated_at", models.DateTimeField(auto_now=True)), | ||
| ("chunk_text", models.TextField(verbose_name="Chunk Text")), | ||
| ( | ||
| "embedding", | ||
| pgvector.django.vector.VectorField( | ||
| dimensions=1536, verbose_name="Chunk Embedding" | ||
| ), | ||
| ), | ||
| ( | ||
| "message", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="chunks", | ||
| to="slack.message", | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "verbose_name": "Chunks", | ||
| "db_table": "ai_chunks", | ||
| "unique_together": {("message", "chunk_text")}, | ||
| }, | ||
| ), | ||
| ] |
22 changes: 22 additions & 0 deletions
22
backend/apps/ai/migrations/0002_rename_chunk_text_chunk_text_and_more.py
This file contains hidden or 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,22 @@ | ||
| # Generated by Django 5.2.3 on 2025-06-26 21:04 | ||
|
|
||
| from django.db import migrations | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("ai", "0001_initial"), | ||
| ("slack", "0018_conversation_sync_messages"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RenameField( | ||
| model_name="chunk", | ||
| old_name="chunk_text", | ||
| new_name="text", | ||
| ), | ||
| migrations.AlterUniqueTogether( | ||
| name="chunk", | ||
| unique_together={("message", "text")}, | ||
| ), | ||
| ] |
27 changes: 27 additions & 0 deletions
27
backend/apps/ai/migrations/0003_alter_chunk_options_alter_chunk_embedding_and_more.py
This file contains hidden or 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,27 @@ | ||
| # Generated by Django 5.2.3 on 2025-06-26 21:45 | ||
|
|
||
| import pgvector.django.vector | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("ai", "0002_rename_chunk_text_chunk_text_and_more"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterModelOptions( | ||
| name="chunk", | ||
| options={"verbose_name": "Chunk"}, | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="chunk", | ||
| name="embedding", | ||
| field=pgvector.django.vector.VectorField(dimensions=1536, verbose_name="Embedding"), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="chunk", | ||
| name="text", | ||
| field=models.TextField(verbose_name="Text"), | ||
| ), | ||
| ] |
Empty file.
This file contains hidden or 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 @@ | ||
| from .chunk import Chunk |
This file contains hidden or 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,74 @@ | ||
| """Slack app chunk model.""" | ||
|
|
||
| from django.db import models | ||
| from langchain.text_splitter import RecursiveCharacterTextSplitter | ||
| from pgvector.django import VectorField | ||
|
|
||
| from apps.common.models import BulkSaveModel, TimestampedModel | ||
| from apps.common.utils import truncate | ||
| from apps.slack.models.message import Message | ||
|
|
||
|
|
||
| class Chunk(TimestampedModel): | ||
| """Slack Chunk model.""" | ||
|
|
||
| class Meta: | ||
| db_table = "ai_chunks" | ||
| verbose_name = "Chunk" | ||
| unique_together = ("message", "text") | ||
|
|
||
| embedding = VectorField(verbose_name="Embedding", dimensions=1536) | ||
| message = models.ForeignKey(Message, on_delete=models.CASCADE, related_name="chunks") | ||
| text = models.TextField(verbose_name="Text") | ||
|
|
||
| def __str__(self): | ||
| """Human readable representation.""" | ||
| return ( | ||
| f"Chunk {self.id} for Message {self.message.slack_message_id}: " | ||
| f"{truncate(self.text, 50)}" | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def bulk_save(chunks, fields=None): | ||
| """Bulk save chunks.""" | ||
| BulkSaveModel.bulk_save(Chunk, chunks, fields=fields) | ||
|
|
||
| @staticmethod | ||
| def split_text(text: str) -> list[str]: | ||
| """Split text into chunks.""" | ||
| return RecursiveCharacterTextSplitter( | ||
| chunk_size=300, | ||
| chunk_overlap=40, | ||
| length_function=len, | ||
| separators=["\n\n", "\n", " ", ""], | ||
| ).split_text(text) | ||
|
|
||
| @staticmethod | ||
| def update_data( | ||
| text: str, | ||
| message: Message, | ||
| embedding, | ||
| *, | ||
| save: bool = True, | ||
| ) -> "Chunk | None": | ||
| """Update chunk data. | ||
|
|
||
| Args: | ||
| text (str): The text content of the chunk. | ||
| message (Message): The message this chunk belongs to. | ||
| embedding (list): The embedding vector for the chunk. | ||
| save (bool): Whether to save the chunk to the database. | ||
|
|
||
| Returns: | ||
| Chunk: The updated chunk instance. | ||
|
|
||
| """ | ||
| if Chunk.objects.filter(message=message, text=text).exists(): | ||
| return None | ||
|
|
||
| chunk = Chunk(message=message, text=text, embedding=embedding) | ||
|
|
||
| if save: | ||
| chunk.save() | ||
|
|
||
| return chunk | ||
arkid15r marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.