DEV Community

Cover image for LangChain: Not only ChatGPT, but how to use OpenAI to leverage your business
marcomaggiotti
marcomaggiotti

Posted on • Updated on

LangChain: Not only ChatGPT, but how to use OpenAI to leverage your business

What you will find here :

  • An introduction to Langchain with examples.
  • The concept of how to add value to your hidden data and documentation.
  • An overview of what GenAI can do nowadays.
  • Only prompt engineering will be used in this article with Langchain; the Agents, Chains, and Memory will be covered in the next ones.

As we can easily see the open source models of LLMs ( Large Language Models) are invading the market and the tech world, the question is :

IS YOUR BUSINESS READY TO ADOPT AND USE OPEN LLM MODELS?

When your competitors leverage the potential of the data they own, you really don't want to fall behind – believe me. So, I've written this for you.
I've written this article to highlight the potential of LangChain and provide an overview of what is available in the market, additionally, I aimed to democritize the concept and educate people about AI tools.

As I've mentioned in my previous articles, I prefer a practical approach over excessive blablabla. Here, you'll find a brief description of what LangChain is, but feel free to jump straight to the code if you prefer :D I don't mind.

Those contents are nothing more than what you can find easily in the net and in the original website or in the amazing course of Andrew NG DeepLearning.ai where I took several ideas and snippets.
There are countless articles repeating the same information, but my goal is to provide knowledge that you won't easily find elsewhere.

What is LangChain?

LangChain is a framework like a toolbox for computer programmers who work with artificial intelligence and language models. It helps them use big language models in their applications by connecting them to other data sources. This makes it easier to create apps that understand and use human language.

If you know how to program in Python, JavaScript, or TypeScript, you can use LangChain to leverage the potential of your applications. Has been offered to the open-source community, so is free to use and modify.

Why is LangChain important?

LangChain is like a magic wand for programmers who make apps that can talk and write like humans. It makes it simple to connect big language models to lots of data, making it easy for the apps to learn from the latest information. For example, if you have a talking AI, it can stay up to date with current events because of LangChain.

First of all you need to get the secret key from openAI :

OpenAi API KEY

Let's put our hands on the code :

We need to install the openai in the PyCharm Terminal :

pip install openai
Enter fullscreen mode Exit fullscreen mode

Tittoken is used by OpenAi :

pip install tiktoken 
Enter fullscreen mode Exit fullscreen mode

To load environmental variables :

pip install python-dotenv
Enter fullscreen mode Exit fullscreen mode

Fun fact: I had issues installing dotenv using PyCharm. So, if you encounter the same problem, you may need to change the interpreter to match the one on your local machine. This issue could be related to file permissions, although I wasn't able to investigate it thoroughly.

n.b.:
To change the interpreter in PyCharm you need to go
File-Settings-Project-Python Interpreter

pip install langchain
Enter fullscreen mode Exit fullscreen mode
import os
import openai
from dotenv import load_dotenv, find_dotenv

from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory

os.environ['OPENAI_API_KEY'] = 'YOUR-OPENAI-KEY'

# Normally you need to have a ".env" file with all the desired
# parameters, but for making it simple I put it here
_ = load_dotenv(find_dotenv()) # read local .env file

openai.api_key = os.environ['OPENAI_API_KEY']

# account for deprecation of LLM model
import datetime
# Get the current date
current_date = datetime.datetime.now().date()

# Define the date after which the model should be set to "gpt-3.5-turbo"
target_date = datetime.date(2024, 6, 12)

# Set the model variable based on the current date
if current_date > target_date:
    llm_model = "gpt-3.5-turbo"
else:
    llm_model = "gpt-3.5-turbo-0301"
Enter fullscreen mode Exit fullscreen mode

The Prompt Templates

The fundamental concept of LangChain is the orchestration of the prompt templates, models, agents and memory. Here we are going to
show prompt templates including their management and optimization.

As a first example I'd like introduce you to Chat prompt templates that take a list of chat messages as an input. Each chat message is associated with a role (e.g, AI, Human or System).

ChatPromptTemplate is pre-made starting conversation that direct the interaction beetween the bot and user, with this you can have a well-organized chat with the AI model. We can choose the topic, area of discussion, environment and more to increase the engagement and ensure that the conversation with the chatbot remains as meaninfull, engaging, and helpful as possible.

It's like having a roadmap for your conversation, making it simple steer, maintain the discussion in the right direction. In LangChain, we use message prompt templates to create and deal with interactions, so we can make the most of what the chat model can do.

System and Human prompts differ in their roles and purposes when interacting with chat models. SystemMessagePromptTemplate provides initial instructions, context, or data for the AI model, while HumanMessagePromptTemplate are messages from the user that the AI model responds to.

Let's check a Use Case App, like a peaceful translator for angry Tripadvisor comments :

import os
import openai

from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) # read local .env file
openai.api_key = os.environ['OPENAI_API_KEY']

from langchain.chat_models import ChatOpenAI

import datetime
# Get the current date
current_date = datetime.datetime.now().date()

# Define the date after which the model should be set to "gpt-3.5-turbo"
target_date = datetime.date(2024, 6, 12)

# Set the model variable based on the current date
if current_date > target_date:
    llm_model = "gpt-3.5-turbo"
else:
    llm_model = "gpt-3.5-turbo-0301"

