Skip to content

Commit

Permalink
Merge pull request #335 from langchain-ai/nc/10may/rm-langserve-dep
Browse files Browse the repository at this point in the history
Remove dependency on langserve
  • Loading branch information
nfcampos authored May 10, 2024
2 parents 28c0ec9 + e88009f commit 40a76db
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
22 changes: 21 additions & 1 deletion backend/app/api/runs.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import Any, Dict, Optional, Sequence, Union
from uuid import UUID

import langsmith.client
from fastapi import APIRouter, BackgroundTasks, HTTPException
from fastapi.exceptions import RequestValidationError
from langchain.pydantic_v1 import ValidationError
from langchain_core.messages import AnyMessage
from langchain_core.runnables import RunnableConfig
from langserve.schema import FeedbackCreateRequest
from langsmith.utils import tracing_is_enabled
from pydantic import BaseModel, Field
from sse_starlette import EventSourceResponse
Expand Down Expand Up @@ -102,6 +102,26 @@ async def config_schema() -> dict:
if tracing_is_enabled():
langsmith_client = langsmith.client.Client()

class FeedbackCreateRequest(BaseModel):
"""
Shared information between create requests of feedback and feedback objects
"""

run_id: UUID
"""The associated run ID this feedback is logged for."""

key: str
"""The metric name, tag, or aspect to provide feedback on."""

score: Optional[Union[float, int, bool]] = None
"""Value or score to assign the run."""

value: Optional[Union[float, int, bool, str, Dict]] = None
"""The display value for the feedback if not a metric."""

comment: Optional[str] = None
"""Comment or explanation for the feedback."""

@router.post("/feedback")
def create_run_feedback(feedback_create_req: FeedbackCreateRequest) -> dict:
"""
Expand Down
12 changes: 9 additions & 3 deletions backend/app/stream.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import functools
from typing import Any, AsyncIterator, Dict, Optional, Sequence, Union

import orjson
import structlog
from langchain_core.messages import AnyMessage, BaseMessage, message_chunk_to_message
from langchain_core.runnables import Runnable, RunnableConfig
from langserve.serialization import WellKnownLCSerializer

logger = structlog.get_logger(__name__)

Expand Down Expand Up @@ -54,7 +54,13 @@ async def astream_state(
yield [messages[message.id]]


_serializer = WellKnownLCSerializer()
def _default(obj) -> Any:
if hasattr(obj, "dict") and callable(obj.dict):
return obj.dict()
raise TypeError(f"Object of type {obj.__class__.__name__} is not JSON serializable")


dumps = functools.partial(orjson.dumps, default=_default)


async def to_sse(messages_stream: MessagesStream) -> AsyncIterator[dict]:
Expand All @@ -72,7 +78,7 @@ async def to_sse(messages_stream: MessagesStream) -> AsyncIterator[dict]:
else:
yield {
"event": "data",
"data": _serializer.dumps(
"data": dumps(
[message_chunk_to_message(msg) for msg in chunk]
).decode(),
}
Expand Down
24 changes: 1 addition & 23 deletions backend/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ sse-starlette = "^1.6.5"
tomli-w = "^1.0.0"
uvicorn = "^0.23.2"
fastapi = "^0.103.2"
langserve = "0.0.32"
# Uncomment if you need to work from a development branch
# This will only work for local development though!
# langchain = { git = "git@github.com:langchain-ai/langchain.git/", branch = "nc/subclass-runnable-binding" , subdirectory = "libs/langchain"}
Expand Down

0 comments on commit 40a76db

Please sign in to comment.