Skip to content

Instantly share code, notes, and snippets.

@SergioEanX
Created November 23, 2024 18:29
Show Gist options
  • Select an option

  • Save SergioEanX/cd0aad96ab6b5ffc2ab09fb3d408260b to your computer and use it in GitHub Desktop.

Select an option

Save SergioEanX/cd0aad96ab6b5ffc2ab09fb3d408260b to your computer and use it in GitHub Desktop.
How to connect to weaviate embedded, if already running reconnect otherwise instantiate a new instance
def get_weaviate():
try:
# Try to connect to an existing Weaviate instance
client = weaviate.connect_to_local(port=8079, grpc_port=50060)
if client.is_ready():
print("Connected to an already running Weaviate instance.")
return client
# else:
# print("Weaviate instance not ready.")
except WeaviateConnectionError as e:
print("No Weaviate instance is currently running; attempting to define a new embedded instance.")
# Start Weaviate Embedded if no instance is running
embedded_path = "./weaviate-data" # Path for embedded data
client = weaviate.WeaviateClient(
additional_headers={
"X-Cohere-Api-Key": <!!!!! ADD YOU API KEY HERE !!!!!!>
},
embedded_options=EmbeddedOptions(
version="latest",
additional_env_vars={
"ENABLE_MODULES": "text2vec-openai,text2vec-cohere",
"PERSISTENCE_DATA_PATH":embedded_path,
"LOG_LEVEL":"error"
},
))
print("Weaviate Embedded started.")
if not client.is_connected():
client.connect()
return client
except WeaviateBaseError as e:
print(e)
# Connect to Weaviate
client = get_weaviate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment