# Weaviate Hybrid Retriever issue in Langchain for custom vectors

**URL:** <https://forum.weaviate.io/t/weaviate-hybrid-retriever-issue-in-langchain-for-custom-vectors/970>\
**Category:** Support\
**Created:** [November 16, 2023, 10:14pm UTC](https://forum.weaviate.io/t/weaviate-hybrid-retriever-issue-in-langchain-for-custom-vectors/970 "2023-11-16T22:14:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tsan](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@Tsan](https://forum.weaviate.io/u/Tsan)\
**Post date:** [November 16, 2023, 10:14pm UTC](https://forum.weaviate.io/t/weaviate-hybrid-retriever-issue-in-langchain-for-custom-vectors/970/1 "2023-11-16T22:14:20Z")

</div>

Hi,

I am trying to use WeaviateHybridSearchRetriever from Langchain and getting this error when I query:

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/weaviate/original/1X/968ac8bf5f092b2c73e42df8fad8733dd2af777c.png)

I am using custom vectors. This is my code

# # For using WCS

import weaviate  
import json  
import os  
from langchain.vectorstores import Weaviate  
from langchain.llms import OpenAI  
from langchain.chains import ChatVectorDBChain  
from langchain.embeddings.huggingface import HuggingFaceEmbeddings  
from langchain.document\_loaders import PyPDFDirectoryLoader  
from langchain.embeddings.huggingface import HuggingFaceEmbeddings  
from langchain.text\_splitter import TokenTextSplitter, CharacterTextSplitter  
from langchain.retrievers.weaviate\_hybrid\_search import WeaviateHybridSearchRetriever  
from langchain.schema import Document  
import numpy as np  
import json

WEAVIATEURL =

client = weaviate.Client(  
url = WEAVIATEURL, # Replace with your endpoint  
auth\_client\_secret=weaviate.AuthApiKey(api\_key=API\_KEY), # Replace w/ your Weaviate instance API key  
)

meta\_info = client.get\_meta()  
print(json.dumps(meta\_info, indent=2))  
client.schema.get()

###############################################################################################

pdf\_path = dir\_of\_pdfs  
loader = PyPDFDirectoryLoader(pdf\_path)  
documents = loader.load()

text\_splitter = TokenTextSplitter(chunk\_size=500, chunk\_overlap=25)  
docs = text\_splitter.split\_documents(documents)  
print (f’Now you have {len(docs)} documents’)

embed\_model\_id = ‘sentence-transformers/all-MiniLM-L6-v2’

embed\_model = HuggingFaceEmbeddings(  
model\_name=embed\_model\_id,  
model\_kwargs={‘device’: “cpu”},  
encode\_kwargs={‘device’: “cpu”, ‘batch\_size’: 32}  
)

doc\_func\_source = lambda x: x.metadata[‘source’]  
doc\_func\_content = lambda x: x.page\_content  
content\_docs = list(map(doc\_func\_content, docs))  
source\_docs = list(map(doc\_func\_source, docs))

embeddings = embed\_model.embed\_documents(content\_docs)  
print (len(embeddings))  
print (len(embeddings[0]))

############################################################################################################

#writing my vectors in JSON

# Format the vectors in the desired JSON structure

combined\_data = [{“Content”: content, “Source”: source, “Vector”: vector}  
for content, source, vector in zip(content\_docs, source\_docs, embeddings)]

# Dump the formatted vectors to a JSON file

with open(‘data.json’, ‘w’) as json\_file:  
json.dump(combined\_data, json\_file)

#############################################################################################################

#creating class

if client.schema.exists(“Ragdocs”):  
client.schema.delete\_class(“Ragdocs”)

# schema for class

class\_obj = {  
‘class’ :‘Ragdocs’,  
“vectorizer”: “none” # If set to “none” you must always provide vectors yourself. Could be any other “text2vec-\*” also.

}

client.schema.create\_class(class\_obj) # returns null on success

####################################################################################################################

#importing data

# Read and load the JSON data from the file

with open(‘data.json’, ‘r’) as file:  
data = json.load(file)

# Configure batch process

client.batch.configure(batch\_size=1)

# Configure a batch process

with client.batch as batch:  
# Batch import all Questions  
for i, d in enumerate(data):  
print(f"importing document no: {i+1}")

```
    properties = {
        "text": d["Content"],
        "source": d["Source"],            
    }
   
    custom_vector = d["Vector"]  

    client.batch.add_data_object(
        properties,
        "Ragdocs",
        vector=custom_vector
    )

```

##########################################################################################################################

retriever = WeaviateHybridSearchRetriever(  
client=client,  
index\_name=“Ragdocs”, #index name is class name  
text\_key=“text”,  
attributes=,  
create\_schema\_if\_missing=True,  
)

# code ran without any issue until here

################################################################################################################################  
retriever.get\_relevant\_documents(“what is OpenVINO?”)

From the error it looks like I need to provide vectors for the query separately as I am not using the vectorizer module of weaviate? How do I provide vectors for the query through the retriever?

---

<div class="post-metadata">

**Author:** ![DudaNogueira](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dudanogueira/32/7846_2.png) [@DudaNogueira](https://forum.weaviate.io/u/DudaNogueira)\
**Post date:** [November 18, 2023, 1:06pm UTC](https://forum.weaviate.io/t/weaviate-hybrid-retriever-issue-in-langchain-for-custom-vectors/970/2 "2023-11-18T13:06:38Z")

</div>

Hi!

Have you seen [this python notebook](https://github.com/langchain-ai/langchain/blob/43dad6cb9126c4ffe5b6073f82119f5f3a382d85/docs/docs/integrations/retrievers/weaviate-hybrid.ipynb)?

I believe that you need to **initiate Weaviate client passing the OpenAI API Key so it can vectorize your query**.

here:

```auto
import os

import weaviate

WEAVIATE_URL = os.getenv("WEAVIATE_URL")
auth_client_secret = (weaviate.AuthApiKey(api_key=os.getenv("WEAVIATE_API_KEY")),)
client = weaviate.Client(
    url=WEAVIATE_URL,
    additional_headers={
        "X-Openai-Api-Key": os.getenv("OPENAI_API_KEY"),
    },
)

# client.schema.delete_all()

```

Also, you will probabyl find this recipe interesting 😉

> <https://github.com/weaviate/recipes/tree/main/integrations/langchain/loading-data>
>
> //github.com/weaviate/recipes/tree/main/integrations/langchain/loading-data

Let me know if that helps 🙂

---

<div class="post-metadata">

**Author:** ![americanthinker](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/americanthinker/32/643_2.png) [@americanthinker](https://forum.weaviate.io/u/americanthinker)\
**Post date:** [December 9, 2023, 2:54pm UTC](https://forum.weaviate.io/t/weaviate-hybrid-retriever-issue-in-langchain-for-custom-vectors/970/3 "2023-12-09T14:54:04Z")

</div>

I found this page after having the same issue. I instantiated a WeaviateHybridSearchRetriever retriever but there isn’t a place (as far as I can tell) where I can include the vector representation of the user query. Hence the error. I think this is more on the langchain side than the Weaviate side. BTW, @DudaNogueira your answer to the original issue on this page, created by @Tsan won’t work because you suggested that he pass an OpenAI API key to use the ada embedding model, but all of his vectors on his Weaviate cluster were made using the SentenceTransformers `all-MiniLM-L6-v2` model.

---

<div class="post-metadata">

**Author:** ![DudaNogueira](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dudanogueira/32/7846_2.png) [@DudaNogueira](https://forum.weaviate.io/u/DudaNogueira)\
**Post date:** [December 11, 2023, 9:28pm UTC](https://forum.weaviate.io/t/weaviate-hybrid-retriever-issue-in-langchain-for-custom-vectors/970/4 "2023-12-11T21:28:42Z")

</div>

Hi @americanthinker !!

I believe you may be able to pass the vector using the hybrid\_search\_kwargs, [according to here](https://github.com/langchain-ai/langchain/blob/c0f4b95aa9961724ab4569049b4c3bc12ebbacfc/libs/langchain/langchain/retrievers/weaviate_hybrid_search.py#L130)

something along the lines of (I have not test):

[considering this python notebook](https://github.com/langchain-ai/langchain/blob/c0f4b95aa9961724ab4569049b4c3bc12ebbacfc/docs/docs/integrations/retrievers/weaviate-hybrid.ipynb#L76)

```auto
retriever.get_relevant_documents("the ethical implications of AI", hybrid_search_kwargs={"vector": [x,y,z]} )

```

Let me know if that works 🙂
