Skip to content

Commit

Permalink
tutorial111
Browse files Browse the repository at this point in the history
  • Loading branch information
ronidas39 committed Aug 28, 2024
1 parent e28fbb5 commit 89ec7cd
Show file tree
Hide file tree
Showing 2 changed files with 237 additions and 0 deletions.
237 changes: 237 additions & 0 deletions tutorial111/main.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.messages import AIMessage,HumanMessage\n",
"from langchain_openai import ChatOpenAI\n",
"from langchain_core.chat_history import BaseChatMessageHistory,InMemoryChatMessageHistory\n",
"from langchain_core.runnables.history import RunnableWithMessageHistory\n",
"llm=ChatOpenAI(model=\"gpt-4o\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"response=llm.invoke(\n",
" [\n",
" HumanMessage(content=\"the price of 1kg mango is 50 inr and 1kg apple is 60inr\"),\n",
" AIMessage(content=\"Ok i know the price of mango & apple , it is 50 inr for 1kg for mango and 60 inr for 1kg for apple\"),\n",
" HumanMessage(content=\"to buy 5kg apple how much money required ?\")\n",
" ]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'To buy 5 kg of apples at a price of 60 INR per kg, you would need:\\n\\n\\\\[ 5 \\\\text{ kg} \\\\times 60 \\\\text{ INR/kg} = 300 \\\\text{ INR} \\\\]\\n\\nSo, you would need 300 INR to buy 5 kg of apples.'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response.content"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"memory_store={}\n",
"def get_session_history(session_id: str) -> BaseChatMessageHistory:\n",
" if session_id not in memory_store:\n",
" memory_store[session_id]=InMemoryChatMessageHistory()\n",
" return memory_store[session_id]"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"config={\"configurable\": {\"session_id\": \"roni\"}}"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"Got it! I'll remember that the price of Bitcoin is $50,000. How can I assist you further?\""
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"runnableWithMessageHistory=RunnableWithMessageHistory(llm,get_session_history)\n",
"response=runnableWithMessageHistory.invoke([\n",
" HumanMessage(content=\"please remember the price of btcoin is 50000$\")\n",
"],config=config)\n",
"response.content"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'You mentioned earlier that the price of Bitcoin is $50,000. How can I assist you further with this information?'"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response=runnableWithMessageHistory.invoke([\n",
" HumanMessage(content=\"what is the price of bitcoin\")\n",
"],config=config)\n",
"response.content"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'roni': InMemoryChatMessageHistory(messages=[HumanMessage(content='please remember the price of btcoin is 50000$'), AIMessage(content=\"Got it! I'll remember that the price of Bitcoin is $50,000. How can I assist you further?\", response_metadata={'token_usage': {'completion_tokens': 23, 'prompt_tokens': 19, 'total_tokens': 42}, 'model_name': 'gpt-4o-2024-05-13', 'system_fingerprint': 'fp_157b3831f5', 'finish_reason': 'stop', 'logprobs': None}, id='run-fd59245c-ec38-4137-9704-9d2ddbabd04c-0', usage_metadata={'input_tokens': 19, 'output_tokens': 23, 'total_tokens': 42}), HumanMessage(content='what is the price of bitcoin'), AIMessage(content='You mentioned earlier that the price of Bitcoin is $50,000. How can I assist you further with this information?', response_metadata={'token_usage': {'completion_tokens': 24, 'prompt_tokens': 56, 'total_tokens': 80}, 'model_name': 'gpt-4o-2024-05-13', 'system_fingerprint': 'fp_a2ff031fb5', 'finish_reason': 'stop', 'logprobs': None}, id='run-f552628f-eab0-470b-9461-313b95f93990-0', usage_metadata={'input_tokens': 56, 'output_tokens': 24, 'total_tokens': 80})])}"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"memory_store"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"Understood! If you have any specific questions or need assistance related to your American Express (Amex) credit card, feel free to ask. Whether it's about rewards, fees, features, or general usage, I'm here to help. Just let me know what you need!\""
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"config={\"configurable\": {\"session_id\": \"bob\"}}\n",
"response=runnableWithMessageHistory.invoke([\n",
" HumanMessage(content=\"i am using amex credit card remember this\")\n",
"],config=config)\n",
"response.content"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"InMemoryChatMessageHistory(messages=[HumanMessage(content='please remember the price of btcoin is 50000$'), AIMessage(content=\"Got it! I'll remember that the price of Bitcoin is $50,000. How can I assist you further?\", response_metadata={'token_usage': {'completion_tokens': 23, 'prompt_tokens': 19, 'total_tokens': 42}, 'model_name': 'gpt-4o-2024-05-13', 'system_fingerprint': 'fp_157b3831f5', 'finish_reason': 'stop', 'logprobs': None}, id='run-fd59245c-ec38-4137-9704-9d2ddbabd04c-0', usage_metadata={'input_tokens': 19, 'output_tokens': 23, 'total_tokens': 42}), HumanMessage(content='what is the price of bitcoin'), AIMessage(content='You mentioned earlier that the price of Bitcoin is $50,000. How can I assist you further with this information?', response_metadata={'token_usage': {'completion_tokens': 24, 'prompt_tokens': 56, 'total_tokens': 80}, 'model_name': 'gpt-4o-2024-05-13', 'system_fingerprint': 'fp_a2ff031fb5', 'finish_reason': 'stop', 'logprobs': None}, id='run-f552628f-eab0-470b-9461-313b95f93990-0', usage_metadata={'input_tokens': 56, 'output_tokens': 24, 'total_tokens': 80}), HumanMessage(content='what is the price of bitcoin'), AIMessage(content='As of your last update, you mentioned the price of Bitcoin was $50,000. For the most accurate and up-to-date information, I recommend checking a reliable financial news source or cryptocurrency exchange.', response_metadata={'token_usage': {'completion_tokens': 40, 'prompt_tokens': 94, 'total_tokens': 134}, 'model_name': 'gpt-4o-2024-05-13', 'system_fingerprint': 'fp_c9aa9c0491', 'finish_reason': 'stop', 'logprobs': None}, id='run-7e1c6405-5530-4e74-867f-beee4d12cd1b-0', usage_metadata={'input_tokens': 94, 'output_tokens': 40, 'total_tokens': 134})])"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"memory_store[\"roni\"]"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"I'm sorry, but I don't have access to personal or private information about you, including the type of credit card you are using. If you need help determining the type of credit card you have, you can typically find this information on the card itself or by contacting your credit card issuer. Is there anything else I can assist you with?\""
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"config={\"configurable\": {\"session_id\": \"roni\"}}\n",
"response=runnableWithMessageHistory.invoke([\n",
" HumanMessage(content=\"what type of credit card i am using?\")\n",
"],config=config)\n",
"response.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Binary file added tutorial111/tutorial111.pptx
Binary file not shown.

0 comments on commit 89ec7cd

Please sign in to comment.