Skip to content

Commit 2258fe5

Browse files
committed
add new search graph examples
1 parent c47a505 commit 2258fe5

File tree

6 files changed

+134
-107
lines changed

6 files changed

+134
-107
lines changed

examples/azure/search_graph_azure.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
Example of Search Graph
3+
"""
4+
5+
import os
6+
from dotenv import load_dotenv
7+
from langchain_openai import AzureChatOpenAI
8+
from langchain_openai import AzureOpenAIEmbeddings
9+
from scrapegraphai.graphs import SearchGraph
10+
from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
11+
load_dotenv()
12+
13+
FILE_NAME = "inputs/example.json"
14+
curr_dir = os.path.dirname(os.path.realpath(__file__))
15+
file_path = os.path.join(curr_dir, FILE_NAME)
16+
17+
with open(file_path, 'r', encoding="utf-8") as file:
18+
text = file.read()
19+
20+
# ************************************************
21+
# Initialize the model instances
22+
# ************************************************
23+
24+
llm_model_instance = AzureChatOpenAI(
25+
openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],
26+
azure_deployment=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT_NAME"]
27+
)
28+
29+
embedder_model_instance = AzureOpenAIEmbeddings(
30+
azure_deployment=os.environ["AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME"],
31+
openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],
32+
)
33+
34+
# ************************************************
35+
# Create the JSONScraperGraph instance and run it
36+
# ************************************************
37+
38+
graph_config = {
39+
"llm": {"model_instance": llm_model_instance},
40+
"embeddings": {"model_instance": embedder_model_instance}
41+
}
42+
43+
# ************************************************
44+
# Create the SearchGraph instance and run it
45+
# ************************************************
46+
47+
search_graph = SearchGraph(
48+
prompt="List me the best escursions near Trento",
49+
config=graph_config
50+
)
51+
52+
result = search_graph.run()
53+
print(result)
54+
55+
# ************************************************
56+
# Get graph execution info
57+
# ************************************************
58+
59+
graph_exec_info = search_graph.get_execution_info()
60+
print(prettify_exec_info(graph_exec_info))
61+
62+
# Save to json and csv
63+
convert_to_csv(result, "result")
64+
convert_to_json(result, "result")

examples/gemini/search_graph_gemini.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
"temperature": 0,
2222
"streaming": True
2323
},
24+
"max_results": 5,
25+
"verbose": True,
2426
}
2527

2628
# ************************************************

examples/local_models/Docker/search_graph_docker.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
from scrapegraphai.graphs import SearchGraph
6-
from scrapegraphai.utils import convert_to_csv, convert_to_json
6+
from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
77

88
# ************************************************
99
# Define the configuration for the graph
@@ -20,21 +20,30 @@
2020
"embeddings": {
2121
"model": "ollama/nomic-embed-text",
2222
"temperature": 0,
23-
}
23+
},
24+
"max_results": 5,
25+
"verbose": True,
2426
}
2527

2628
# ************************************************
2729
# Create the SearchGraph instance and run it
2830
# ************************************************
2931

3032
search_graph = SearchGraph(
31-
prompt="List me all the regions of Italy.",
33+
prompt="List me the best escursions near Trento",
3234
config=graph_config
3335
)
3436

3537
result = search_graph.run()
3638
print(result)
3739

40+
# ************************************************
41+
# Get graph execution info
42+
# ************************************************
43+
44+
graph_exec_info = search_graph.get_execution_info()
45+
print(prettify_exec_info(graph_exec_info))
46+
3847
# Save to json and csv
3948
convert_to_csv(result, "result")
4049
convert_to_json(result, "result")
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Example of Search Graph
3+
"""
4+
from scrapegraphai.graphs import SearchGraph
5+
from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
6+
7+
# ************************************************
8+
# Define the configuration for the graph
9+
# ************************************************
10+
11+
12+
graph_config = {
13+
"llm": {
14+
"model": "ollama/mistral",
15+
"temperature": 0,
16+
"format": "json", # Ollama needs the format to be specified explicitly
17+
# "model_tokens": 2000, # set context length arbitrarily,
18+
"base_url": "http://localhost:11434", # set ollama URL arbitrarily
19+
},
20+
"embeddings": {
21+
"model": "ollama/nomic-embed-text",
22+
"temperature": 0,
23+
"base_url": "http://localhost:11434", # set ollama URL arbitrarily
24+
},
25+
"max_results": 5,
26+
"verbose": True,
27+
}
28+
29+
# ************************************************
30+
# Create the SearchGraph instance and run it
31+
# ************************************************
32+
33+
search_graph = SearchGraph(
34+
prompt="List me the best escursions near Trento",
35+
config=graph_config
36+
)
37+
38+
result = search_graph.run()
39+
print(result)
40+
41+
# ************************************************
42+
# Get graph execution info
43+
# ************************************************
44+
45+
graph_exec_info = search_graph.get_execution_info()
46+
print(prettify_exec_info(graph_exec_info))
47+
48+
# Save to json and csv
49+
convert_to_csv(result, "result")
50+
convert_to_json(result, "result")

examples/openai/search_graph_multi.py

Lines changed: 0 additions & 98 deletions
This file was deleted.

scrapegraphai/graphs/abstract_graph.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(self, prompt: str, config: dict, source: Optional[str] = None):
4545
self.config = config
4646
self.llm_model = self._create_llm(config["llm"], chat=True)
4747
self.embedder_model = self._create_default_embedder(llm_config=config["llm"]
48-
) if "embeddings" not in config else self._create_embedder(
48+
) if "embeddings" not in config else self._create_embedder(
4949
config["embeddings"])
5050

5151
# Create the graph
@@ -54,7 +54,8 @@ def __init__(self, prompt: str, config: dict, source: Optional[str] = None):
5454
self.execution_info = None
5555

5656
# Set common configuration parameters
57-
self.verbose = False if config is None else config.get("verbose", False)
57+
self.verbose = False if config is None else config.get(
58+
"verbose", False)
5859
self.headless = True if config is None else config.get(
5960
"headless", True)
6061
common_params = {"headless": self.headless,
@@ -63,11 +64,10 @@ def __init__(self, prompt: str, config: dict, source: Optional[str] = None):
6364
"embedder_model": self.embedder_model}
6465
self.set_common_params(common_params, overwrite=False)
6566

66-
6767
def set_common_params(self, params: dict, overwrite=False):
6868
"""
6969
Pass parameters to every node in the graph unless otherwise defined in the graph.
70-
70+
7171
Args:
7272
params (dict): Common parameters and their values.
7373
"""
@@ -89,7 +89,6 @@ def _set_model_token(self, llm):
8989
self.model_token = models_tokens['mistral'][llm.repo_id]
9090
except KeyError:
9191
raise KeyError("Model not supported")
92-
9392
elif 'Google' in str(type(llm)):
9493
try:
9594
if 'gemini' in llm.model:
@@ -216,7 +215,8 @@ def _create_default_embedder(self, llm_config=None) -> object:
216215
ValueError: If the model is not supported.
217216
"""
218217
if isinstance(self.llm_model, Gemini):
219-
return GoogleGenerativeAIEmbeddings(google_api_key=llm_config['api_key'], model="models/embedding-001")
218+
return GoogleGenerativeAIEmbeddings(google_api_key=llm_config['api_key'],
219+
model="models/embedding-001")
220220
if isinstance(self.llm_model, OpenAI):
221221
return OpenAIEmbeddings(api_key=self.llm_model.openai_api_key)
222222
elif isinstance(self.llm_model, AzureOpenAIEmbeddings):

0 commit comments

Comments
 (0)