Skip to content

Commit da79228

Browse files
committed
LangChain: Implement suggestions by CodeRabbit
1 parent add6eb6 commit da79228

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

docs/integrate/langchain/tutorial.md

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
## Introduction
55

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.
6+
**LangChain** is a framework for developing applications powered by language models.
7+
This tutorial uses LangChain to interact with CrateDB using only natural language without writing any SQL.
8+
To achieve that, you will need a CrateDB instance running, an OpenAI API key, and some Python knowledge.
79

810
## Set up database
911

@@ -32,45 +34,62 @@ INSERT INTO doc.people VALUES ('John M', {"like"=[1,2,3],"dislike"=[4,5]}, 1, 'n
3234
First, install the required libraries
3335

3436
```shell
35-
pip install 'langchain[openai]' 'sqlalchemy-cratedb'
37+
pip install --upgrade langchain-community langchain-openai 'sqlalchemy-cratedb>=0.42.0.dev2'
3638
```
3739

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.
40+
Once installed, import the required components:
41+
Also, before running the code snippet below, make sure to replace the
42+
`<API_KEY>` with your OpenAI API key. Besides that, replace the URI with
43+
the correct connection string to your CrateDB instance. Finally, enter
44+
your question as a string replacing `<TEXT_QUESTION>` in the code.
3945

4046
```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
4647
import os
48+
from langchain_community.agent_toolkits import create_sql_agent
49+
from langchain_community.utilities import SQLDatabase
50+
from langchain_openai import ChatOpenAI
4751

48-
os.environ["OPENAI_API_KEY"] = "<API_KEY>"
52+
# Prefer exporting OPENAI_API_KEY in the shell (not in code).
53+
# os.environ["OPENAI_API_KEY"] = "<API_KEY>"
4954

50-
llm=OpenAI(temperature=0) #play around with this parameter
51-
#change the URI below to match your CrateDB instance
55+
# Change the database URI below to match your CrateDB instance.
56+
# e.g., local: "crate://localhost:4200"
57+
# e.g., Cloud (with TLS): "crate://<user>:<password>@<cluster>.cratedb.net:4200?ssl=true"
58+
# Play around with the LLM temperature parameter.
5259
db = SQLDatabase.from_uri("crate://")
53-
toolkit = SQLDatabaseToolkit(db=db,llm=llm)
5460

61+
llm = ChatOpenAI(temperature=0)
5562
agent_executor = create_sql_agent(
5663
llm=llm,
57-
toolkit=toolkit,
64+
db=db,
5865
verbose=True
5966
)
6067

61-
agent_executor.run("<TEXT_QUESTION>")
68+
agent_executor.invoke("<TEXT_QUESTION>")
6269
```
6370

6471
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.
6572

6673
![Screenshot 2023-08-24 at 09.27.52|599x500](https://us1.discourse-cdn.com/flex020/uploads/crate/original/2X/8/8b84ca86108a641c944c880894c0b9ac19628a52.png){h=400px}
6774

75+
Now that everything is set up, feel free to explore with different questions
76+
and tables. Keep in mind that connecting an LLM agent to CrateDB enables it
77+
to run queries on your data. For production, use a least‑privileged database
78+
user and avoid sending sensitive data to external providers unless
79+
contractually approved.
6880

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 the [LangChain Introduction].
81+
If you are looking for a different model, you can explore the options
82+
available on the [LangChain Introduction].
7083

7184
## Summary
7285

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.
86+
This tutorial demonstrates how to use LangChain to interact with CrateDB by
87+
writing questions in English.
88+
If you explore different questions, you may encounter some wrong answers,
89+
so we recommend you use this tool with caution and always check the reasoning
90+
it provides behind every answer. If you are looking for more exciting
91+
integrations, have a look at the {ref}`integrations section <integrations>`
92+
in our documentation.
7493

7594

7695
[LangChain Introduction]: https://python.langchain.com/docs/introduction/

0 commit comments

Comments
 (0)