Notebook to demonstrate Zero shot and Few shot Learning

%pip install langchain langchain_groq
Collecting langchain
  Downloading langchain-0.2.13-py3-none-any.whl (997 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 997.8/997.8 kB 3.7 MB/s eta 0:00:00a 0:00:01
Collecting langchain_groq
  Downloading langchain_groq-0.1.9-py3-none-any.whl (14 kB)
Requirement already satisfied: PyYAML>=5.3 in /Users/nipun/mambaforge/lib/python3.10/site-packages (from langchain) (6.0)
Collecting SQLAlchemy<3,>=1.4 (from langchain)
  Downloading SQLAlchemy-2.0.32-cp310-cp310-macosx_11_0_arm64.whl (2.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.1/2.1 MB 6.9 MB/s eta 0:00:00a 0:00:01
Collecting aiohttp<4.0.0,>=3.8.3 (from langchain)
  Downloading aiohttp-3.10.3-cp310-cp310-macosx_11_0_arm64.whl (388 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 388.5/388.5 kB 6.9 MB/s eta 0:00:00a 0:00:01
Collecting async-timeout<5.0.0,>=4.0.0 (from langchain)
  Downloading async_timeout-4.0.3-py3-none-any.whl (5.7 kB)
Collecting langchain-core<0.3.0,>=0.2.30 (from langchain)
  Downloading langchain_core-0.2.30-py3-none-any.whl (384 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 384.8/384.8 kB 7.8 MB/s eta 0:00:00a 0:00:01
Collecting langchain-text-splitters<0.3.0,>=0.2.0 (from langchain)
  Downloading langchain_text_splitters-0.2.2-py3-none-any.whl (25 kB)
Collecting langsmith<0.2.0,>=0.1.17 (from langchain)
  Downloading langsmith-0.1.99-py3-none-any.whl (140 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 140.4/140.4 kB 13.7 MB/s eta 0:00:00
Requirement already satisfied: numpy<2,>=1 in /Users/nipun/mambaforge/lib/python3.10/site-packages (from langchain) (1.24.3)
Collecting pydantic<3,>=1 (from langchain)
  Downloading pydantic-2.8.2-py3-none-any.whl (423 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 423.9/423.9 kB 8.0 MB/s eta 0:00:00a 0:00:01
Requirement already satisfied: requests<3,>=2 in /Users/nipun/mambaforge/lib/python3.10/site-packages (from langchain) (2.29.0)
Requirement already satisfied: tenacity!=8.4.0,<9.0.0,>=8.1.0 in /Users/nipun/mambaforge/lib/python3.10/site-packages (from langchain) (8.2.3)
Collecting groq<1,>=0.4.1 (from langchain_groq)
  Downloading groq-0.9.0-py3-none-any.whl (103 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 103.5/103.5 kB 2.5 MB/s eta 0:00:0000:01
Collecting aiohappyeyeballs>=2.3.0 (from aiohttp<4.0.0,>=3.8.3->langchain)
  Downloading aiohappyeyeballs-2.3.5-py3-none-any.whl (12 kB)
Collecting aiosignal>=1.1.2 (from aiohttp<4.0.0,>=3.8.3->langchain)
  Downloading aiosignal-1.3.1-py3-none-any.whl (7.6 kB)
Requirement already satisfied: attrs>=17.3.0 in /Users/nipun/mambaforge/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (23.1.0)
Collecting frozenlist>=1.1.1 (from aiohttp<4.0.0,>=3.8.3->langchain)
  Downloading frozenlist-1.4.1-cp310-cp310-macosx_11_0_arm64.whl (52 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 52.2/52.2 kB 7.1 MB/s eta 0:00:00
Requirement already satisfied: multidict<7.0,>=4.5 in /Users/nipun/mambaforge/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.4)
Requirement already satisfied: yarl<2.0,>=1.0 in /Users/nipun/mambaforge/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.2)
Requirement already satisfied: anyio<5,>=3.5.0 in /Users/nipun/mambaforge/lib/python3.10/site-packages (from groq<1,>=0.4.1->langchain_groq) (3.6.2)
Collecting distro<2,>=1.7.0 (from groq<1,>=0.4.1->langchain_groq)
  Downloading distro-1.9.0-py3-none-any.whl (20 kB)
Requirement already satisfied: httpx<1,>=0.23.0 in /Users/nipun/mambaforge/lib/python3.10/site-packages (from groq<1,>=0.4.1->langchain_groq) (0.23.3)
Requirement already satisfied: sniffio in /Users/nipun/mambaforge/lib/python3.10/site-packages (from groq<1,>=0.4.1->langchain_groq) (1.3.0)
Collecting typing-extensions<5,>=4.7 (from groq<1,>=0.4.1->langchain_groq)
  Downloading typing_extensions-4.12.2-py3-none-any.whl (37 kB)
Collecting jsonpatch<2.0,>=1.33 (from langchain-core<0.3.0,>=0.2.30->langchain)
  Downloading jsonpatch-1.33-py2.py3-none-any.whl (12 kB)
Collecting packaging<25,>=23.2 (from langchain-core<0.3.0,>=0.2.30->langchain)
  Downloading packaging-24.1-py3-none-any.whl (53 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 54.0/54.0 kB 1.1 MB/s eta 0:00:00ta 0:00:01
Collecting orjson<4.0.0,>=3.9.14 (from langsmith<0.2.0,>=0.1.17->langchain)
  Downloading orjson-3.10.7-cp310-cp310-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl (251 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 251.3/251.3 kB 9.1 MB/s eta 0:00:00
Collecting annotated-types>=0.4.0 (from pydantic<3,>=1->langchain)
  Downloading annotated_types-0.7.0-py3-none-any.whl (13 kB)
Collecting pydantic-core==2.20.1 (from pydantic<3,>=1->langchain)
  Downloading pydantic_core-2.20.1-cp310-cp310-macosx_11_0_arm64.whl (1.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 6.0 MB/s eta 0:00:00a 0:00:01
Requirement already satisfied: charset-normalizer<4,>=2 in /Users/nipun/mambaforge/lib/python3.10/site-packages (from requests<3,>=2->langchain) (3.1.0)
Requirement already satisfied: idna<4,>=2.5 in /Users/nipun/mambaforge/lib/python3.10/site-packages (from requests<3,>=2->langchain) (3.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/nipun/mambaforge/lib/python3.10/site-packages (from requests<3,>=2->langchain) (1.26.15)
Requirement already satisfied: certifi>=2017.4.17 in /Users/nipun/mambaforge/lib/python3.10/site-packages (from requests<3,>=2->langchain) (2023.5.7)
Requirement already satisfied: httpcore<0.17.0,>=0.15.0 in /Users/nipun/mambaforge/lib/python3.10/site-packages (from httpx<1,>=0.23.0->groq<1,>=0.4.1->langchain_groq) (0.16.3)
Requirement already satisfied: rfc3986[idna2008]<2,>=1.3 in /Users/nipun/mambaforge/lib/python3.10/site-packages (from httpx<1,>=0.23.0->groq<1,>=0.4.1->langchain_groq) (1.5.0)
Requirement already satisfied: jsonpointer>=1.9 in /Users/nipun/mambaforge/lib/python3.10/site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.3.0,>=0.2.30->langchain) (2.0)
Requirement already satisfied: h11<0.15,>=0.13 in /Users/nipun/mambaforge/lib/python3.10/site-packages (from httpcore<0.17.0,>=0.15.0->httpx<1,>=0.23.0->groq<1,>=0.4.1->langchain_groq) (0.14.0)
Installing collected packages: typing-extensions, packaging, orjson, jsonpatch, frozenlist, distro, async-timeout, annotated-types, aiohappyeyeballs, SQLAlchemy, pydantic-core, aiosignal, pydantic, aiohttp, langsmith, groq, langchain-core, langchain-text-splitters, langchain_groq, langchain
  Attempting uninstall: typing-extensions
    Found existing installation: typing_extensions 4.5.0
    Uninstalling typing_extensions-4.5.0:
      Successfully uninstalled typing_extensions-4.5.0
  Attempting uninstall: packaging
    Found existing installation: packaging 23.1
    Uninstalling packaging-23.1:
      Successfully uninstalled packaging-23.1
  Attempting uninstall: jsonpatch
    Found existing installation: jsonpatch 1.32
    Uninstalling jsonpatch-1.32:
      Successfully uninstalled jsonpatch-1.32
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
jupyter 1.0.0 requires qtconsole, which is not installed.
streamlit 1.31.0 requires packaging<24,>=16.8, but you have packaging 24.1 which is incompatible.
Successfully installed SQLAlchemy-2.0.32 aiohappyeyeballs-2.3.5 aiohttp-3.10.3 aiosignal-1.3.1 annotated-types-0.7.0 async-timeout-4.0.3 distro-1.9.0 frozenlist-1.4.1 groq-0.9.0 jsonpatch-1.33 langchain-0.2.13 langchain-core-0.2.30 langchain-text-splitters-0.2.2 langchain_groq-0.1.9 langsmith-0.1.99 orjson-3.10.7 packaging-24.1 pydantic-2.8.2 pydantic-core-2.20.1 typing-extensions-4.12.2
Note: you may need to restart the kernel to use updated packages.
import pandas as pd 
from langchain_groq.chat_models import ChatGroq
import json
with open("../secrets.json") as f:
    secrets = json.load(f)   
Groq_Token = secrets["groq"]
# Groq API and Models 

groq_models = {"llama3-70b": "llama3-70b-8192", "mixtral": "mixtral-8x7b-32768", "gemma-7b": "gemma-7b-it","llama3.1-70b":"llama-3.1-70b-versatile","llama3-8b":"llama3-8b-8192","llama3.1-8b":"llama-3.1-8b-instant","gemma-9b":"gemma2-9b-it"}

NOTE : DO NOT SHARE THE API KEY WITH ANYONE. DO NOT COMMIT THE API KEY TO GITHUB.

Always do a sanity check before committing the code to github. If the key is found in the code, you will be penalized with a 0.5 marks deduction.

Zero Shot

# Statement 
sentence = "The product quality is amazing but the delivery was delayed. However I am happy with the customer service."

sentence_2 = "The product quality is amazing and the delivery was not delayed. I am happy with the customer service."
# System Prompts 
query = f"""
* You are a sentiment analysis model. 
* Your task is to analyze the sentiment expressed in the given text and classify it as 'positive', 'negative', or 'neutral'. 
* Provide the sentiment label and, if necessary, a brief explanation of your reasoning.

Sentence: {sentence}
""" 


# To use Groq LLMs 
model_name = "llama3-70b" # We can choose any model from the groq_models dictionary
llm = ChatGroq(model=groq_models[model_name], api_key=Groq_Token, temperature=0)
answer = llm.invoke(query)

print(answer.content)
Sentiment label: Neutral

Explanation: The sentence expresses mixed sentiments. The words "amazing" and "happy" convey a positive sentiment, indicating satisfaction with the product quality and customer service. However, the phrase "delivery was delayed" expresses a negative sentiment, indicating dissatisfaction with the delivery experience. Overall, the positive and negative sentiments balance each other out, resulting in a neutral sentiment label.

Few Shot

# Statement 
sentence = "The product quality is amazing but the delivery was delayed. However I am happy with the customer service."

# System Prompts 
query = f"""
* You are a sentiment analysis model. 
* Your task is to analyze the sentiment expressed in the given text and classify it as 'positive', 'negative', or 'neutral'. 
* Provide the sentiment label and, if necessary, a brief explanation of your reasoning.

Here are few examples:
1. Sentence: 'The customer service was excellent, and I received my order quickly.'
Sentiment: Positive

2. Sentence: 'The food was bland and the service was slow.'
Sentiment: Negative

3. Sentence: 'The product is okay, but it's not worth the price.'
Sentiment: Neutral

Sentence: {sentence}
""" 

# To use Groq LLMs 
model_name = "llama3-70b" # We can choose any model from the groq_models dictionary
llm = ChatGroq(model=groq_models[model_name], api_key=Groq_Token, temperature=0)
answer = llm.invoke(query)

print(answer.content)
Sentiment: Positive

Explanation: Although the sentence mentions a negative aspect ("the delivery was delayed"), the positive sentiments ("The product quality is amazing" and "I am happy with the customer service") outweigh the negative one, resulting in an overall positive sentiment. The use of the word "amazing" and "happy" also indicates a strong positive emotion, which contributes to the positive sentiment classification.