How To Chat With Langflow From A Python Script
Introduction
In this article we will demonstrate how to chat with Langflow from a Python script. The first example script, was generated by Langflow and shows how to send a single chat message to Langflow.
The second example, demonstrates how to have a conversation with Langflow. In the second example, the script maintains the session id. The session_id is stored and passed with each call to run_flow. This allows subsequent messages to be part of the same conversation.
Single Chat Message Example
The code for the example chat_example.py
- From the command prompt, the example script executed with the message "Why is the sky blue?"
python chat_example.py "why is the sky blue?"
- The JSON array response from the example script
{
"session_id": "6bd4d148-969b-4cb6-933b-7583da515d14",
"outputs": [
{
"inputs": {
"input_value": "why is the sky blue?"
},
"outputs": [
{
"results": {
"message": {
"text_key": "text",
"data": {
"text": "Arrr, ye be wonderin' why the sky be as blue as the deep sea, eh? Well, matey, 'tis all because of the way the sunlight dances through the air. Ye see, the sun's rays be made of all sorts of colors, but when they hit the atmosphere, the blue light be scattered about more than the others. So, when ye look up, it be the blue that fills yer eyes, just like the ocean on a clear day. Aye, 'tis a wondrous thing, the sky!",
"sender": "Machine",
"sender_name": "AI",
"session_id": "6bd4d148-969b-4cb6-933b-7583da515d14",
"files": [],
"timestamp": "2024-11-08 08:49:15",
"flow_id": "6bd4d148-969b-4cb6-933b-7583da515d14"
},
"default_value": "",
"text": "Arrr, ye be wonderin' why the sky be as blue as the deep sea, eh? Well, matey, 'tis all because of the way the sunlight dances through the air. Ye see, the sun's rays be made of all sorts of colors, but when they hit the atmosphere, the blue light be scattered about more than the others. So, when ye look up, it be the blue that fills yer eyes, just like the ocean on a clear day. Aye, 'tis a wondrous thing, the sky!",
"sender": "Machine",
"sender_name": "AI",
"files": [],
"session_id": "6bd4d148-969b-4cb6-933b-7583da515d14",
"timestamp": "2024-11-08 08:49:15",
"flow_id": "6bd4d148-969b-4cb6-933b-7583da515d14"
}
},
"artifacts": {
"message": "Arrr, ye be wonderin' why the sky be as blue as the deep sea, eh? Well, matey, 'tis all because of the way the sunlight dances through the air. Ye see, the sun's rays be made of all sorts of colors, but when they hit the atmosphere, the blue light be scattered about more than the others. So, when ye look up, it be the blue that fills yer eyes, just like the ocean on a clear day. Aye, 'tis a wondrous thing,
the sky!",
"sender": "Machine",
"sender_name": "AI",
"files": [],
"type": "object"
},
"outputs": {
"message": {
"message": {
"text": "Arrr, ye be wonderin' why the sky be as blue as the deep sea, eh? Well, matey, 'tis all because of the way the sunlight dances through the air. Ye see, the sun's rays be made of all sorts of colors, but when they hit the atmosphere, the blue light be scattered about more than the others. So, when ye look up, it be the blue that fills yer eyes, just like the ocean on a clear day. Aye, 'tis a wondrous thing, the sky!",
"sender": "Machine",
"sender_name": "AI",
"session_id": "6bd4d148-969b-4cb6-933b-7583da515d14",
"files": [],
"timestamp": "2024-11-08 08:49:15",
"flow_id": "6bd4d148-969b-4cb6-933b-7583da515d14"
},
"type": "object"
}
},
"logs": {
"message": []
},
"messages": [
{
"message": "Arrr, ye be wonderin' why the sky be as blue as the deep sea, eh? Well, matey, 'tis all because of the way the sunlight dances through the air. Ye see, the sun's rays be made of all sorts of
colors, but when they hit the atmosphere, the blue light be scattered about more than the others. So, when ye look up, it be the blue that fills yer eyes, just like the ocean on a clear day. Aye, 'tis a wondrous thing, the sky!",
"sender": "Machine",
"sender_name": "AI",
"session_id": "6bd4d148-969b-4cb6-933b-7583da515d14",
"component_id": "ChatOutput-45bZS",
"files": [],
"type": "message"
}
],
"component_display_name": "Chat Output",
"component_id": "ChatOutput-45bZS",
"used_frozen_result": false
}
]
}
]
}
Multiple Chat Messages Example
The code for the example chat_session.py
- From the command prompt, execute the python script
python chat_session.py
- Example output from the script
User: Why is the sky blue?
AI: Arrr, matey! The sky be blue 'cause o' the way the sunlight dances through the air, aye. When the sun's rays be hittin' the atmosphere, they scatter about, and the blue light be scatterin' more than the rest, givin' the sky its fine azure hue. So hoist the sails and enjoy the view, ye landlubber!
User: Can you tell me a joke?
AI: Arrr, why did the pirate go to school? To improve his arrr-ticulation! Har har har!
User: What's the weather like today?
AI: Arrr, matey! I be not havin' the latest weather charts in me treasure chest, but if ye be lookin' out to sea or checkin' the skies, ye might find yer answer. If the wind be blowin' fierce and the clouds be dark, batten down the hatches! If the sun be shinin' and the seas be calm, it be a fine day for sailin'. Keep yer spyglass handy and trust yer instincts, savvy?
User: Thank you!
AI: Arrr, ye be welcome, matey! If ye be needin' anythin' else, just give a holler! 🏴☠️