Skip to content

Commit 437e48f

Browse files
authored
Merge pull request #567 from goasleep/feature/add_model_instance_info
2 parents 117fca0 + a8ca1fc commit 437e48f

File tree

6 files changed

+141
-0
lines changed

6 files changed

+141
-0
lines changed

docs/source/scrapers/llm.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,35 @@ We can also pass a model instance for the chat model and the embedding model. Fo
194194
"model_instance": embedder_model_instance
195195
}
196196
}
197+
198+
Other LLM models
199+
^^^^^^^^^^^^^^^^
200+
201+
We can also pass a model instance for the chat model and the embedding model through the **model_instance** parameter.
202+
This feature enables you to utilize a Langchain model instance.
203+
You will discover the model you require within the provided list:
204+
205+
- `chat model list <https://python.langchain.com/v0.2/docs/integrations/chat/#all-chat-models>`_
206+
- `embedding model list <https://python.langchain.com/v0.2/docs/integrations/text_embedding/#all-embedding-models>`_.
207+
208+
For instance, consider **chat model** Moonshot. We can integrate it in the following manner:
209+
210+
.. code-block:: python
211+
212+
from langchain_community.chat_models.moonshot import MoonshotChat
213+
214+
# The configuration parameters are contingent upon the specific model you select
215+
llm_instance_config = {
216+
"model": "moonshot-v1-8k",
217+
"base_url": "https://api.moonshot.cn/v1",
218+
"moonshot_api_key": "MOONSHOT_API_KEY",
219+
}
220+
221+
llm_model_instance = MoonshotChat(**llm_instance_config)
222+
graph_config = {
223+
"llm": {
224+
"model_instance": llm_model_instance,
225+
"model_tokens": 5000
226+
},
227+
}
228+

examples/model_instance/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MOONLIGHT_API_KEY="YOUR MOONLIGHT API KEY"
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
Basic example of scraping pipeline using SmartScraper and model_instace
3+
"""
4+
5+
import os, json
6+
from scrapegraphai.graphs import SmartScraperGraph
7+
from scrapegraphai.utils import prettify_exec_info
8+
from langchain_community.chat_models.moonshot import MoonshotChat
9+
from dotenv import load_dotenv
10+
load_dotenv()
11+
12+
# ************************************************
13+
# Define the configuration for the graph
14+
# ************************************************
15+
16+
17+
llm_instance_config = {
18+
"model": "moonshot-v1-8k",
19+
"base_url": "https://api.moonshot.cn/v1",
20+
"moonshot_api_key": os.getenv("MOONLIGHT_API_KEY"),
21+
}
22+
23+
24+
llm_model_instance = MoonshotChat(**llm_instance_config)
25+
26+
graph_config = {
27+
"llm": {
28+
"model_instance": llm_model_instance,
29+
"model_tokens": 10000
30+
},
31+
"verbose": True,
32+
"headless": True,
33+
}
34+
35+
# ************************************************
36+
# Create the SmartScraperGraph instance and run it
37+
# ************************************************
38+
39+
smart_scraper_graph = SmartScraperGraph(
40+
prompt="List me what does the company do, the name and a contact email.",
41+
source="https://scrapegraphai.com/",
42+
config=graph_config
43+
)
44+
45+
result = smart_scraper_graph.run()
46+
print(json.dumps(result, indent=4))
47+
48+
# ************************************************
49+
# Get graph execution info
50+
# ************************************************
51+
52+
graph_exec_info = smart_scraper_graph.get_execution_info()
53+
print(prettify_exec_info(graph_exec_info))

examples/moonshot/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MOONLIGHT_API_KEY="YOUR MOONLIGHT API KEY"

examples/moonshot/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This folder offer an example of how to use ScrapeGraph-AI with Moonshot and SmartScraperGraph. More usage examples can refer to openai exapmles.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
Basic example of scraping pipeline using SmartScraper and model_instace
3+
"""
4+
5+
import os, json
6+
from scrapegraphai.graphs import SmartScraperGraph
7+
from scrapegraphai.utils import prettify_exec_info
8+
from langchain_community.chat_models.moonshot import MoonshotChat
9+
from dotenv import load_dotenv
10+
load_dotenv()
11+
12+
# ************************************************
13+
# Define the configuration for the graph
14+
# ************************************************
15+
16+
17+
llm_instance_config = {
18+
"model": "moonshot-v1-8k",
19+
"base_url": "https://api.moonshot.cn/v1",
20+
"moonshot_api_key": os.getenv("MOONLIGHT_API_KEY"),
21+
}
22+
23+
24+
llm_model_instance = MoonshotChat(**llm_instance_config)
25+
26+
graph_config = {
27+
"llm": {
28+
"model_instance": llm_model_instance,
29+
"model_tokens": 10000
30+
},
31+
"verbose": True,
32+
"headless": True,
33+
}
34+
35+
# ************************************************
36+
# Create the SmartScraperGraph instance and run it
37+
# ************************************************
38+
39+
smart_scraper_graph = SmartScraperGraph(
40+
prompt="List me what does the company do, the name and a contact email.",
41+
source="https://scrapegraphai.com/",
42+
config=graph_config
43+
)
44+
45+
result = smart_scraper_graph.run()
46+
print(json.dumps(result, indent=4))
47+
48+
# ************************************************
49+
# Get graph execution info
50+
# ************************************************
51+
52+
graph_exec_info = smart_scraper_graph.get_execution_info()
53+
print(prettify_exec_info(graph_exec_info))

0 commit comments

Comments
 (0)