-
Notifications
You must be signed in to change notification settings - Fork 2
Feature/ia #91
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
Open
TheSinnerAR
wants to merge
2
commits into
dev
Choose a base branch
from
feature/ia
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature/ia #91
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "sonarCloudOrganization": "omics-datascience", | ||
| "projectKey": "omics-datascience_multiomix", | ||
| "region": "EU" | ||
| } |
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,39 @@ | ||
| from django.contrib import admin | ||
| from .models import Conversation, Message, CuratedDocument | ||
|
|
||
|
|
||
| @admin.register(CuratedDocument) | ||
| class CuratedDocumentAdmin(admin.ModelAdmin): | ||
| list_display = ['title', 'category', 'is_active', 'updated_at'] | ||
| list_filter = ['is_active', 'category'] | ||
| search_fields = ['title', 'content'] | ||
|
|
||
| def save_model(self, request, obj, form, change): | ||
| """Auto-generate embedding when saving a CuratedDocument.""" | ||
| super().save_model(request, obj, form, change) | ||
| try: | ||
| from assistant.services.embedding_service import embedding_service | ||
| text = f'{obj.title}\n{obj.content}' | ||
| obj.embedding = embedding_service.embed(text) | ||
| CuratedDocument.objects.filter(pk=obj.pk).update(embedding=obj.embedding) | ||
| except Exception as e: | ||
| self.message_user(request, f'Warning: could not generate embedding: {e}', level='warning') | ||
|
|
||
|
|
||
| @admin.register(Conversation) | ||
| class ConversationAdmin(admin.ModelAdmin): | ||
| list_display = ['id', 'user', 'title', 'created_at', 'updated_at'] | ||
| list_filter = ['user'] | ||
| search_fields = ['title', 'user__username'] | ||
| readonly_fields = ['created_at', 'updated_at'] | ||
|
|
||
|
|
||
| @admin.register(Message) | ||
| class MessageAdmin(admin.ModelAdmin): | ||
| list_display = ['id', 'conversation', 'role', 'content_preview', 'created_at'] | ||
| list_filter = ['role'] | ||
| readonly_fields = ['created_at', 'embedding'] | ||
|
|
||
| def content_preview(self, obj): | ||
| return obj.content[:80] | ||
| content_preview.short_description = 'Content' |
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,6 @@ | ||
| from django.apps import AppConfig | ||
|
|
||
|
|
||
| class AssistantConfig(AppConfig): | ||
| default_auto_field = 'django.db.models.BigAutoField' | ||
| name = 'assistant' |
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,71 @@ | ||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
| import django.db.models.deletion | ||
| import pgvector.django | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunSQL( | ||
| sql='CREATE EXTENSION IF NOT EXISTS vector;', | ||
| reverse_sql='DROP EXTENSION IF EXISTS vector;', | ||
| ), | ||
| migrations.CreateModel( | ||
| name='Conversation', | ||
| fields=[ | ||
| ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
| ('title', models.CharField(blank=True, max_length=200, null=True)), | ||
| ('created_at', models.DateTimeField(auto_now_add=True)), | ||
| ('updated_at', models.DateTimeField(auto_now=True)), | ||
| ('user', models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name='conversations', | ||
| to=settings.AUTH_USER_MODEL, | ||
| )), | ||
| ], | ||
| options={ | ||
| 'ordering': ['-updated_at'], | ||
| }, | ||
| ), | ||
| migrations.CreateModel( | ||
| name='Message', | ||
| fields=[ | ||
| ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
| ('role', models.CharField( | ||
| choices=[('user', 'User'), ('assistant', 'Assistant')], | ||
| max_length=10, | ||
| )), | ||
| ('content', models.TextField()), | ||
| ('embedding', pgvector.django.VectorField(blank=True, dimensions=384, null=True)), | ||
| ('created_at', models.DateTimeField(auto_now_add=True)), | ||
| ('conversation', models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name='messages', | ||
| to='assistant.conversation', | ||
| )), | ||
| ], | ||
| options={ | ||
| 'ordering': ['created_at'], | ||
| }, | ||
| ), | ||
| migrations.CreateModel( | ||
| name='CuratedDocument', | ||
| fields=[ | ||
| ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
| ('title', models.CharField(max_length=300)), | ||
| ('content', models.TextField()), | ||
| ('category', models.CharField(blank=True, max_length=100, null=True)), | ||
| ('embedding', pgvector.django.VectorField(blank=True, dimensions=384, null=True)), | ||
| ('is_active', models.BooleanField(default=True)), | ||
| ('created_at', models.DateTimeField(auto_now_add=True)), | ||
| ('updated_at', models.DateTimeField(auto_now=True)), | ||
| ], | ||
| ), | ||
| ] |
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,47 @@ | ||
| from django.contrib.auth.models import User | ||
| from django.db import models | ||
| from pgvector.django import VectorField | ||
|
|
||
|
|
||
| class Conversation(models.Model): | ||
| user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='conversations') | ||
| title = models.CharField(max_length=200, null=True, blank=True) | ||
| created_at = models.DateTimeField(auto_now_add=True) | ||
| updated_at = models.DateTimeField(auto_now=True) | ||
|
|
||
| class Meta: | ||
| ordering = ['-updated_at'] | ||
|
|
||
| def __str__(self): | ||
| return self.title or f'Conversation {self.pk}' | ||
|
|
||
|
|
||
| class Message(models.Model): | ||
| class Role(models.TextChoices): | ||
| USER = 'user', 'User' | ||
| ASSISTANT = 'assistant', 'Assistant' | ||
|
|
||
| conversation = models.ForeignKey(Conversation, on_delete=models.CASCADE, related_name='messages') | ||
| role = models.CharField(max_length=10, choices=Role.choices) | ||
| content = models.TextField() | ||
| embedding = VectorField(dimensions=384, null=True, blank=True) | ||
| created_at = models.DateTimeField(auto_now_add=True) | ||
|
|
||
| class Meta: | ||
| ordering = ['created_at'] | ||
|
|
||
| def __str__(self): | ||
| return f'{self.role}: {self.content[:50]}' | ||
|
|
||
|
|
||
| class CuratedDocument(models.Model): | ||
| title = models.CharField(max_length=300) | ||
| content = models.TextField() | ||
| category = models.CharField(max_length=100, null=True, blank=True) | ||
| embedding = VectorField(dimensions=384, null=True, blank=True) | ||
| is_active = models.BooleanField(default=True) | ||
| created_at = models.DateTimeField(auto_now_add=True) | ||
| updated_at = models.DateTimeField(auto_now=True) | ||
|
|
||
| def __str__(self): | ||
| return self.title |
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 @@ | ||
| from rest_framework import serializers | ||
| from .models import Conversation, Message | ||
|
|
||
|
|
||
| class MessageSerializer(serializers.ModelSerializer): | ||
| class Meta: | ||
| model = Message | ||
| fields = ['id', 'role', 'content', 'created_at'] | ||
|
|
||
|
|
||
| class ConversationSerializer(serializers.ModelSerializer): | ||
| messages = MessageSerializer(many=True, read_only=True) | ||
|
|
||
| class Meta: | ||
| model = Conversation | ||
| fields = ['id', 'title', 'created_at', 'updated_at', 'messages'] | ||
|
|
||
|
|
||
| class ConversationListSerializer(serializers.ModelSerializer): | ||
| class Meta: | ||
| model = Conversation | ||
| fields = ['id', 'title', 'created_at', 'updated_at'] |
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,53 @@ | ||
| from typing import List, Optional | ||
| from django.conf import settings | ||
|
|
||
|
|
||
| class EmbeddingService: | ||
| """Singleton lazy-loader for HuggingFace sentence-transformers embeddings.""" | ||
|
|
||
| _instance: Optional['EmbeddingService'] = None | ||
| _model = None | ||
|
|
||
| def __new__(cls) -> 'EmbeddingService': | ||
| if cls._instance is None: | ||
| cls._instance = super().__new__(cls) | ||
| return cls._instance | ||
|
|
||
| def _get_model(self): | ||
| if self._model is None: | ||
| import os | ||
| from langchain_huggingface import HuggingFaceEmbeddings | ||
|
|
||
| # Use HF_TOKEN if provided, otherwise run fully offline (model cached locally). | ||
| # Inference always runs locally — no text data is ever sent to HuggingFace. | ||
| hf_token = os.getenv('HF_TOKEN') or None | ||
| local_only = not hf_token and self._is_cached(settings.ASSISTANT_EMBEDDING_MODEL) | ||
|
|
||
| self._model = HuggingFaceEmbeddings( | ||
| model_name=settings.ASSISTANT_EMBEDDING_MODEL, | ||
| model_kwargs={'device': 'cpu'}, | ||
| encode_kwargs={'normalize_embeddings': True}, | ||
| cache_folder=os.getenv('HF_CACHE_DIR', None), | ||
| **({"huggingface_api_token": hf_token} if hf_token else {}), | ||
| ) | ||
| # Silence future hub warnings once the model is loaded | ||
| os.environ.setdefault('TOKENIZERS_PARALLELISM', 'false') | ||
| return self._model | ||
|
|
||
| @staticmethod | ||
| def _is_cached(model_name: str) -> bool: | ||
| """Check if the model weights are already in the local HuggingFace cache.""" | ||
| import os | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mover arriba, decile al boludo de Claude que los imports siempre van a arriba, desde 1980 🙏 |
||
| cache_dir = os.getenv('HF_HOME', os.path.expanduser('~/.cache/huggingface')) | ||
| model_slug = model_name.replace('/', '--') | ||
| hub_path = os.path.join(cache_dir, 'hub', f'models--{model_slug}') | ||
| return os.path.isdir(hub_path) | ||
|
|
||
| def embed(self, text: str) -> List[float]: | ||
| return self._get_model().embed_query(text) | ||
|
|
||
| def embed_many(self, texts: List[str]) -> List[List[float]]: | ||
| return self._get_model().embed_documents(texts) | ||
|
|
||
|
|
||
| embedding_service = EmbeddingService() | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from django.conf import settingsy usarlos comosettings.tokenizers_parallelism.