[GH-ISSUE #10008] Request for Help: Checking My ollama - Python - Based Embed Code #6560

Closed
opened 2026-04-12 18:11:18 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @20246688 on GitHub (Mar 27, 2025).
Original GitHub issue: https://github.com/ollama/ollama/issues/10008

import asyncio
from ollama import AsyncClient
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",
]

client = chromadb.Client()
collection = client.create_collection(name="docs")

async def store_embeddings(ollama_client):
model_name = 'granite-embedding:278m'
for i, doc in enumerate(documents):
try:
response = await ollama_client.embed(model=model_name, input=doc)
embeddings = response.get("embeddings", [])
if embeddings:
collection.add(
ids=[str(i)],
embeddings=embeddings,
documents=[doc]
)
else:
print(f"Failed to store document {i}: Empty embedding vector")
except Exception as e:
print(f"Failed to store document {i}: {e}")

async def query_embedding(ollama_client, query_text):
model_name = 'granite-embedding:278m'
try:
response = await ollama_client.embed(model=model_name, input=query_text)
embeddings = response.get("embeddings", [])
if not embeddings:
print("Query failed: Empty embedding vector")
return "No matching content found"

    if isinstance(embeddings, list) and len(embeddings) == 1 and isinstance(embeddings[0], list):
        embeddings = embeddings[0]

    results = collection.query(query_embeddings=embeddings, n_results=1)
    return results['documents'][0][0] if results['documents'] else "No matching content found"
except Exception as e:
    print(f"Query failed: {e}")
    return "Query failed"

async def main():
ollama_client = AsyncClient(host='XXX')
await store_embeddings(ollama_client)

query_text = "What animals are llamas related to?"
result = await query_embedding(ollama_client, query_text)
print(f"Result: {result}")

if name == "main":
asyncio.run(main())

Hello! I wrote a Python program based on the embed demo in ollama-Python. But I found the results weren't ideal. Could you please help me check if there's anything wrong with the code?

Originally created by @20246688 on GitHub (Mar 27, 2025). Original GitHub issue: https://github.com/ollama/ollama/issues/10008 import asyncio from ollama import AsyncClient 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", ] client = chromadb.Client() collection = client.create_collection(name="docs") async def store_embeddings(ollama_client): model_name = 'granite-embedding:278m' for i, doc in enumerate(documents): try: response = await ollama_client.embed(model=model_name, input=doc) embeddings = response.get("embeddings", []) if embeddings: collection.add( ids=[str(i)], embeddings=embeddings, documents=[doc] ) else: print(f"Failed to store document {i}: Empty embedding vector") except Exception as e: print(f"Failed to store document {i}: {e}") async def query_embedding(ollama_client, query_text): model_name = 'granite-embedding:278m' try: response = await ollama_client.embed(model=model_name, input=query_text) embeddings = response.get("embeddings", []) if not embeddings: print("Query failed: Empty embedding vector") return "No matching content found" if isinstance(embeddings, list) and len(embeddings) == 1 and isinstance(embeddings[0], list): embeddings = embeddings[0] results = collection.query(query_embeddings=embeddings, n_results=1) return results['documents'][0][0] if results['documents'] else "No matching content found" except Exception as e: print(f"Query failed: {e}") return "Query failed" async def main(): ollama_client = AsyncClient(host='XXX') await store_embeddings(ollama_client) query_text = "What animals are llamas related to?" result = await query_embedding(ollama_client, query_text) print(f"Result: {result}") if __name__ == "__main__": asyncio.run(main()) Hello! I wrote a Python program based on the embed demo in ollama-Python. But I found the results weren't ideal. Could you please help me check if there's anything wrong with the code?
GiteaMirror added the question label 2026-04-12 18:11:18 -05:00
Author
Owner

@rick-github commented on GitHub (Mar 27, 2025):

What's not ideal? Please wrap the code in a markdown block (```python ... ```).

<!-- gh-comment-id:2756968237 --> @rick-github commented on GitHub (Mar 27, 2025): What's not ideal? Please wrap the code in a markdown block (\`\`\`python ... \`\`\`).
Author
Owner

@20246688 commented on GitHub (Mar 27, 2025):

import asyncio
from ollama import AsyncClient
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",
]

