Skip to content

Commit 07de1c6

Browse files
authored
docs: adds code agent tutorial (#382)
Co-authored-by: jayhack <[email protected]>
1 parent c391d99 commit 07de1c6

File tree

5 files changed

+211
-50
lines changed

5 files changed

+211
-50
lines changed

docs/introduction/overview.mdx

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,16 @@ pip install codegen
4646

4747
## What can I do with Codegen?
4848

49-
Codegen enables you to programmatically manipulate code with scale and precision.
50-
51-
<Frame caption="Call graph visualization for modal/modal-client/_Client">
52-
<iframe
53-
width="100%"
54-
height="500px"
55-
scrolling="no"
56-
src={`https://codegen.sh/embedded/graph?id=66e2e195-ceec-4935-876a-ed4cfc1731c7&zoom=0.5&targetNodeName=_Client`}
57-
className="rounded-xl"
58-
style={{
59-
backgroundColor: "#15141b",
60-
}}
61-
></iframe>
62-
</Frame>
63-
<Info>
64-
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)
65-
</Info>
66-
67-
Common use cases include:
49+
Codegen's simple yet powerful APIs enable a range of applications, including:
6850

6951
<CardGroup cols={2}>
52+
<Card
53+
title="Build a Code Agent"
54+
icon="robot"
55+
href="/tutorials/build-code-agent"
56+
>
57+
Create an intelligent agent that can analyze and manipulate your codebase using natural language.
58+
</Card>
7059
<Card
7160
title="Visualize Your Codebase"
7261
icon="diagram-project"
@@ -76,27 +65,37 @@ Common use cases include:
7665
</Card>
7766
<Card
7867
title="Mine Codebase Data"
79-
icon="robot"
68+
icon="database"
8069
href="/tutorials/training-data"
8170
>
8271
Create high-quality training data for fine-tuning LLMs on your codebase.
8372
</Card>
8473
<Card
85-
title="Eliminate Feature Flags"
86-
icon="flag"
87-
href="/tutorials/manage-feature-flags"
74+
title="Build Codemods"
75+
icon="wand-magic-sparkles"
76+
href="/tutorials/at-a-glance#api-migrations"
8877
>
89-
Add, remove, and update feature flags across your application.
90-
</Card>
91-
<Card
92-
title="Organize Your Codebase"
93-
icon="folder-tree"
94-
href="/tutorials/organize-your-codebase"
95-
>
96-
Restructure files, enforce naming conventions, and improve project layout.
78+
Create powerful code transformations to automate large-scale changes.
9779
</Card>
9880
</CardGroup>
9981

82+
See below for an example call graph visualization generated with Codegen.
83+
84+
<Frame caption="Call graph visualization for modal/modal-client/_Client">
85+
<iframe
86+
width="100%"
87+
height="500px"
88+
scrolling="no"
89+
src={`https://codegen.sh/embedded/graph?id=66e2e195-ceec-4935-876a-ed4cfc1731c7&zoom=0.5&targetNodeName=_Client`}
90+
className="rounded-xl"
91+
style={{
92+
backgroundColor: "#15141b",
93+
}}
94+
></iframe>
95+
</Frame>
96+
<Info>
97+
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)
98+
</Info>
10099

101100
## Get Started
102101

