lima
lima

Reputation: 293

How to connect my ChromaDB collection and using langchain

I just have a question for connect ChromaDB with langchain

Already tested chromadb and langchain using from_documents

But using Chroma.from_documents function that is always an embedding cost, right?

So I'm Already have embedding data So

When I receive request then make a collection and want to return result

from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.chat_models import ChatOpenAI
from langchain.vectorstores import Chroma
from langchain.chains import RetrievalQA
from langchain.chains import VectorDBQA
from langchain.chains import RetrievalQAWithSourcesChain

import chromadb


chroma_client = chromadb.Client()
collection = chroma_client.create_collection(name = "Testing")
idx = [str(i) for i in range(len(embedded_df))]
documents = list(embedded_df['title'].values)
metadatas = embedded_df[['text']].to_dict(orient='record')
embedding_list = list(embedded_df['text_embedding'].values)
collection.add(
    documents = documents,
    embeddings = embedding_list,
    metadatas = metadatas,
    ids = idx
)
embedding_func = OpenAIEmbeddings(model='text-embedding-ada-002', query_model_name = 'text-embedding-ada-002')
langchainChroma = Chroma(client = chroma_client, collection_name = 'Testing', embedding_function=embedding_func)
llm = ChatOpenAI()
retriver = langchainChroma.as_retriever(search_kwargs={"k":2})
qa = RetrievalQA.from_chain_type(llm = llm, chain_type='stuff', retriever = retriver)
query = """What is your most favorite food?"""
result = qa.run(query)
print(result)

But this code cannot return my data Just return chatgpt response

How to connect my ChromaDB collection and using langchain

Upvotes: 0

Views: 8344

Answers (3)

cavalier
cavalier

Reputation: 81

You create a chromadb httpclient and pass that to langchain's chroma implementation

import chromadb
from langchain_community.vectorstores import Chroma
from chromadb.config import Settings
    
chromdb_host_sensitive = env_vars["chromdb_host_sensitive"]
chromdb_port_sensitive = env_vars["chromdb_port_sensitive"]
chromdb_user_sensitive = env_vars["chromdb_user_sensitive"]
chromdb_password_sensitive = env_vars["chromdb_password_sensitive"]
    
    
chroma_client = chromadb.HttpClient(
            host=chromdb_host_sensitive, port=chromdb_port_sensitive,
            settings=Settings(
                
chroma_client_auth_provider="chromadb.auth.basic.BasicAuthClientProvider",
                chroma_client_auth_credentials=f"{chromdb_user_sensitive}: 
                {chromdb_password_sensitive}"
            )
        )
    
coll_name = env_vars["chroma_collection_name"]
vector_store = Chroma(
            client=chroma_client, collection_name=coll_name, 
            embedding_function=embeddings)

Upvotes: 1

darniz
darniz

Reputation: 31

i had the same issue my langchain chroma client is

langchain_chroma =  Chroma(
client=client,
collection_name="cricket",
embedding_function=embeddings
)

my other code is

ConversationalRetrievalChain.from_llm(llm=ChatOpenAI(temperature=0.1,
                                                          model_name='gpt-3.5-turbo'),
    retriever=langchain_chroma.as_retriever(),
    memory =memory_store)

Upvotes: 1

Rajeev Rajan
Rajeev Rajan

Reputation: 11

I had the same question and found away to tap in to the db and look through the documents .

refer to the documentation https://python.langchain.com/docs/integrations/vectorstores/chroma

on your code, something like the below may help.

db_records = langchainChroma.get()
print(db_records['documents']) # you may want to change the key as needed. 

Upvotes: 1

Related Questions