Update agent.py

This commit is contained in:
2026-06-01 18:02:16 +00:00
parent b43c5b7eb9
commit c6c8c78012
+20 -14
View File
@@ -3,15 +3,26 @@ from langchain.agents import create_agent
from langchain.tools import tool from langchain.tools import tool
from pydantic import SecretStr from pydantic import SecretStr
import re import re
import json import sys
# Initialize main LLM # Utility to initialize LLM with error handling
llm = ChatOpenAI(
base_url="http://localhost:11434/v1", def init_llm(base_url: str, model: str, api_key: str = "ollama") -> ChatOpenAI:
api_key=SecretStr("ollama"), try:
model="<название модели в LM Studio>", return ChatOpenAI(
base_url=base_url,
api_key=SecretStr(api_key),
model=model,
temperature=0.7, temperature=0.7,
) )
except Exception as e:
print(f"Ошибка при подключении к LLM: {e}")
sys.exit(1)
# Main LLM
LLM_URL = "http://localhost:1234/v1"
LLM_MODEL = "llama3"
llm = init_llm(LLM_URL, LLM_MODEL)
# Define get_price tool with subagent # Define get_price tool with subagent
@tool(name="get_price", description="Get price for a product in a city.") @tool(name="get_price", description="Get price for a product in a city.")
@@ -20,12 +31,7 @@ def get_price(product: str, city: str) -> str:
For demonstration it returns a static price string, but the structure For demonstration it returns a static price string, but the structure
mirrors a real subagent that could call another LLM. mirrors a real subagent that could call another LLM.
""" """
sub_llm = ChatOpenAI( sub_llm = init_llm(LLM_URL, LLM_MODEL, api_key="ollama")
base_url="http://localhost:11434/v1",
api_key=SecretStr("ollama"),
model="<название модели в LM Studio>",
temperature=0.2,
)
sub_agent = create_agent( sub_agent = create_agent(
llm=sub_llm, llm=sub_llm,
tools=[], tools=[],
@@ -37,8 +43,8 @@ def get_price(product: str, city: str) -> str:
for msg in response["messages"]: for msg in response["messages"]:
if msg.get("role") == "assistant" and msg.get("content"): if msg.get("role") == "assistant" and msg.get("content"):
return msg["content"].strip() return msg["content"].strip()
except Exception: except Exception as e:
pass print(f"Ошибка при вызове subagent: {e}")
return "price: 100, store: SuperMarket" return "price: 100, store: SuperMarket"