OpenAIEmbeddingFunction accepts an api_base, so Chroma can embed your documents and queries with a SambaStack embedding model. You then answer questions with a SambaNova chat model.
Prerequisites
- A SambaStack instance that serves
E5-Mistral-7B-Instruct, plus the instance’s base URL and API key. See API keys and URLs. - A chat model on that instance. This guide uses
gpt-oss-120b. - Python 3.10 or higher.
Installation and setup
Create a virtual environment
Install dependencies
chromadb 1.5.9 and openai 3.35.1.Set your SambaStack base URL and API key
/v1.Embed, store, and query
Run the steps in this section in one Python file or session. Each step uses variables from the steps before it.Create a collection that embeds with SambaNova
api_base and api_key_env_var to Chroma’s OpenAIEmbeddingFunction. api_key_env_var makes Chroma read your SambaNova key instead of the default CHROMA_OPENAI_API_KEY. Do not set api_type. Chroma’s own example sets it to azure for Azure OpenAI, which is not what you want here.PersistentClient saves the collection to ./chroma-data. Use chromadb.EphemeralClient() to keep everything in memory instead. Chroma defaults to squared L2 distance, so the configuration argument sets cosine distance, which is the usual choice for text embeddings.Add your documents
Query the collection
doc1, the passage about the SN40L chip, at a cosine distance of about 0.24. The next closest is about 0.36.E5-Mistral-7B-Instruct is an instruction-tuned model. Chroma embeds query_texts as plain text, and this example still ranks the right passage first. To format a query as an instruction, embed it yourself and pass the vector instead:Answer with a SambaNova chat model
question is the same question you searched with in step 3.The model answers that SambaNova makes the SN40L chip.Delete the collection when you are done
./chroma-data folder.Use Chroma with LangChain
Thelangchain-chroma package wraps the same database. Install it with the OpenAI embeddings integration:
langchain-chroma 1.1.0 and langchain-openai 1.7.2.
Set check_embedding_ctx_length=False. LangChain otherwise sends token IDs instead of text, and the SambaStack embeddings endpoint rejects them.
persist_directory="./chroma-data" to Chroma.from_texts.
Additional resources
Chroma documentation
Chroma OpenAI embedding function
OpenAIEmbeddingFunction and its parameters.LangChain Chroma integration
langchain-chroma vector store.Implement embeddings features
Troubleshooting
The SAMBANOVA_API_KEY environment variable is not set
The SAMBANOVA_API_KEY environment variable is not set
ValueError: The SAMBANOVA_API_KEY environment variable is not set. when the variable named in api_key_env_var is missing. Export it in the shell you run Python from. If you omit api_key_env_var, Chroma looks for CHROMA_OPENAI_API_KEY instead.404 page not found from the embeddings endpoint
404 page not found from the embeddings endpoint
/v1. Set SAMBANOVA_BASE_URL to your SambaStack base URL followed by /v1.401 invalid_api_key from the embeddings endpoint
401 invalid_api_key from the embeddings endpoint
404 model_not_found for text-embedding-ada-002
404 model_not_found for text-embedding-ada-002
model_name, so Chroma requested its default model, text-embedding-ada-002. Set model_name="E5-Mistral-7B-Instruct". If the error names E5-Mistral-7B-Instruct, your SambaStack instance does not serve it, or your key cannot access it. List the models the instance serves with curl -H "Authorization: Bearer $SAMBANOVA_API_KEY" "$SAMBANOVA_BASE_URL/models".400 Invalid type for 'input': expected string, got array
400 Invalid type for 'input': expected string, got array
check_embedding_ctx_length=False to OpenAIEmbeddings, as shown above.
