|
| 1 | +"""Demo implementation of an agent with Codegen tools.""" |
| 2 | + |
| 3 | +from langchain import hub |
| 4 | +from langchain.agents import AgentExecutor |
| 5 | +from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent |
| 6 | +from langchain_core.chat_history import ChatMessageHistory |
| 7 | +from langchain_core.runnables.history import RunnableWithMessageHistory |
| 8 | +from langchain_openai import ChatOpenAI |
| 9 | + |
| 10 | +from codegen import Codebase |
| 11 | +from codegen.sdk.enums import ProgrammingLanguage |
| 12 | + |
| 13 | +from .tools import ( |
| 14 | + CommitTool, |
| 15 | + CreateFileTool, |
| 16 | + DeleteFileTool, |
| 17 | + EditFileTool, |
| 18 | + ListDirectoryTool, |
| 19 | + MoveSymbolTool, |
| 20 | + RenameFileTool, |
| 21 | + RevealSymbolTool, |
| 22 | + SearchTool, |
| 23 | + SemanticEditTool, |
| 24 | + ViewFileTool, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +def create_codebase_agent( |
| 29 | + codebase: Codebase, |
| 30 | + model_name: str = "gpt-4", |
| 31 | + temperature: float = 0, |
| 32 | + verbose: bool = True, |
| 33 | +) -> RunnableWithMessageHistory: |
| 34 | + """Create an agent with all codebase tools. |
| 35 | +
|
| 36 | + Args: |
| 37 | + codebase: The codebase to operate on |
| 38 | + model_name: Name of the model to use (default: gpt-4) |
| 39 | + temperature: Model temperature (default: 0) |
| 40 | + verbose: Whether to print agent's thought process (default: True) |
| 41 | +
|
| 42 | + Returns: |
| 43 | + Initialized agent with message history |
| 44 | + """ |
| 45 | + # Initialize language model |
| 46 | + llm = ChatOpenAI( |
| 47 | + model_name=model_name, |
| 48 | + temperature=temperature, |
| 49 | + ) |
| 50 | + |
| 51 | + # Get all codebase tools |
| 52 | + tools = [ |
| 53 | + ViewFileTool(codebase), |
| 54 | + ListDirectoryTool(codebase), |
| 55 | + SearchTool(codebase), |
| 56 | + EditFileTool(codebase), |
| 57 | + CreateFileTool(codebase), |
| 58 | + DeleteFileTool(codebase), |
| 59 | + RenameFileTool(codebase), |
| 60 | + MoveSymbolTool(codebase), |
| 61 | + RevealSymbolTool(codebase), |
| 62 | + SemanticEditTool(codebase), |
| 63 | + CommitTool(codebase), |
| 64 | + ] |
| 65 | + |
| 66 | + # Get the prompt to use |
| 67 | + prompt = hub.pull("hwchase17/openai-functions-agent") |
| 68 | + |
| 69 | + # Create the agent |
| 70 | + agent = OpenAIFunctionsAgent( |
| 71 | + llm=llm, |
| 72 | + tools=tools, |
| 73 | + prompt=prompt, |
| 74 | + ) |
| 75 | + |
| 76 | + # Create the agent executor |
| 77 | + agent_executor = AgentExecutor( |
| 78 | + agent=agent, |
| 79 | + tools=tools, |
| 80 | + verbose=verbose, |
| 81 | + ) |
| 82 | + |
| 83 | + # Create message history handler |
| 84 | + message_history = ChatMessageHistory() |
| 85 | + |
| 86 | + # Wrap with message history |
| 87 | + return RunnableWithMessageHistory( |
| 88 | + agent_executor, |
| 89 | + lambda session_id: message_history, |
| 90 | + input_messages_key="input", |
| 91 | + history_messages_key="chat_history", |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + # Initialize codebase |
| 97 | + print("Initializing codebase...") |
| 98 | + codebase = Codebase.from_repo("fastapi/fastapi", programming_language=ProgrammingLanguage.PYTHON) |
| 99 | + |
| 100 | + # Create agent with history |
| 101 | + print("Creating agent...") |
| 102 | + agent = create_codebase_agent(codebase) |
| 103 | + |
| 104 | + print("\nAsking agent to analyze symbol relationships...") |
| 105 | + result = agent.invoke( |
| 106 | + {"input": "What are the dependencies of the reveal_symbol function?"}, |
| 107 | + config={"configurable": {"session_id": "demo"}}, |
| 108 | + ) |
| 109 | + print("Messages:", result["messages"]) |
0 commit comments