Skip to content

Instantly share code, notes, and snippets.

@pchalasani
Last active November 8, 2025 23:03
Show Gist options
  • Select an option

  • Save pchalasani/509dc2bc254b09b4637896b9ae26269d to your computer and use it in GitHub Desktop.

Select an option

Save pchalasani/509dc2bc254b09b4637896b9ae26269d to your computer and use it in GitHub Desktop.
m10 code assignment: file_assistant.py
"""
Task 1-2 Part 2: File Assistant Agent
Create an agent that can use the file tools to perform operations
based on natural language requests. This agent will interpret user
intent and use the appropriate tools.
Complete the TODOs below to implement the file assistant.
"""
import os
from dotenv import load_dotenv
import langroid as lr
import langroid.language_models as lm
from langroid.agent.tools.orchestration import DoneTool
from file_tools import ListDirTool, ReadFileTool, WriteFileTool
# Load environment variables from .env file
load_dotenv()
# Get model from environment, default to Gemini if not set
CHAT_MODEL = os.getenv("OPENAI_CHAT_MODEL", "gemini/gemini-2.5-flash")
class FileAssistantConfig(lr.ChatAgentConfig):
"""Configuration for the File Assistant agent."""
# TODO 1: Set a descriptive name for the agent
name: str = "TODO: Add agent name"
# TODO 2: Configure the LLM
# Hint: Use lm.OpenAIGPTConfig with chat_model=CHAT_MODEL, max_output_tokens=1000
llm = None # Replace with proper configuration
# TODO 3: Nudge the LLM to use tools when it forgets
# IMPORTANT: This nudges the LLM to use a tool when it forgets
handle_llm_no_tool:str = f"""
You FORGOT to use one of your TOOLs! Remember that:
- TODO: in each bullet, list a tool and its purpose
"""
# TODO 4: Write a system message that:
# - Explains the agent is a helpful file assistant
# - Lists available tools (use tool.name() to get actual names):
# - ListDirTool for listing directory contents
# - ReadFileTool for reading files
# - WriteFileTool for writing files
# - DoneTool to signal completion and return results in `content` field
# - Provides guidance on how to use tools appropriately
# - Instructs to provide clear, helpful responses
# - IMPORTANT: Must use DoneTool to return the response in detail.
# Regarding naming the tools, note that the agent is unaware of the class names of
# the tools, so you have to get the `name()` method of the tool class to
# get the actual name, e.g. `ReadFileTool.name()`.
system_message: str = f"""
TODO: <Write your system message here; do NOT change the text below!>
When users ask to perform file operations:
1. Use an appropriate TOOL (possibly sequentially, one after the other) based on their request.
2. Once you have completed the task, use the TOOL {DoneTool.name()}
to indicate completion, with the `content` field containing the FULL response
that you want to provide to the user.
3. If an error occurs, use the TOOL {DoneTool.name()}
with the `content` field containing the error message.
NEVER OUTPUT plain-text messages; ALWAYS emit one of your TOOLs
Be concise but informative in your responses.
IMPORTANT: You CANNOT use multiple tools at once! Use one tool at a time,
wait for the result, and THEN decide what to do next.
CRITICAL:
When your task is complete,
- you MUST use the `{DoneTool.name()}` tool to indicate completion, AND
- you MUST use the `content` field to return any response you wish to
provide. This is ESSENTIAL since the user will NOT be able to see anything outside of
this tool! In fact it is best if you do NOT output ANY text OTHER THAN A TOOL.
"""
def run_file_assistant(prompt: str) -> str:
"""
Create and run a file assistant agent with the given prompt.
Args:
prompt: The user's request to the file assistant
Returns:
str: The agent's response
"""
# TODO 5: Create the agent configuration
config = None # Replace with FileAssistantConfig instance
# TODO 6: Create the ChatAgent
agent = None # Replace with lr.ChatAgent instance
# TODO 7: Enable the agent to use all file tools and the DoneTool
... # your code here
# TODO 8: Create a Task with the agent
# Hint: Set interactive=False for automated operation
task = None # Replace with lr.Task instance
# TODO 9: Run the task with the prompt
# IMPORTANT - you MUST set turns=10 in the run method, to avoid infinite loops
result = None # Replace with task run result
# TODO 10: Return the string representation of the result
# Note: result is a ChatDocument, which has a content attribute of type str
pass # Replace with return statement
# Example usage (uncomment to test)
# if __name__ == "__main__":
# # Test listing files
# response = run_file_assistant("List all files in the myfiles directory")
# print("List response:")
# print(response)
#
# # Test reading a file
# response = run_file_assistant("Read the file myfiles/beethoven.md")
# print("\nRead response:")
# print(response)
#
# # Test writing a file
# response = run_file_assistant("Write 'Hello, World!' to myfiles/test.txt")
# print("\nWrite response:")
# print(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment