diff --git a/docs/introduction/overview.mdx b/docs/introduction/overview.mdx index 4189bbca3..db7d8dffa 100644 --- a/docs/introduction/overview.mdx +++ b/docs/introduction/overview.mdx @@ -46,27 +46,16 @@ pip install codegen ## What can I do with Codegen? -Codegen enables you to programmatically manipulate code with scale and precision. - - - - - -View source code on [modal/modal-client](https://github.com/modal-labs/modal-client/blob/cbac0d80dfd98588027ecd21850152776be3ab82/modal/client.py#L70). View codemod on [codegen.sh](https://www.codegen.sh/codemod/66e2e195-ceec-4935-876a-ed4cfc1731c7/public/diff) - - -Common use cases include: +Codegen's simple yet powerful APIs enable a range of applications, including: + + Create an intelligent agent that can analyze and manipulate your codebase using natural language. + Create high-quality training data for fine-tuning LLMs on your codebase. - Add, remove, and update feature flags across your application. - - - Restructure files, enforce naming conventions, and improve project layout. + Create powerful code transformations to automate large-scale changes. +See below for an example call graph visualization generated with Codegen. + + + + + +View source code on [modal/modal-client](https://github.com/modal-labs/modal-client/blob/cbac0d80dfd98588027ecd21850152776be3ab82/modal/client.py#L70). View codemod on [codegen.sh](https://www.codegen.sh/codemod/66e2e195-ceec-4935-876a-ed4cfc1731c7/public/diff) + ## Get Started diff --git a/docs/mint.json b/docs/mint.json index 7947f5aee..4e41df7fb 100644 --- a/docs/mint.json +++ b/docs/mint.json @@ -82,9 +82,10 @@ "group": "Tutorials", "pages": [ "tutorials/at-a-glance", - "tutorials/migrating-apis", - "tutorials/codebase-visualization", + "tutorials/build-code-agent", "tutorials/training-data", + "tutorials/codebase-visualization", + "tutorials/migrating-apis", "tutorials/organize-your-codebase", "tutorials/modularity", "tutorials/manage-feature-flags", diff --git a/docs/tutorials/at-a-glance.mdx b/docs/tutorials/at-a-glance.mdx index d6ec007dd..4a4da1fd9 100644 --- a/docs/tutorials/at-a-glance.mdx +++ b/docs/tutorials/at-a-glance.mdx @@ -10,6 +10,13 @@ Explore our tutorials to learn how to use Codegen for various code transformatio ## Featured Tutorials + + Create an intelligent code agent with Langchain and powerful, codegen-powered tools + Create high-quality training data for LLM pre-training similar to word2vec or node2vec - - Add, remove, and update feature flags across your application. - View the full code in our [examples repository](https://github.com/codegen-sh/codegen-sdk/tree/develop/src/codegen/extensions/langchain) + +## Step 1: Setting Up the Agent + +First, let's import the necessary components and create our agent: + +```python +from langchain_openai import ChatOpenAI +from codegen import Codebase +from codegen.extensions.langchain import create_codebase_agent + +# Initialize codebase +codebase = Codebase.from_repo("fastapi/fastapi") + +# Create the agent with GPT-4 +agent = create_codebase_agent( + codebase=codebase, + model_name="gpt-4", + temperature=0, + verbose=True +) +``` + +The agent is initialized with: +- A Codebase instance to operate on +- An LLM (GPT-4 in this case) +- Tools for code manipulation +- A conversation memory to maintain context + +## Step 2: Available Tools + +The agent comes with several built-in tools for code operations: + +```python +tools = [ + ViewFileTool(codebase), # View file contents + ListDirectoryTool(codebase), # List directory contents + SearchTool(codebase), # Search code + EditFileTool(codebase), # Edit files + CreateFileTool(codebase), # Create new files + DeleteFileTool(codebase), # Delete files + RenameFileTool(codebase), # Rename files + MoveSymbolTool(codebase), # Move functions/classes + RevealSymbolTool(codebase), # Analyze symbol relationships + SemanticEditTool(codebase), # Make semantic edits + CommitTool(codebase), # Commit changes +] +``` + +Each tool provides specific capabilities to the agent, allowing it to perform complex code operations. + +## Step 3: Interacting with the Agent + +Let's see some examples of how to interact with the agent: + +```python +# Analyze dependencies +result = agent.invoke( + { + "input": "What are the dependencies of the FastAPI class?", + "config": {"configurable": {"session_id": "demo"}} + } +) +print(result["output"]) + +# Find usage patterns +result = agent.invoke( + { + "input": "Show me examples of dependency injection in the codebase", + "config": {"configurable": {"session_id": "demo"}} + } +) +print(result["output"]) + +# Perform code analysis +result = agent.invoke( + { + "input": "What's the most complex function in terms of dependencies?", + "config": {"configurable": {"session_id": "demo"}} + } +) +print(result["output"]) +``` + +The agent maintains conversation history, so it can reference previous queries and build context over time. + +## Step 4: Code Manipulation + +The agent can also perform code changes: + +```python +# Move a function to a new file +result = agent.invoke( + { + "input": "Move the validate_email function to validation_utils.py", + "config": {"configurable": {"session_id": "demo"}} + } +) + +# Rename a class and update all references +result = agent.invoke( + { + "input": "Rename the UserModel class to User and update all imports", + "config": {"configurable": {"session_id": "demo"}} + } +) + +# Add error handling +result = agent.invoke( + { + "input": "Add proper error handling to the process_data function", + "config": {"configurable": {"session_id": "demo"}} + } +) +``` + +The agent will: +1. Analyze the current code state +2. Plan the necessary changes +3. Execute the changes while maintaining code correctness +4. Update all related imports and references + +## Advanced Usage + +### Adding Custom Tools + +You can extend the agent with custom tools: + +```python +from langchain.tools import BaseTool +from pydantic import BaseModel, Field + +class CustomToolInput(BaseModel): + """Input schema for custom tool.""" + param: str = Field(..., description="Parameter description") + +class CustomCodeTool(BaseTool): + """A custom tool for the code agent.""" + name = "custom_tool" + description = "Description of what the tool does" + args_schema = CustomToolInput + + def _run(self, param: str) -> str: + # Tool implementation + return f"Processed {param}" + +# Add custom tool to agent +tools.append(CustomCodeTool()) +agent = create_codebase_agent( + codebase=codebase, + tools=tools, + model_name="gpt-4" +) +``` \ No newline at end of file diff --git a/src/codegen/extensions/langchain/tools.py b/src/codegen/extensions/langchain/tools.py index f51872dce..fcfcd2997 100644 --- a/src/codegen/extensions/langchain/tools.py +++ b/src/codegen/extensions/langchain/tools.py @@ -205,15 +205,7 @@ def _run( collect_usages: bool = True, ) -> str: # Find the symbol first - found_symbol = None - for file in self.codebase.files: - for symbol in file.symbols: - if symbol.name == symbol_name: - found_symbol = symbol - break - if found_symbol: - break - + found_symbol = self.codebase.get_symbol(symbol_name) result = reveal_symbol( found_symbol, degree,