dify icon indicating copy to clipboard operation
dify copied to clipboard

code is wrong

Open heyb130 opened this issue 2 years ago • 1 comments

Self Checks

  • [X] This is only for bug report, if you would like to ask a question, please head to Discussions.
  • [X] I have searched for existing issues search for existing issues, including closed ones.
  • [X] I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [X] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • [X] Please do not modify this template :) and fill in all the required fields.

Dify version

main

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

1719369906670

✔️ Expected Behavior

6/10000 实时翻译 划译 solve the problem

❌ Actual Behavior

No response

heyb130 avatar Jun 26 '24 02:06 heyb130

@heyb130 Hello there! I'm here to assist you with any bugs, questions, or contributions you may have. Let's work together to resolve this issue.

To resolve the issue where the if statement checking dataset.indexing_technique and embedding_model_type_instance results in no response or output when summing tokens from embedding_model_instance.get_tokens(document.page_content), you need to ensure that the embedding_model_instance is correctly instantiated and that the get_tokens method is properly called. Here is a possible solution:

  1. Ensure that the embedding_model_instance is correctly instantiated.
  2. Verify that the get_tokens method is called correctly and returns the expected output.

Here is a code snippet that demonstrates how to achieve this:

if dataset.indexing_technique == 'high_quality':
    try:
        model_manager = ModelManager()
        embedding_model_instance = model_manager.get_model_instance(
            tenant_id=dataset.tenant_id,
            provider=dataset.embedding_model_provider,
            model_type=ModelType.TEXT_EMBEDDING,
            model=dataset.embedding_model
        )
        
        # Ensure that the embedding_model_instance is not None
        if embedding_model_instance:
            tokens = embedding_model_instance.get_tokens(document.page_content)
            total_tokens = sum(tokens)
            print(f"Total tokens: {total_tokens}")
        else:
            print("Embedding model instance is None.")
    except LLMBadRequestError:
        raise ValueError("No Embedding Model available. Please configure a valid provider in the Settings -> Model Provider.")
    except ProviderTokenNotInitError as ex:
        raise ValueError(f"The dataset is unavailable, due to: {ex.description}")
else:
    print("Indexing technique is not 'high_quality'.")

This code ensures that:

  • The embedding_model_instance is correctly instantiated.
  • The get_tokens method is called on the embedding_model_instance.
  • The tokens are summed and printed.

Additionally, ensure that the embedding_model_instance is correctly handled in the _process_chunk method as shown in the indexing_runner.py file:

def _process_chunk(self, flask_app, index_processor, chunk_documents, dataset, dataset_document, embedding_model_instance):
    with flask_app.app_context():
        # check document is paused
        self._check_document_paused_status(dataset_document.id)

        tokens = 0
        if dataset.indexing_technique == 'high_quality' or embedding_model_instance:
            tokens += sum(
                embedding_model_instance.get_text_embedding_num_tokens(
                    [document.page_content]
                )
                for document in chunk_documents
            )

        # load index
        index_processor.load(dataset, chunk_documents, with_keywords=False)

This ensures that the tokens are correctly calculated and processed during the chunk processing phase [1][2].

To continue talking to Dosu, mention @dosu.

dosubot[bot] avatar Jun 26 '24 02:06 dosubot[bot]