Skip to content

Commit

Permalink
patch: name used for tracing should take into account APIRouter path (#…
Browse files Browse the repository at this point in the history
…476)

If there is a router at path /foo, and a runnable is added to that
router at
path /bar, the name of the runnable should be /foo/bar for logging
purposes.
  • Loading branch information
eyurtsev authored Feb 22, 2024
1 parent 54725a9 commit e1cec83
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
38 changes: 38 additions & 0 deletions examples/router/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python
"""Example LangChain Server that uses a Fast API Router.
When applications grow, it becomes useful to use FastAPI's Router to organize
the routes.
See more documentation at:
https://fastapi.tiangolo.com/tutorial/bigger-applications/
"""
from fastapi import APIRouter, FastAPI
from langchain.chat_models import ChatAnthropic, ChatOpenAI

from langserve import add_routes

app = FastAPI()

router = APIRouter(prefix="/models")

# Invocations to this router will appear in trace logs as /models/openai
add_routes(
router,
ChatOpenAI(),
path="/openai",
)
# Invocations to this router will appear in trace logs as /models/anthropic
add_routes(
router,
ChatAnthropic(),
path="/anthropic",
)

app.include_router(router)

if __name__ == "__main__":
import uvicorn

uvicorn.run(app, host="localhost", port=8000)
22 changes: 15 additions & 7 deletions langserve/api_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ async def _unpack_request_config(


def _update_config_with_defaults(
path: str,
run_name: str,
incoming_config: RunnableConfig,
request: Request,
*,
Expand Down Expand Up @@ -209,7 +209,7 @@ def _update_config_with_defaults(
metadata.update(hosted_metadata)

non_overridable_default_config = RunnableConfig(
run_name=path,
run_name=run_name,
metadata=metadata,
)

Expand Down Expand Up @@ -536,6 +536,14 @@ def __init__(

self._path = path
self._base_url = prefix + path
# Setting the run name explicitly
# the run name is set to the base url, which takes into account
# the prefix (e.g., if there's an APIRouter used) and the path relative
# to the router.
# If the base path is /foo/bar, the run name will be /foo/bar
# and when tracing information is logged, we'll be able to see
# traces for the path /foo/bar.
self._run_name = self._base_url
self._include_callback_events = include_callback_events
self._per_req_config_modifier = per_req_config_modifier
self._serializer = WellKnownLCSerializer()
Expand Down Expand Up @@ -663,7 +671,7 @@ async def _get_config_and_input(
server_config=server_config,
)
config = _update_config_with_defaults(
self._path,
self._run_name,
user_provided_config,
request,
endpoint=endpoint,
Expand Down Expand Up @@ -814,7 +822,7 @@ async def batch(
_add_callbacks(config_, [aggregator])
final_configs.append(
_update_config_with_defaults(
self._path, config_, request, endpoint="batch"
self._run_name, config_, request, endpoint="batch"
)
)

Expand Down Expand Up @@ -1236,7 +1244,7 @@ async def input_schema(
server_config=server_config,
)
config = _update_config_with_defaults(
self._path, user_provided_config, request
self._run_name, user_provided_config, request
)

return self._runnable.get_input_schema(config).schema()
Expand Down Expand Up @@ -1264,7 +1272,7 @@ async def output_schema(
server_config=server_config,
)
config = _update_config_with_defaults(
self._path, user_provided_config, request
self._run_name, user_provided_config, request
)
return self._runnable.get_output_schema(config).schema()

Expand All @@ -1291,7 +1299,7 @@ async def config_schema(
server_config=server_config,
)
config = _update_config_with_defaults(
self._path, user_provided_config, request
self._run_name, user_provided_config, request
)
return (
self._runnable.with_config(config)
Expand Down

0 comments on commit e1cec83

Please sign in to comment.