Spaces:
Sleeping
Sleeping
File size: 3,811 Bytes
948e715 e0ec272 948e715 e0ec272 948e715 e0ec272 948e715 e0ec272 948e715 e0ec272 948e715 e0ec272 948e715 e0ec272 948e715 e0ec272 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
from typing import TypedDict, Optional
import os
from dotenv import load_dotenv
from google import genai
from langgraph.graph import StateGraph
from pydantic import Field , BaseModel
load_dotenv()
from google.genai.types import Tool, GenerateContentConfig, GoogleSearch, UrlContext
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
class SummaryResponse(BaseModel):
title: Optional[str] = Field(default=None , description="The title of the content")
category: Optional[str] = Field(default=None , description="The category of the content")
tags: Optional[list[str]] = Field(default=[] , description="The tags of the content")
references: Optional[list[str]] = Field(default=[] , description="The references of the content")
summary: Optional[str] = Field(default="" , description="The summary of the content")
tools = []
tools.append(Tool(url_context=UrlContext))
tools.append(Tool(google_search=GoogleSearch))
class State(TypedDict):
urls: Optional[list[str]] = Field(default=[])
codes: Optional[str] = Field(default="")
notes: Optional[str] = Field(default="")
summary_content: Optional[str] = Field(default="" , description="The summary of all urls, codes, and notes")
title: Optional[str] = Field(default="" , description="The title of the content")
category: Optional[str] = Field(default="" , description="The category of the content")
tags: Optional[list[str]] = Field(default=[] , description="The tags of the content")
references: Optional[list[str]] = Field(default=[] , description="The references of the content")
summary_response: Optional[SummaryResponse] = Field(default=None , description="The summary of the content")
def summarize_user_input(state: State) -> State:
client = genai.Client(api_key=GOOGLE_API_KEY)
response = client.models.generate_content(
model="gemini-2.5-flash-preview-05-20",
config=GenerateContentConfig(
tools=tools,
system_instruction="You are a helpful assistant that summarizes from urls, codes, and notes",
),
contents=f"""
Summarize the following urls, codes, and notes:
Urls: {
state["urls"]
}
Codes: {state["codes"]}
Notes: {state["notes"]}
And Give the complete summary to as blog to post on medium.com
Give the title of the blog.
Give the category of the blog.
Give the tags of the blog.
Search and always give five references from the internet to support the summary.
"""
)
summary_response = response.text
state["summary_response"] = summary_response
return state
def get_summary_response(state: State) -> State:
client = genai.Client(api_key=GOOGLE_API_KEY)
response = client.models.generate_content(
model="gemini-2.5-flash-preview-05-20",
contents="Structured the summary of the content . The summary is: " + state["summary_response"] + " and give the title, category, tags, and references",
config={
"response_mime_type": "application/json",
"response_schema": SummaryResponse,
},
)
summary : SummaryResponse = response.parsed
state["title"] = summary.title
state["category"] = summary.category
state["tags"] = summary.tags
state["references"] = summary.references
state["summary_content"] = summary.summary
return state
builder = StateGraph(State)
builder.add_node("summarize", summarize_user_input)
builder.add_node("get_summary_response", get_summary_response)
builder.add_edge("__start__", "summarize")
builder.add_edge("summarize", "get_summary_response")
builder.add_edge("get_summary_response", "__end__")
graph = builder.compile()
graph.name = "summarize_agent"
|