diff --git a/agent.py b/agent.py index dccd4c7..9b4a717 100644 --- a/agent.py +++ b/agent.py @@ -3,15 +3,26 @@ from langchain.agents import create_agent from langchain.tools import tool from pydantic import SecretStr import re -import json +import sys -# Initialize main LLM -llm = ChatOpenAI( - base_url="http://localhost:11434/v1", - api_key=SecretStr("ollama"), - model="<название модели в LM Studio>", - temperature=0.7, -) +# Utility to initialize LLM with error handling + +def init_llm(base_url: str, model: str, api_key: str = "ollama") -> ChatOpenAI: + try: + return ChatOpenAI( + base_url=base_url, + api_key=SecretStr(api_key), + model=model, + 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 @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 mirrors a real subagent that could call another LLM. """ - sub_llm = ChatOpenAI( - base_url="http://localhost:11434/v1", - api_key=SecretStr("ollama"), - model="<название модели в LM Studio>", - temperature=0.2, - ) + sub_llm = init_llm(LLM_URL, LLM_MODEL, api_key="ollama") sub_agent = create_agent( llm=sub_llm, tools=[], @@ -37,8 +43,8 @@ def get_price(product: str, city: str) -> str: for msg in response["messages"]: if msg.get("role") == "assistant" and msg.get("content"): return msg["content"].strip() - except Exception: - pass + except Exception as e: + print(f"Ошибка при вызове subagent: {e}") return "price: 100, store: SuperMarket"