Load Existing Messages into Chat Playground #176

Open
opened 2026-02-16 00:19:11 -05:00 by yindo · 2 comments
Owner

Originally created by @brycecoldcocacola on GitHub (May 8, 2024).

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.

Originally created by @brycecoldcocacola on GitHub (May 8, 2024). 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: ```python 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: <img width="1791" alt="Screenshot 2024-05-08 at 2 15 35 PM" src="https://github.com/langchain-ai/langserve/assets/11841639/27116877-5cbd-4d8a-8c37-489941f397b0"> Because I specified `("ai", "Hey Jessica! Just wanted to check in on you. How are you doing?")` in my chat prompt.
yindo added the playgroundenhancement labels 2026-02-16 00:19:11 -05:00
Author
Owner

@brycecoldcocacola commented on GitHub (May 8, 2024):

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

@brycecoldcocacola commented on GitHub (May 8, 2024): related: https://github.com/langchain-ai/langserve/issues/598#issuecomment-2051999511
Author
Owner

@eyurtsev commented on GitHub (May 15, 2024):

There's no way to do this currently :(

@eyurtsev commented on GitHub (May 15, 2024): There's no way to do this currently :(
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langserve#176