import os import asyncio from langchain_openai import ChatOpenAI, OpenAIEmbeddings from langchain_chroma import Chroma 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 – always OpenRouter 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, ) # Embeddings and vector store for RAG embeddings = OpenAIEmbeddings( model="text-embedding-3-small", base_url="https://openrouter.ai/api/v1", api_key=os.getenv("OPENAI_API_KEY"), ) vector_store = Chroma(collection_name="knowledge", embedding_function=embeddings) # Backend for file operations – virtual mode so no real files are created backend = CompositeBackend( default=LocalShellBackend(root_dir="./workspace", virtual_mode=True, inherit_env=True), routes={}, ) # Tool that performs a vector search and returns the top 3 snippets @tool def rag_search(query: str) -> str: """Search the vector store for relevant documents and return a concise summary.""" docs = vector_store.similarity_search(query, k=3) if not docs: return "No relevant information found." snippets = "\n---\n".join(doc.page_content for doc in docs) return f"Top matches:\n{snippets}" # Create the deep agent with the RAG tool agent = create_deep_agent( model=llm, tools=[rag_search], backend=backend, system_prompt="You are a helpful assistant that uses a knowledge base to answer questions. Use the rag_search tool when you need external information.", ) async def main(): # Example user query user_query = "What are the main causes of climate change?" result = await agent.ainvoke( {"messages": [HumanMessage(content=user_query)]}, {"configurable": {"thread_id": "session-1"}}, ) # Print the final assistant message print(result["messages"][-1].content) if __name__ == "__main__": asyncio.run(main())