### Load and Use SFR-Embedding-Code-2B_R Model Source: https://github.com/salesforce/sfr-embedding-code-2b_r/blob/main/README.md Loads the Sentence Transformer model and computes embeddings for queries and passages. Ensure 'transformers' and 'sentence-transformers' libraries are installed. ```python # Load the Sentence Transformer model, including pooling model = SentenceTransformer('Salesforce/SFR-Embedding-Code-2B_R', trust_remote_code=True) # Compute the embeddings for both queries and passages. Use 'prompt' for queries only query_embeddings = model.encode(queries, prompt=query_instruction_example) passage_embeddings = model.encode(passages) # Compute the similarities between the queries and passages similarities = model.similarity(query_embeddings, passage_embeddings) print(similarities) # tensor([[0.6927, 0.5842]]) ``` -------------------------------- ### Initialize Code Repository Source: https://context7.com/salesforce/sfr-embedding-code-2b_r/llms.txt Define a list of code snippets to be indexed for search. ```python code_repository = [ {"id": 1, "code": "def factorial(n):\n return 1 if n <= 1 else n * factorial(n-1)"}, {"id": 2, "code": "def fibonacci(n):\n if n <= 1: return n\n return fibonacci(n-1) + fibonacci(n-2)"}, {"id": 3, "code": "def is_prime(n):\n if n < 2: return False\n for i in range(2, int(n**0.5)+1):\n if n % i == 0: return False\n return True"}, {"id": 4, "code": "def gcd(a, b):\n while b: a, b = b, a % b\n return a"}, ] ``` -------------------------------- ### Load Model with Sentence Transformers Source: https://context7.com/salesforce/sfr-embedding-code-2b_r/llms.txt Load the model using the Sentence Transformers library for a simplified API. Queries require a specific prompt format with 'Instruct:' and 'Query:' prefixes. ```python from sentence_transformers import SentenceTransformer # Load model with Sentence Transformers model = SentenceTransformer( 'Salesforce/SFR-Embedding-Code-2B_R', trust_remote_code=True ) # Query instruction format for Sentence Transformers query_instruction = "Instruct: Given Code or Text, retrieval relevant content\nQuery: " # Queries and passages queries = ["how to implement quick sort in Python?"] passages = [ "def quick_sort(arr):\n if len(arr) <= 1:\n return arr\n pivot = arr[len(arr) // 2]\n left = [x for x in arr if x < pivot]\n middle = [x for x in arr if x == pivot]\n right = [x for x in arr if x > pivot]\n return quick_sort(left) + middle + quick_sort(right)", "def bubble_sort(arr):\n n = len(arr)\n for i in range(n):\n for j in range(0, n-i-1):\n if arr[j] > arr[j+1]:\n arr[j], arr[j+1] = arr[j+1], arr[j]\n return arr" ] # Encode with prompt for queries only query_embeddings = model.encode(queries, prompt=query_instruction) passage_embeddings = model.encode(passages) # Compute similarities using built-in method similarities = model.similarity(query_embeddings, passage_embeddings) print(f"Similarities: {similarities}") ``` -------------------------------- ### Initialize Model for Code Search Indexing Source: https://context7.com/salesforce/sfr-embedding-code-2b_r/llms.txt Load the AutoModel for creating a searchable index of code snippets for semantic code search applications. This is a foundational step for building custom search solutions. ```python import torch import torch.nn.functional as F from transformers import AutoModel model = AutoModel.from_pretrained( 'Salesforce/SFR-Embedding-Code-2B_R', trust_remote_code=True ) ``` -------------------------------- ### Python Quick Sort Implementation Source: https://github.com/salesforce/sfr-embedding-code-2b_r/blob/main/README.md A standard implementation of the quick sort algorithm in Python. Useful for sorting lists efficiently. ```python def quick_sort(arr): if len(arr) <= 1: return arr pivot = arr[len(arr) // 2] left = [x for x in arr if x < pivot] middle = [x for x in arr if x == pivot] right = [x for x in arr if x > pivot] return quick_sort(left) + middle + quick_sort(right) ``` -------------------------------- ### Encode Queries and Passages with Custom Instruction Source: https://context7.com/salesforce/sfr-embedding-code-2b_r/llms.txt Use this method when you need to encode both queries and passages for similarity comparison. Ensure passages are pre-defined and queries are formatted with the specified instruction. ```python passages = [ "def quick_sort(arr):\n if len(arr) <= 1:\n return arr\n pivot = arr[len(arr) // 2]\n left = [x for x in arr if x < pivot]\n middle = [x for x in arr if x == pivot]\n right = [x for x in arr if x > pivot]\n return quick_sort(left) + middle + quick_sort(right)", "def bubble_sort(arr):\n n = len(arr)\n for i in range(n):\n for j in range(0, n-i-1):\n if arr[j] > arr[j+1]:\n arr[j], arr[j+1] = arr[j+1], arr[j]\n return arr" ] # Encode both queries and passages max_length = 32768 query_embeddings = model.encode_queries(queries, instruction=query_instruction, max_length=max_length) passage_embeddings = model.encode_corpus(passages, max_length=max_length) # Normalize embeddings query_embeddings = F.normalize(query_embeddings, p=2, dim=1) passage_embeddings = F.normalize(passage_embeddings, p=2, dim=1) # Compute similarity scores (scaled by 100) scores = (query_embeddings @ passage_embeddings.T) * 100 print(f"Similarity scores: {scores.tolist()}") ``` -------------------------------- ### Encode Search Queries with Instructions Source: https://context7.com/salesforce/sfr-embedding-code-2b_r/llms.txt Generate embeddings for search queries using an instruction prefix. Normalize the resulting embeddings for cosine similarity. ```python import torch.nn.functional as F from transformers import AutoModel model = AutoModel.from_pretrained( 'Salesforce/SFR-Embedding-Code-2B_R', trust_remote_code=True ) # Define the retrieval task instruction query_instruction = "Given Code or Text, retrieval relevant content" # List of queries to encode queries = [ "how to implement quick sort in Python?", "binary search algorithm implementation", "how to reverse a linked list?" ] # Encode queries with instruction and max sequence length max_length = 32768 query_embeddings = model.encode_queries( queries, instruction=query_instruction, max_length=max_length ) # Normalize embeddings for cosine similarity query_embeddings = F.normalize(query_embeddings, p=2, dim=1) print(f"Query embeddings shape: {query_embeddings.shape}") # Output: Query embeddings shape: torch.Size([3, 2304]) ``` -------------------------------- ### Encode Queries with Sentence Transformers Source: https://github.com/salesforce/sfr-embedding-code-2b_r/blob/main/README.md This snippet demonstrates how to encode queries for retrieval using the Sentence Transformers library. Similar to the Transformers approach, queries require an accompanying instruction. ```python from sentence_transformers import SentenceTransformer # Each query needs to be accompanied by an corresponding instruction describing the task. query_instruction_example = "Instruct: Given Code or Text, retrieval relevant content\nQuery: " queries = ["how to implement quick sort in Python?"] ``` -------------------------------- ### Implement Search Function Source: https://context7.com/salesforce/sfr-embedding-code-2b_r/llms.txt Define a function to encode natural language queries with instructions and retrieve the top-k relevant code snippets. ```python def search_code(query: str, top_k: int = 2): instruction = "Given a natural language query, retrieve relevant code implementations" query_embedding = model.encode_queries([query], instruction=instruction, max_length=max_length) query_embedding = F.normalize(query_embedding, p=2, dim=1) scores = (query_embedding @ code_embeddings.T).squeeze() top_indices = torch.argsort(scores, descending=True)[:top_k] results = [] for idx in top_indices: results.append({ "id": code_repository[idx]["id"], "code": code_repository[idx]["code"], "score": scores[idx].item() }) return results ``` -------------------------------- ### Execute Code Search Source: https://context7.com/salesforce/sfr-embedding-code-2b_r/llms.txt Perform a search query and display the results. ```python results = search_code("calculate the greatest common divisor") for r in results: print(f"ID: {r['id']}, Score: {r['score']:.4f}") print(f"Code: {r['code'][:50]}...") ``` -------------------------------- ### Custom Instruction for Bug Search Source: https://context7.com/salesforce/sfr-embedding-code-2b_r/llms.txt Customize the instruction to match your specific retrieval task, such as searching for code containing bugs. The instruction should describe the type of content you're searching for. ```python import torch.nn.functional as F from transformers import AutoModel model = AutoModel.from_pretrained( 'Salesforce/SFR-Embedding-Code-2B_R', trust_remote_code=True ) # Custom instruction for bug-related code search bug_search_instruction = "Given a bug description, retrieve the relevant code that contains the bug" bug_queries = [ "infinite loop in sorting function", "null pointer exception in list operations" ] # Code snippets with potential bugs code_corpus = [ "def buggy_sort(arr):\n i = 0\n while i < len(arr): # Bug: missing increment, causes infinite loop\n if arr[i] > arr[i+1]:\n arr[i], arr[i+1] = arr[i+1], arr[i]\n return arr", "def process_list(items):\n result = items.first() # Bug: 'first()' doesn't exist on lists\n return result.upper()" ] max_length = 32768 query_embeddings = model.encode_queries(bug_queries, instruction=bug_search_instruction, max_length=max_length) corpus_embeddings = model.encode_corpus(code_corpus, max_length=max_length) # Normalize and compute scores query_embeddings = F.normalize(query_embeddings, p=2, dim=1) corpus_embeddings = F.normalize(corpus_embeddings, p=2, dim=1) scores = (query_embeddings @ corpus_embeddings.T) * 100 print(f"Bug search scores:\n{scores.tolist()}") ``` -------------------------------- ### Load SFR-Embedding-Code-2B_R with Transformers Source: https://context7.com/salesforce/sfr-embedding-code-2b_r/llms.txt Initialize the model using the AutoModel interface. Requires setting trust_remote_code=True to load the custom architecture. ```python import torch import torch.nn.functional as F from transformers import AutoModel # Load model with trust_remote_code for custom architecture model = AutoModel.from_pretrained( 'Salesforce/SFR-Embedding-Code-2B_R', trust_remote_code=True ) # Model is ready for encoding queries and passages # Supports max_length up to 32768 tokens print(f"Model loaded successfully") # Output: Model loaded successfully ``` -------------------------------- ### Python Bubble Sort Implementation Source: https://github.com/salesforce/sfr-embedding-code-2b_r/blob/main/README.md A basic implementation of the bubble sort algorithm in Python. Suitable for small datasets or educational purposes. ```python def bubble_sort(arr): n = len(arr) for i in range(n): for j in range(0, n-i-1): if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] return arr ``` -------------------------------- ### Encode Queries and Passages with Transformers Source: https://github.com/salesforce/sfr-embedding-code-2b_r/blob/main/README.md Use this snippet to encode queries and passages for retrieval tasks using the Transformers library. Ensure queries are accompanied by an instruction. The model and tokenizer are loaded using `AutoModel.from_pretrained`. ```python import torch.nn.functional as F from transformers import AutoTokenizer, AutoModel # Each query needs to be accompanied by an corresponding instruction describing the task. query_instruction_example = "Given Code or Text, retrieval relevant content" queries = [ "how to implement quick sort in Python?" ] # No instruction needed for retrieval passages passages = [ "def quick_sort(arr):\n if len(arr) <= 1:\n return arr pivot = arr[len(arr) // 2] left = [x for x in arr if x < pivot] middle = [x for x in arr if x == pivot] right = [x for x in arr if x > pivot] return quick_sort(left) + middle + quick_sort(right)", "def bubble_sort(arr):\n n = len(arr) for i in range(n): for j in range(0, n-i-1): if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] return arr" ] # load model with tokenizer model = AutoModel.from_pretrained('Salesforce/SFR-Embedding-Code-2B_R', trust_remote_code=True) # get the embeddings max_length = 32768 query_embeddings = model.encode_queries(queries, instruction=query_instruction_example, max_length=max_length) passage_embeddings = model.encode_corpus(passages, max_length=max_length) # normalize embeddings query_embeddings = F.normalize(query_embeddings, p=2, dim=1) passage_embeddings = F.normalize(passage_embeddings, p=2, dim=1) scores = (query_embeddings @ passage_embeddings.T) * 100 print(scores.tolist()) # [[69.26929473876953, 58.41606903076172]] ``` -------------------------------- ### Encode Code and Text Corpus Source: https://context7.com/salesforce/sfr-embedding-code-2b_r/llms.txt Generate embeddings for corpus documents without using an instruction prefix. Normalize the embeddings before comparison. ```python import torch.nn.functional as F from transformers import AutoModel model = AutoModel.from_pretrained( 'Salesforce/SFR-Embedding-Code-2B_R', trust_remote_code=True ) # Code snippets to index passages = [ """def quick_sort(arr): if len(arr) <= 1: return arr pivot = arr[len(arr) // 2] left = [x for x in arr if x < pivot] middle = [x for x in arr if x == pivot] right = [x for x in arr if x > pivot] return quick_sort(left) + middle + quick_sort(right)""", """def bubble_sort(arr): n = len(arr) for i in range(n): for j in range(0, n-i-1): if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] return arr""", """def binary_search(arr, target): left, right = 0, len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1""" ] # Encode corpus without instruction max_length = 32768 passage_embeddings = model.encode_corpus(passages, max_length=max_length) # Normalize for cosine similarity passage_embeddings = F.normalize(passage_embeddings, p=2, dim=1) print(f"Passage embeddings shape: {passage_embeddings.shape}") # Output: Passage embeddings shape: torch.Size([3, 2304]) ``` -------------------------------- ### Generate Code Embeddings Source: https://context7.com/salesforce/sfr-embedding-code-2b_r/llms.txt Generate normalized embeddings for the code corpus using a maximum sequence length of 32,768. ```python max_length = 32768 code_texts = [item["code"] for item in code_repository] code_embeddings = model.encode_corpus(code_texts, max_length=max_length) code_embeddings = F.normalize(code_embeddings, p=2, dim=1) ``` -------------------------------- ### Compute Similarity Scores Source: https://context7.com/salesforce/sfr-embedding-code-2b_r/llms.txt Calculate relevance scores between query and passage embeddings using matrix multiplication. ```python import torch.nn.functional as F from transformers import AutoModel model = AutoModel.from_pretrained( 'Salesforce/SFR-Embedding-Code-2B_R', trust_remote_code=True ) # Prepare query query_instruction = "Given Code or Text, retrieval relevant content" queries = ["how to implement quick sort in Python?"] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.