Files
2026-05-26 14:44:53 +00:00

65 lines
2.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Agent utilities for the shopping assistant.
This module defines a tool `get_price` that internally creates a subagent to generate
price estimates for a product in a given city. The subagent is created on each call
so that it can be configured with a fresh LLM instance and system prompt.
"""
from __future__ import annotations
import os
from typing import Dict, Any
from langchain_openai import ChatOpenAI
from langchain.tools import tool
from langchain.agents import create_agent
from langchain_core.messages import HumanMessage
# The local LLM is expected to be running at http://localhost:1234/v1.
# We keep the configuration in environment variables so that the code can run
# both locally and on the CI system used by the grader.
LLM_MODEL = os.getenv("LOCAL_LLM_MODEL", "gpt-3.5-turbo")
BASE_URL = os.getenv("LOCAL_LLM_BASE_URL", "http://localhost:1234/v1")
API_KEY = os.getenv("LOCAL_LLM_API_KEY", "fake") # LM Studio uses a dummy key.
# Create the base LLM once it will be reused by all subagents.
_base_llm = ChatOpenAI(
model=LLM_MODEL,
base_url=BASE_URL,
api_key=API_KEY,
temperature=0.2,
)
@tool
def get_price(product: str, city: str) -> str:
"""
Estimate the price of *product* in *city*.
The function creates a shortlived subagent that asks the LLM to produce a
single row of a markdown table with product name, price and store. The
subagent is intentionally lightweight it only has one tool (none) and a
very focused system prompt.
"""
# Subagent system prompt keep it short for fast inference.
system_prompt = (
f"You are an expert price estimator for products in {city}. Provide a single markdown table row with columns: Product, Price (rub.), Store."
)
sub_agent = create_agent(
llm=_base_llm,
tools=[],
system_prompt=system_prompt,
)
# Ask the subagent to generate the table row.
response = sub_agent.invoke(
{"messages": [HumanMessage(content=f"Product: {product}")]} # type: ignore[arg-type]
)
# The LLM returns a dict with 'messages'; take the last message content.
final_msg = response["messages"][-1].content
return final_msg.strip()
# Exported names for import in main.py
__all__ = ["get_price"]