Loren commited on
Commit
df0f53d
·
verified ·
1 Parent(s): e3e2445

Upload 3 files

Browse files
Files changed (3) hide show
  1. embeddings.py +6 -3
  2. models.py +2 -0
  3. requirements.txt +1 -0
embeddings.py CHANGED
@@ -22,6 +22,7 @@ import unicodedata
22
  import chromadb
23
  from sentence_transformers import SentenceTransformer, CrossEncoder
24
  import torch
 
25
  from config import (
26
  CHROMA_COLLECTION_NAME, MODEL_NAME, SIMILARITY_THRESHOLD,
27
  TOP_K, CROSS_ENCODER_MODEL_NAME, CROSS_ENCODER_THRESHOLD
@@ -255,10 +256,11 @@ class EmbeddingManager:
255
 
256
  # Appliquer le Cross-Encoder entre la question et chaque formulation candidate
257
  pairs = [(normalized_question, candidate.formulation) for candidate in candidates]
258
- cross_scores = self.cross_encoder.predict(pairs)
259
 
260
- for candidate, cross_score in zip(candidates, cross_scores):
261
- candidate.cross_encoder_score = float(cross_score)
 
262
 
263
  # Sélectionner le meilleur score cross-encoder et le second meilleur pour calculer l'écart entre les 2
264
  ranked_candidates = sorted(
@@ -294,6 +296,7 @@ class EmbeddingManager:
294
  answer="Je ne dispose pas d'une réponse suffisamment pertinente pour cette question.",
295
  theme=best_candidate.theme,
296
  similarity_score=round(best_candidate.similarity_score or 0.0, 4),
 
297
  cross_encoder_score=round(best_candidate.cross_encoder_score or 0.0, 4),
298
  list_candidates=candidates,
299
  confidence=False,
 
22
  import chromadb
23
  from sentence_transformers import SentenceTransformer, CrossEncoder
24
  import torch
25
+ from scipy.special import expit
26
  from config import (
27
  CHROMA_COLLECTION_NAME, MODEL_NAME, SIMILARITY_THRESHOLD,
28
  TOP_K, CROSS_ENCODER_MODEL_NAME, CROSS_ENCODER_THRESHOLD
 
256
 
257
  # Appliquer le Cross-Encoder entre la question et chaque formulation candidate
258
  pairs = [(normalized_question, candidate.formulation) for candidate in candidates]
259
+ cross_logits = self.cross_encoder.predict(pairs)
260
 
261
+ for candidate, cross_logit in zip(candidates, cross_logits):
262
+ candidate.cross_encoder_logit = float(cross_logit)
263
+ candidate.cross_encoder_score = float(expit(cross_logit)) # Sigmoid pour obtenir un score entre 0 et 1
264
 
265
  # Sélectionner le meilleur score cross-encoder et le second meilleur pour calculer l'écart entre les 2
266
  ranked_candidates = sorted(
 
296
  answer="Je ne dispose pas d'une réponse suffisamment pertinente pour cette question.",
297
  theme=best_candidate.theme,
298
  similarity_score=round(best_candidate.similarity_score or 0.0, 4),
299
+ cross_encoder_logit=round(best_candidate.cross_encoder_logit or 0.0, 4),
300
  cross_encoder_score=round(best_candidate.cross_encoder_score or 0.0, 4),
301
  list_candidates=candidates,
302
  confidence=False,
models.py CHANGED
@@ -33,6 +33,7 @@ class SemanticSearchCandidate:
33
  theme: str
34
  response: str
35
  similarity_score: float
 
36
  cross_encoder_score: float | None = None
37
 
38
  class AnswerOutput(BaseModel):
@@ -42,6 +43,7 @@ class AnswerOutput(BaseModel):
42
  answer: str = Field(..., description="Answer found in the FAQ knowledge base")
43
  theme: str = Field(..., description="Answer theme or category")
44
  similarity_score: float = Field(..., ge=0.0, le=1.0, description="Cosine similarity score in [0, 1]")
 
45
  cross_encoder_score: Optional[float] = Field(None, description="Cross-encoder score")
46
  confidence: bool = Field(..., description="True if the score meets the confidence threshold, otherwise False")
47
  list_candidates: Optional[list[SemanticSearchCandidate]] = Field(None, description="List of semantic search candidates")
 
33
  theme: str
34
  response: str
35
  similarity_score: float
36
+ cross_encoder_logit: float | None = None
37
  cross_encoder_score: float | None = None
38
 
39
  class AnswerOutput(BaseModel):
 
43
  answer: str = Field(..., description="Answer found in the FAQ knowledge base")
44
  theme: str = Field(..., description="Answer theme or category")
45
  similarity_score: float = Field(..., ge=0.0, le=1.0, description="Cosine similarity score in [0, 1]")
46
+ cross_encoder_logit: Optional[float] = Field(None, description="Cross-encoder logit score")
47
  cross_encoder_score: Optional[float] = Field(None, description="Cross-encoder score")
48
  confidence: bool = Field(..., description="True if the score meets the confidence threshold, otherwise False")
49
  list_candidates: Optional[list[SemanticSearchCandidate]] = Field(None, description="List of semantic search candidates")
requirements.txt CHANGED
@@ -6,4 +6,5 @@ sentence-transformers==2.5.0
6
  torch==2.1.1
7
  transformers==4.36.0
8
  pandas==2.1.3
 
9
  python-dotenv==1.0.0
 
6
  torch==2.1.1
7
  transformers==4.36.0
8
  pandas==2.1.3
9
+ scipy
10
  python-dotenv==1.0.0