|
| 1 | +""" |
| 2 | +Example of Search Graph |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +from dotenv import load_dotenv |
| 7 | +load_dotenv() |
| 8 | + |
| 9 | +from scrapegraphai.graphs import SearchGraph |
| 10 | +from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info |
| 11 | + |
| 12 | +from pydantic import BaseModel, Field |
| 13 | +from typing import List |
| 14 | +from langchain_openai import AzureChatOpenAI |
| 15 | +from langchain_openai import AzureOpenAIEmbeddings |
| 16 | + |
| 17 | +# ************************************************ |
| 18 | +# Define the output schema for the graph |
| 19 | +# ************************************************ |
| 20 | + |
| 21 | +class Dish(BaseModel): |
| 22 | + name: str = Field(description="The name of the dish") |
| 23 | + description: str = Field(description="The description of the dish") |
| 24 | + |
| 25 | +class Dishes(BaseModel): |
| 26 | + dishes: List[Dish] |
| 27 | + |
| 28 | +# ************************************************ |
| 29 | +# Define the configuration for the graph |
| 30 | +# ************************************************ |
| 31 | + |
| 32 | + |
| 33 | +llm_model_instance = AzureChatOpenAI( |
| 34 | + openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"], |
| 35 | + azure_deployment=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT_NAME"] |
| 36 | +) |
| 37 | + |
| 38 | +embedder_model_instance = AzureOpenAIEmbeddings( |
| 39 | + azure_deployment=os.environ["AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME"], |
| 40 | + openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"], |
| 41 | +) |
| 42 | + |
| 43 | +# ************************************************ |
| 44 | +# Create the SmartScraperGraph instance and run it |
| 45 | +# ************************************************ |
| 46 | + |
| 47 | +graph_config = { |
| 48 | + "llm": {"model_instance": llm_model_instance}, |
| 49 | + "embeddings": {"model_instance": embedder_model_instance} |
| 50 | +} |
| 51 | + |
| 52 | +# ************************************************ |
| 53 | +# Create the SearchGraph instance and run it |
| 54 | +# ************************************************ |
| 55 | + |
| 56 | +search_graph = SearchGraph( |
| 57 | + prompt="List me Chioggia's famous dishes", |
| 58 | + config=graph_config, |
| 59 | + schema=Dishes |
| 60 | +) |
| 61 | + |
| 62 | +result = search_graph.run() |
| 63 | +print(result) |
| 64 | + |
| 65 | +# ************************************************ |
| 66 | +# Get graph execution info |
| 67 | +# ************************************************ |
| 68 | + |
| 69 | +graph_exec_info = search_graph.get_execution_info() |
| 70 | +print(prettify_exec_info(graph_exec_info)) |
| 71 | + |
| 72 | +# Save to json and csv |
| 73 | +convert_to_csv(result, "result") |
| 74 | +convert_to_json(result, "result") |
0 commit comments