Skip to content

Commit 58678ea

Browse files
karynzvamotl
authored andcommitted
LangChain: Starter tutorial
1 parent 8946a4b commit 58678ea

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

docs/integrate/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(integrate)=
2-
2+
(integrations)=
33
# Integrations
44

55
You have a variety of options to connect and integrate 3rd-party

docs/integrate/langchain/index.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ natural language without writing any SQL.
7171
To achieve that, you will need a CrateDB instance running, an OpenAI API key,
7272
and some Python knowledge.
7373

74-
[![Navigate to Tutorial](https://img.shields.io/badge/Navigate%20to-Tutorial-darkblue?logo=Markdown)][How to set up LangChain with CrateDB]
74+
- {ref}`langchain-tutorial`
7575
:::
7676
:::{grid-item}
7777
:columns: 3
@@ -204,6 +204,12 @@ solution.
204204
::::
205205

206206

207+
:::{toctree}
208+
:maxdepth: 1
209+
:hidden:
210+
Tutorial <tutorial>
211+
:::
212+
207213

208214
[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
209215
[How to set up LangChain with CrateDB]: https://community.cratedb.com/t/how-to-set-up-langchain-with-cratedb/1576
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
![Screenshot 2023-08-24 at 09.27.52|599x500](https://us1.discourse-cdn.com/flex020/uploads/crate/original/2X/8/8b84ca86108a641c944c880894c0b9ac19628a52.png){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

Comments
 (0)