️ DEPRECATED GITLAB INSTANCE ️ This GitLab is now read-only for reference. Please use https://gitlab.iauro.co for all new work.

Migration completed on September 17, 2025

Commit 5938c798 authored by Pratiksha Patil's avatar Pratiksha Patil

6th Assignment of Prompt Engineering

parent 0976d334
OPENAI_API_KEY=your_openai_key_here
# Prompt Engineering Assignment
## 📌 Objective
Create and test reusable prompt templates for:
- Sentiment Analysis
- FAQ Answering
## 🛠️ Tools Used
- OpenAI GPT-4 API
- LangChain
- Python
- Nemo Guardrails
## 🧪 Run Examples
### 1. Setup
```bash
pip install -r requirements.txt
```
### 2. Run Sentiment Analysis Prompts
```bash
python prompts/sentiment_zero_few_shot.py
python prompts/langchain_sentiment.py
```
### 3. Run FAQ Answering
```bash
python prompts/faq_prompting.py
python prompts/langchain_faq.py
```
### 4. Run All Tests
```bash
python test_cases/run_all_tests.py
```
input:
flows:
- name: deny-sensitive
type: regex
pattern: ".*(ssn|credit card|password).*"
action: block
import openai
import os
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
def ask(prompt):
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}]
)
print("Prompt:\n", prompt)
print("Response:\n", response['choices'][0]['message']['content'])
ask("What is the return policy?")
few_shot = """
Q: What is the warranty?
A: 12 months.
Q: Can I return after 30 days?
A: Yes, up to 45 days.
Q: What is the return policy?
"""
ask(few_shot)
ask("You are a customer service agent. What is the return policy?")
from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
import os
llm = ChatOpenAI(model_name="gpt-4", temperature=0)
template = PromptTemplate(
input_variables=["question"],
template="""
You are an intelligent FAQ assistant.
Q: {question}
A:"""
)
chain = LLMChain(llm=llm, prompt=template)
print(chain.run("How long is the warranty?"))
print(chain.run("Can I cancel my subscription anytime?"))
from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
import os
llm = ChatOpenAI(model_name="gpt-4", temperature=0)
template = PromptTemplate(
input_variables=["sentence"],
template="Classify sentiment: \"{sentence}\". Respond with Positive/Negative/Neutral."
)
chain = LLMChain(llm=llm, prompt=template)
print(chain.run("The product is amazing!"))
print(chain.run("Worst experience ever."))
import openai
import os
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
def ask(prompt):
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}]
)
print("Prompt:\n", prompt)
print("Response:\n", response['choices'][0]['message']['content'])
ask("Classify sentiment: 'I love this product'. Respond with Positive/Negative/Neutral.")
few_shot = """
Classify sentiment:
\"I hate it.\" -> Negative
\"It's fine.\" -> Neutral
\"I love it!\" -> Positive
Sentence: \"I am disappointed.\"
"""
ask(few_shot)
cot_prompt = """
Step-by-step analyze:
Sentence: \"The experience was terrible and support was rude.\"
Step 1: Identify keywords → terrible, rude
Step 2: Tone is negative
Sentiment: Negative
"""
ask(cot_prompt)
openai
python-dotenv
langchain
nemo-guardrails
print("\n>>> TEST: Sentiment (Zero/Few/CoT)")
exec(open("prompts/sentiment_zero_few_shot.py").read())
print("\n>>> TEST: FAQ Prompting")
exec(open("prompts/faq_prompting.py").read())
print("\n>>> TEST: LangChain Sentiment")
exec(open("prompts/langchain_sentiment.py").read())
print("\n>>> TEST: LangChain FAQ")
exec(open("prompts/langchain_faq.py").read())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment