Skip to content

Commit d10f012

Browse files
authored
docs: Update langchain code example to reflect changes in the langchain (#1051)
1 parent fb90e91 commit d10f012

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

sources/platform/integrations/ai/langchain.md

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,20 @@ but if you prefer to use JavaScript, you can follow the same steps in the [JavaS
2020

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

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

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

27-
First, import `os`, `VectorstoreIndexCreator`, `ApifyWrapper`, and `Document` into your source code:
27+
First, import all required packages:
2828

2929
```python
3030
import os
3131

3232
from langchain.indexes import VectorstoreIndexCreator
3333
from langchain_community.utilities import ApifyWrapper
3434
from langchain_core.document_loaders.base import Document
35+
from langchain_openai import OpenAI
36+
from langchain_openai.embeddings import OpenAIEmbeddings
3537
```
3638

3739
Find your [Apify API token](https://console.apify.com/account/integrations) and [OpenAI API key](https://platform.openai.com/account/api-keys) and initialize these into environment variable:
@@ -57,22 +59,26 @@ loader = apify.call_actor(
5759
)
5860
```
5961

60-
_NOTE: The Actor call function can take some time as it loads the data from LangChain documentation website._
62+
:::note Crawling may take some time
63+
64+
The Actor call may take some time as it crawls the LangChain documentation website.
65+
66+
:::
6167

6268
Initialize the vector index from the crawled documents:
6369

6470
```python
65-
index = VectorstoreIndexCreator().from_loaders([loader])
71+
index = VectorstoreIndexCreator(embedding=OpenAIEmbeddings()).from_loaders([loader])
6672
```
6773

6874
And finally, query the vector index:
6975

7076
```python
7177
query = "What is LangChain?"
72-
result = index.query_with_sources(query)
78+
result = index.query_with_sources(query, llm=OpenAI())
7379

74-
print(result["answer"])
75-
print(result["sources"])
80+
print("answer:", result["answer"])
81+
print("source:", result["sources"])
7682
```
7783

7884
If you want to test the whole example, you can simply create a new file, `langchain_integration.py`, and copy the whole code into it.
@@ -83,35 +89,37 @@ import os
8389
from langchain.indexes import VectorstoreIndexCreator
8490
from langchain_community.utilities import ApifyWrapper
8591
from langchain_core.document_loaders.base import Document
92+
from langchain_openai import OpenAI
93+
from langchain_openai.embeddings import OpenAIEmbeddings
8694

8795
os.environ["OPENAI_API_KEY"] = "Your OpenAI API key"
8896
os.environ["APIFY_API_TOKEN"] = "Your Apify API token"
8997

9098
apify = ApifyWrapper()
9199

100+
print("Call website content crawler ...")
92101
loader = apify.call_actor(
93102
actor_id="apify/website-content-crawler",
94103
run_input={"startUrls": [{"url": "https://python.langchain.com/docs/get_started/introduction"}], "maxCrawlPages": 10, "crawlerType": "cheerio"},
95-
dataset_mapping_function=lambda item: Document(
96-
page_content=item["text"] or "", metadata={"source": item["url"]}
97-
),
104+
dataset_mapping_function=lambda item: Document(page_content=item["text"] or "", metadata={"source": item["url"]}),
98105
)
99-
index = VectorstoreIndexCreator().from_loaders([loader])
106+
print("Compute embeddings...")
107+
index = VectorstoreIndexCreator(embedding=OpenAIEmbeddings()).from_loaders([loader])
100108
query = "What is LangChain?"
101-
result = index.query_with_sources(query)
109+
result = index.query_with_sources(query, llm=OpenAI())
102110

103-
print(result["answer"])
104-
print(result["sources"])
111+
print("answer:", result["answer"])
112+
print("source:", result["sources"])
105113
```
106114

107115
To run it, you can use the following command: `python langchain_integration.py`
108116

109117
After running the code, you should see the following output:
110118

111119
```text
112-
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.
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.
113121
114-
https://python.langchain.com
122+
source: https://python.langchain.com
115123
```
116124

117125
LangChain is a standard interface through which you can interact with a variety of large language models (LLMs). It provides modules you can use to build language model applications. It also provides chains and agents with memory capabilities.

0 commit comments

Comments
 (0)