Skip to content

Commit dd54bf9

Browse files
jayhackkopekC
authored andcommitted
docs: langchain + modal examples (#386)
Co-authored-by: KopekC <[email protected]>
1 parent 1fec4b3 commit dd54bf9

File tree

8 files changed

+192
-150
lines changed

8 files changed

+192
-150
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ repos:
7272
- id: deptry
7373
pass_filenames: false
7474
always_run: true
75-
entry: bash -c "uv run --frozen --all-extras --dev deptry src --ignore DEP001"
75+
entry: bash -c "uv run --frozen --all-extras --dev deptry src --ignore DEP001 --extend-exclude 'codegen-examples/.*'"
7676

7777
- repo: https://github.com/renovatebot/pre-commit-hooks
7878
rev: 39.164.1
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Codegen LangChain Agent Example
2+
3+
<p align="center">
4+
<a href="https://docs.codegen.com/tutorials/build-code-agent">
5+
<img src="https://i.imgur.com/6RF9W0z.jpeg" />
6+
</a>
7+
</p>
8+
9+
<h2 align="center">
10+
Build an intelligent code agent with LangChain and Codegen
11+
</h2>
12+
13+
<div align="center">
14+
15+
[![Documentation](https://img.shields.io/badge/Docs-docs.codegen.com-purple?style=flat-square)](https://docs.codegen.com/tutorials/build-code-agent)
16+
[![License](https://img.shields.io/badge/Code%20License-Apache%202.0-gray?&color=gray)](https://github.com/codegen-sh/codegen-sdk/tree/develop?tab=Apache-2.0-1-ov-file)
17+
18+
</div>
19+
20+
This example demonstrates how to build an intelligent code agent using Codegen's LangChain integration. The agent can analyze and manipulate codebases using natural language commands.
21+
22+
## Quick Start
23+
24+
```python
25+
from codegen import Codebase
26+
from codegen.extensions.langchain import create_codebase_agent
27+
28+
# Initialize codebase
29+
codebase = Codebase.from_repo("fastapi/fastapi")
30+
31+
# Create the agent
32+
agent = create_codebase_agent(codebase=codebase, model_name="gpt-4", verbose=True)
33+
34+
# Ask the agent to analyze code
35+
result = agent.invoke({"input": "What are the dependencies of the FastAPI class?", "config": {"configurable": {"session_id": "demo"}}})
36+
print(result["output"])
37+
```
38+
39+
## Installation
40+
41+
```bash
42+
# Install dependencies
43+
pip install modal-client codegen langchain langchain-openai
44+
45+
# Run the example
46+
python run.py
47+
```
48+
49+
## Available Tools
50+
51+
The agent comes with several built-in tools for code operations:
52+
53+
- `ViewFileTool`: View file contents and metadata
54+
- `ListDirectoryTool`: List directory contents
55+
- `SearchTool`: Search code using regex
56+
- `EditFileTool`: Edit file contents
57+
- `CreateFileTool`: Create new files
58+
- `DeleteFileTool`: Delete files
59+
- `RenameFileTool`: Rename files and update imports
60+
- `MoveSymbolTool`: Move functions/classes between files
61+
- `RevealSymbolTool`: Analyze symbol dependencies
62+
- `SemanticEditTool`: Make semantic code edits
63+
- `CommitTool`: Commit changes to disk
64+
65+
## Example Operations
66+
67+
The agent can perform various code analysis and manipulation tasks:
68+
69+
```python
70+
# Analyze dependencies
71+
agent.invoke({"input": "What are the dependencies of the reveal_symbol function?", "config": {"configurable": {"session_id": "demo"}}})
72+
73+
# Find usage patterns
74+
agent.invoke({"input": "Show me examples of dependency injection in the codebase", "config": {"configurable": {"session_id": "demo"}}})
75+
76+
# Move code
77+
agent.invoke({"input": "Move the validate_email function to validation_utils.py", "config": {"configurable": {"session_id": "demo"}}})
78+
```
79+
80+
## Learn More
81+
82+
- [Full Tutorial](https://docs.codegen.com/tutorials/build-code-agent)
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
"""Demo implementation of an agent with Codegen tools."""
2+
3+
from codegen import Codebase
4+
from codegen.extensions.langchain.tools import (
5+
CommitTool,
6+
CreateFileTool,
7+
DeleteFileTool,
8+
EditFileTool,
9+
ListDirectoryTool,
10+
MoveSymbolTool,
11+
RenameFileTool,
12+
RevealSymbolTool,
13+
SearchTool,
14+
SemanticEditTool,
15+
ViewFileTool,
16+
)
17+
from codegen.sdk.enums import ProgrammingLanguage
18+
from langchain import hub
19+
from langchain.agents import AgentExecutor
20+
from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent
21+
from langchain_core.chat_history import ChatMessageHistory
22+
from langchain_core.runnables.history import RunnableWithMessageHistory
23+
from langchain_openai import ChatOpenAI
24+
25+
26+
def create_codebase_agent(
27+
codebase: Codebase,
28+
model_name: str = "gpt-4o",
29+
temperature: float = 0,
30+
verbose: bool = True,
31+
) -> RunnableWithMessageHistory:
32+
"""Create an agent with all codebase tools.
33+
34+
Args:
35+
codebase: The codebase to operate on
36+
model_name: Name of the model to use (default: gpt-4)
37+
temperature: Model temperature (default: 0)
38+
verbose: Whether to print agent's thought process (default: True)
39+
40+
Returns:
41+
Initialized agent with message history
42+
"""
43+
# Initialize language model
44+
llm = ChatOpenAI(
45+
model_name=model_name,
46+
temperature=temperature,
47+
)
48+
49+
# Get all codebase tools
50+
tools = [
51+
ViewFileTool(codebase),
52+
ListDirectoryTool(codebase),
53+
SearchTool(codebase),
54+
EditFileTool(codebase),
55+
CreateFileTool(codebase),
56+
DeleteFileTool(codebase),
57+
RenameFileTool(codebase),
58+
MoveSymbolTool(codebase),
59+
RevealSymbolTool(codebase),
60+
SemanticEditTool(codebase),
61+
CommitTool(codebase),
62+
]
63+
64+
# Get the prompt to use
65+
prompt = hub.pull("hwchase17/openai-functions-agent")
66+
67+
# Create the agent
68+
agent = OpenAIFunctionsAgent(
69+
llm=llm,
70+
tools=tools,
71+
prompt=prompt,
72+
)
73+
74+
# Create the agent executor
75+
agent_executor = AgentExecutor(
76+
agent=agent,
77+
tools=tools,
78+
verbose=verbose,
79+
)
80+
81+
# Create message history handler
82+
message_history = ChatMessageHistory()
83+
84+
# Wrap with message history
85+
return RunnableWithMessageHistory(
86+
agent_executor,
87+
lambda session_id: message_history,
88+
input_messages_key="input",
89+
history_messages_key="chat_history",
90+
)
91+
92+
93+
if __name__ == "__main__":
94+
# Initialize codebase
95+
print("Initializing codebase...")
96+
codebase = Codebase.from_repo("fastapi/fastapi", programming_language=ProgrammingLanguage.PYTHON)
97+
98+
# Create agent with history
99+
print("Creating agent...")
100+
agent = create_codebase_agent(codebase)
101+
102+
print("\nAsking agent to analyze symbol relationships...")
103+
result = agent.invoke(
104+
{"input": "What are the dependencies of the reveal_symbol function?"},
105+
config={"configurable": {"session_id": "demo"}},
106+
)
107+
print("Messages:", result["messages"])

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ dev-dependencies = [
151151
"loguru>=0.7.3",
152152
"httpx<0.28.2,>=0.28.1",
153153
"jupyterlab>=4.3.5",
154+
"modal>=0.73.25",
154155
]
155156

156157

src/codegen/extensions/langchain/agent.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from langchain_openai import ChatOpenAI
99

1010
from codegen import Codebase
11-
from codegen.sdk.enums import ProgrammingLanguage
1211

1312
from .tools import (
1413
CommitTool,
@@ -27,7 +26,7 @@
2726

2827
def create_codebase_agent(
2928
codebase: Codebase,
30-
model_name: str = "gpt-4",
29+
model_name: str = "gpt-4o",
3130
temperature: float = 0,
3231
verbose: bool = True,
3332
) -> RunnableWithMessageHistory:
@@ -90,20 +89,3 @@ def create_codebase_agent(
9089
input_messages_key="input",
9190
history_messages_key="chat_history",
9291
)
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"])

src/codegen/extensions/modal/README.md

Lines changed: 0 additions & 68 deletions
This file was deleted.

src/codegen/extensions/modal/api.py

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/codegen/extensions/modal/pyproject.toml

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)