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

def getmostsimilarquestion(userText, history): print(userText) Compute embedding for the…

  1. User

    def get_most_similar_question(userText, history): print(userText)

    # Compute embedding for the user input text using the initial retrieval model
    inp_emb = retrieval_model.encode([userText])
    # Calculate similarity scores between user input text and data embeddings
    corr = np.inner(inp_emb, data_emb).flatten()
    # Get indices of the top 5 most similar texts
    top_5_idx = np.argsort(corr)[-5:]
    print('idx',top_5_idx)
    
    top_5_values = corr[top_5_idx]
    print('values',top_5_values)
    # Retrieve the texts/questions corresponding to these indices
    top_5_texts = new_df.iloc[top_5_idx, 1].values  # Column 1 contains the texts/questions
    print(top_5_texts)
    

    # Re-encode these top 5 texts using the new retrieval model

    data_emb_1 = new_retrieval_model(top_5_texts)

    # Calculate similarity with the input text using the new model's embeddings

    inp_emb_1 = new_retrieval_model([userText])

    corr_1 = np.inner(inp_emb_1, data_emb_1).flatten()

    print('corr_1',corr_1)

    # Identify the highest similarity score and its index

    top_1_idx = np.argmax(corr_1)

    print('top_1_idx',top_1_idx)

    top_1_idx = top_1_idx+1

    print('top_1_idx_11',top_1_idx)

    top_1_value = corr_1[top_1_idx]

    print('top_1_value',top_1_value)

    top_1_texts = top_5_texts[top_1_idx+1]

    print('top_1_texts',top_1_texts)

    print('Matching score:', top_5_values[0])
    

    print('Matching score second time:', top_1_value)

    # Decision to call predict or return the highest ranked text based on similarity threshold
    
    if top_5_values[0] < 0.8:
        # Placeholder for the 'predict' function, assuming you have it defined elsewhere to handle insufficient similarity scenarios
        return predict(userText, history)
    else:
        # Crafting a DataFrame to convert to HTML, using top_1_idx to find original index in new_df
        most_similar_data = new_df.iloc[top_5_idx, [1]]  # Adjusted for Python's indexing
        most_similar_data_df = pd.DataFrame(most_similar_data)
        df_html_with_sql = most_similar_data_df.to_html(index=False)
        html_output = f"<h3>Here is the top matching result(s) for your query:</h3>{df_html_with_sql}"
        return html_output
    

    find top 20 matches first and then again apply the same model to top 20 for fresh embeddings and get top 10 results then then top 5 results and then top1 result

  2. Assistant

    To enhance the similarity matching as you described, you would start by finding the top 20 matches, then refine the search to the top 10, then the top 5, and finally select the top 1 result based on re-computed embeddings at each step. This iterative refinement could help in honing in on the most relevant match by progressively narrowing down the pool of candidates.

    Below is an adjusted version of the code you provided that includes these modifications. Note that this code assumes the existence of retrieval_model, new_retrieval_model, data_emb, new_df, and the necessary libraries (numpy as np and pandas as pd) being properly imported and initialized.

    import numpy as np
    import pandas as pd
    
    def get_most_similar_question(userText, history):
        print(userText)
        
        # Initial embeddings using the original retrieval model
        inp_emb = retrieval_model.encode([userText])
        corr = np.inner(inp_emb, data_emb).flatten()
        
        # First refinement to top 20
        top_20_idx = np.argsort(corr)[-20:]
        top_20_texts = new_df.iloc[top_20_idx, 1].values  # Assuming the search queries are in column 1
        
        # Re-compute embeddings for top 20 and narrow down to top 10
        top_20_emb = retrieval_model.encode(list(top_20_texts))
        corr_20 = np.inner(retrieval_model.encode([userText]), top_20_emb).flatten()
        top_10_idx = np.argsort(corr_20)[-10:]
        top_10_texts = top_20_texts[top_10_idx]
        
        # Further refinement to top 5 using the new model
        top_10_emb = new_retrieval_model.encode(list(top_10_texts))
        corr_10 = np.inner(new_retrieval_model.encode([userText]), top_10_emb).flatten()
        top_5_idx = np.argsort(corr_10)[-5:]
        top_5_texts = top_10_texts[top_5_idx]
        
        # Final selection of top 1
        top_5_emb = new_retrieval_model.encode(list(top_5_texts))
        corr_5 = np.inner(new_retrieval_model.encode([userText]), top_5_emb).flatten()
        top_1_idx = np.argmax(corr_5)
        top_1_text = top_5_texts[top_1_idx]
        
        print(f"Top matching text: {top_1_text}")
        print(f"Matching score: {corr_5[top_1_idx]}")
        
        # Further actions based on the similarity score, for example:
        if corr_5[top_1_idx] < 0.8:
            # Call to a 'predict' function for low similarity cases
            return predict(userText, history)
        else:
            # Return or display the most similar question
            return f"<p>Most similar question: {top_1_text}</p>"
    
    # Please adapt the placeholders (like `retrieval_model`, `new_retrieval_model`, `predict`, `data_emb`, `new_df`) to your actual implementation details.
    

    This code sequentially refines the search scope and recalculates embeddings at each step to ensure gradual focus on the most relevant text based on the similarity scores. Remember to replace placeholders such as retrieval_model, new_retrieval_model, data_emb, new_df, and predict function with your actual implementations or variables.

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