# LLamaIndex use text2vec\_cohere defined in weaviate cloud instance

**URL:** <https://forum.weaviate.io/t/llamaindex-use-text2vec-cohere-defined-in-weaviate-cloud-instance/3313>\
**Category:** General\
**Created:** [August 9, 2024, 8:52am UTC](https://forum.weaviate.io/t/llamaindex-use-text2vec-cohere-defined-in-weaviate-cloud-instance/3313 "2024-08-09T08:52:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SergioEanX](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/sergioeanx/32/2031_2.png) [@SergioEanX](https://forum.weaviate.io/u/SergioEanX)\
**Post date:** [August 9, 2024, 8:52am UTC](https://forum.weaviate.io/t/llamaindex-use-text2vec-cohere-defined-in-weaviate-cloud-instance/3313/1 "2024-08-09T08:52:16Z")

</div>

I want to insert data into weaviate cloud instance using textevec\_cohere and allow weaviate to create the embeddings.  
I’m using Llamaindex to get data from web and then insert it but I get an error stating that I have to define openai\_key.  
Below my code.

> import weaviate  
> import os  
> from typing import List  
> from llama\_index.core.schema import BaseNode, Document  
> from llama\_index.readers.web import SimpleWebPageReader  
> from llama\_index.core.node\_parser import SimpleNodeParser  
> from llama\_index.vector\_stores.weaviate import WeaviateVectorStore  
> from llama\_index.core.storage import StorageContext  
> from llama\_index.core import VectorStoreIndex, Settings  
> import weaviate.classes.config as wc  
> from dotenv import load\_dotenv  
> from weaviate.classes.init import Auth, AdditionalConfig, Timeout  
> from weaviate.exceptions import WeaviateBaseError
> 
> # get the data from the web
> 
> def AddData() → (List[BaseNode], List[Document]):  
> docs = SimpleWebPageReader(html\_to\_text=True).load\_data(  
> [“[LlamaIndex and Weaviate | Weaviate - Vector Database](http://weaviate.io/blog/llamaindex-and-weaviate/)”]  
> )  
> print(f"Loaded {len(docs)} documents")  
> parser = SimpleNodeParser()  
> nodes = parser.get\_nodes\_from\_documents(docs, show\_progress=True)  
> for n in nodes:  
> print(n.get\_content())  
> return nodes, docs
> 
> client = None  
> load\_dotenv()
> 
> try:
> 
> ```
> headers = {
> # "X-OpenAI-Api-Key": os.getenv("OPENAI_APIKEY"),
> "X-Cohere-Api-Key": os.getenv("COHERE_APIKEY")
> }
> 
> nodes, documents = AddData()
> 
> client = weaviate.connect_to_weaviate_cloud(
> cluster_url=os.getenv("WEAVIATE_URL"),
> auth_credentials=Auth.api_key(os.getenv("WEAVIATE_APIKEY")),
> additional_config=AdditionalConfig(
> timeout=Timeout(init=30, query=60, insert=30), # Values in seconds
> ),
> 
> headers=headers,
> skip_init_checks=False
> )
> 
> # Necessary for Cohere?
> os.environ["COHERE_API_KEY"] = os.getenv("COHERE_APIKEY")
> 
> if client.is_ready():
> print(f"Weaviate is ready! Successfully connected to {client.get_meta()}")
> else:
> print("Failed to connect to Weaviate Cloud")
> exit(0)
> 
> # get existing collections
> collections = client.collections.list_all()
> if len(collections) > 0:
> print(f"Found {len(collections)} collections:")
> [print(c) for c in collections]
> else:
> print("No collections found")
> # Check if BlogPosts collection exists
> if client.collections.get("BlogPosts").exists():
> print("Collection 'BlogPosts' already exists")
> else:
> client.collections.create(
> name="BlogPosts",
> description="A collection of blog posts",
> properties=[
> wc.Property(name="content",
> data_type=wc.DataType.TEXT,
> description="The content of the blog post"),
> ],
> # Define the vectorizer module
> vectorizer_config=wc.Configure.Vectorizer.text2vec_cohere(),
> # Define the generative module
> generative_config=wc.Configure.Generative.cohere()
> )
> 
> vector_store = WeaviateVectorStore(weaviate_client=client, index_name="BlogPosts", text_key="content")
> storage_context = StorageContext.from_defaults(vector_store=vector_store)
> # we initiate our index
> index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)
> 
> ```
> 
> except WeaviateBaseError as e:  
> print(f"Failed to connect to Weaviate Cloud: {e.message}“)  
> exit(0)  
> except Exception as e:  
> print(f"An error occurred: {e}”)  
> exit(0)
> 
> finally:  
> if client is not None:  
> client.close()

I get error:

Could not load OpenAI embedding model. If you intended to use OpenAI, please check your OPENAI\_API\_KEY.  
Original error:  
No API key found for OpenAI.  
Please set either the OPENAI\_API\_KEY environment variable or openai.api\_key prior to initialization.  
API keys can be found or created at [https://platform.openai.com/account/api-keys](https://platform.openai.com/account/api-keys)

Consider using embed\_model=‘local’.  
Visit our documentation for more embedding options: [Redirecting...](https://docs.llamaindex.ai/en/stable/module_guides/models/embeddings.html#modules)

---

<div class="post-metadata">

**Author:** ![SergioEanX](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/sergioeanx/32/2031_2.png) [@SergioEanX](https://forum.weaviate.io/u/SergioEanX)\
**Post date:** [August 9, 2024, 2:39pm UTC](https://forum.weaviate.io/t/llamaindex-use-text2vec-cohere-defined-in-weaviate-cloud-instance/3313/2 "2024-08-09T14:39:55Z")

</div>

Finally solved using:

Settings.embed\_model = CohereEmbedding(cohere\_api\_key=os.getenv(“COHERE\_APIKEY”))

Below the revisited code. Any suggestion to improve it further? Am I doing something wrong?

> import weaviate  
> import asyncio  
> import os  
> import weaviate.classes.config as wc  
> from typing import List  
> from llama\_index.core.schema import BaseNode, Document  
> from llama\_index.readers.web import SimpleWebPageReader  
> from llama\_index.core.node\_parser import SimpleNodeParser  
> from llama\_index.vector\_stores.weaviate import WeaviateVectorStore  
> from llama\_index.core.storage import StorageContext  
> from llama\_index.core import VectorStoreIndex, Settings  
> from llama\_index.embeddings.cohere import CohereEmbedding  
> from dotenv import load\_dotenv  
> from weaviate.classes.init import Auth, AdditionalConfig, Timeout  
> from weaviate.exceptions import WeaviateBaseError
> 
> # get the data from the web
> 
> def AddData() → (List[BaseNode], List[Document]):  
> docs = SimpleWebPageReader(html\_to\_text=True).load\_data(  
> [“[LlamaIndex and Weaviate | Weaviate - Vector Database](http://weaviate.io/blog/llamaindex-and-weaviate/)”]  
> )  
> print(f"Loaded {len(docs)} documents")  
> parser = SimpleNodeParser()  
> nodes = parser.get\_nodes\_from\_documents(docs, show\_progress=True)  
> for n in nodes:  
> print(n.get\_content())  
> return nodes, docs
> 
> async def main():  
> client = None  
> load\_dotenv()
> 
> ```
> try:
> 
> headers = {
> # "X-OpenAI-Api-Key": os.getenv("OPENAI_APIKEY"),
> "X-Cohere-Api-Key": os.getenv("COHERE_APIKEY")
> }
> 
> client = weaviate.connect_to_weaviate_cloud(
> cluster_url=os.getenv("WEAVIATE_URL"),
> auth_credentials=Auth.api_key(os.getenv("WEAVIATE_APIKEY")),
> additional_config=AdditionalConfig(
> timeout=Timeout(init=30, query=60, insert=30), # Values in seconds
> ),
> 
> headers=headers,
> skip_init_checks=False
> )
> 
> # Necessary for Cohere?
> os.environ["COHERE_API_KEY"] = os.getenv("COHERE_APIKEY")
> 
> if client.is_ready():
> print(f"Weaviate is ready! Successfully connected to {client.get_meta()}")
> else:
> print("Failed to connect to Weaviate Cloud")
> exit(0)
> 
> # get existing collections
> collections = client.collections.list_all()
> if len(collections) > 0:
> print(f"Found {len(collections)} collections:")
> [print(c) for c in collections]
> else:
> print("No collections found")
> # Check if BlogPosts collection exists
> if client.collections.get("BlogPosts").exists():
> print("Collection 'BlogPosts' already exists")
> else:
> client.collections.create(
> name="BlogPosts",
> description="A collection of blog posts",
> properties=[
> wc.Property(name="content",
> data_type=wc.DataType.TEXT,
> description="The content of the blog post"),
> ],
> # Define the vectorizer module
> vectorizer_config=wc.Configure.Vectorizer.text2vec_cohere(),
> # Define the generative module
> generative_config=wc.Configure.Generative.cohere()
> )
> 
> doInsert = False
> nodes, documents = AddData()
> vector_store = WeaviateVectorStore(weaviate_client=client, index_name="BlogPosts", text_key="content")
> Settings.embed_model = CohereEmbedding(cohere_api_key=os.getenv("COHERE_APIKEY"))
> if doInsert:
> storage_context = StorageContext.from_defaults(vector_store=vector_store)
> # we initiate our index
> index = VectorStoreIndex.from_documents(documents=documents,
> storage_context=storage_context,
> show_progress=True)
> 
> retriever = VectorStoreIndex.from_vector_store(vector_store).as_retriever(
> similarity_top_k=1
> )
> 
> nodes = retriever.retrieve("What is weaviate?")
> print(nodes[0])
> 
> except WeaviateBaseError as e:
> print(f"Failed to connect to Weaviate Cloud: {e.message}")
> exit(0)
> except Exception as e:
> print(f"An error occurred: {e}")
> exit(0)
> 
> finally:
> if client is not None:
> client.close()
> 
> ```
> 
> asyncio.run(main())

---

<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:** [August 9, 2024, 6:30pm UTC](https://forum.weaviate.io/t/llamaindex-use-text2vec-cohere-defined-in-weaviate-cloud-instance/3313/3 "2024-08-09T18:30:44Z")

</div>

hi!

Thanks for sharing!
