Notebooks
W
Weaviate
Langchain Weaviate Query Agent

Langchain Weaviate Query Agent

vector-searchagentsvector-databaseretrieval-augmented-generationllm-frameworksfunction-callingweaviate-recipesintegrationsPythongenerative-aillm-agent-frameworkslangchain

Open In Colab

Weaviate Query Agent with LangChain

This notebook will show you how to define the Weaviate Query Agent as a tool with LangChain.

Requirements

  1. Weaviate Cloud instance (WCD): The Weaviate Query Agent is only accessible through WCD at the moment. You can create a serverless cluster or a free 14-day sandbox here.
  2. Install LangChain with pip install langchain. We're using version 0.3.20 at the time of writing this notebook.
  3. Install the Weaviate Agents package with pip install weaviate-agents
  4. You'll need a Weaviate cluster with data. If you don't have one, check out this notebook to import the Weaviate Blogs.

Import libraries and keys

[2]
/usr/local/lib/python3.11/site-packages/IPython/core/interactiveshell.py:3508: LangChainDeprecationWarning: As of langchain-core 0.3.0, LangChain uses pydantic v2 internally. The langchain.pydantic_v1 module was a compatibility shim for pydantic v1, and should no longer be used. Please update the code to import from Pydantic directly.

For example, replace imports like: `from langchain.pydantic_v1 import BaseModel`
with: `from pydantic import BaseModel`
or the v1 compatibility namespace if you are working in a code base that has not been fully upgraded to pydantic 2 yet. 	from pydantic.v1 import BaseModel

  exec(code_obj, self.user_global_ns, self.user_ns)
[3]

Initialize model

[4]

Create Query Agent tool

[7]
[8]

Query time

[9]
AIMessage(content='To run Weaviate with Docker, you can follow these steps:\n\n1. **Install Docker**: Make sure Docker is installed on your machine. You can download it from the [Docker website](https://www.docker.com/products/docker-desktop).\n\n2. **Pull Weaviate Image**: Open your terminal and pull the Weaviate Docker image from Docker Hub using the following command:\n   ```bash\n   docker pull semitechnologies/weaviate\n   ```\n\n3. **Run Weaviate Container**: You can run Weaviate using the following command:\n   ```bash\n   docker run -d -p 8080:8080 semitechnologies/weaviate\n   ```\n   This command will run Weaviate in a detached mode (`-d`), mapping the container\'s port 8080 to your local machine\'s port 8080.\n\n4. **Check if Running**: To verify if the Weaviate container is running, you can use:\n   ```bash\n   docker ps\n   ```\n   This command lists all running containers. You should see the Weaviate container listed there.\n\n5. **Access Weaviate**: Once the container is running, you can access Weaviate\'s RESTful API at `http://localhost:8080/v1`.\n\n6. **Environment Configuration (Optional)**: If you need to customize the environment variables, you can do so by specifying them in the `docker run` command using the `-e` flag. For example, to set a specific contextionary (knowledge graph context), you might use:\n   ```bash\n   docker run -d -p 8080:8080 -e CONTEXTIONARY_URL="your_contextionary_url" semitechnologies/weaviate\n   ```\n\n7. **Stopping the Container**: To stop the container, find its container ID using `docker ps` and then run:\n   ```bash\n   docker stop <container_id>\n   ```\n\n8. **Remove the Container**: If you want to remove the container, use:\n   ```bash\n   docker rm <container_id>\n   ```\n\nWith these steps, you should be able to run and manage Weaviate using Docker.', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 462, 'prompt_tokens': 91, 'total_tokens': 553, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_eb9dce56a8', 'finish_reason': 'stop', 'logprobs': None}, id='run-d7553ed3-6ade-4066-abc0-a64ff1caff58-0', usage_metadata={'input_tokens': 91, 'output_tokens': 462, 'total_tokens': 553, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})