Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

The following code is not working as intented, even though an error should be raised…

  1. User

    The following code is not working as intented, even though an error should be raised when the content vector is null it skips it you know why? for _ in range(RETRY_COUNT): try: doc["content_vector"] = embeddings_engine.embed_query(doc["content"] ) break except: sleep(30) if doc["content_vector"] is None: raise Exception(f"Error getting embedding for chunk={chunk}")

  2. Assistant

    If the embed_query function in embeddings_engine is supposed to raise an exception when doc["content"] is None, but it doesn't, there are a few potential issues that could be happening:

    1. Inside the embed_query method, the case where doc["content"] is None might not be properly checked, and as a result, no exception is raised.

    2. If an exception is raised inside embed_query but it's caught and silenced within that function (for example, with a try-except block that passes), you wouldn't see it in your outer code.

    3. If the embed_query function does not raise an exception but rather returns None, and this is considered normal behavior, then the doc["content_vector"] would legitimately be None without an exception being raised.

    4. If there is any other type of mishandling of doc["content_vector"] (like setting it to None instead of raising an exception) inside the embed_query method, this could lead to no exception being raised outside.

    The code snippet also uses a bare except: without specifying the exception type, which is not recommended because it will catch literally any exception, including system interrupts, which can make it harder to debug problems. It's better to catch specific exceptions that you expect could occur.

    Also, an exception should be raised if doc["content_vector"] is None after all retries have been exhausted. However, it is important to note that the if block that raises the exception is outside the for loop – ensure there's no code between the end of the for loop and the if check that could alter doc["content_vector"]. Otherwise, doc["content_vector"] might be set to None after the for loop, and before the if check, accidentally.

    Here's a revised version of your code with a specified exception type and an added print statement to debug:

    RETRY_COUNT = 3
    
    for _ in range(RETRY_COUNT):
        try:
            doc["content_vector"] = embeddings_engine.embed_query(doc["content"])
            if doc["content_vector"] is not None:
                break
        except SpecificEmbeddingException as e:
            print(f"An error occurred: {e}")
            sleep(30)
    
    if doc["content_vector"] is None:
        raise Exception(f"Error getting embedding for chunk={chunk}")
    

    In this revised code, replace SpecificEmbeddingException with the actual exception type that you would expect from your embed_query method if it fails. If the method might return None normally, then handle that case as shown above, and not through an exception.

    Finally, make sure that the embeddings_engine.embed_query() method is designed to raise an exception when doc["content"] is None if that is the behavior you're relying on. Otherwise, you would need to explicitly check for None before calling the function and handle the situation according to your application's logic.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.