Skip to content

Commit

Permalink
Setup New Project template
Browse files Browse the repository at this point in the history
(Remove react agent code)
  • Loading branch information
hinthornw committed Sep 17, 2024
2 parents 523dc46 + 609708e commit 706df9d
Show file tree
Hide file tree
Showing 20 changed files with 170 additions and 785 deletions.
4 changes: 1 addition & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
TAVILY_API_KEY=...

# To separate your traces from other application
LANGSMITH_PROJECT=retrieval-agent
LANGSMITH_PROJECT=new-agent

# The following depend on your selected configuration

Expand Down
1 change: 0 additions & 1 deletion .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ jobs:
- name: Run integration tests
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
TAVILY_API_KEY: ${{ secrets.TAVILY_API_KEY }}
LANGSMITH_API_KEY: ${{ secrets.LANGSMITH_API_KEY }}
LANGSMITH_TRACING: true
run: |
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ TEST_FILE ?= tests/unit_tests/
test:
python -m pytest $(TEST_FILE)

integration_tests:
python -m pytest tests/integration_tests

test_watch:
python -m ptw --snapshot-update --now . -- -vv tests/unit_tests

Expand Down
438 changes: 30 additions & 408 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion langgraph.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"dependencies": ["."],
"graphs": {
"agent": "./src/react_agent/graph.py:graph"
"agent": "./src/agent/graph.py:graph"
},
"env": ".env"
}
16 changes: 5 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "react-agent"
name = "agent"
version = "0.0.1"
description = "Starter template for making a custom Reasoning and Action agent (using tool calling) in LangGraph."
description = "Starter template for making a new agent LangGraph."
authors = [
{ name = "William Fu-Hinthorn", email = "[email protected]" },
]
Expand All @@ -10,13 +10,7 @@ license = { text = "MIT" }
requires-python = ">=3.9"
dependencies = [
"langgraph>=0.2.6",
"langchain-openai>=0.1.22",
"langchain-anthropic>=0.1.23",
"langchain>=0.2.14",
"langchain-fireworks>=0.1.7",
"python-dotenv>=1.0.1",
"langchain-community>=0.2.17",
"tavily-python>=0.4.0",
]


Expand All @@ -28,10 +22,10 @@ requires = ["setuptools>=73.0.0", "wheel"]
build-backend = "setuptools.build_meta"

[tool.setuptools]
packages = ["langgraph.templates.react_agent", "react_agent"]
packages = ["langgraph.templates.agent", "agent"]
[tool.setuptools.package-dir]
"langgraph.templates.react_agent" = "src/react_agent"
"react_agent" = "src/react_agent"
"langgraph.templates.agent" = "src/agent"
"agent" = "src/agent"


[tool.setuptools.package-data]
Expand Down
8 changes: 8 additions & 0 deletions src/agent/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""New LangGraph Agent.
This module defines a custom graph.
"""

from agent.graph import graph

__all__ = ["graph"]
24 changes: 24 additions & 0 deletions src/agent/configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Define the configurable parameters for the agent."""

from __future__ import annotations

from dataclasses import dataclass, fields
from typing import Optional

from langchain_core.runnables import RunnableConfig


@dataclass(kw_only=True)
class Configuration:
"""The configuration for the agent."""

model_name: str = "my-model"

@classmethod
def from_runnable_config(
cls, config: Optional[RunnableConfig] = None
) -> Configuration:
"""Create a Configuration instance from a RunnableConfig object."""
configurable = (config.get("configurable") or {}) if config else {}
_fields = {f.name for f in fields(cls) if f.init}
return cls(**{k: v for k, v in configurable.items() if k in _fields})
66 changes: 66 additions & 0 deletions src/agent/graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""Define a simple chatbot agent.
This agent returns a predefined response without using an actual LLM.
"""

from typing import Dict, List

from langchain_core.messages import AIMessage
from langchain_core.runnables import RunnableConfig
from langgraph.graph import StateGraph

from agent.configuration import Configuration
from agent.state import State

# Define the function that simulates calling a model


async def call_model(
state: State, config: RunnableConfig
) -> Dict[str, List[AIMessage]]:
"""Simulate calling an LLM by returning a predefined response.
Args:
_state (State): The current state of the conversation (unused).
_config (RunnableConfig): Configuration for the run (unused).
Returns:
dict: A dictionary containing the predefined response message.
"""
configuration = Configuration.from_runnable_config(config)
# Return a predefined response
return {
"messages": [AIMessage(content=f"Hi there! This is {configuration.model_name}")]
}


# Define the routing function
def route(state: State) -> str:
"""Determine whether to continue the conversation or end it.
Args:
state (State): The current state of the conversation.
Returns:
str: Either "call_model" to continue or "__end__" to finish.
"""
if len(state.messages) > 5: # End after 5 interactions
return "__end__"
return "call_model"


# Define a new graph
workflow = StateGraph(State, config_schema=Configuration)

# Add the node to the graph
workflow.add_node("call_model", call_model)

# Set the entrypoint as `call_model`
workflow.add_edge("__start__", "call_model")

# Add conditional edge
workflow.add_conditional_edges("call_model", route)

# Compile the workflow into an executable graph
graph = workflow.compile()
graph.name = "Simple Chatbot" # This defines the custom name in LangSmith
25 changes: 25 additions & 0 deletions src/agent/state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Define the state structures for the agent."""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import List

from langchain_core.messages import AnyMessage
from langgraph.graph import add_messages
from typing_extensions import Annotated


@dataclass
class State:
"""Defines the input state for the agent, representing a narrower interface to the outside world.
This class is used to define the initial state and structure of incoming data.
"""

messages: Annotated[List[AnyMessage], add_messages] = field(default_factory=list)
"""
Messages tracking the primary execution state of the agent.
Typically accumulates a pattern of user, assistant, user, ... etc. messages.
"""
9 changes: 0 additions & 9 deletions src/react_agent/__init__.py

This file was deleted.

49 changes: 0 additions & 49 deletions src/react_agent/configuration.py

This file was deleted.

127 changes: 0 additions & 127 deletions src/react_agent/graph.py

This file was deleted.

5 changes: 0 additions & 5 deletions src/react_agent/prompts.py

This file was deleted.

Loading

0 comments on commit 706df9d

Please sign in to comment.