Skip to content

Commit f2fdeea

Browse files
committed
add /agents/chat_with_tools_agent API
1 parent 3533981 commit f2fdeea

File tree

8 files changed

+77
-17
lines changed

8 files changed

+77
-17
lines changed

.env.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ DIFY_API_KEY="xxx"
3636
## Cosmos DB Settings
3737
COSMOSDB_HOST="https://xxx.documents.azure.com:443/"
3838
COSMOSDB_KEY="xxx"
39-
COSMOSDB_DATABASE_NAME="langgraph"
40-
COSMOSDB_CONTAINER_NAME="docs_kabuto"
39+
COSMOSDB_DATABASE_NAME="template_langgraph"
40+
COSMOSDB_CONTAINER_NAME="kabuto"
4141
COSMOSDB_PARTITION_KEY="/id"
4242

4343
# ---------

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,4 @@ langgraph-studio: ## run LangGraph Studio
118118

119119
.PHONY: fastapi-dev
120120
fastapi-dev: ## run FastAPI
121-
uv run fastapi dev ./template_langgraph/services/fastapis.py
121+
uv run fastapi dev ./template_langgraph/services/fastapis/main.py

template_langgraph/loggers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import logging
22

33

4-
def get_logger(name: str = "default") -> logging.Logger:
4+
def get_logger(
5+
name: str = "default",
6+
verbosity: int = logging.INFO,
7+
) -> logging.Logger:
58
"""
69
Get a logger with the specified name.
710
811
Args:
912
name (str): The name of the logger.
13+
verbosity (int): The logging level (default: logging.INFO).
1014
Returns:
1115
logging.Logger: Configured logger instance.
1216
"""
1317
logger = logging.getLogger(name)
18+
logger.setLevel(verbosity)
1419
formatter = logging.Formatter("%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)")
1520
handler = logging.StreamHandler()
1621
handler.setFormatter(formatter)

template_langgraph/services/fastapis.py

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

template_langgraph/services/fastapis/__init__.py

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from fastapi import FastAPI
2+
3+
from template_langgraph.services.fastapis.routers import agents as agents_router
4+
5+
app = FastAPI()
6+
7+
8+
app.include_router(
9+
agents_router.router,
10+
prefix="/agents",
11+
tags=["agents"],
12+
)

template_langgraph/services/fastapis/routers/__init__.py

Whitespace-only changes.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import logging
2+
3+
from fastapi import APIRouter
4+
from pydantic import BaseModel, ConfigDict
5+
6+
from template_langgraph.agents.chat_with_tools_agent.agent import AgentState
7+
from template_langgraph.agents.chat_with_tools_agent.agent import graph as chat_with_tools_agent
8+
from template_langgraph.loggers import get_logger
9+
10+
router = APIRouter()
11+
logger = get_logger(
12+
name=__name__,
13+
verbosity=logging.DEBUG,
14+
)
15+
16+
17+
class RunChatWithToolsAgentRequest(BaseModel):
18+
model_config = ConfigDict(extra="ignore")
19+
question: str
20+
21+
22+
class RunChatWithToolsAgentResponse(BaseModel):
23+
model_config = ConfigDict(extra="ignore")
24+
response: str
25+
26+
27+
@router.post(
28+
"/chat_with_tools_agent/",
29+
response_model=RunChatWithToolsAgentResponse,
30+
operation_id="create_transcription_job",
31+
)
32+
async def run_chat_with_tools_agent(
33+
request: RunChatWithToolsAgentRequest,
34+
) -> RunChatWithToolsAgentResponse:
35+
async for event in chat_with_tools_agent.astream(
36+
input=AgentState(
37+
messages=[
38+
{
39+
"role": "user",
40+
"content": request.question,
41+
},
42+
],
43+
),
44+
config={
45+
"recursion_limit": 30,
46+
},
47+
):
48+
logger.debug(f"Event received: {event}")
49+
try:
50+
response = event["chat_with_tools"]["messages"][0].content
51+
return RunChatWithToolsAgentResponse(response=response)
52+
except Exception as e:
53+
logger.error(f"Error processing event: {e}")
54+
return RunChatWithToolsAgentResponse(
55+
response=f"An error occurred while processing your request with {e}",
56+
)

0 commit comments

Comments
 (0)