docs/mint.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@
8282
"group": "Tutorials",
8383
"pages": [
8484
"tutorials/at-a-glance",
85-
"tutorials/migrating-apis",
86-
"tutorials/codebase-visualization",
85+
"tutorials/build-code-agent",
8786
"tutorials/training-data",
87+
"tutorials/codebase-visualization",
88+
"tutorials/migrating-apis",
8889
"tutorials/organize-your-codebase",
8990
"tutorials/modularity",
9091
"tutorials/manage-feature-flags",

docs/tutorials/at-a-glance.mdx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ Explore our tutorials to learn how to use Codegen for various code transformatio
1010
## Featured Tutorials
1111

1212
<CardGroup cols={2}>
13+
<Card
14+
title="Build a Code Agent"
15+
icon="robot"
16+
href="/tutorials/build-code-agent"
17+
>
18+
Create an intelligent code agent with Langchain and powerful, codegen-powered tools
19+
</Card>
1320
<Card
1421
title="Visualize Your Codebase"
1522
icon="diagram-project"
@@ -19,18 +26,11 @@ Explore our tutorials to learn how to use Codegen for various code transformatio
1926
</Card>
2027
<Card
2128
title="Mine Training Data"
22-
icon="robot"
29+
icon="database"
2330
href="/tutorials/training-data"
2431
>
2532
Create high-quality training data for LLM pre-training similar to word2vec or node2vec
2633
</Card>
27-
<Card
28-
title="Manage Feature Flags"
29-
icon="flag"
30-
href="/tutorials/manage-feature-flags"
31-
>
32-
Add, remove, and update feature flags across your application.
33-
</Card>
3434
<Card
3535
title="Delete Dead Code"
3636
icon="broom"

docs/tutorials/build-code-agent.mdx

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
---
2+
title: "Building a Code Agent with LangChain"
3+
sidebarTitle: "Code Agent"
4+
icon: "robot"
5+
iconType: "solid"
6+
---
7+
8+
This guide demonstrates how to build an intelligent code agent that can analyze and manipulate codebases using Codegen's LangChain integration.
9+
10+
This agent access to powerful code viewing and manipulation tools powered by Codegen, including:
11+
- `RevealSymbolTool`: reveal all N-th degree dependencies and usages of a function
12+
- `MoveSymbolTool`: move a symbol between files, updating all imports etc. (guaranteed correctness)
13+
- `SemanticEditTool`: implementation of Cursor-style smart file editing
14+
- `SemanticSearchTool`: search over an index of vector embeddings for files
15+
16+
<Info>View the full code in our [examples repository](https://github.com/codegen-sh/codegen-sdk/tree/develop/src/codegen/extensions/langchain)</Info>
17+
18+
## Step 1: Setting Up the Agent
19+
20+
First, let's import the necessary components and create our agent:
21+
22+
```python
23+
from langchain_openai import ChatOpenAI
24+
from codegen import Codebase
25+
from codegen.extensions.langchain import create_codebase_agent
26+
27+
# Initialize codebase
28+
codebase = Codebase.from_repo("fastapi/fastapi")
29+
30+
# Create the agent with GPT-4
31+
agent = create_codebase_agent(
32+
codebase=codebase,
33+
model_name="gpt-4",
34+
temperature=0,
35+
verbose=True
36+
)
37+
```
38+
39+
The agent is initialized with:
40+
- A Codebase instance to operate on
41+
- An LLM (GPT-4 in this case)
42+
- Tools for code manipulation
43+
- A conversation memory to maintain context
44+
45+
## Step 2: Available Tools
46+
47+
The agent comes with several built-in tools for code operations:
48+
49+
```python
50+
tools = [
51+
ViewFileTool(codebase), # View file contents
52+
ListDirectoryTool(codebase), # List directory contents
53+
SearchTool(codebase), # Search code
54+
EditFileTool(codebase), # Edit files
55+
CreateFileTool(codebase), # Create new files
56+
DeleteFileTool(codebase), # Delete files
57+
RenameFileTool(codebase), # Rename files
58+
MoveSymbolTool(codebase), # Move functions/classes
59+
RevealSymbolTool(codebase), # Analyze symbol relationships
60+
SemanticEditTool(codebase), # Make semantic edits
61+
CommitTool(codebase), # Commit changes
62+
]
63+
```
64+
65+
Each tool provides specific capabilities to the agent, allowing it to perform complex code operations.
66+
67+
## Step 3: Interacting with the Agent
68+
69+
Let's see some examples of how to interact with the agent:
70+
71+
```python
72+
# Analyze dependencies
73+
result = agent.invoke(
74+
{
75+
"input": "What are the dependencies of the FastAPI class?",
76+
"config": {"configurable": {"session_id": "demo"}}
77+
}
78+
)
79+
print(result["output"])
80+
81+
# Find usage patterns
82+
result = agent.invoke(
83+
{
84+
"input": "Show me examples of dependency injection in the codebase",
85+
"config": {"configurable": {"session_id": "demo"}}
86+
}
87+
)
88+
print(result["output"])
89+
90+
# Perform code analysis
91+
result = agent.invoke(
92+
{
93+
"input": "What's the most complex function in terms of dependencies?",
94+
"config": {"configurable": {"session_id": "demo"}}
95+
}
96+
)
97+
print(result["output"])
98+
```
99+
100+
The agent maintains conversation history, so it can reference previous queries and build context over time.
101+
102+
## Step 4: Code Manipulation
103+
104+
The agent can also perform code changes:
105+
106+
```python
107+
# Move a function to a new file
108+
result = agent.invoke(
109+
{
110+
"input": "Move the validate_email function to validation_utils.py",
111+
"config": {"configurable": {"session_id": "demo"}}
112+
}
113+
)
114+
115+
# Rename a class and update all references
116+
result = agent.invoke(
117+
{
118+
"input": "Rename the UserModel class to User and update all imports",
119+
"config": {"configurable": {"session_id": "demo"}}
120+
}
121+
)
122+
123+
# Add error handling
124+
result = agent.invoke(
125+
{
126+
"input": "Add proper error handling to the process_data function",
127+
"config": {"configurable": {"session_id": "demo"}}
128+
}
129+
)
130+
```
131+
132+
The agent will:
133+
1. Analyze the current code state
134+
2. Plan the necessary changes
135+
3. Execute the changes while maintaining code correctness
136+
4. Update all related imports and references
137+
138+
## Advanced Usage
139+
140+
### Adding Custom Tools
141+
142+
You can extend the agent with custom tools:
143+
144+
```python
145+
from langchain.tools import BaseTool
146+
from pydantic import BaseModel, Field
147+
148+
class CustomToolInput(BaseModel):
149+
"""Input schema for custom tool."""
150+
param: str = Field(..., description="Parameter description")
151+
152+
class CustomCodeTool(BaseTool):
153+
"""A custom tool for the code agent."""
154+
name = "custom_tool"
155+
description = "Description of what the tool does"
156+
args_schema = CustomToolInput
157+
158+
def _run(self, param: str) -> str:
159+
# Tool implementation
160+
return f"Processed {param}"
161+
162+
# Add custom tool to agent
163+
tools.append(CustomCodeTool())
164+
agent = create_codebase_agent(
165+
codebase=codebase,
166+
tools=tools,
167+
model_name="gpt-4"
168+
)
169+
```

src/codegen/extensions/langchain/tools.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,7 @@ def _run(
205205
collect_usages: bool = True,
206206
) -> str:
207207
# Find the symbol first
208-
found_symbol = None
209-
for file in self.codebase.files:
210-
for symbol in file.symbols:
211-
if symbol.name == symbol_name:
212-
found_symbol = symbol
213-
break
214-
if found_symbol:
215-
break
216-
208+
found_symbol = self.codebase.get_symbol(symbol_name)
217209
result = reveal_symbol(
218210
found_symbol,
219211
degree,

0 commit comments

Comments
 (0)