0% found this document useful (0 votes)
15 views5 pages

Prerequisites For All Programs: Install Required Packages

Uploaded by

ASR PANDEY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Prerequisites For All Programs: Install Required Packages

Uploaded by

ASR PANDEY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Prerequisites for All Programs

1. Install Required Packages: Open your terminal and run the following commands: bash

• pip install -U langchain-community pip install -U langchain-openai

• Set Up OpenAI API Key: Obtain your OpenAI API key from the OpenAI website if you don’t
have one.
• Add the API Key: You can set your API key in one of two ways:
• Environment Variable:
bash export OPENAI_API_KEY='your_openai_api_key_here' # Unix-

based systems bash

• set OPENAI_API_KEY='your_openai_api_key_here' # Windows

• Directly in Code: You can include the API key when initializing the ChatOpenAI instance
in each program.

Program 1: Simple Echo Bot python from

langchain_openai import ChatOpenAI

# Initialize the ChatOpenAI model with your API key and temperature setting
chat = ChatOpenAI(openai_api_key='your_openai_api_key_here', temperature=0.5)

# Define a function to echo back the user's input


def echo_bot(user_input):
# Generate a response from the model based on user input
response = chat.generate(user_input)
return response

# Example usage of the echo_bot function


print(echo_bot("Hello!"))

Program 2: Simple Keyword-based Response python


from langchain_openai import ChatOpenAI

# Initialize the ChatOpenAI model with your API key and temperature setting
chat = ChatOpenAI(openai_api_key='your_openai_api_key_here', temperature=0.5)

# Define a function that responds based on specific keywords in user input


def keyword_response(user_input): # Check for "hello" keyword in user
input if "hello" in user_input.lower(): return "Hi there! How
can I help you?" # Check for "bye" keyword in user input elif
"bye" in user_input.lower():
return "Goodbye! Have a great day!"
else:
# Generate a response from the model for other inputs
return chat.generate(user_input)

# Example usage of the keyword_response function


print(keyword_response("Hello!"))

Program 3: Simple FAQ Bot python from

langchain_openai import ChatOpenAI

# Initialize the ChatOpenAI model with your API key and temperature setting
chat = ChatOpenAI(openai_api_key='your_openai_api_key_here', temperature=0.5)

# Define a dictionary of frequently asked questions and their answers


faq = {
"what is your name?": "I am a chatbot created using LangChain.",
"how can I reset my password?": "You can reset your password by clicking on
'Forgot Password'.",
}

# Define a function to respond to FAQs


