Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,31 @@
Je bent een behulpzame assistent die helpt navigeren in het openbaar vervoer.
Je geeft beknopt maar vriendelijk antwoord.
Beëindig het gesprek niet vroegtijdig.
Je geeft reisadvies, of met de trein of met de fiets, afhankelijk van de beschikbare tools.
Hou het kort zonder vragen en opmerkingen.
Als het laatste bericht van de LLM is, dan zoek het laatste bericht van de klant op en beantwoord dat in plaats van het bericht van de LLM.
Geef treinadvies als dat nog niet eerdere is gegeven, anders geef fietsadvies.
"""
)

)

@tool
def get_departure_times(origin: str, destination: str):
"""Retrieve departure times for a specified journey."""
def get_departure_times(origin: str, destination: str):
"""Retrieve train departure times for a specified journey."""
if not origin or not destination:
raise ValueError("Both origin and destination are required.")

# Simulated response (In a real application, integrate with an API)
return f"De vertrektijden vanaf {origin} met bestemming {destination} zijn om 10:00, 10:30 en 11:00."

@tool
def get_bicycle_route(origin: str, destination: str):
"""Retrieve bicycle route for a specified journey."""
if not origin or not destination:
raise ValueError("Both origin and destination are required.")

# Simulated response (In a real application, integrate with an API)
return f"De fietsroute vanaf {origin} met bestemming {destination} loopt door de Parallelweg en via de Afsluitdijk."


class Executor:
def __init__(
Expand All @@ -45,7 +57,7 @@ def __init__(
):
"""Initialize the executor with a language model-powered agent."""
self.model = model or ChatOpenAI(model="gpt-4o-mini", temperature=0.2)
self.tools = tools or [get_departure_times]
self.tools = tools or [get_departure_times, get_bicycle_route]
self.config = config or {
"configurable": {
"thread_id": str(uuid.uuid4()),
Expand Down
20 changes: 17 additions & 3 deletions agent/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
async def on_message(msg: cl.Message):
config = {"configurable": {"thread_id": cl.context.session.id}}
cb = cl.LangchainCallbackHandler()
final_answer = cl.Message(content="")

answer1 = cl.Message(content="")
executor = Executor(config=config)
async with executor.agent_context() as graph:
message_stream = graph.astream(
Expand All @@ -19,6 +20,19 @@ async def on_message(msg: cl.Message):
)
async for msg, metadata in message_stream:
if msg.content and isinstance(msg, AIMessage):
await final_answer.stream_token(msg.content)
await answer1.stream_token(msg.content)

await final_answer.send()
answer2 = cl.Message(content="")
executor = Executor(config=config)
async with executor.agent_context() as graph:
message_stream = graph.astream(
{"messages": [HumanMessage(content=msg.content)]},
stream_mode="messages",
config=RunnableConfig(callbacks=[cb], **config)
)
async for msg, metadata in message_stream:
if msg.content and isinstance(msg, AIMessage):
await answer2.stream_token(msg.content)

await answer1.send()
await answer2.send()