add main.py
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
import os, asyncio
|
import os
|
||||||
|
import asyncio
|
||||||
from langchain_openai import ChatOpenAI
|
from langchain_openai import ChatOpenAI
|
||||||
from langchain_core.messages import HumanMessage
|
from langchain_core.messages import HumanMessage
|
||||||
from langchain.tools import tool
|
from langchain.tools import tool
|
||||||
from deepagents import create_deep_agent
|
from deepagents import create_deep_agent
|
||||||
from deepagents.backends import FilesystemBackend, LocalShellBackend, CompositeBackend
|
from deepagents.backends import FilesystemBackend, CompositeBackend
|
||||||
|
|
||||||
|
# LLM configuration using OpenRouter
|
||||||
llm = ChatOpenAI(
|
llm = ChatOpenAI(
|
||||||
model="openai/gpt-oss-20b:free",
|
model="openai/gpt-oss-20b:free",
|
||||||
base_url="https://openrouter.ai/api/v1",
|
base_url="https://openrouter.ai/api/v1",
|
||||||
@@ -12,14 +14,14 @@ llm = ChatOpenAI(
|
|||||||
temperature=0.0,
|
temperature=0.0,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Backend: real filesystem in current directory
|
||||||
backend = CompositeBackend([
|
backend = CompositeBackend([
|
||||||
LocalShellBackend(workspace_dir="./workspace"),
|
|
||||||
FilesystemBackend(),
|
FilesystemBackend(),
|
||||||
])
|
])
|
||||||
|
|
||||||
@tool
|
@tool
|
||||||
def web_search(query: str) -> str:
|
async def web_search(query: str) -> str:
|
||||||
"""Search the web for information.""
|
"""Search the web for information using DuckDuckGo."""
|
||||||
try:
|
try:
|
||||||
from duckduckgo_search import DDGS
|
from duckduckgo_search import DDGS
|
||||||
with DDGS() as ddgs:
|
with DDGS() as ddgs:
|
||||||
@@ -28,14 +30,25 @@ def web_search(query: str) -> str:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return f"Search error: {e}"
|
return f"Search error: {e}"
|
||||||
|
|
||||||
|
@tool
|
||||||
|
def write_file(path: str, content: str) -> str:
|
||||||
|
"""Write a file to the local filesystem."""
|
||||||
|
try:
|
||||||
|
with open(path, 'w', encoding='utf-8') as f:
|
||||||
|
f.write(content)
|
||||||
|
return f"File written to {path}"
|
||||||
|
except Exception as e:
|
||||||
|
return f"Error writing file: {e}"
|
||||||
|
|
||||||
agent = create_deep_agent(
|
agent = create_deep_agent(
|
||||||
llm=llm,
|
llm=llm,
|
||||||
tools=[web_search],
|
tools=[web_search, write_file],
|
||||||
backend=backend,
|
backend=backend,
|
||||||
system_prompt="You are a helpful research agent.",
|
system_prompt="You are a helpful research assistant. Your task is to search the web for information on a given topic and then create virtual files with the results. At the end of the conversation, ensure all generated files are written to disk.",
|
||||||
)
|
)
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
|
# Example interaction: ask about Python best practices
|
||||||
result = await agent.ainvoke(
|
result = await agent.ainvoke(
|
||||||
{"messages": [HumanMessage(content="Search for Python best practices and save to results.txt")]},
|
{"messages": [HumanMessage(content="Search for Python best practices and save to results.txt")]},
|
||||||
{"configurable": {"thread_id": "session-1"}},
|
{"configurable": {"thread_id": "session-1"}},
|
||||||
@@ -43,4 +56,4 @@ async def main():
|
|||||||
print(result["messages"][-1].content)
|
print(result["messages"][-1].content)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
|
|||||||
Reference in New Issue
Block a user