Python LangChain: Create a Full-fledged App

Soumya Agarwal
By
Soumya Agarwal
I'm a BTech graduate from IIITM Gwalior. I have been actively working with large MNCs like ZS and Amazon. My development skills include Android and Python...
10 Min Read

In this tutorial, we have provided the basic code to create the LangChain ChatBot app. You’ll find comprehensive examples, instructions, and guidance to help you.

LangChain ChatBot App

Today, we’ll try to create a chatbot that generates poems based on user-provided prompts. Firstly, let’s try to add a boilerplate code to create a simple app.

A Chatbot that Generates Poems

If you have not yet set up the LangChain, then please install it first. Run the simple pip command to fetch the library from PyPi’s default repository. After that, you can follow the example code of a chatbot that generates poems based on user-provided prompts.

1. Installation:

  • Install LangChain using pip: pip install langchain

2. Import necessary libraries:

Hello, just for your note we are using the text-davinci-003 model here. It is a large language model (LLM) from OpenAI. More importantly, it provides the ability to chat, write poems, translate languages, and answer your questions.

import langchain
from langchain.llms import OpenAI

# Choose your desired LLM (replace "text-davinci-003" if needed)
llm = OpenAI(temperature=0.7, model_name="text-davinci-003")

3. Define a function to generate poems:

def generate_poem(prompt):
    poem = llm.run(prompt, max_tokens=150, stop=None)  # Adjust max_tokens as needed
    return poem.text.strip()

4. Create a basic user interface:

while True:
    prompt = input("Enter a poem prompt (or 'quit' to exit): ")
    if prompt.lower() == "quit":
        break

    poem = generate_poem(prompt)
    print("\nGenerated poem:\n", poem)

5. Run the code:

  • Save the code as a Python file (e.g., langchain_poetry.py).
  • Execute it in your terminal: python langchain_poetry.py

There are still many things like the ones below that can be added to the LangChain ChatBot.

More Enriched ChatBot Code

We’ll add some of the below additions to our ChatBot app.

  • Experiment with temperatures and prompts: Adjust the temperature parameter for creativity and try different prompts to explore the LLM’s capabilities.
  • Handle errors and API limits: Implement error handling and consider API usage limits.
  • Enhance functionality: Add features like keyword filtering, multi-line prompts, or user feedback loops.
  • Explore other LLMs: LangChain supports various LLMs; try different ones for different tasks.
  • Consider security and privacy: Be mindful of potential risks when handling user input and LLM responses.

Let’s now get into the code part.

import langchain
from langchain.llms import OpenAI

llm = OpenAI(temperature=0.7, model_name="text-davinci-003")

def generate_response(prompt):
    response = llm.run(prompt, max_tokens=150, stop=None)
    return response.text.strip()

def handle_user_input():
    prompt = input("You: ")

    if prompt.lower() == "quit":
        print("Chatbot: Goodbye!")
        return False

    response = generate_response(prompt)
    print("Chatbot:", response)
    return True

while True:
    if not handle_user_input():
        break

The core changes we made are:

  • Continuous conversation loop: The while True loop keeps the conversation going until the user enters “quit.”
  • User input handling: The handle_user_input function takes user input, generates a response, and prints it.
  • Exit condition: The if prompt.lower() == "quit": statement allows the user to gracefully exit the conversation.

Also, to take the chatbot closer to a working app, we can add several features:

1. Prompt variations and user guidance

The LanghainChatbot app takes conversation customization to the next level. It uses both pre-written prompts and user-driven guidance to steer the chat in your desired direction. Imagine choosing an existing prompt like “Tell me a joke” to instantly spark humor, or injecting your own keywords and setting the mood – like asking for “calm and informative” travel advice. This flexibility lets you mold the chatbot’s responses to your specific needs and preferences, making each interaction truly your own.

  • Offer different poem styles (haiku, sonnet, etc.) through pre-defined prompts or user selection.
  • Provide examples of successful prompts to guide users.
  • Implement keyword detection to trigger specific poem forms based on user input.

2. User feedback and iteration

In the Langchain Chatbot app, your voice matters. It’s designed to actively learn and evolve from your feedback, ensuring a continuous improvement cycle. Each time you interact with the chatbot, you have the opportunity to provide valuable feedback on its responses. This feedback is carefully analyzed and used to refine the chatbot’s language model, making it smarter and more responsive over time. Think of it as a personal language coach that gets better with every conversation, tailoring its responses to your unique preferences and needs.

  • Allow users to rate generated poems (good/bad) to provide feedback to the LLM.
  • Use the feedback to adjust the temperature or refine the prompt for the next iteration.
  • Offer options to revise specific lines or verses within the poem.

