From add6eb66e586271ed3ad9e9a76e092f080544fe7 Mon Sep 17 00:00:00 2001 From: Karyn Azevedo Date: Sun, 14 Sep 2025 19:01:24 +0200 Subject: [PATCH 1/2] LangChain: Starter tutorial --- docs/integrate/index.md | 2 +- docs/integrate/langchain/index.md | 8 ++- docs/integrate/langchain/tutorial.md | 76 ++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 docs/integrate/langchain/tutorial.md diff --git a/docs/integrate/index.md b/docs/integrate/index.md index 1028d555..c66c6dac 100644 --- a/docs/integrate/index.md +++ b/docs/integrate/index.md @@ -1,5 +1,5 @@ (integrate)= - +(integrations)= # Integrations You have a variety of options to connect and integrate 3rd-party diff --git a/docs/integrate/langchain/index.md b/docs/integrate/langchain/index.md index f7e34db9..a6425807 100644 --- a/docs/integrate/langchain/index.md +++ b/docs/integrate/langchain/index.md @@ -71,7 +71,7 @@ natural language without writing any SQL. To achieve that, you will need a CrateDB instance running, an OpenAI API key, and some Python knowledge. -[![Navigate to Tutorial](https://img.shields.io/badge/Navigate%20to-Tutorial-darkblue?logo=Markdown)][How to set up LangChain with CrateDB] +- {ref}`langchain-tutorial` ::: :::{grid-item} :columns: 3 @@ -204,6 +204,12 @@ solution. :::: +:::{toctree} +:maxdepth: 1 +:hidden: +Tutorial +::: + [End-to-End RAG with CrateDB and LangChain]: https://speakerdeck.com/cratedb/how-to-use-private-data-in-generative-ai-end-to-end-solution-for-rag-with-cratedb-and-langchain [How to set up LangChain with CrateDB]: https://community.cratedb.com/t/how-to-set-up-langchain-with-cratedb/1576 diff --git a/docs/integrate/langchain/tutorial.md b/docs/integrate/langchain/tutorial.md new file mode 100644 index 00000000..12bbba0e --- /dev/null +++ b/docs/integrate/langchain/tutorial.md @@ -0,0 +1,76 @@ +(langchain-tutorial)= +# How to set up LangChain with CrateDB + +## Introduction + +**LangChain** is a framework for developing applications powered by language models. For this tutorial, we are going to use it to interact with CrateDB using only natural language without writing any SQL. To achieve that, you will need a CrateDB instance running, an OpenAI API key, and some Python knowledge. + +## Set up database + +If you are new to CrateDB, check the deployment options available [here](https://cratedb.com/download) for download options or here for the [free cloud](https://cratedb.com/lp-crfree?hsCtaTracking=3d8a5114-8592-4ec6-adc7-3714f5fe403d%7Cf1d1dba0-a936-41c2-9c7f-1ffb2c24745a) deployment, choose the one that suits you best. Once CrateDB is set up, create and populate the table as seen below. + +```psql +CREATE TABLE IF NOT EXISTS "doc"."people" ( + "name" TEXT, + "info" OBJECT(DYNAMIC) AS ( + "like" ARRAY(BIGINT), + "dislike" ARRAY(BIGINT) + ), + "house_id" INTEGER, + "description" TEXT +); +``` + +```psql +INSERT INTO doc.people VALUES ('John M', {"like"=[1,2,3],"dislike"=[4,5]}, 1, 'nice person'), + ('John T', {"like"=[2],"dislike"=[1]}, 2, 'tall person'), + ('Mary P', {"like"=[2,3],"dislike"=[7]}, 3, 'smart person'); +``` + +## Use LangChain + +First, install the required libraries + +```shell +pip install 'langchain[openai]' 'sqlalchemy-cratedb' +``` + +Once installed, import every component that will be used as follows. Also, before running the code snippet below, make sure to replace the with your OpenAI API key. Besides that, replace the URI with the correct connection string to your CrateDB instance. Finally, enter your question as a string replacing in the code. + +```python +from langchain.agents import create_sql_agent +from langchain.agents.agent_toolkits import SQLDatabaseToolkit +from langchain.sql_database import SQLDatabase +from langchain.llms.openai import OpenAI +from langchain.agents import AgentExecutor +import os + +os.environ["OPENAI_API_KEY"] = "" + +llm=OpenAI(temperature=0) #play around with this parameter +#change the URI below to match your CrateDB instance +db = SQLDatabase.from_uri("crate://") +toolkit = SQLDatabaseToolkit(db=db,llm=llm) + +agent_executor = create_sql_agent( + llm=llm, + toolkit=toolkit, + verbose=True +) + +agent_executor.run("") +``` + +If you ask the question “Who is tall?“ to the model, the result will be as follows. As you can see, it queries all the tables and, based on their names and their columns' names, it decides which is relevant to the query. You can track the reasoning behind the decision-making process the model uses. + +![Screenshot 2023-08-24 at 09.27.52|599x500](https://us1.discourse-cdn.com/flex020/uploads/crate/original/2X/8/8b84ca86108a641c944c880894c0b9ac19628a52.png){h=400px} + + +Now that everything is set up, feel free to explore with different questions and tables. Keep in mind, that by connecting to CrateDB with the OpenAI API, you are giving it access to perform a variety of queries on your data, so avoid using it with your production environment or use a specific user with limited permissions. If you are looking for a different model, you can explore the options available on the [LangChain Introduction]. + +## Summary + +This tutorial covered the use of LangChain to interact with CrateDB by simply writing questions in English. If you explore different questions, you may encounter some wrong answers, so we recommend you use this tool with caution and always check the reasoning it provides behind every answer. If you are looking for more exciting integrations, have a look at the {ref}`integrations section ` in our documentation. + + +[LangChain Introduction]: https://python.langchain.com/docs/introduction/ From da792281e4377cb5acb33d56a9de89f78e3d9945 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Tue, 16 Sep 2025 12:41:12 +0200 Subject: [PATCH 2/2] LangChain: Implement suggestions by CodeRabbit --- docs/integrate/langchain/tutorial.md | 51 +++++++++++++++++++--------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/docs/integrate/langchain/tutorial.md b/docs/integrate/langchain/tutorial.md index 12bbba0e..05e9fd10 100644 --- a/docs/integrate/langchain/tutorial.md +++ b/docs/integrate/langchain/tutorial.md @@ -3,7 +3,9 @@ ## Introduction -**LangChain** is a framework for developing applications powered by language models. For this tutorial, we are going to use it to interact with CrateDB using only natural language without writing any SQL. To achieve that, you will need a CrateDB instance running, an OpenAI API key, and some Python knowledge. +**LangChain** is a framework for developing applications powered by language models. +This tutorial uses LangChain to interact with CrateDB using only natural language without writing any SQL. +To achieve that, you will need a CrateDB instance running, an OpenAI API key, and some Python knowledge. ## Set up database @@ -32,45 +34,62 @@ INSERT INTO doc.people VALUES ('John M', {"like"=[1,2,3],"dislike"=[4,5]}, 1, 'n First, install the required libraries ```shell -pip install 'langchain[openai]' 'sqlalchemy-cratedb' +pip install --upgrade langchain-community langchain-openai 'sqlalchemy-cratedb>=0.42.0.dev2' ``` -Once installed, import every component that will be used as follows. Also, before running the code snippet below, make sure to replace the with your OpenAI API key. Besides that, replace the URI with the correct connection string to your CrateDB instance. Finally, enter your question as a string replacing in the code. +Once installed, import the required components: +Also, before running the code snippet below, make sure to replace the +`` with your OpenAI API key. Besides that, replace the URI with +the correct connection string to your CrateDB instance. Finally, enter +your question as a string replacing `` in the code. ```python -from langchain.agents import create_sql_agent -from langchain.agents.agent_toolkits import SQLDatabaseToolkit -from langchain.sql_database import SQLDatabase -from langchain.llms.openai import OpenAI -from langchain.agents import AgentExecutor import os +from langchain_community.agent_toolkits import create_sql_agent +from langchain_community.utilities import SQLDatabase +from langchain_openai import ChatOpenAI -os.environ["OPENAI_API_KEY"] = "" +# Prefer exporting OPENAI_API_KEY in the shell (not in code). +# os.environ["OPENAI_API_KEY"] = "" -llm=OpenAI(temperature=0) #play around with this parameter -#change the URI below to match your CrateDB instance +# Change the database URI below to match your CrateDB instance. +# e.g., local: "crate://localhost:4200" +# e.g., Cloud (with TLS): "crate://:@.cratedb.net:4200?ssl=true" +# Play around with the LLM temperature parameter. db = SQLDatabase.from_uri("crate://") -toolkit = SQLDatabaseToolkit(db=db,llm=llm) +llm = ChatOpenAI(temperature=0) agent_executor = create_sql_agent( llm=llm, - toolkit=toolkit, + db=db, verbose=True ) -agent_executor.run("") +agent_executor.invoke("") ``` If you ask the question “Who is tall?“ to the model, the result will be as follows. As you can see, it queries all the tables and, based on their names and their columns' names, it decides which is relevant to the query. You can track the reasoning behind the decision-making process the model uses. ![Screenshot 2023-08-24 at 09.27.52|599x500](https://us1.discourse-cdn.com/flex020/uploads/crate/original/2X/8/8b84ca86108a641c944c880894c0b9ac19628a52.png){h=400px} +Now that everything is set up, feel free to explore with different questions +and tables. Keep in mind that connecting an LLM agent to CrateDB enables it +to run queries on your data. For production, use a least‑privileged database +user and avoid sending sensitive data to external providers unless +contractually approved. -Now that everything is set up, feel free to explore with different questions and tables. Keep in mind, that by connecting to CrateDB with the OpenAI API, you are giving it access to perform a variety of queries on your data, so avoid using it with your production environment or use a specific user with limited permissions. If you are looking for a different model, you can explore the options available on the [LangChain Introduction]. +If you are looking for a different model, you can explore the options +available on the [LangChain Introduction]. ## Summary -This tutorial covered the use of LangChain to interact with CrateDB by simply writing questions in English. If you explore different questions, you may encounter some wrong answers, so we recommend you use this tool with caution and always check the reasoning it provides behind every answer. If you are looking for more exciting integrations, have a look at the {ref}`integrations section ` in our documentation. +This tutorial demonstrates how to use LangChain to interact with CrateDB by +writing questions in English. +If you explore different questions, you may encounter some wrong answers, +so we recommend you use this tool with caution and always check the reasoning +it provides behind every answer. If you are looking for more exciting +integrations, have a look at the {ref}`integrations section ` +in our documentation. [LangChain Introduction]: https://python.langchain.com/docs/introduction/