|
| 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 | + |
| 15 | +# ************************************************ |
| 16 | +# Define the output schema for the graph |
| 17 | +# ************************************************ |
| 18 | + |
| 19 | +class Dish(BaseModel): |
| 20 | + name: str = Field(description="The name of the dish") |
| 21 | + description: str = Field(description="The description of the dish") |
| 22 | + |
| 23 | +class Dishes(BaseModel): |
| 24 | + dishes: List[Dish] |
| 25 | + |
| 26 | +# ************************************************ |
| 27 | +# Define the configuration for the graph |
| 28 | +# ************************************************ |
| 29 | + |
| 30 | +openai_key = os.getenv("OPENAI_APIKEY") |
| 31 | + |
| 32 | +graph_config = { |
| 33 | + "llm": { |
| 34 | + "api_key": openai_key, |
| 35 | + "model": "gpt-3.5-turbo", |
| 36 | + }, |
| 37 | + "max_results": 2, |
| 38 | + "verbose": True, |
| 39 | +} |
| 40 | + |
| 41 | +# ************************************************ |
| 42 | +# Create the SearchGraph instance and run it |
| 43 | +# ************************************************ |
| 44 | + |
| 45 | +search_graph = SearchGraph( |
| 46 | + prompt="List me Chioggia's famous dishes", |
| 47 | + config=graph_config, |
| 48 | + schema=Dishes |
| 49 | +) |
| 50 | + |
| 51 | +result = search_graph.run() |
| 52 | +print(result) |
| 53 | + |
| 54 | +# ************************************************ |
| 55 | +# Get graph execution info |
| 56 | +# ************************************************ |
| 57 | + |
| 58 | +graph_exec_info = search_graph.get_execution_info() |
| 59 | +print(prettify_exec_info(graph_exec_info)) |
| 60 | + |
| 61 | +# Save to json and csv |
| 62 | +convert_to_csv(result, "result") |
| 63 | +convert_to_json(result, "result") |
0 commit comments