add main.py

This commit is contained in:
2026-05-26 12:16:00 +00:00
parent 0f2a4f9236
commit f575589014
+18 -40
View File
@@ -1,25 +1,26 @@
import os, asyncio
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 configuration
# LLM via BroJS
llm = ChatOpenAI(
model="openai/gpt-oss-20b:free",
base_url="https://openrouter.ai/api/v1",
api_key=os.getenv("OPENAI_API_KEY"),
temperature=0.0,
base_url="https://platform.brojs.ru/jrnl-bh/api/inference/v1",
api_key=os.getenv("JOURNAL_MCP_PAT"),
temperature=0.5,
)
# Backend: virtual FS + real shell
# Backend: local shell + virtual FS
backend = CompositeBackend([
LocalShellBackend(workspace_dir="./workspace"),
FilesystemBackend(),
])
# Tool: web search using duckduckgo-search
# Web search tool using duckduckgo-search
@tool
def web_search(query: str) -> str:
"""Search the web for information."""
@@ -31,48 +32,25 @@ def web_search(query: str) -> str:
except Exception as e:
return f"Search error: {e}"
# Tool: create virtual file in the virtual FS
@tool
def create_virtual_file(path: str, content: str) -> str:
"""Create a file in the virtual filesystem."""
try:
backend.write_file(path, content)
return f"Virtual file {path} created."
except Exception as e:
return f"Error creating virtual file: {e}"
# Tool: write virtual files to real filesystem
@tool
def write_to_real_fs() -> str:
"""Copy all files from virtual FS to the real workspace."""
try:
# FilesystemBackend stores files in memory; we iterate over its internal dict if available
if hasattr(backend, "_backend") and isinstance(backend._backend, FilesystemBackend):
fs = backend._backend
for path, content in fs._files.items():
dst = os.path.join("./real_files", path.lstrip("/"))
os.makedirs(os.path.dirname(dst), exist_ok=True)
with open(dst, "w", encoding="utf-8") as f:
f.write(content)
return "All virtual files written to real filesystem."
except Exception as e:
return f"Error writing to real FS: {e}"
# Create the deep agent
# Deep agent
agent = create_deep_agent(
llm=llm,
tools=[web_search, create_virtual_file, write_to_real_fs],
tools=[web_search],
backend=backend,
system_prompt="You are a deep agent that can search the web, create virtual files, and finally write them to the real filesystem.",
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 interaction: search, create file, write to real FS
# 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="Search for Python async programming examples and store results in a file called async_examples.txt")]},
{"messages": [HumanMessage(content=user_query)]},
{"configurable": {"thread_id": "session-1"}},
)
print(result["messages"][-1].content)
# 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())