|
| 1 | +(langchain-tutorial)= |
| 2 | +# How to set up LangChain with CrateDB |
| 3 | + |
| 4 | +## Introduction |
| 5 | + |
| 6 | +**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. |
| 7 | + |
| 8 | +## Set up database |
| 9 | + |
| 10 | +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. |
| 11 | + |
| 12 | +```psql |
| 13 | +CREATE TABLE IF NOT EXISTS "doc"."people" ( |
| 14 | + "name" TEXT, |
| 15 | + "info" OBJECT(DYNAMIC) AS ( |
| 16 | + "like" ARRAY(BIGINT), |
| 17 | + "dislike" ARRAY(BIGINT) |
| 18 | + ), |
| 19 | + "house_id" INTEGER, |
| 20 | + "description" TEXT |
| 21 | +); |
| 22 | +``` |
| 23 | + |
| 24 | +```psql |
| 25 | +INSERT INTO doc.people VALUES ('John M', {"like"=[1,2,3],"dislike"=[4,5]}, 1, 'nice person'), |
| 26 | + ('John T', {"like"=[2],"dislike"=[1]}, 2, 'tall person'), |
| 27 | + ('Mary P', {"like"=[2,3],"dislike"=[7]}, 3, 'smart person'); |
| 28 | +``` |
| 29 | + |
| 30 | +## Use LangChain |
| 31 | + |
| 32 | +First, install the required libraries |
| 33 | + |
| 34 | +```shell |
| 35 | +pip install 'langchain[openai]' 'sqlalchemy-cratedb' |
| 36 | +``` |
| 37 | + |
| 38 | +Once installed, import every component that will be used as follows. Also, before running the code snippet below, make sure to replace the <API_KEY> 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 <TEXT_QUESTION> in the code. |
| 39 | + |
| 40 | +```python |
| 41 | +from langchain.agents import create_sql_agent |
| 42 | +from langchain.agents.agent_toolkits import SQLDatabaseToolkit |
| 43 | +from langchain.sql_database import SQLDatabase |
| 44 | +from langchain.llms.openai import OpenAI |
| 45 | +from langchain.agents import AgentExecutor |
| 46 | +import os |
| 47 | + |
| 48 | +os.environ["OPENAI_API_KEY"]= <API_KEY> |
| 49 | + |
| 50 | +llm=OpenAI(temperature=0) #play around with this parameter |
| 51 | +#change the URI below to match your CrateDB instance |
| 52 | +db = SQLDatabase.from_uri("crate://") |
| 53 | +toolkit = SQLDatabaseToolkit(db=db,llm=llm) |
| 54 | + |
| 55 | +agent_executor = create_sql_agent( |
| 56 | + llm=llm, |
| 57 | + toolkit=toolkit, |
| 58 | + verbose=True |
| 59 | +) |
| 60 | + |
| 61 | +agent_executor.run(<TEXT_QUESTION>) |
| 62 | +``` |
| 63 | + |
| 64 | +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. |
| 65 | + |
| 66 | +{h=400px} |
| 67 | + |
| 68 | + |
| 69 | +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 [LangChain documentation](https://python.langchain.com/docs/get_started/introduction.html). |
| 70 | + |
| 71 | +## Summary |
| 72 | + |
| 73 | +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 <integrate>` in our documentation. |
0 commit comments