3. Additional functionalities

Langchain Chatbot lets you customize conversations, provide feedback, and get things done.

With a variety of prompts and guidance options, you can steer the conversation in any direction you like. Your feedback helps the chatbot learn and improve over time. And with additional functionalities like booking flights and translating languages, you can get things done without leaving the chat.

In short, Langchain Chatbot is a powerful tool that can help you have smarter conversations and get more done.

  • Integrate a rhyming dictionary to ensure rhyme schemes in relevant poem styles.
  • Implement sentiment analysis to adjust the poem’s tone based on the user’s prompt.
  • Add sharing options to let users save or share their favorite poems.

4. User interface enhancement

  • Consider using a graphical user interface (GUI) instead of command-line input for a more user-friendly experience.
  • Display the generated poem in a visually appealing format (different fonts, colors, etc.).
  • Add animations or interactive elements to enhance the experience.

5. Deployment and accessibility

  • Consider web app development to make the chatbot accessible online.
  • Integrate with messaging platforms like Telegram or Discord for wider access.
  • Add text-to-speech functionality to read the poems aloud.

Remember:

  • These are just suggestions, and the final features depend on your specific goals and target audience.
  • Carefully assess the costs and limitations of using LLMs like OpenAI for commercial applications.
  • Continuously test and refine the chatbot to ensure a smooth and enjoyable user experience.

With these additions, you can transform the basic chatbot into a more engaging and interactive application, offering personalized poem generation for a wider audience.

More Enhanced LangChain ChatBot

Here is the code for a more enhanced version of the ChatBot.

import langchain
from langchain.llms import OpenAI

llm = OpenAI(temperature=0.7, model_name="text-davinci-003")

def identify_poem_style(prompt):
    """Identifies the poem style based on keywords in the prompt."""
    poem_styles = {
        "haiku": ["haiku", "5-7-5"],
        "sonnet": ["sonnet", "14 lines", "iambic pentameter"],
        # Add more styles as needed
    }
    for style, keywords in poem_styles.items():
        if any(keyword in prompt.lower() for keyword in keywords):
            return style
    return None  # No style identified

def add_style_guidance(prompt, poem_style):
    """Adds prompts or examples to guide the LLM towards the desired style."""
    if poem_style:
        style_guidance = {
            "haiku": "Write a haiku about",
            "sonnet": "Compose a sonnet in iambic pentameter on the topic of",
            # Add guidance for other styles
        }
        return prompt + style_guidance[poem_style]
    else:
        return prompt  # No style guidance needed

def generate_response(prompt):
    response = llm.run(prompt, max_tokens=150, stop=None)
    return response.text.strip()

def handle_user_input():
    prompt = input("You: ")

    if prompt.lower() in ["quit", "exit"]:
        print("Chatbot: Goodbye!")
        return False

    poem_style = identify_poem_style(prompt)  # New function for style detection
    prompt = add_style_guidance(prompt, poem_style)  # New function for prompt guidance

    response = generate_response(prompt)
    print_enhanced_poem(response)  # New function for visual formatting

    feedback = get_user_feedback()  # New function for feedback
    if feedback == "bad":
        # Offer options to revise or refine the prompt

    return True

# ... other functions for style detection, prompt guidance, visual formatting, and feedback handling

while True:
    if not handle_user_input():
        break

Key additions:

  • Identify poem style: The identify_poem_style function analyzes the prompt for keywords to determine the desired poem form (e.g., haiku, sonnet).
  • Add style guidance: The add_style_guidance function appends appropriate prompts or examples to the user’s input to guide the LLM toward the desired style.
  • Print enhanced poem: The print_enhanced_poem function formats the generated poem with visual enhancements (e.g., spacing, colors, or fonts).
  • Get user feedback: The get_user_feedback function prompts the user to rate the generated poem and provides options for revision if needed.

Refer to these resources if you are enthusiastic about creating LangChain applications:
Introduction to LangChain: How to Use With Python
How to Create LangChain Agent in Python
LangChain Memory Basics Explained

Before You Leave

It is just the beginning, you can add more functionality, take it further, or even write better code and create one of the most engaging LangChain ChatBots.

Lastly, our site needs your support to remain free. Share this post on social media (Linkedin/Facebook) if you gained some knowledge from this tutorial.

Enjoy coding,
TechBeamers.

Share This Article
Subscribe
Notify of
guest

0 Comments
Newest
Oldest
Inline Feedbacks
View all comments