mirror of
https://github.com/langchain-ai/langserve.git
synced 2026-07-16 09:34:30 -04:00
How to make the new variables input available via query method? #91
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @xiechengmude on GitHub (Jan 10, 2024).
Question:
If I create the new varialbels: input_variables=["history", "input","lession", "affection"],
and setting like the below code.
I cant make the right query via the swagger docs from langserve. Whats the problem here?
Thank you !
`
#!/usr/bin/env python
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
from operator import itemgetter
from langchain.prompts import PromptTemplate,ChatPromptTemplate, MessagesPlaceholder
from langchain.chat_models import ChatOpenAI
from langserve import add_routes
from langchain.schema.output_parser import StrOutputParser
from langchain.schema.runnable import RunnablePassthrough
from langchain.schema.chat_history import BaseChatMessageHistory
from langchain.schema.runnable.history import RunnableWithMessageHistory
from langchain import globals
globals.set_debug(True)
REDIS_URL = "redis://localhost:6379/0"
llm = ChatOpenAI()
_DEFAULT_TEMPLATE = """You are excellent assistant.\n
Based on the pieces of previous conversation:{history}\n
The talking lession: {lession}\n
affection number: {affection}\n
Human: {question}\n
\n
Answer:
"""
BASIC_PROMPT = PromptTemplate(
input_variables=["history", "input","lession", "affection"],
template=_DEFAULT_TEMPLATE
)
app = FastAPI(
title="A simple Server",
version="0.1",
description="A Simple Api Server Using Runnable Interfaces",
)
chain_with_history = RunnableWithMessageHistory(
{"lession": RunnablePassthrough(),
"affection": RunnablePassthrough(),
"question": RunnablePassthrough()}
|PROMPT | llm ,
lambda session_id: RedisChatMessageHistory(session_id, url=REDIS_URL),
input_messages_key="input",
history_messages_key="history",
verbose=True,
max_message_history=5,
)
chain_with_history.invoke({"lession":"Math Lession", "affection":66,
"question":"whats the importanct to learn math well?"})
add_routes(
app,
chain_with_history,
path="/query-test",
)
if name == "main":
uvicorn.run("test:app", host="0.0.0.0", port=7000, reload=True)
`