Skip to content

Python scraper based on AI

add new search graph examples

VinciGit002258fe5ac0fd
diff --git a/examples/azure/search_graph_azure.py b/examples/azure/search_graph_azure.py
new file mode 100644
index 00000000..fe8efb31
--- /dev/null
+++ b/examples/azure/search_graph_azure.py
@@ -0,0 +1,64 @@
+"""
+Example of Search Graph
+"""
+
+import os
+from dotenv import load_dotenv
+from langchain_openai import AzureChatOpenAI
+from langchain_openai import AzureOpenAIEmbeddings
+from scrapegraphai.graphs import SearchGraph
+from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
+load_dotenv()
+
+FILE_NAME = "inputs/example.json"
+curr_dir = os.path.dirname(os.path.realpath(__file__))
+file_path = os.path.join(curr_dir, FILE_NAME)
+
+with open(file_path, 'r', encoding="utf-8") as file:
+ text = file.read()
+
+# ************************************************
+# Initialize the model instances
+# ************************************************
+
+llm_model_instance = AzureChatOpenAI(
+ openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],
+ azure_deployment=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT_NAME"]
+)
+
+embedder_model_instance = AzureOpenAIEmbeddings(
+ azure_deployment=os.environ["AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME"],
+ openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],
+)
+
+# ************************************************
+# Create the JSONScraperGraph instance and run it
+# ************************************************
+
+graph_config = {
+ "llm": {"model_instance": llm_model_instance},
+ "embeddings": {"model_instance": embedder_model_instance}
+}
+
+# ************************************************
+# Create the SearchGraph instance and run it
+# ************************************************
+
+search_graph = SearchGraph(
+ prompt="List me the best escursions near Trento",
+ config=graph_config
+)
+
+result = search_graph.run()
+print(result)
+
+# ************************************************
+# Get graph execution info
+# ************************************************
+
+graph_exec_info = search_graph.get_execution_info()
+print(prettify_exec_info(graph_exec_info))
+
+# Save to json and csv
+convert_to_csv(result, "result")
+convert_to_json(result, "result")
diff --git a/examples/gemini/search_graph_gemini.py b/examples/gemini/search_graph_gemini.py
index d213cf38..a985f5f3 100644
--- a/examples/gemini/search_graph_gemini.py
+++ b/examples/gemini/search_graph_gemini.py
@@ -21,6 +21,8 @@ graph_config = {
"temperature": 0,
"streaming": True
},
+ "max_results": 5,
+ "verbose": True,
}
# ************************************************
diff --git a/examples/local_models/Docker/search_graph_docker.py b/examples/local_models/Docker/search_graph_docker.py
index 59770c11..1db5c207 100644
--- a/examples/local_models/Docker/search_graph_docker.py
+++ b/examples/local_models/Docker/search_graph_docker.py
@@ -3,7 +3,7 @@ Example of Search Graph
"""
from scrapegraphai.graphs import SearchGraph
-from scrapegraphai.utils import convert_to_csv, convert_to_json
+from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
# ************************************************
# Define the configuration for the graph
@@ -20,7 +20,9 @@ graph_config = {
"embeddings": {
"model": "ollama/nomic-embed-text",
"temperature": 0,
- }
+ },
+ "max_results": 5,
+ "verbose": True,
}
# ************************************************
@@ -28,13 +30,20 @@ graph_config = {
# ************************************************
search_graph = SearchGraph(
- prompt="List me all the regions of Italy.",
+ prompt="List me the best escursions near Trento",
config=graph_config
)
result = search_graph.run()
print(result)
+# ************************************************
+# Get graph execution info
+# ************************************************
+
+graph_exec_info = search_graph.get_execution_info()
+print(prettify_exec_info(graph_exec_info))
+
# Save to json and csv
convert_to_csv(result, "result")
convert_to_json(result, "result")
diff --git a/examples/local_models/Ollama/search_graph_ollama.py b/examples/local_models/Ollama/search_graph_ollama.py
new file mode 100644
index 00000000..cfa73e3e
--- /dev/null
+++ b/examples/local_models/Ollama/search_graph_ollama.py
@@ -0,0 +1,50 @@
+"""
+Example of Search Graph
+"""
+from scrapegraphai.graphs import SearchGraph
+from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+
+
+graph_config = {
+ "llm": {
+ "model": "ollama/mistral",
+ "temperature": 0,
+ "format": "json", # Ollama needs the format to be specified explicitly
+ # "model_tokens": 2000, # set context length arbitrarily,
+ "base_url": "http://localhost:11434", # set ollama URL arbitrarily
+ },
+ "embeddings": {
+ "model": "ollama/nomic-embed-text",
+ "temperature": 0,
+ "base_url": "http://localhost:11434", # set ollama URL arbitrarily
+ },
+ "max_results": 5,
+ "verbose": True,
+}
+
+# ************************************************
+# Create the SearchGraph instance and run it
+# ************************************************
+
+search_graph = SearchGraph(
+ prompt="List me the best escursions near Trento",
+ config=graph_config
+)
+
+result = search_graph.run()
+print(result)
+
+# ************************************************
+# Get graph execution info
+# ************************************************
+
+graph_exec_info = search_graph.get_execution_info()
+print(prettify_exec_info(graph_exec_info))
+
+# Save to json and csv
+convert_to_csv(result, "result")
+convert_to_json(result, "result")
diff --git a/examples/openai/search_graph_multi.py b/examples/openai/search_graph_multi.py
deleted file mode 100644
index 962397c7..00000000
--- a/examples/openai/search_graph_multi.py
+++ /dev/null
@@ -1,98 +0,0 @@
-"""
-Example of custom graph using existing nodes
-"""
-
-import os
-from dotenv import load_dotenv
-from langchain_openai import OpenAIEmbeddings
-from scrapegraphai.models import OpenAI
-from scrapegraphai.graphs import BaseGraph, SmartScraperGraph
-from scrapegraphai.nodes import SearchInternetNode, GraphIteratorNode, MergeAnswersNode
-load_dotenv()
-
-# ************************************************
-# Define the configuration for the graph
-# ************************************************
-
-openai_key = os.getenv("OPENAI_APIKEY")
-
-graph_config = {
- "llm": {
- "api_key": openai_key,
- "model": "gpt-3.5-turbo",
- },
-}
-
-# ************************************************
-# Create a SmartScraperGraph instance
-# ************************************************
-
-smart_scraper_graph = SmartScraperGraph(
- prompt="",
- source="",
- config=graph_config
-)
-
-# ************************************************
-# Define the graph nodes
-# ************************************************
-
-llm_model = OpenAI(graph_config["llm"])
-embedder = OpenAIEmbeddings(api_key=llm_model.openai_api_key)
-
-search_internet_node = SearchInternetNode(
- input="user_prompt",
- output=["urls"],
- node_config={
- "llm_model": llm_model,
- "max_results": 5, # num of search results to fetch
- "verbose": True,
- }
-)
-
-graph_iterator_node = GraphIteratorNode(
- input="user_prompt & urls",
- output=["results"],
- node_config={
- "graph_instance": smart_scraper_graph,
- "verbose": True,
- }
-)
-
-merge_answers_node = MergeAnswersNode(
- input="user_prompt & results",
- output=["answer"],
- node_config={
- "llm_model": llm_model,
- "verbose": True,
- }
-)
-
-# ************************************************
-# Create the graph by defining the connections
-# ************************************************
-
-graph = BaseGraph(
- nodes=[
- search_internet_node,
- graph_iterator_node,
- merge_answers_node
- ],
- edges=[
- (search_internet_node, graph_iterator_node),
- (graph_iterator_node, merge_answers_node)
- ],
- entry_point=search_internet_node
-)
-
-# ************************************************
-# Execute the graph
-# ************************************************
-
-result, execution_info = graph.execute({
- "user_prompt": "List me all the typical Chioggia dishes."
-})
-
-# get the answer from the result
-result = result.get("answer", "No answer found.")
-print(result)
diff --git a/scrapegraphai/graphs/abstract_graph.py b/scrapegraphai/graphs/abstract_graph.py
index e81af701..b015907c 100644
--- a/scrapegraphai/graphs/abstract_graph.py
+++ b/scrapegraphai/graphs/abstract_graph.py
@@ -45,7 +45,7 @@ class AbstractGraph(ABC):
self.config = config
self.llm_model = self._create_llm(config["llm"], chat=True)
self.embedder_model = self._create_default_embedder(llm_config=config["llm"]
- ) if "embeddings" not in config else self._create_embedder(
+ ) if "embeddings" not in config else self._create_embedder(
config["embeddings"])
# Create the graph
@@ -54,7 +54,8 @@ class AbstractGraph(ABC):
self.execution_info = None
# Set common configuration parameters
- self.verbose = False if config is None else config.get("verbose", False)
+ self.verbose = False if config is None else config.get(
+ "verbose", False)
self.headless = True if config is None else config.get(
"headless", True)
common_params = {"headless": self.headless,
@@ -63,11 +64,10 @@ class AbstractGraph(ABC):
"embedder_model": self.embedder_model}
self.set_common_params(common_params, overwrite=False)
-
def set_common_params(self, params: dict, overwrite=False):
"""
Pass parameters to every node in the graph unless otherwise defined in the graph.
-
+
Args:
params (dict): Common parameters and their values.
"""
@@ -89,7 +89,6 @@ class AbstractGraph(ABC):
self.model_token = models_tokens['mistral'][llm.repo_id]
except KeyError:
raise KeyError("Model not supported")
-
elif 'Google' in str(type(llm)):
try:
if 'gemini' in llm.model:
@@ -216,7 +215,8 @@ class AbstractGraph(ABC):
ValueError: If the model is not supported.
"""
if isinstance(self.llm_model, Gemini):
- return GoogleGenerativeAIEmbeddings(google_api_key=llm_config['api_key'], model="models/embedding-001")
+ return GoogleGenerativeAIEmbeddings(google_api_key=llm_config['api_key'],
+ model="models/embedding-001")
if isinstance(self.llm_model, OpenAI):
return OpenAIEmbeddings(api_key=self.llm_model.openai_api_key)
elif isinstance(self.llm_model, AzureOpenAIEmbeddings):