Hello Learners…
Welcome to the blog…
Table Of Contents
- Introduction
- EasyLLM: Migrating from OpenAI to HuggingFace Made Simple
- Clients Supported By EasyLLM
- EasyLLM Example Using Python
- How To Migrate from OpenAI to HuggingFace
- Summary
- References
Introduction
In this post, we discuss EasyLLM: Migrating from OpenAI to HuggingFace Made Simple.EasyLLM is an open-source project that provides helpful tools and methods for working with large language models (LLMs), both open-source and closed-source.
Language models have revolutionized the world of natural language processing, enabling sophisticated applications like machine translation, sentiment analysis, and text generation. With the rise of large language models (LLMs), such as OpenAI’s GPT series and huggingface’s Transformers, the capabilities of NLP have reached new heights.
EasyLLM: Migrating from OpenAI to HuggingFace Made Simple
EasyLLM implements clients that are compatible with OpenAI’s Completion API. This means you can easily replace openai.ChatCompletion
, openai.Completion
, openai.Embedding
with, for example, huggingface.ChatCompletion
, huggingface.Completion
or huggingface.Embedding
by changing one line of code.
The seamless switching between OpenAI’s Completion API and huggingface’s ChatCompletion, Completion, or Embedding with just a single line of code opens up new avenues for experimentation and creativity.
Clients Supported By EasyLLM
huggingface
– HuggingFace modelshuggingface.ChatCompletion
– Chat with LLMshuggingface.Completion
– Text completion with LLMshuggingface.Embedding
– Create embeddings with LLMs
EasyLLM Example Using Python
Requirements
We have to require an API key from the hugging face inference
- The module automatically loads the HuggingFace API key from the environment variable HUGGINGFACE_TOKEN or from the HuggingFace CLI configuration file.
- huggingface.api_key=”hf_xxx”
To use EasyLLM first we have to install it, we can install it using the ‘pip‘ python package manager.
pip install easyllm
Now create a file called app.py and paste the below code,
from easyllm.clients import huggingface
from easyllm.prompt_utils import build_llama2_prompt
# helper to build llama2 prompt
huggingface.prompt_builder = build_llama2_prompt
response = huggingface.ChatCompletion.create(
model="meta-llama/Llama-2-70b-chat-hf",
messages=[
{"role": "system", "content": "\nYou are a helpful assistant speaking like a pirate. argh!"},
{"role": "user", "content": "What is the sun?"},
],
temperature=0.9,
top_p=0.6,
max_tokens=256,
)
print(response)
And when we run the file we get the response below,
{
"id": "hf-lVC2iTMkFJ",
"object": "chat.completion",
"created": 1690661144,
"model": "meta-llama/Llama-2-70b-chat-hf",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": " Arrrr, the sun be a big ol' ball o' fire in the sky, me hearty! It be the source o' light and warmth for our fair planet, and it be a mighty powerful force, savvy? Without the sun, we'd be sailin' through the darkness, lost and cold, so let's give a hearty \"Yarrr!\" for the sun, me hearties! Arrrr!"
},
"finish_reason": null
}
],
"usage": {
"prompt_tokens": 111,
"completion_tokens": 299,
"total_tokens": 410
}
}
How To Migrate from OpenAI to HuggingFace
Migrating from OpenAI to HuggingFace is easy. Just change the import statement and the client you want to use and optionally the prompt builder.
- import openai
+ from easyllm.clients import huggingface
+ from easyllm.prompt_utils import build_llama2_prompt
+ huggingface.prompt_builder = build_llama2_prompt
- response = openai.ChatCompletion.create(
+ response = huggingface.ChatCompletion.create(
- model="gpt-3.5-turbo",
+ model="meta-llama/Llama-2-70b-chat-hf",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Knock knock."},
],
)
Make sure when you switch your client that your hyperparameters are still valid. For example, temperature
of GPT-3 might be different than temperature
of Llama-2
.
Summary
EasyLLM presents an exciting opportunity for developers and researchers to leverage the power of large language models in a simple and efficient manner.
Empower yourself to innovate, create, and make a lasting impact in the world of language processing.
Happy Learning And Keep Learning…
Thank You…