def faq_bot(user_input):
# Look up the user input in the FAQ dictionary
response = faq.get(user_input.lower(), "I'm sorry, I don't have an answer
for that.")
return response

# Example usage of the faq_bot function


print(faq_bot("What is your name?"))

Program 4: Simple Conversational Flow


python from langchain_openai import

ChatOpenAI

# Initialize the ChatOpenAI model with your API key and temperature setting
chat = ChatOpenAI(openai_api_key='your_openai_api_key_here', temperature=0.7)

# Define a function to facilitate a conversation with the user


def conversation_flow(): print("Welcome to the chatbot!")
# Greet the user
while True: user_input = input("You: ") # Get
user input # Check if the user wants to exit the
conversation if user_input.lower() == "exit":
print("Goodbye!") # Farewell message
break
# Generate a response from the model based on user input
response = chat.generate(user_input)
print("Bot:", response) # Print the bot's response
# Start the conversation flow
conversation_flow()

Program 5: Memory-Enabled Bot python

from langchain_openai import ChatOpenAI

# Initialize the ChatOpenAI model with your API key and temperature setting
chat = ChatOpenAI(openai_api_key='your_openai_api_key_here', temperature=0.5)

# Initialize an empty list to store memory of user inputs


memory = []

# Define a function that remembers user inputs def


memory_bot(user_input): memory.append(user_input) #
Store user input in memory # Generate a response from
the model based on user input
response = chat.generate(user_input)
return response

# Example usage of the memory_bot function


print(memory_bot("What's your name?"))
print("Memory:", memory) # Print the stored memory

Program 6: Enhanced FAQ with Context


python from langchain_openai import

ChatOpenAI

# Initialize the ChatOpenAI model with your API key and temperature setting
chat = ChatOpenAI(openai_api_key='your_openai_api_key_here', temperature=0.5)

# Define a dictionary of FAQs


faq = {
"what is your name?": "I am a chatbot created using LangChain.",
"how can I reset my password?": "You can reset your password by clicking on
'Forgot Password'.",
}

# Define a function that checks FAQs and generates responses


def faq_with_context(user_input):
# Look for a response in the FAQ dictionary
response = faq.get(user_input.lower()) if response:
return response # Return the FAQ answer if found
else:
# Generate a response from the model if not found in FAQs
return chat.generate(user_input)

# Example usage of the faq_with_context function


print(faq_with_context("How can I reset my password?"))
print(faq_with_context("Tell me about your features."))
Program 7: Multi-turn Conversation with Context
python from langchain_openai import ChatOpenAI

# Initialize the ChatOpenAI model with your API key and temperature setting
chat = ChatOpenAI(openai_api_key='your_openai_api_key_here', temperature=0.5)

# Define a function to handle multi-turn conversations def


multi_turn_conversation(): context = [] # Initialize a list to
store conversation context print("Welcome! You can start
chatting.") # Greet the user while True: user_input =
input("You: ") # Get user input # Check if the user wants
to exit the conversation if user_input.lower() == "exit":
print("Goodbye!") # Farewell message
break
context.append(user_input) # Add user input to context
# Generate a response based on the entire conversation context
response = chat.generate(" ".join(context))
print("Bot:", response) # Print the bot's response

# Start the multi-turn conversation


multi_turn_conversation()

Program 8: Customizing Responses with Intent Recognition


python from langchain_openai import ChatOpenAI

# Initialize the ChatOpenAI model with your API key and temperature setting
chat = ChatOpenAI(openai_api_key='your_openai_api_key_here', temperature=0.5)

# Define a function that recognizes user intents


def recognize_intent(user_input): # Check
for specific keywords in user input if
"help" in user_input.lower():
return "How can I assist you?" # Respond to help request
elif "info" in user_input.lower(): return "What information are
you looking for?" # Respond to info
request
else:
# Generate a response from the model for other inputs
return chat.generate(user_input)

# Example usage of the recognize_intent function


print(recognize_intent("I need some help.")) print(recognize_intent("Tell
me more about this."))

Program 9: Chatbot with Sentiment Analysis python

from langchain_openai import ChatOpenAI


from langchain.sentiment_analysis import SentimentAnalyzer
# Initialize the ChatOpenAI model with your API key and temperature setting
chat = ChatOpenAI(openai_api_key='your_openai_api_key_here', temperature=0.5)
sentiment_analyzer = SentimentAnalyzer() # Initialize sentiment analyzer

# Define a function that responds based on sentiment analysis def


sentiment_bot(user_input): sentiment =
sentiment_analyzer.analyze(user_input) # Analyze sentiment of
user input if sentiment == "positive": return "I'm glad to hear
that! How can I help further?" # Respond to
positive sentiment elif sentiment == "negative": return "I'm sorry
to hear that. What can I do to assist you?" # Respond to negative sentiment
else:
# Generate a response from the model for neutral sentiment
return chat.generate(user_input)

# Example usage of the sentiment_bot function


print(sentiment_bot("I love this!"))
print(sentiment_bot("I'm not happy with the service."))

Program 10: Full-fledged Chatbot with Dynamic Learning


python from langchain_openai import ChatOpenAI

# Initialize the ChatOpenAI model with your API key and temperature setting
chat = ChatOpenAI(openai_api_key='your_openai_api_key_here', temperature=0.5)
faq = {} # Initialize an empty dictionary for dynamic FAQs

# Define a function that allows adding FAQs dynamically


def dynamic_learning_bot(user_input): # Check if
user input is an instruction to add a FAQ if
user_input.lower().startswith("add faq"):
# Extract question and answer from the user input
_, question, answer = user_input.split('|')
faq[question.strip()] = answer.strip() # Store the new FAQ
return "FAQ added!" # Confirmation message
else:
# Look up the user input in the FAQ dictionary
response = faq.get(user_input.lower()) if
response:
return response # Return the FAQ answer if found
else:
# Generate a response from the model if not found in FAQs
return chat.generate(user_input)
# Example usage of the dynamic_learning_bot function
print(dynamic_learning_bot("add faq | What is your

You might also like