Created
September 6, 2025 09:51
-
-
Save blackestwhite/7f068fead72916152e608a48acc9fa84 to your computer and use it in GitHub Desktop.
Ollama EmbeddingGemma (with Gemma3n:e4b) example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ollama | |
| import chromadb | |
| documents = [ | |
| "Llamas are members of the camelid family meaning they're pretty closely related to vicuñas and camels", | |
| "Llamas were first domesticated and used as pack animals 4,000 to 5,000 years ago in the Peruvian highlands", | |
| "Llamas can grow as much as 6 feet tall though the average llama between 5 feet 6 inches and 5 feet 9 inches tall", | |
| "Llamas weigh between 280 and 450 pounds and can carry 25 to 30 percent of their body weight", | |
| "Llamas are vegetarians and have very efficient digestive systems", | |
| "Llamas live to be about 20 years old, though some only live for 15 years and others live to be 30 years old", | |
| ] | |
| # Connect to ChromaDB Docker container | |
| client = chromadb.HttpClient(host="localhost", port=8000) | |
| # Try to get existing collection or create new one | |
| try: | |
| collection = client.get_collection(name="docs") | |
| # Delete existing collection to start fresh | |
| client.delete_collection(name="docs") | |
| collection = client.create_collection(name="docs") | |
| except: | |
| collection = client.create_collection(name="docs") | |
| # store each document in a vector embedding database | |
| for i, d in enumerate(documents): | |
| response = ollama.embed(model="embeddinggemma", input=d) | |
| embeddings = response["embeddings"][0] # Extract the first embedding from the array | |
| collection.add( | |
| ids=[str(i)], | |
| embeddings=[embeddings], | |
| documents=[d] | |
| ) | |
| # an example input | |
| input = "What animals are llamas related to?" | |
| # generate an embedding for the input and retrieve the most relevant doc | |
| response = ollama.embed( | |
| model="embeddinggemma", | |
| input=input | |
| ) | |
| results = collection.query( | |
| query_embeddings=[response["embeddings"][0]], # Extract the first embedding from the array | |
| n_results=1 | |
| ) | |
| data = results['documents'][0][0] | |
| print(f'Found document: {data}') | |
| # generate a response combining the prompt and data we retrieved in step 2 | |
| output = ollama.generate( | |
| model="gemma3n:e4b", | |
| prompt=f"Using this data: {data}. Respond to this prompt: {input}" | |
| ) | |
| print(output['response']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment