Unverified Commit ebc2cdad authored by Jyong's avatar Jyong Committed by GitHub

fix annotation query exception (#1771)

Co-authored-by: 's avatarjyong <jyong@dify.ai>
parent 5bb84193
...@@ -341,66 +341,70 @@ class Completion: ...@@ -341,66 +341,70 @@ class Completion:
app = conversation_message_task.app app = conversation_message_task.app
annotation_reply = app_model_config.annotation_reply_dict annotation_reply = app_model_config.annotation_reply_dict
if annotation_reply['enabled']: if annotation_reply['enabled']:
score_threshold = annotation_reply.get('score_threshold', 1) try:
embedding_provider_name = annotation_reply['embedding_model']['embedding_provider_name'] score_threshold = annotation_reply.get('score_threshold', 1)
embedding_model_name = annotation_reply['embedding_model']['embedding_model_name'] embedding_provider_name = annotation_reply['embedding_model']['embedding_provider_name']
# get embedding model embedding_model_name = annotation_reply['embedding_model']['embedding_model_name']
embedding_model = ModelFactory.get_embedding_model( # get embedding model
tenant_id=app.tenant_id, embedding_model = ModelFactory.get_embedding_model(
model_provider_name=embedding_provider_name, tenant_id=app.tenant_id,
model_name=embedding_model_name model_provider_name=embedding_provider_name,
) model_name=embedding_model_name
embeddings = CacheEmbedding(embedding_model) )
embeddings = CacheEmbedding(embedding_model)
dataset_collection_binding = DatasetCollectionBindingService.get_dataset_collection_binding( dataset_collection_binding = DatasetCollectionBindingService.get_dataset_collection_binding(
embedding_provider_name, embedding_provider_name,
embedding_model_name, embedding_model_name,
'annotation' 'annotation'
) )
dataset = Dataset( dataset = Dataset(
id=app.id, id=app.id,
tenant_id=app.tenant_id, tenant_id=app.tenant_id,
indexing_technique='high_quality', indexing_technique='high_quality',
embedding_model_provider=embedding_provider_name, embedding_model_provider=embedding_provider_name,
embedding_model=embedding_model_name, embedding_model=embedding_model_name,
collection_binding_id=dataset_collection_binding.id collection_binding_id=dataset_collection_binding.id
) )
vector_index = VectorIndex( vector_index = VectorIndex(
dataset=dataset, dataset=dataset,
config=current_app.config, config=current_app.config,
embeddings=embeddings embeddings=embeddings
) )
documents = vector_index.search( documents = vector_index.search(
conversation_message_task.query, conversation_message_task.query,
search_type='similarity_score_threshold', search_type='similarity_score_threshold',
search_kwargs={ search_kwargs={
'k': 1, 'k': 1,
'score_threshold': score_threshold, 'score_threshold': score_threshold,
'filter': { 'filter': {
'group_id': [dataset.id] 'group_id': [dataset.id]
}
} }
} )
) if documents:
if documents: annotation_id = documents[0].metadata['annotation_id']
annotation_id = documents[0].metadata['annotation_id'] score = documents[0].metadata['score']
score = documents[0].metadata['score'] annotation = AppAnnotationService.get_annotation_by_id(annotation_id)
annotation = AppAnnotationService.get_annotation_by_id(annotation_id) if annotation:
if annotation: conversation_message_task.annotation_end(annotation.content, annotation.id, annotation.account.name)
conversation_message_task.annotation_end(annotation.content, annotation.id, annotation.account.name) # insert annotation history
# insert annotation history AppAnnotationService.add_annotation_history(annotation.id,
AppAnnotationService.add_annotation_history(annotation.id, app.id,
app.id, annotation.question,
annotation.question, annotation.content,
annotation.content, conversation_message_task.query,
conversation_message_task.query, conversation_message_task.user.id,
conversation_message_task.user.id, conversation_message_task.message.id,
conversation_message_task.message.id, from_source,
from_source, score)
score) return True
return True except Exception as e:
logging.warning(f'Query annotation failed, exception: {str(e)}.')
return False
return False return False
@classmethod @classmethod
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment