|
| 1 | +# Example based on https://ollama.com/blog/embedding-models |
| 2 | +# using objectbox as a vector store |
| 3 | + |
| 4 | +import ollama |
| 5 | +import objectbox |
| 6 | + |
| 7 | +documents = [ |
| 8 | + "Llamas are members of the camelid family meaning they're pretty closely related to vicuñas and camels", |
| 9 | + "Llamas were first domesticated and used as pack animals 4,000 to 5,000 years ago in the Peruvian highlands", |
| 10 | + "Llamas can grow as much as 6 feet tall though the average llama between 5 feet 6 inches and 5 feet 9 inches tall", |
| 11 | + "Llamas weigh between 280 and 450 pounds and can carry 25 to 30 percent of their body weight", |
| 12 | + "Llamas are vegetarians and have very efficient digestive systems", |
| 13 | + "Llamas live to be about 20 years old, though some only live for 15 years and others live to be 30 years old", |
| 14 | +] |
| 15 | + |
| 16 | + |
| 17 | +from objectbox.model import * |
| 18 | +from objectbox.model.properties import * |
| 19 | +import numpy as np |
| 20 | + |
| 21 | + |
| 22 | +@Entity(id=1, uid=1) |
| 23 | +class DocumentEmbedding: |
| 24 | + id = Id(id=1, uid=1001) |
| 25 | + document = Property(str, id=2, uid=1002) |
| 26 | + embedding = Property(np.ndarray, type=PropertyType.floatVector, id=3, uid=1003, index=HnswIndex( |
| 27 | + id=3, uid=10001, |
| 28 | + dimensions=1024, |
| 29 | + distance_type=HnswDistanceType.EUCLIDEAN |
| 30 | + )) |
| 31 | + |
| 32 | +model = Model() |
| 33 | +model.entity(DocumentEmbedding, last_property_id=IdUid(3, 1003)) |
| 34 | +model.last_entity_id = IdUid(1, 1) |
| 35 | +model.last_index_id = IdUid(3,10001) |
| 36 | + |
| 37 | +ob = objectbox.Builder().model(model).build() |
| 38 | +box = objectbox.Box(ob, DocumentEmbedding) |
| 39 | + |
| 40 | + |
| 41 | +# store each document in a vector embedding database |
| 42 | +for i, d in enumerate(documents): |
| 43 | + response = ollama.embeddings(model="mxbai-embed-large", prompt=d) |
| 44 | + embedding = response["embedding"] |
| 45 | + |
| 46 | + box.put(DocumentEmbedding(document=d,embedding=embedding)) |
| 47 | + |
| 48 | +# an example prompt |
| 49 | +prompt = "What animals are llamas related to?" |
| 50 | + |
| 51 | +# generate an embedding for the prompt and retrieve the most relevant doc |
| 52 | +response = ollama.embeddings( |
| 53 | + prompt=prompt, |
| 54 | + model="mxbai-embed-large" |
| 55 | +) |
| 56 | + |
| 57 | + |
| 58 | +embedding_prop: Property = DocumentEmbedding.get_property("embedding") |
| 59 | +query = box.query( |
| 60 | + embedding_prop.nearest_neighbor(response["embedding"], 1) |
| 61 | +).build() |
| 62 | + |
| 63 | +results = query.find_with_scores() |
| 64 | +data = results[0][0].document |
| 65 | + |
| 66 | + |
| 67 | +# generate a response combining the prompt and data we retrieved in step 2 |
| 68 | +output = ollama.generate( |
| 69 | + model="llama2", |
| 70 | + prompt=f"Using this data: {data}. Respond to this prompt: {prompt}" |
| 71 | +) |
| 72 | + |
| 73 | +print(output['response']) |
0 commit comments