Skip to content

docs: langchain + modal examples #386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ repos:
- id: deptry
pass_filenames: false
always_run: true
entry: bash -c "uv run --frozen --all-extras --dev deptry src --ignore DEP001"
entry: bash -c "uv run --frozen --all-extras --dev deptry src --ignore DEP001 --extend-exclude 'codegen-examples/.*'"

- repo: https://github.com/renovatebot/pre-commit-hooks
rev: 39.164.1
Expand Down
82 changes: 82 additions & 0 deletions codegen-examples/examples/langchain_agent/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Codegen LangChain Agent Example

<p align="center">
<a href="https://docs.codegen.com/tutorials/build-code-agent">
<img src="https://i.imgur.com/6RF9W0z.jpeg" />
</a>
</p>

<h2 align="center">
Build an intelligent code agent with LangChain and Codegen
</h2>

<div align="center">

[![Documentation](https://img.shields.io/badge/Docs-docs.codegen.com-purple?style=flat-square)](https://docs.codegen.com/tutorials/build-code-agent)
[![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)

</div>

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.

## Quick Start

```python
from codegen import Codebase
from codegen.extensions.langchain import create_codebase_agent

# Initialize codebase
codebase = Codebase.from_repo("fastapi/fastapi")

# Create the agent
agent = create_codebase_agent(codebase=codebase, model_name="gpt-4", verbose=True)

# Ask the agent to analyze code
result = agent.invoke({"input": "What are the dependencies of the FastAPI class?", "config": {"configurable": {"session_id": "demo"}}})
print(result["output"])
```

## Installation

```bash
# Install dependencies
pip install modal-client codegen langchain langchain-openai

# Run the example
python run.py
```

## Available Tools

The agent comes with several built-in tools for code operations:

- `ViewFileTool`: View file contents and metadata
- `ListDirectoryTool`: List directory contents
- `SearchTool`: Search code using regex
- `EditFileTool`: Edit file contents
- `CreateFileTool`: Create new files
- `DeleteFileTool`: Delete files
- `RenameFileTool`: Rename files and update imports
- `MoveSymbolTool`: Move functions/classes between files
- `RevealSymbolTool`: Analyze symbol dependencies
- `SemanticEditTool`: Make semantic code edits
- `CommitTool`: Commit changes to disk

## Example Operations

The agent can perform various code analysis and manipulation tasks:

```python
# Analyze dependencies
agent.invoke({"input": "What are the dependencies of the reveal_symbol function?", "config": {"configurable": {"session_id": "demo"}}})

# Find usage patterns
agent.invoke({"input": "Show me examples of dependency injection in the codebase", "config": {"configurable": {"session_id": "demo"}}})

# Move code
agent.invoke({"input": "Move the validate_email function to validation_utils.py", "config": {"configurable": {"session_id": "demo"}}})
```

## Learn More

- [Full Tutorial](https://docs.codegen.com/tutorials/build-code-agent)
107 changes: 107 additions & 0 deletions codegen-examples/examples/langchain_agent/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"""Demo implementation of an agent with Codegen tools."""

from codegen import Codebase
from codegen.extensions.langchain.tools import (
CommitTool,
CreateFileTool,
DeleteFileTool,
EditFileTool,
ListDirectoryTool,
MoveSymbolTool,
RenameFileTool,
RevealSymbolTool,
SearchTool,
SemanticEditTool,
ViewFileTool,
)
from codegen.sdk.enums import ProgrammingLanguage
from langchain import hub
from langchain.agents import AgentExecutor
from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent
from langchain_core.chat_history import ChatMessageHistory

Check failure on line 21 in codegen-examples/examples/langchain_agent/run.py

View workflow job for this annotation

GitHub Actions / mypy

error: Module "langchain_core.chat_history" has no attribute "ChatMessageHistory"; maybe "BaseChatMessageHistory" or "InMemoryChatMessageHistory"? [attr-defined]
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_openai import ChatOpenAI


def create_codebase_agent(
codebase: Codebase,
model_name: str = "gpt-4o",
temperature: float = 0,
verbose: bool = True,
) -> RunnableWithMessageHistory:
"""Create an agent with all codebase tools.

Args:
codebase: The codebase to operate on
model_name: Name of the model to use (default: gpt-4)
temperature: Model temperature (default: 0)
verbose: Whether to print agent's thought process (default: True)

Returns:
Initialized agent with message history
"""
# Initialize language model
llm = ChatOpenAI(
model_name=model_name,
temperature=temperature,
)

# Get all codebase tools
tools = [
ViewFileTool(codebase),
ListDirectoryTool(codebase),
SearchTool(codebase),
EditFileTool(codebase),
CreateFileTool(codebase),
DeleteFileTool(codebase),
RenameFileTool(codebase),
MoveSymbolTool(codebase),
RevealSymbolTool(codebase),
SemanticEditTool(codebase),
CommitTool(codebase),
]

# Get the prompt to use
prompt = hub.pull("hwchase17/openai-functions-agent")

# Create the agent
agent = OpenAIFunctionsAgent(
llm=llm,
tools=tools,
prompt=prompt,
)

# Create the agent executor
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
verbose=verbose,
)

# Create message history handler
message_history = ChatMessageHistory()

# Wrap with message history
return RunnableWithMessageHistory(
agent_executor,

Check failure on line 86 in codegen-examples/examples/langchain_agent/run.py

View workflow job for this annotation

GitHub Actions / mypy

error: Argument 1 to "RunnableWithMessageHistory" has incompatible type "AgentExecutor"; expected "Runnable[Sequence[BaseMessage] | dict[str, Any], str | BaseMessage | Sequence[BaseMessage] | dict[str, Any]] | Runnable[PromptValue | str | Sequence[BaseMessage | list[str] | tuple[str, str] | str | dict[str, Any]], BaseMessage | str]" [arg-type]
lambda session_id: message_history,
input_messages_key="input",
history_messages_key="chat_history",
)


if __name__ == "__main__":
# Initialize codebase
print("Initializing codebase...")
codebase = Codebase.from_repo("fastapi/fastapi", programming_language=ProgrammingLanguage.PYTHON)

# Create agent with history
print("Creating agent...")
agent = create_codebase_agent(codebase)

print("\nAsking agent to analyze symbol relationships...")
result = agent.invoke(
{"input": "What are the dependencies of the reveal_symbol function?"},
config={"configurable": {"session_id": "demo"}},
)
print("Messages:", result["messages"])
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ dev-dependencies = [
"loguru>=0.7.3",
"httpx<0.28.2,>=0.28.1",
"jupyterlab>=4.3.5",
"modal>=0.73.25",
]


Expand Down
20 changes: 1 addition & 19 deletions src/codegen/extensions/langchain/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
from langchain import hub
from langchain.agents import AgentExecutor
from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent
from langchain_core.chat_history import ChatMessageHistory

Check failure on line 6 in src/codegen/extensions/langchain/agent.py

View workflow job for this annotation

GitHub Actions / mypy

error: Module "langchain_core.chat_history" has no attribute "ChatMessageHistory"; maybe "BaseChatMessageHistory" or "InMemoryChatMessageHistory"? [attr-defined]
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_openai import ChatOpenAI

from codegen import Codebase
from codegen.sdk.enums import ProgrammingLanguage

from .tools import (
CommitTool,
Expand All @@ -27,7 +26,7 @@

def create_codebase_agent(
codebase: Codebase,
model_name: str = "gpt-4",
model_name: str = "gpt-4o",
temperature: float = 0,
verbose: bool = True,
) -> RunnableWithMessageHistory:
Expand Down Expand Up @@ -85,25 +84,8 @@

# Wrap with message history
return RunnableWithMessageHistory(
agent_executor,

Check failure on line 87 in src/codegen/extensions/langchain/agent.py

View workflow job for this annotation

GitHub Actions / mypy

error: Argument 1 to "RunnableWithMessageHistory" has incompatible type "AgentExecutor"; expected "Runnable[Sequence[BaseMessage] | dict[str, Any], str | BaseMessage | Sequence[BaseMessage] | dict[str, Any]] | Runnable[PromptValue | str | Sequence[BaseMessage | list[str] | tuple[str, str] | str | dict[str, Any]], BaseMessage | str]" [arg-type]
lambda session_id: message_history,
input_messages_key="input",
history_messages_key="chat_history",
)


if __name__ == "__main__":
# Initialize codebase
print("Initializing codebase...")
codebase = Codebase.from_repo("fastapi/fastapi", programming_language=ProgrammingLanguage.PYTHON)

# Create agent with history
print("Creating agent...")
agent = create_codebase_agent(codebase)

print("\nAsking agent to analyze symbol relationships...")
result = agent.invoke(
{"input": "What are the dependencies of the reveal_symbol function?"},
config={"configurable": {"session_id": "demo"}},
)
print("Messages:", result["messages"])
68 changes: 0 additions & 68 deletions src/codegen/extensions/modal/README.md

This file was deleted.

56 changes: 0 additions & 56 deletions src/codegen/extensions/modal/api.py

This file was deleted.

6 changes: 0 additions & 6 deletions src/codegen/extensions/modal/pyproject.toml

This file was deleted.

Loading