# You can play with the temperature value later once 
# you will have a good result, we can talk how too approach 
# improvements in neural networks and AI in the next tutorials
chat = ChatOpenAI(temperature=0.0, model=llm_model)

# ChatPromptTemplate
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate

customer_style = """American english \
in a peaceful tone as a five stars receptionist
"""

template_string = """ 
Translate the text \
into a style that is {style}, replace the words and sentences that could appear offensive : \
{text}
"""

prompt_template = ChatPromptTemplate.from_template(template_string)
prompt_template.messages[0].prompt
prompt_template.messages[0].prompt.input_variables

customer_email = """
Do not stay in this absolute dump of a hotel, and I use the term hotel very loosely. 
I cannot convey strongly enough how disgusting this place is.blood stained headboards 
that have clearly been up since the world war (the first one), rude staff, windows that 
won't close, no hot water, broken furniture, dirty utensils, broken light fixings and 
actual disgusting garbage in the kettle. Pretty sure I'm going to end up with some sort of rash/ 
disease due to sanitation conditions similar to those of a homeless criminal squat. 
In summary...this place is a complete hole.

"""
customer_messages = prompt_template.format_messages(
                    style=customer_style,
                    text=customer_email)

print(type(customer_messages))
print(type(customer_messages[0]))
print(customer_messages[0])

customer_response = chat(customer_messages)

print(customer_response.content)
Enter fullscreen mode Exit fullscreen mode

I am completely aware that the comment is really rude, but it's a real one taken from the internet. I didn't hide the words, except for some that were really offensive, with the purpose of showing the potential and value of this solution :

As a five-star receptionist, I would not recommend staying at this hotel. While I understand that everyone has different preferences, I personally found the accommodations to be lacking. The headboards appeared to have some stains, and some of the furniture was broken. Additionally, the windows did not seem to close properly, and there were some issues with the hot water. The staff could have been more polite, and some of the utensils were not as clean as I would have liked. Lastly, I did notice some unsanitary conditions, such as a dirty kettle. Overall, I would suggest looking into other options before booking a stay here.

This is just a random example but it is evident how the text has been filtered and shaped in a better and more tolerant way, this to avoid unuseful conflicts or polemic for example and focus on the real feedback of the customer.

In the second example I want to show how is possible to adapt the prompt replacing values to customize the requests.

I want to have a menu realized by a Michelin star chef :
system_template = "You are an experienced cooking michelin chef"

Three parameter as an Input:

INPUTS:
courses: The number of courses that we want in our meal, 5, 7, or 9
nationality: The target cooking style by nationality
daysPerWeek: The number of days per week when the restaurant is open


from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate

def consultant_chef(courses, nationality, daysPerWeek):
    '''
    INPUTS:
        courses: The number of courses that we want in our meal, 5, 7, or 9
        nationality: The target cooking style by nationality
        daysPerWeek: The number of days per week when the restaurant is open
    '''

    system_template = "You are an experienced cooking michelin chef"
    system_message_prompt = SystemMessagePromptTemplate.from_template(system_template)

    human_template = "I would to prepare a menu with {courses} courses meal. My nationality is {nationality} " \
                     "and I would like a menu for {daysPerWeek} days per week. Every recipes need to cite the exact" \
                     " source, url, name of the recipe, restaurant or chef where the ispiration came from."
    human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)

    chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])

    request = chat_prompt.format_prompt(courses=courses, nationality=nationality, daysPerWeek=daysPerWeek).to_messages()

    chat = ChatOpenAI()
    result = chat(request)
    return result.content

print(consultant_chef(courses='5', nationality='italian', daysPerWeek='7'))
Enter fullscreen mode Exit fullscreen mode

You can think that you can replace the values from the Prompt Template with your own company data. You can have an automation that produce results processing your data and taking insights from.

Conclusions :

We can consider this framework as a tool that can assist our apps and business, it enables us to master the potential of ChatGPT while still allowing us to review the final result and use our business data according to our preferences.
A more complex and exhaustif use case involves to connecting our database and utilizing our local data. In the context of our Customer Feedback example we can apply it for different type of customers, by age, by job profile, by, etc.
For the menu app we can apply by cooking style, allergies, food intolerances, etc... The possibilities are really counteless, you name it.
Try to envision changing the values used in the code snippets automatically and iteratively, replace them with your own data that are just waiting to be utilized.

Finally some tips to how prepare a prompt :

  • define who will use the chat;

  • be clear on the topic you want and the result will be more precise;

  • isolate the core of the conversation that need to be engaged, don't be to general and dispersive

  • add customization, variables that can be replaced with every day values and personal information that you don't want to share with chatgpt.

  • test it and refactor it, check the final results and adapt it to the most valuable for your business that you can have

So enjoy the article and please please please, leave a feedback or remark if you need, don't like something or you think that there is something wrong. You can write also rude comments :D I will use the prompts to translate them in a more lovely way <3

p.s.: I will add a git repo in the next days, Italian and French version will come, sorry I am on my way to learn German :D
There are already the next articles in draft talking about how to connect real time database, how to extract insight from video, documents as pdf, words, audio and more. So let's keep in contact ;)

The following sources were used in this articles :
Cover
LangChainDocs
Andrew NG Amazing courses
TripAdvisor Comment used for the test

Top comments (0)