Spaces:
Sleeping
Sleeping
File size: 10,165 Bytes
287a0bc |
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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
" # Alternative Embeddings\n",
" \n",
" This notebook demonstrates how to use alternative embedding functions.\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import chromadb"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"client = chromadb.Client()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from chromadb.utils import embedding_functions"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Using OpenAI Embeddings. This assumes you have the openai package installed\n",
"openai_ef = embedding_functions.OpenAIEmbeddingFunction(\n",
" api_key=\"OPENAI_KEY\", # Replace with your own OpenAI API key\n",
" model_name=\"text-embedding-ada-002\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Create a new chroma collection\n",
"openai_collection = client.get_or_create_collection(name=\"openai_embeddings\", embedding_function=openai_ef)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"openai_collection.add(\n",
" documents=[\"This is a document\", \"This is another document\"],\n",
" metadatas=[{\"source\": \"my_source\"}, {\"source\": \"my_source\"}],\n",
" ids=[\"id1\", \"id2\"]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'ids': [['id1', 'id2']],\n",
" 'distances': [[0.1385088860988617, 0.2017185091972351]],\n",
" 'metadatas': [[{'source': 'my_source'}, {'source': 'my_source'}]],\n",
" 'embeddings': None,\n",
" 'documents': [['This is a document', 'This is another document']]}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results = openai_collection.query(\n",
" query_texts=[\"This is a query document\"],\n",
" n_results=2\n",
")\n",
"results"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# Using Cohere Embeddings. This assumes you have the cohere package installed\n",
"cohere_ef = embedding_functions.CohereEmbeddingFunction(\n",
" api_key=\"COHERE_API_KEY\", \n",
" model_name=\"large\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# Create a new chroma collection\n",
"cohere_collection = client.create_collection(name=\"cohere_embeddings\", embedding_function=cohere_ef)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"cohere_collection.add(\n",
" documents=[\"This is a document\", \"This is another document\"],\n",
" metadatas=[{\"source\": \"my_source\"}, {\"source\": \"my_source\"}],\n",
" ids=[\"id1\", \"id2\"]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'ids': [['id1', 'id2']],\n",
" 'embeddings': None,\n",
" 'documents': [['This is a document', 'This is another document']],\n",
" 'metadatas': [[{'source': 'my_source'}, {'source': 'my_source'}]],\n",
" 'distances': [[4343.1328125, 5653.28759765625]]}"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results = cohere_collection.query(\n",
" query_texts=[\"This is a query document\"],\n",
" n_results=2\n",
")\n",
"results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Using Instructor models. The embedding function requires the InstructorEmbedding package. \n",
"# To install it, run pip install InstructorEmbedding\n",
"\n",
"\n",
"#uses base model and cpu\n",
"instructor_ef = embedding_functions.InstructorEmbeddingFunction() \n",
"\n",
"# For task specific embeddings, add an instruction\n",
"# instructor_ef = embedding_functions.InstructorEmbeddingFunction(\n",
"# instruction=\"Represent the Wikipedia document for retrieval: \"\n",
"# )\n",
"\n",
"# Uses hkunlp/instructor-xl model and GPU\n",
"#instructor_ef = embedding_functions.InstructorEmbeddingFunction(model_name=\"hkunlp/instructor-xl\", device=\"cuda\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create a collection with the instructor embedding function\n",
"instructor_collection = client.create_collection(name=\"instructor_embeddings\", embedding_function=instructor_ef)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"instructor_collection.add(\n",
" documents=[\"This is a document\", \"This is another document\"],\n",
" metadatas=[{\"source\": \"my_source\"}, {\"source\": \"my_source\"}],\n",
" ids=[\"id1\", \"id2\"]\n",
")\n",
"\n",
"# Adding documents with an instruction\n",
"# instructor_ef = embedding_functions.InstructorEmbeddingFunction(\n",
"# instruction=\"Represent the Science sentence: \"\n",
"# )\n",
"# instructor_collection = client.create_collection(name=\"instructor_embeddings\", embedding_function=instructor_ef)\n",
"# instructor_collection.add(documents=[\"Parton energy loss in QCD matter\"], ids=[\"id1\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"results = instructor_collection.query(\n",
" query_texts=[\"This is a query document\"],\n",
" n_results=2\n",
")\n",
"results\n",
"\n",
"# Querying with an instruction\n",
"# instructor_ef = embedding_functions.InstructorEmbeddingFunction(instruction=\"Represent the Wikipedia question for retrieving supporting documents: \")\n",
"# instructor_collection = client.get_collection(name=\"instructor_embeddings\", embedding_function=instructor_ef)\n",
"# results = instructor_collection.query(query_texts=[\"where is the food stored in a yam plant\"])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Using HuggingFace models. The embedding function a huggingface api_key\n",
"huggingface_ef = embedding_functions.HuggingFaceEmbeddingFunction(\n",
" api_key=\"HUGGINGFACE_API_KEY\", # Replace with your own HuggingFace API key\n",
" model_name=\"sentence-transformers/all-MiniLM-L6-v2\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# Create a new HuggingFace collection\n",
"huggingface_collection = client.create_collection(name=\"huggingface_embeddings\", embedding_function=huggingface_ef)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"huggingface_collection.add(\n",
" documents=[\"This is a document\", \"This is another document\"],\n",
" metadatas=[{\"source\": \"my_source\"}, {\"source\": \"my_source\"}],\n",
" ids=[\"id1\", \"id2\"]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'ids': [['id1', 'id2']],\n",
" 'embeddings': None,\n",
" 'documents': [['This is a document', 'This is another document']],\n",
" 'metadatas': [[{'source': 'my_source'}, {'source': 'my_source'}]],\n",
" 'distances': [[0.7111215591430664, 1.010978102684021]]}"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results = huggingface_collection.query(\n",
" query_texts=[\"This is a query document\"],\n",
" n_results=2\n",
")\n",
"results"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|