|
3 | 3 |
|
4 | 4 | ## Introduction |
5 | 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. |
| 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. |
7 | 9 |
|
8 | 10 | ## Set up database |
9 | 11 |
|
@@ -32,45 +34,62 @@ INSERT INTO doc.people VALUES ('John M', {"like"=[1,2,3],"dislike"=[4,5]}, 1, 'n |
32 | 34 | First, install the required libraries |
33 | 35 |
|
34 | 36 | ```shell |
35 | | -pip install 'langchain[openai]' 'sqlalchemy-cratedb' |
| 37 | +pip install --upgrade langchain-community langchain-openai 'sqlalchemy-cratedb>=0.42.0.dev2' |
36 | 38 | ``` |
37 | 39 |
|
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. |
39 | 45 |
|
40 | 46 | ```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 | 47 | 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 |
47 | 51 |
|
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>" |
49 | 54 |
|
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. |
52 | 59 | db = SQLDatabase.from_uri("crate://") |
53 | | -toolkit = SQLDatabaseToolkit(db=db,llm=llm) |
54 | 60 |
|
| 61 | +llm = ChatOpenAI(temperature=0) |
55 | 62 | agent_executor = create_sql_agent( |
56 | 63 | llm=llm, |
57 | | - toolkit=toolkit, |
| 64 | + db=db, |
58 | 65 | verbose=True |
59 | 66 | ) |
60 | 67 |
|
61 | | -agent_executor.run("<TEXT_QUESTION>") |
| 68 | +agent_executor.invoke("<TEXT_QUESTION>") |
62 | 69 | ``` |
63 | 70 |
|
64 | 71 | 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 | 72 |
|
66 | 73 | {h=400px} |
67 | 74 |
|
| 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. |
68 | 80 |
|
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]. |
70 | 83 |
|
71 | 84 | ## Summary |
72 | 85 |
|
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. |
74 | 93 |
|
75 | 94 |
|
76 | 95 | [LangChain Introduction]: https://python.langchain.com/docs/introduction/ |
0 commit comments