-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
organize settings, elasticsearch api and siae index/meta infos
- Loading branch information
1 parent
7be4dc3
commit 59bf5d8
Showing
5 changed files
with
41 additions
and
22 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
26 changes: 8 additions & 18 deletions
26
lemarche/siaes/management/commands/put_siaes_in_elasticsearch_index.py
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 |
---|---|---|
@@ -1,49 +1,39 @@ | ||
import time | ||
|
||
from django.conf import settings | ||
from django.core.management.base import BaseCommand | ||
from django.db.models import TextField | ||
from django.db.models.functions import Length | ||
from langchain.embeddings.openai import OpenAIEmbeddings | ||
from langchain.vectorstores import ElasticVectorSearch | ||
|
||
from lemarche.siaes.models import Siae | ||
from lemarche.utils.apis.api_elasticsearch import URL_WITH_USER | ||
from lemarche.utils.commands import BaseCommand | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "" | ||
|
||
def handle(self, *args, **options): | ||
self.stdout.write(self.style.SUCCESS("put siae to elasticsearch index started..")) | ||
self.stdout_success("put siae to elasticsearch index started..") | ||
|
||
# Elasticsearch as a vector db | ||
url = ( | ||
f"https://{settings.ELASTICSEARCH_USERNAME}:{settings.ELASTICSEARCH_PASSWORD}@" | ||
f"{settings.ELASTICSEARCH_HOST}:443" | ||
) | ||
embeddings = OpenAIEmbeddings() | ||
db = ElasticVectorSearch( | ||
embedding=embeddings, elasticsearch_url=url, index_name=settings.ELASTICSEARCH_INDEX_SIAES | ||
embedding=embeddings, elasticsearch_url=URL_WITH_USER, index_name=settings.ELASTICSEARCH_INDEX_SIAES | ||
) | ||
|
||
# Siaes with completed description | ||
TextField.register_lookup(Length) # at least 10 characters | ||
siaes = Siae.objects.filter(description__length__gt=9).all() | ||
|
||
for siae in siaes: | ||
text = siae.description | ||
if siae.offers.count() > 0: | ||
offers = "\n\nPrestations:\n" | ||
for offer in siae.offers.all(): | ||
offers += f"- {offer.name}:\n{offer.description}\n\n" | ||
text += offers | ||
|
||
db.from_texts( | ||
[text], | ||
metadatas=[{"id": siae.id, "name": siae.name, "website": siae.website if siae.website else ""}], | ||
[siae.elasticsearch_index_text], | ||
metadatas=[siae.elasticsearch_index_metadata], | ||
embedding=embeddings, | ||
elasticsearch_url=url, | ||
elasticsearch_url=URL_WITH_USER, | ||
index_name=settings.ELASTICSEARCH_INDEX_SIAES, | ||
) | ||
time.sleep(1) | ||
self.stdout.write(self.style.SUCCESS(f"{siae.name} added !")) | ||
self.stdout_success(f"{siae.name} added !") |
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
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