Skip to content

Commit f55f27c

Browse files
jayhacktkucar
authored andcommitted
[WIP] Langchain demo (#374)
# Motivation <!-- Why is this change necessary? --> # Content <!-- Please include a summary of the change --> # Testing <!-- How was the change tested? --> # Please check the following before marking your PR as ready for review - [ ] I have added tests for my changes - [ ] I have updated the documentation or added new documentation as needed --------- Co-authored-by: jayhack <[email protected]>
1 parent 4abaae0 commit f55f27c

File tree

12 files changed

+2932
-5
lines changed

12 files changed

+2932
-5
lines changed

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ dependencies = [
6262
"tomlkit>=0.13.2",
6363
"python-semantic-release",
6464
"uvicorn[standard]>=0.30.0",
65+
"langchain[openai]",
66+
"langchain_core",
67+
"langchain_openai",
6568
]
6669

6770
license = { text = "Apache-2.0" }
@@ -146,6 +149,7 @@ dev-dependencies = [
146149
"pytest-asyncio<1.0.0,>=0.21.1",
147150
"loguru>=0.7.3",
148151
"httpx<0.28.2,>=0.28.1",
152+
"jupyterlab>=4.3.5",
149153
]
150154

151155

src/codegen/extensions/__init__.py

Whitespace-only changes.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""Langchain tools for workspace operations."""
2+
3+
from langchain.tools import BaseTool
4+
5+
from codegen import Codebase
6+
7+
from .tools import (
8+
CommitTool,
9+
CreateFileTool,
10+
DeleteFileTool,
11+
EditFileTool,
12+
ListDirectoryTool,
13+
RevealSymbolTool,
14+
SearchTool,
15+
SemanticEditTool,
16+
ViewFileTool,
17+
)
18+
19+
__all__ = [
20+
# Tool classes
21+
"CommitTool",
22+
"CreateFileTool",
23+
"DeleteFileTool",
24+
"EditFileTool",
25+
"ListDirectoryTool",
26+
"RevealSymbolTool",
27+
"SearchTool",
28+
"SemanticEditTool",
29+
"ViewFileTool",
30+
# Helper functions
31+
"get_workspace_tools",
32+
]
33+
34+
35+
def get_workspace_tools(codebase: Codebase) -> list[BaseTool]:
36+
"""Get all workspace tools initialized with a codebase.
37+
38+
Args:
39+
codebase: The codebase to operate on
40+
41+
Returns:
42+
List of initialized Langchain tools
43+
"""
44+
return [
45+
ViewFileTool(codebase),
46+
ListDirectoryTool(codebase),
47+
SearchTool(codebase),
48+
EditFileTool(codebase),
49+
CreateFileTool(codebase),
50+
DeleteFileTool(codebase),
51+
CommitTool(codebase),
52+
RevealSymbolTool(codebase),
53+
SemanticEditTool(codebase),
54+
]
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)