File size: 6,598 Bytes
e386d7a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | {
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# BGE-Code-v1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 0. Installation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install -U FlagEmbedding"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| Model | Language | Parameters | Model Size | Description | Base Model |\n",
"|:-------|:--------:|:--------------:|:--------------:|:-----------------:|:----------------:|\n",
"| [BAAI/bge-code-v1](https://huggingface.co/BAAI/bge-code-v1) | Multi-lingual | 1.54B | 6.18 GB | LLM-based code embedding model with strong text retrieval and multilingual capabilities. | Qwen-2.5-Coder-1.5B |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[BGE-Code-v1](https://huggingface.co/BAAI/bge-code-v1)** is an LLM-based code embedding model that supports code retrieval, text retrieval, and multilingual retrieval. It primarily demonstrates the following capabilities:\n",
"- Superior Code Retrieval Performance: The model demonstrates exceptional code retrieval capabilities, supporting natural language queries in both English and Chinese, as well as 20 programming languages.\n",
"- Robust Text Retrieval Capabilities: The model maintains strong text retrieval capabilities comparable to text embedding models of similar scale.\n",
"- Extensive Multilingual Support: BGE-Code-v1 offers comprehensive multilingual retrieval capabilities, excelling in languages such as English, Chinese, Japanese, French, and more."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from transformers import AutoTokenizer, AutoModel\n",
"import torch, os\n",
"\n",
"tokenizer = AutoTokenizer.from_pretrained(\"BAAI/bge-code-v1\")\n",
"raw_model = AutoModel.from_pretrained(\"BAAI/bge-code-v1\")\n",
"\n",
"raw_model.eval()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Usage"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Given the following tiny corpus:"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"corpus = [\"\"\"\n",
"def func_1(arr, target):\n",
" low, high = 0, len(arr) - 1\n",
" while low <= high:\n",
" mid = (low + high) // 2\n",
" if arr[mid] == target: return mid\n",
" elif arr[mid] < target: low = mid + 1\n",
" else: high = mid - 1\n",
" return -1\n",
"\"\"\",\n",
"\"\"\"\n",
"def func_2(n, memo={}):\n",
" if n <= 1: return n\n",
" if n not in memo:\n",
" memo[n] = fib(n-1, memo) + fib(n-2, memo)\n",
" return memo[n]\n",
"\"\"\",\n",
"\"\"\"\n",
"def func_3(a, b):\n",
" while b:\n",
" a, b = b, a % b\n",
" return a\n",
"\"\"\",\n",
"\"\"\"\n",
"def func_4(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\n",
"\"\"\",\n",
"\"\"\"\n",
"int func_5(const vector<int>& arr, int target) {\n",
" int low = 0, high = arr.size() - 1;\n",
" while (low <= high) {\n",
" int mid = low + (high - low) / 2;\n",
" if (arr[mid] == target) return mid;\n",
" else if (arr[mid] < target) low = mid + 1;\n",
" else high = mid - 1;\n",
" }\n",
" return -1;\n",
"}\n",
"\"\"\"\n",
"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We want to find the answer to the following question:"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"query = \"The fastest way to find an element in a sorted array\""
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Loading checkpoint shards: 100%|ββββββββββ| 2/2 [00:00<00:00, 6.08it/s]\n"
]
}
],
"source": [
"from FlagEmbedding import FlagLLMModel\n",
"\n",
"model = FlagLLMModel('BAAI/bge-code-v1', \n",
" query_instruction_format=\"<instruct>{}\\n<query>{}\",\n",
" query_instruction_for_retrieval=\"Given a question in text, retrieve SQL queries that are appropriate responses to the question.\",\n",
" trust_remote_code=True,\n",
" devices=0,\n",
" use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"((1536,), (5, 1536))"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"query_emb = model.encode_queries(query)\n",
"corpus_emb = model.encode_corpus(corpus)\n",
"query_emb.shape, corpus_emb.shape"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0.4553 0.2172 0.2277 0.196 0.4355]\n"
]
}
],
"source": [
"similarity = query_emb @ corpus_emb.T\n",
"print(similarity)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can see that the elements with index 0 and 5, which are the implementation of binary search in Python and C++, have conspicuously higher similarity than other candidates."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.16"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|