Scrapegraph-ai/examples/graph_examples/custom_graph_example.py
Lorenzo Padoan d0f4b0159f DEV gemini support for simple custom graph
-Not supported yet smartscrapergrapn with gemini
2024-03-14 19:35:04 +01:00

61 lines
1.5 KiB
Python

"""
Example of custom graph using existing nodes
"""
import os
from dotenv import load_dotenv
#from scrapegraphai.models import OpenAI
from scrapegraphai.models import Gemini
from scrapegraphai.graphs import BaseGraph
from scrapegraphai.nodes import FetchHTMLNode, ParseNode, GenerateAnswerNodeVanilla
load_dotenv()
# Define the configuration for the language model
""" openai_key = os.getenv("OPENAI_APIKEY")
llm_config = {
"api_key": openai_key,
"model_name": "gpt-3.5-turbo",
"temperature": 0,
"streaming": True
}
model = OpenAI(llm_config) """
gemini_key = os.getenv("GOOGLE_API_KEY")
llm_config = {
"api_key": gemini_key,
"model_name": "gemini-pro",
}
model = Gemini(llm_config)
# define the nodes for the graph
fetch_html_node = FetchHTMLNode("fetch_html")
parse_document_node = ParseNode(doc_type="html", chunks_size=4000, node_name="parse_document")
generate_answer_node = GenerateAnswerNodeVanilla(model, "generate_answer")
# create the graph
graph = BaseGraph(
nodes={
fetch_html_node,
parse_document_node,
generate_answer_node
},
edges={
(fetch_html_node, parse_document_node),
(parse_document_node,generate_answer_node)
},
entry_point=fetch_html_node
)
# execute the graph
inputs = {"user_input": "List me the projects with their description",
"url": "https://perinim.github.io/projects/"}
result = graph.execute(inputs)
# get the answer from the result
answer = result.get("answer", "No answer found.")
print(answer)