client = chromadb.Client()
collection = client.create_collection(name="docs")


async def store_embeddings(ollama_client):
    model_name = 'granite-embedding:278m'
    for i, doc in enumerate(documents):
        try:
            response = await ollama_client.embed(model=model_name, input=doc)
            embeddings = response.get("embeddings", [])
            if embeddings:
                collection.add(
                    ids=[str(i)],
                    embeddings=embeddings,
                    documents=[doc]
                )
            else:
                print(f"Failed to store document {i}: Empty embedding vector")
        except Exception as e:
            print(f"Failed to store document {i}: {e}")


async def query_embedding(ollama_client, query_text):
    model_name = 'granite-embedding:278m'
    try:
        response = await ollama_client.embed(model=model_name, input=query_text)
        embeddings = response.get("embeddings", [])
        if not embeddings:
            print("Query failed: Empty embedding vector")
            return "No matching content found"

        if isinstance(embeddings, list) and len(embeddings) == 1 and isinstance(embeddings[0], list):
            embeddings = embeddings[0]

        results = collection.query(query_embeddings=embeddings, n_results=1)
        return results['documents'][0][0] if results['documents'] else "No matching content found"
    except Exception as e:
        print(f"Query failed: {e}")
        return "Query failed"


async def main():
    ollama_client = AsyncClient(host='XXX')
    await store_embeddings(ollama_client)

    query_text = "What animals are llamas related to?"
    result = await query_embedding(ollama_client, query_text)
    print(f"Result: {result}")


if __name__ == "__main__":
    asyncio.run(main())

I'm really sorry. I'm not familiar with this. Is it like this?I mainly want to ask if this code is written correctly. Can it make the vectors be generated correctly and used for proper matching? I referred to the code on this website: https://ollama.com/blog/embedding-models.

<!-- gh-comment-id:2757002828 --> @20246688 commented on GitHub (Mar 27, 2025): ```python import asyncio from ollama import AsyncClient 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", ] client = chromadb.Client() collection = client.create_collection(name="docs") async def store_embeddings(ollama_client): model_name = 'granite-embedding:278m' for i, doc in enumerate(documents): try: response = await ollama_client.embed(model=model_name, input=doc) embeddings = response.get("embeddings", []) if embeddings: collection.add( ids=[str(i)], embeddings=embeddings, documents=[doc] ) else: print(f"Failed to store document {i}: Empty embedding vector") except Exception as e: print(f"Failed to store document {i}: {e}") async def query_embedding(ollama_client, query_text): model_name = 'granite-embedding:278m' try: response = await ollama_client.embed(model=model_name, input=query_text) embeddings = response.get("embeddings", []) if not embeddings: print("Query failed: Empty embedding vector") return "No matching content found" if isinstance(embeddings, list) and len(embeddings) == 1 and isinstance(embeddings[0], list): embeddings = embeddings[0] results = collection.query(query_embeddings=embeddings, n_results=1) return results['documents'][0][0] if results['documents'] else "No matching content found" except Exception as e: print(f"Query failed: {e}") return "Query failed" async def main(): ollama_client = AsyncClient(host='XXX') await store_embeddings(ollama_client) query_text = "What animals are llamas related to?" result = await query_embedding(ollama_client, query_text) print(f"Result: {result}") if __name__ == "__main__": asyncio.run(main()) ``` I'm really sorry. I'm not familiar with this. Is it like this?I mainly want to ask if this code is written correctly. Can it make the vectors be generated correctly and used for proper matching? I referred to the code on this website: https://ollama.com/blog/embedding-models.
Author
Owner

@rick-github commented on GitHub (Mar 27, 2025):

Yes, it generates vectors and they are used to return appropriate results.

<!-- gh-comment-id:2757814839 --> @rick-github commented on GitHub (Mar 27, 2025): Yes, it generates vectors and they are used to return appropriate results.
Author
Owner

@20246688 commented on GitHub (Mar 28, 2025):

是的,它会生成向量,并用于返回适当的结果。

Thanks for your approval! I'll keep trying to improve it.

<!-- gh-comment-id:2760000194 --> @20246688 commented on GitHub (Mar 28, 2025): > 是的,它会生成向量,并用于返回适当的结果。 Thanks for your approval! I'll keep trying to improve it.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#6560