Getting started in LLMs with OpenAI
Also comparing results with Hugging Face API
In the previous blog, we discussed about how to get started in LLMs with Hugging Face.
Let’s revisit the framework in LangChain, but this time with arguably, the more popular OpenAI models.
These models are part of Open AI’s proprietary technology and carefully selected training data, which makes them more powerful in many cases.
Steps to access the Hugging Face API token
To follow-along, you’ll need an OpenAI API key. You can create your account on their signup page. The next step will be to navigate to the API keys page to create your secret key. Sometimes, OpenAI provides free credits, but that can differ depending on location.
Once you have the above logistics set-up, you’re ready to get started!
Build an LLM model using OpenAI API
To begin, we’ll import the necessary libraries.
!pip install langchain_openai
from langchain_openai import OpenAIOne great feature about Lang chain is its unified syntax, which makes it super easy to swap one model for another with only a small change in code. We’ll begin with defining the object that will store the OpenAI API key.
openai_api_key = "<Your OpenAI API Key>"Next, we’ll define an LLM using the default OpenAI model available on LangChain, `gpt-3.5-turbo-instruct`
llm = OpenAI(model_name="gpt-3.5-turbo-instruct", openai_api_key=openai_api_key)We have instantiated the llm object, and we’ll pass a text as an input, and then use the llm.invoke() function to predict the words based on the input provided. This is shown in the code below.
input = 'Health is wealth, but is it the order of priority for most of us?'
output = llm.invoke(input)
print(output)The above code will generate the following output:
Unfortunately, it’s not the priority for many. We all are aware of keeping ourselves healthy, but we tend to ignore it. We ignore our health and fitness by saying that we are busy with our work. We work for hours together, neglecting our health which is the most crucial factor in our lives. Why do we need to take care of our health? Let’s discuss why health is wealth. 1. Physical health Physical health is the most vital element of health. We all know the importance of physical health. Regular exercise and healthy food habits help us to stay fit. Our daily routine must include a minimum of 30 mins of physical exercise. It helps to keep diseases at bay. Good nutrition is an essential aspect of health. A balanced diet with all the necessary vitamins and minerals helps us to stay healthy. A healthy meal gives us the energy to work and keeps our immune system strong. 2. Mental health Mental health is also an essential aspect of health. A healthy body leads to a healthy mind. Regular exercise helps to reduce stress, anxiety, depression, and other mental problems. People who are physically active are less likely to suffer from mental illnesses. A healthy mind helps us to think and make decisions better. It also helps us to maintain good relationships.
You can see the response from the llm. Note this can be different at your end, but you get the sense of what to expect from the language model.
Let’s compare this with the output we got from the Hugging Face model.
Health is wealth, but is it the order of priority for most of us? We are all guilty of putting our health on the back burner. We all know that we should be eating healthier, exercising more, and getting enough sleep. But we all have our own priorities and we all have our own ways of prioritizing our lives. I have been working on my health for the past 5 years. I have been working on my health for the past 5 years. I have been working on my
Both the models are giving the response that health is often not the priority for us. However, the OpenAI model gives a more elaborate answer.
It’s to be noted that in this blog, we have used the default models. There are several language models available in both Hugging Face and OpenAI API; and the results and performance will vary based on choice of the model.
Please feel free to share your thoughts. And if you have tried out other models, do share the findings.
If you’re interested in statistics, data science and machine learning, you’ll like these blogs:
- Getting started with LLMs in Hugging Face
- Credit Risk Modelling in Python
- Exploring Credit Risk and IRFS9 Models
- Analyzing Loan Data with Binomial and Poisson Distributions in Python
- Mastering Credit Risk Analysis: A Step-by-Step Guide to Descriptive Statistics in Python
- Introduction to Hypothesis Testing
- Fraud Analytics — Strategies and Approaches
- Understanding Financial Risk Models: A Guide to Credit Risk, Stress Testing, and More
- The What, Why, and How of Generative AI
- Interview-Ready: Top Generative AI Questions You Need to Know
- 10 Movies to Binge-Watch for Data Science and AI Nerds!
- Sentiment Analysis in Python
- Frequently Asked Hypothesis Testing Questions for Data Scientist Interviews
- Frequently Asked Hypothesis Testing Interview Questions for Aspiring Data Scientists (Part 2)
- How to Transition into Data Science from a Non-Technical Background
You can also connect with me on LinkedIn.
#ConversationalAI #Chatbots #OpenAI #Python #LearningJourney #LLM #FinetuningLLMs #PromptEngineering #HuggingFace #LangChain #HuggingFace






