langchain-ai / langserve

LangServe 🦜️🏓
Other
1.95k stars 218 forks source link

Load Existing Messages into Chat Playground #637

Open brycecoldcocacola opened 6 months ago

brycecoldcocacola commented 6 months ago

I don't see a way to start the chat playground with an initial AI message. Use-case here is to explore conversations where the bot will initiate the conversation.

So if I have something like this as a full example:

import datetime as dt

from fastapi import FastAPI
from langserve import add_routes
from langchain_core.prompts.chat import ChatPromptTemplate, MessagesPlaceholder
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_openai import ChatOpenAI
from langchain.tools import tool
from langchain.pydantic_v1 import BaseModel, Field
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage

@tool
def schedule_session(appointment_time: dt.datetime) -> str:
    """Schedule a session with the user."""
    return "ok"

tools = [schedule_session]

gpt_35_turbo = ChatOpenAI(model="gpt-3.5-turbo")

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "You are an empathetic bot reaching out to a friend"),
        ("ai", "Hey Jessica! Just wanted to check in on you. How are you doing?"),
        MessagesPlaceholder("chat_history", optional=True),
        ("human", "{input} {agent_scratchpad}"),
    ]
)

agent = create_tool_calling_agent(llm=gpt_35_turbo, tools=tools, prompt=prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools)

class ChatInputWithHistory(BaseModel):
    """Input for the chat endpoint."""
    input: str
    chat_history: list[HumanMessage | AIMessage | SystemMessage] = Field(
        ...,
        description="The chat messages representing the current conversation.",
    )

playground_compatible_agent_executor = (
    agent_executor | (lambda x: x["output"])
).with_types(input_type=ChatInputWithHistory, output_type=str)

app = FastAPI()

add_routes(
        app,
        playground_compatible_agent_executor,
        path="/chat",
        playground_type="chat",
)

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8080)

Then I would expect my http://localhost:8080/nova/chat/playground/ to open like this:

Screenshot 2024-05-08 at 2 15 35 PM

Because I specified ("ai", "Hey Jessica! Just wanted to check in on you. How are you doing?") in my chat prompt.

brycecoldcocacola commented 6 months ago

related: https://github.com/langchain-ai/langserve/issues/598#issuecomment-2051999511

eyurtsev commented 6 months ago

There's no way to do this currently :(