Repository navigation
Expand file tree
/
Copy pathllms.txt
More file actions
114 lines (86 loc) 路 3.08 KB
/
Copy pathllms.txt
File metadata and controls
114 lines (86 loc) 路 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# LangChain Apify Package Overview
The `langchain-apify` package integrates Apify's web scraping and automation capabilities with LangChain's AI toolkit. Here's how you can use it:
## Setup
The package requires Python 3.11 or newer. To install it, use pip:
```bash
pip install langchain-apify
```
Ensure you have set the `APIFY_TOKEN` environment variable with your Apify API token.
```python
import os
os.environ["APIFY_TOKEN"] = "YOUR_APIFY_TOKEN"
```
## Key Imports
For using the package, import these classes:
```python
from langchain_apify import ApifyActorsTool, ApifyDatasetLoader, ApifyWrapper
```
## ApifyActorsTool
This tool allows running Apify Actors directly from LangChain:
```python
browser = ApifyActorsTool(actor_id='apify/rag-web-browser')
results = browser.invoke(input={
"run_input": {"query": "what is Apify Actor?", "maxResults": 3}
})
```
### Using with an Agent
The ApifyActorsTool can also be integrated with LangChain agents for more dynamic workflows:
```python
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
model = ChatOpenAI(model="gpt-4o-mini")
tools = [browser]
agent = create_react_agent(model, tools)
for chunk in agent.stream(
{"messages": [("human", "search for what is Apify?")]},
stream_mode="values"
):
chunk["messages"][-1].pretty_print()
```
## ApifyDatasetLoader
Use this to load data from Apify datasets into LangChain's Document format:
```python
# Example dataset structure
# [
# {
# "text": "Example text from the website.",
# "url": "http://example.com"
# },
# ...
# ]
loader = ApifyDatasetLoader(
dataset_id="your-dataset-id",
dataset_mapping_function=lambda dataset_item: Document(
page_content=dataset_item["text"],
metadata={"source": dataset_item["url"]}
),
)
documents = loader.load()
```
## ApifyWrapper
This wrapper provides methods to interact with Apify Actors and tasks:
```python
apify = ApifyWrapper()
# Example to call an actor
loader = apify.call_actor(
actor_id="apify/website-content-crawler",
run_input={
"startUrls": [{"url": "https://python.langchain.com/docs/introduction/"}],
"maxCrawlPages": 10,
"crawlerType": "cheerio"
},
dataset_mapping_function=lambda item: Document(
page_content=item["text"] or "",
metadata={"source": item["url"]}
),
)
documents = loader.load()
```
## Usage Tips
- **Authentication:** Ensure your Apify API token is correctly set in the environment.
- **Error Handling:** Look out for specific exceptions like `ValueError` for missing API tokens or `RuntimeError` for failed Actor calls.
- **Async Support:** Use `acall_actor` or `acall_actor_task` for asynchronous operations.
**Note:**
- This document assumes you're familiar with Python and LangChain basics.
- Adjust the `YOUR_APIFY_TOKEN` placeholder with your actual token or follow the instructions for setting environment variables.
- The `dataset_mapping_function` is crucial for shaping the data into `Document` objects that LangChain can understand. Adjust based on the structure of your data.