57 lines
1.8 KiB
Python
57 lines
1.8 KiB
Python
import os
|
|
import asyncio
|
|
from langchain_openai import ChatOpenAI
|
|
from langchain_core.messages import HumanMessage
|
|
from langchain.tools import tool
|
|
from deepagents import create_deep_agent
|
|
from deepagents.backends import FilesystemBackend, LocalShellBackend, CompositeBackend
|
|
|
|
# LLM via BroJS
|
|
llm = ChatOpenAI(
|
|
model="openai/gpt-oss-20b:free",
|
|
base_url="https://platform.brojs.ru/jrnl-bh/api/inference/v1",
|
|
api_key=os.getenv("JOURNAL_MCP_PAT"),
|
|
temperature=0.5,
|
|
)
|
|
|
|
# Backend: local shell + virtual FS
|
|
backend = CompositeBackend([
|
|
LocalShellBackend(workspace_dir="./workspace"),
|
|
FilesystemBackend(),
|
|
])
|
|
|
|
# Web search tool using duckduckgo-search
|
|
@tool
|
|
def web_search(query: str) -> str:
|
|
"""Search the web for information."""
|
|
try:
|
|
from duckduckgo_search import DDGS
|
|
with DDGS() as ddgs:
|
|
results = list(ddgs.text(query, max_results=5))
|
|
return "\n".join(f"{r['title']}: {r['body']}" for r in results)
|
|
except Exception as e:
|
|
return f"Search error: {e}"
|
|
|
|
# Deep agent
|
|
agent = create_deep_agent(
|
|
llm=llm,
|
|
tools=[web_search],
|
|
backend=backend,
|
|
system_prompt="You are a helpful research agent that can search the web and create virtual files. At the end, export files to the real filesystem.",
|
|
)
|
|
|
|
async def main():
|
|
# Example: ask agent to research "Python async" and create a file with results
|
|
user_query = "Python async programming" # can be replaced by user input
|
|
result = await agent.ainvoke(
|
|
{"messages": [HumanMessage(content=user_query)]},
|
|
{"configurable": {"thread_id": "session-1"}},
|
|
)
|
|
# The agent will create virtual files during its execution.
|
|
# After completion, export virtual FS to real FS
|
|
await backend.export_to_real_fs("./exported_files")
|
|
print("Exported virtual files to ./exported_files")
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|