Skip to content

Commit bb8074b

Browse files
committed
update langchain rag docs
1 parent 3571277 commit bb8074b

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

sources/platform/integrations/ai/langchain.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ If you prefer to use JavaScript, you can follow the [JavaScript LangChain docum
2020

2121
Before we start with the integration, we need to install all dependencies:
2222

23-
`pip install apify-client langchain langchain_community langchain_openai openai tiktoken`
23+
`pip install langchain langchain-openai langchain-apify`
2424

2525
After successful installation of all dependencies, we can start writing code.
2626

@@ -30,9 +30,10 @@ First, import all required packages:
3030
import os
3131

3232
from langchain.indexes import VectorstoreIndexCreator
33-
from langchain_community.utilities import ApifyWrapper
34-
from langchain_core.document_loaders.base import Document
35-
from langchain_openai import OpenAI
33+
from langchain_apify import ApifyWrapper
34+
from langchain_core.documents import Document
35+
from langchain_core.vectorstores import InMemoryVectorStore
36+
from langchain_openai import ChatOpenAI
3637
from langchain_openai.embeddings import OpenAIEmbeddings
3738
```
3839

@@ -49,6 +50,7 @@ Note that if you already have some results in an Apify dataset, you can load the
4950

5051
```python
5152
apify = ApifyWrapper()
53+
llm = ChatOpenAI(model="gpt-4o-mini")
5254

5355
loader = apify.call_actor(
5456
actor_id="apify/website-content-crawler",
@@ -68,14 +70,17 @@ The Actor call may take some time as it crawls the LangChain documentation websi
6870
Initialize the vector index from the crawled documents:
6971

7072
```python
71-
index = VectorstoreIndexCreator(embedding=OpenAIEmbeddings()).from_loaders([loader])
73+
index = VectorstoreIndexCreator(
74+
vectorstore_cls=InMemoryVectorStore,
75+
embedding=OpenAIEmbeddings()
76+
).from_loaders([loader])
7277
```
7378

7479
And finally, query the vector index:
7580

7681
```python
7782
query = "What is LangChain?"
78-
result = index.query_with_sources(query, llm=OpenAI())
83+
result = index.query_with_sources(query, llm=llm)
7984

8085
print("answer:", result["answer"])
8186
print("source:", result["sources"])
@@ -87,15 +92,17 @@ If you want to test the whole example, you can simply create a new file, `langch
8792
import os
8893

8994
from langchain.indexes import VectorstoreIndexCreator
90-
from langchain_community.utilities import ApifyWrapper
91-
from langchain_core.document_loaders.base import Document
92-
from langchain_openai import OpenAI
95+
from langchain_apify import ApifyWrapper
96+
from langchain_core.documents import Document
97+
from langchain_core.vectorstores import InMemoryVectorStore
98+
from langchain_openai import ChatOpenAI
9399
from langchain_openai.embeddings import OpenAIEmbeddings
94100

95101
os.environ["OPENAI_API_KEY"] = "Your OpenAI API key"
96102
os.environ["APIFY_API_TOKEN"] = "Your Apify API token"
97103

98104
apify = ApifyWrapper()
105+
llm = ChatOpenAI(model="gpt-4o-mini")
99106

100107
print("Call website content crawler ...")
101108
loader = apify.call_actor(
@@ -104,9 +111,12 @@ loader = apify.call_actor(
104111
dataset_mapping_function=lambda item: Document(page_content=item["text"] or "", metadata={"source": item["url"]}),
105112
)
106113
print("Compute embeddings...")
107-
index = VectorstoreIndexCreator(embedding=OpenAIEmbeddings()).from_loaders([loader])
114+
index = VectorstoreIndexCreator(
115+
vectorstore_cls=InMemoryVectorStore,
116+
embedding=OpenAIEmbeddings()
117+
).from_loaders([loader])
108118
query = "What is LangChain?"
109-
result = index.query_with_sources(query, llm=OpenAI())
119+
result = index.query_with_sources(query, llm=llm)
110120

111121
print("answer:", result["answer"])
112122
print("source:", result["sources"])
@@ -117,9 +127,11 @@ To run it, you can use the following command: `python langchain_integration.py`
117127
After running the code, you should see the following output:
118128

119129
```text
120-
answer: LangChain is a framework for developing applications powered by language models. It provides standard, extendable interfaces, external integrations, and end-to-end implementations for off-the-shelf use. It also integrates with other LLMs, systems, and products to create a vibrant and thriving ecosystem.
130+
answer: LangChain is a framework designed for developing applications powered by large language models (LLMs). It simplifies the
131+
entire application lifecycle, from development to productionization and deployment. LangChain provides open-source components a
132+
nd integrates with various third-party tools, making it easier to build and optimize applications using language models.
121133
122-
source: https://python.langchain.com
134+
source: https://python.langchain.com/docs/get_started/introduction
123135
```
124136

125137
LangChain is a standard interface through which you can interact with a variety of large language models (LLMs).

0 commit comments

Comments
 (0)