mirror of
https://github.com/FDKost/src.git
synced 2026-03-10 18:55:16 +03:00
another changes in hitl
I dont remember what was changed right now :)
This commit is contained in:
BIN
hitl-advanced/__pycache__/hitl.cpython-313.pyc
Normal file
BIN
hitl-advanced/__pycache__/hitl.cpython-313.pyc
Normal file
Binary file not shown.
118
hitl-advanced/hitl.py
Normal file
118
hitl-advanced/hitl.py
Normal file
@@ -0,0 +1,118 @@
|
||||
from langchain.agents import create_agent
|
||||
from langchain_openai import ChatOpenAI
|
||||
from pydantic import SecretStr
|
||||
from langchain.tools import tool
|
||||
|
||||
llm = ChatOpenAI(
|
||||
model="openai/gpt-oss-20b",
|
||||
base_url="http://10.191.8.47:1234/v1",
|
||||
api_key=SecretStr("fake"),
|
||||
temperature=0.7,
|
||||
)
|
||||
|
||||
|
||||
@tool
|
||||
def get_weather(city: str, date: str) -> str:
|
||||
"""Это инструмент для получения погоды в указанном городе"""
|
||||
|
||||
weather_agent = create_agent(
|
||||
model=llm,
|
||||
system_prompt="""
|
||||
Требуется предоставить прогнооз погоды для указанного города в виде таблицы
|
||||
|Дата|Температура|Ветер|Давление|
|
||||
|
||||
на указанную дату. Если данных нет сформируй реалистичный ответ, заполни все ячейки таблицы.
|
||||
Сделай прогноз на основании исторических тенденций
|
||||
""",
|
||||
)
|
||||
|
||||
answer = weather_agent.invoke(
|
||||
{
|
||||
"messages": [
|
||||
{
|
||||
"role": "human",
|
||||
"content": f"Какая погода в городе {city} на дату {date}?",
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
return answer["messages"][-1].content
|
||||
|
||||
|
||||
agent = create_agent(
|
||||
model=llm,
|
||||
tools=[get_weather],
|
||||
system_prompt="Ты полезный ассистент",
|
||||
interrupt_before=['tools']
|
||||
)
|
||||
|
||||
|
||||
def format_message(message) -> str:
|
||||
"""Форматирует одно сообщение для вывода (контент или вызов инструмента)."""
|
||||
if message.content:
|
||||
return message.content
|
||||
|
||||
return f"{message.tool_calls[0]['name']}({message.tool_calls[0]['args']})"
|
||||
|
||||
|
||||
step = 1
|
||||
|
||||
|
||||
def format_chunk_message(chunk):
|
||||
"""Форматирует одно сообщение для вывода (контент или вызов инструмента)."""
|
||||
message, meta = chunk
|
||||
global step
|
||||
|
||||
if meta["langgraph_step"] != step:
|
||||
step = meta["langgraph_step"]
|
||||
print("\n --- --- --- \n")
|
||||
|
||||
if message.content:
|
||||
print(message.content, end="", flush=False)
|
||||
|
||||
|
||||
# answer = llm.invoke(input='Привет')
|
||||
config = {"configurable": {"thread_id": "someThread"}}
|
||||
|
||||
|
||||
def ask_and_run(user_unput: str, config):
|
||||
for chunk in agent.stream(
|
||||
{"messages": [{"role": "human", "content": user_unput}]},
|
||||
config=config,
|
||||
stream_mode=["messages", "updates"],
|
||||
):
|
||||
chunk_type, chunk_data = chunk
|
||||
if chunk_type == "messages":
|
||||
format_chunk_message(chunk_data)
|
||||
|
||||
if chunk_type == "updates":
|
||||
if chunk_data.get("model", None):
|
||||
print(
|
||||
format_message(chunk_data["model"]["messages"][-1]), sep="\n---\n"
|
||||
)
|
||||
|
||||
|
||||
# while True:
|
||||
# user_input = input("\nВы: ")
|
||||
# if user_input == "exit":
|
||||
# break
|
||||
|
||||
# ask_and_run(user_input, config)
|
||||
|
||||
if __name__ == "__main__":
|
||||
while True:
|
||||
user_input = input("\nВы: ")
|
||||
if user_input == "exit":
|
||||
break
|
||||
|
||||
ask_and_run(user_input, config)
|
||||
# for chunk in stream:
|
||||
# chunk_type, chunk_data = chunk
|
||||
|
||||
|
||||
# print('---')
|
||||
# print(*[format_message(m) for m in answer['messages']], sep='\n---\n')
|
||||
# print('---')
|
||||
|
||||
# print(answer.content)
|
||||
Reference in New Issue
Block a user