Reputation: 388
I have created a simple agent that takes a text and extracts specific nouns using a tool. here is my code:
potential_business_object_extractor is the tool Im using. I created two simple states for input and output and wrote a function for a specific node that takes the business_description and uses a tool to return a list of objects.
def potential_business_object_extractor(text: str) -> list:
"""
Extract business objects from the text description using dependency parsing and POS tagging.
"""
# Process the text using Stanza
nlp = stanza.Pipeline(lang='en')
doc = nlp(text)
objects = []
# Step 1: For each sentence in the text, identify nouns and noun phrases
for sentence in doc.sentences:
for word in sentence.words:
# Step 2: Identify nouns and filter out irrelevant ones
if word.upos == 'NOUN' and not is_irrelevant_noun(word):
lemmatized_noun = word.lemma
objects.append(lemmatized_noun)
# Step 3: Remove duplicates
unique_objects = list(set(objects))
return unique_objects
def is_irrelevant_noun(word):
"""
Determine if a noun is irrelevant (e.g., generic terms or certain business jargon).
"""
# Exclude gerunds (ending in -ing), adverbs, and overly generic nouns
return (
word.text.endswith('ing') or
word.upos == 'ADV'
)
llm = ChatOpenAI(model="gpt-4o", temperature=0.4)
class BusinessObjectState(TypedDict):
business_description: str
class Output(BaseModel):
object_list: list = Field(
description="a list of potential business objects",
)
tools = [potential_business_object_extractor]
llm_with_tool = llm.bind_tools(tools)
def object_extractor_node(state: BusinessObjectState):
business_doc = BusinessObjectState['business_description']
prompt = """
you are a business analyst and your task is to extract potential business objects from a business document.
you have access to a set of tools for accomplishing this task
document: {business_description}
"""
sys_mes = prompt.format(business_description=business_doc)
structured_llm = llm.with_structured_output(Output)
object_list = structured_llm.invoke([SystemMessage(content=sys_mes)])
return {'object_list': object_list.object_list}
builder = StateGraph(BusinessObjectState)
builder.add_node('object_extractor_node', object_extractor_node)
builder.add_node('tools', ToolNode(tools))
builder.add_edge(START, 'object_extractor_node')
builder.add_conditional_edges('object_extractor_node', tools_condition)
builder.add_edge('tools', END)
# Compile graph
graph = builder.compile()
biz = """Café Bliss is more than just a coffee shop; it’s a sanctuary for those who appreciate the finer things in life. Nestled in prime locations within bustling urban centers, each of our cafés is designed to be a warm and inviting space where customers can escape the rush of everyday life. The ambiance, crafted with cozy seating, ambient lighting, and a touch of modern elegance, reflects our commitment to creating a welcoming environment that feels like home, no matter where you are."""
graph.invoke({'business_description': biz})
but when I try to invoke I receive this error:
InvalidUpdateError: Must write to at least one of ['business_description']
can you help me please? I dont know what Im doing wrong
I tried changing the state from typed dict to a pydantic BaseModel but nothing changed
Upvotes: 0
Views: 297