Replies: 1 comment 2 replies
-
Hello, @KirtiJha! I'm here to help you with any bugs, questions, or contributions you may have. Let's tackle this issue together! To pass only the last chat message in history with from langchain_community.chat_message_histories import StreamlitChatMessageHistory
# Initialize the message history
history = StreamlitChatMessageHistory(key="langchain_messages")
# Add some messages
history.add_user_message("hi!")
history.add_ai_message("whats up?")
# Access the last message
last_message = history.messages[-1]
# Print the last message content
print(last_message.content) This code snippet initializes the If you want to pass only the last two messages, you can slice the list accordingly: # Access the last two messages
last_two_messages = history.messages[-2:]
# Print the last two messages content
for message in last_two_messages:
print(message.content) This will give you the last two messages in the history. You can then use these messages as needed in your application. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
I am using
StreamlitChatMessageHistory
to pass chat history in my LLM application. It works fine. But it always pass all the session chat history. And as I continue with the conversation, it cross the LLM context limit soon.I want to pass only the last message or last 2 messages as history, but I am not able to do that.
I tried to look into documentation and tried few things, but not able to do that.
System Info
╰─ pip freeze
aiohappyeyeballs==2.4.0
aiohttp==3.10.5
aiolimiter==1.1.0
aiosignal==1.3.1
aiostream==0.5.2
altair==5.4.1
annotated-types==0.7.0
anyio==4.4.0
asgiref==3.8.1
async-timeout==4.0.3
asyncio==3.4.3
attrs==24.2.0
backoff==2.2.1
bcrypt==4.2.0
blinker==1.8.2
build==1.2.1
cachetools==5.5.0
certifi==2024.7.4
charset-normalizer==3.3.2
chroma-hnswlib==0.7.6
chromadb==0.5.5
click==8.1.7
coloredlogs==15.0.1
dataclasses==0.6
dataclasses-json==0.6.7
Deprecated==1.2.14
exceptiongroup==1.2.2
fastapi==0.112.2
filelock==3.15.4
FlashRank==0.2.9
flatbuffers==24.3.25
frozenlist==1.4.1
fsspec==2024.6.1
gitdb==4.0.11
GitPython==3.1.43
google-auth==2.34.0
googleapis-common-protos==1.65.0
greenlet==3.0.3
grpcio==1.66.1
grpclib==0.4.7
h11==0.14.0
h2==4.1.0
hpack==4.0.0
httpcore==1.0.5
httptools==0.6.1
httpx==0.27.2
httpx-sse==0.4.0
huggingface-hub==0.24.6
humanfriendly==10.0
hyperframe==6.0.1
ibm-generative-ai==3.0.0
idna==3.8
importlib_metadata==8.4.0
importlib_resources==6.4.4
Jinja2==3.1.4
jsonpatch==1.33
jsonpointer==3.0.0
jsonschema==4.23.0
jsonschema-specifications==2023.12.1
kubernetes==30.1.0
langchain==0.2.15
langchain-community==0.2.14
langchain-core==0.2.36
langchain-text-splitters==0.2.2
langsmith==0.1.106
markdown-it-py==3.0.0
MarkupSafe==2.1.5
marshmallow==3.22.0
mdurl==0.1.2
mmh3==4.1.0
modal==0.64.61
monotonic==1.6
mpmath==1.3.0
multidict==6.0.5
mypy-extensions==1.0.0
narwhals==1.5.5
numpy==1.26.4
oauthlib==3.2.2
onnxruntime==1.16.3
opentelemetry-api==1.27.0
opentelemetry-exporter-otlp-proto-common==1.27.0
opentelemetry-exporter-otlp-proto-grpc==1.27.0
opentelemetry-instrumentation==0.48b0
opentelemetry-instrumentation-asgi==0.48b0
opentelemetry-instrumentation-fastapi==0.48b0
opentelemetry-proto==1.27.0
opentelemetry-sdk==1.27.0
opentelemetry-semantic-conventions==0.48b0
opentelemetry-util-http==0.48b0
orjson==3.10.7
overrides==7.7.0
packaging==24.1
pandas==2.2.2
pillow==10.4.0
posthog==3.6.0
protobuf==4.25.4
pyarrow==17.0.0
pyasn1==0.6.0
pyasn1_modules==0.4.0
pydantic==2.8.2
pydantic_core==2.20.1
pydeck==0.9.1
Pygments==2.18.0
PyPika==0.48.9
pyproject_hooks==1.1.0
python-dateutil==2.9.0.post0
python-dotenv==1.0.1
pytz==2024.1
PyYAML==6.0.2
referencing==0.35.1
requests==2.32.3
requests-oauthlib==2.0.0
rich==13.8.0
rpds-py==0.20.0
rsa==4.9
shellingham==1.5.4
sigtools==4.0.1
six==1.16.0
smmap==5.0.1
sniffio==1.3.1
SQLAlchemy==2.0.32
starlette==0.38.2
streamlit==1.38.0
sympy==1.13.2
synchronicity==0.7.5
tenacity==8.5.0
tokenizers==0.20.0
toml==0.10.2
tomli==2.0.1
tornado==6.4.1
tqdm==4.66.5
tree-sitter==0.21.3
typer==0.12.5
types-certifi==2021.10.8.3
types-toml==0.10.8.20240310
typing-inspect==0.9.0
typing_extensions==4.12.2
tzdata==2024.1
urllib3==2.2.2
uvicorn==0.30.6
uvloop==0.20.0
watchfiles==0.24.0
websocket-client==1.8.0
websockets==13.0.1
wrapt==1.16.0
yarl==1.9.4
zipp==3.20.1
Platform - MacOS
Python 3.9.13
Beta Was this translation helpful? Give feedback.
All reactions