Case Study
Case Study
# Load two different LLMs (Language Models) for text generation using the GPT-2 model
llm1 = pipeline("text-generation", model="gpt2") # First model for initial text generation
llm2 = pipeline("text-generation", model="gpt2") # Second model for further text generation
# Print the output from the first model to see the generated response
print("Output from LLM1:", generated_text_1)
def chain_llms(input_text):
# Generate a response using the first LLM
response1 = llm1(input_text, max_new_tokens=50)[0]['generated_text']
print("Response from llm1:", response1) # Debugging print
# Use the output of the first LLM as input for the second LLM
response2 = llm2(response1, max_new_tokens=50)[0]['generated_text']
print("Response from llm2:", response2) # Debugging print
return response2
llm1=pipeline("text-generation",model="gpt2")
llm2=pipeline("text-generation",model="gpt2")
def conditional_chain(input_text):
# Check if the input is a question
if "?" in input_text:
# Generate a response using the first LLM
return llm1(input_text, max_length=50)[0]['generated_text']
# If not a question, use the second LLM
return llm2(input_text, max_length=50)[0]['generated_text']
llm1=pipeline("text-generation", model="gpt2")
llm2=pipeline("text-generation", model="gpt2")
# Define initial context for the conversation
context = "AI is a field of study."
def context_chain(input_text):
# Combine context with user input for more informed responses
full_input = f"{context} {input_text}"
# Generate a response using the first LLM
return llm1(full_input, max_length=50)[0]['generated_text']
# Load two different LLMs (Language Models) for text generation using the GPT-2 model
llm1 = pipeline("text-generation", model="gpt2") # First model for initial text generation
llm2 = pipeline("text-generation", model="gpt2") # Second model for further text generation
def load_documents(directory):
# Initialize an empty list to store document contents
texts = []
# Iterate through all files in the specified directory
for filename in os.listdir(directory):
# Open and read each file, appending its content to the list
with open(os.path.join(directory, filename), 'r') as file:
texts.append(file.read())
return texts
import os
def load_documents_with_metadata(directory):
# Initialize a list to hold documents with metadata
documents = []
# Loop through all files in the directory
for filename in os.listdir(directory):
with open(os.path.join(directory, filename), 'r') as file:
text = file.read()
# Append a dictionary with filename and content to the list
documents.append({'filename': filename, 'content': text})
return documents
def preprocess(text):
# Remove stopwords from the text
stop_words = set(stopwords.words('english'))
return ' '.join([word for word in text.split() if word.lower() not in stop_words])
def load_and_preprocess(directory):
# Load and preprocess all documents in the specified directory
return [preprocess(open(os.path.join(directory, file)).read()) for file in os.listdir(directory)]
def load_documents_safe(directory):
# Initialize an empty list for documents
texts = []
# Loop through all files in the directory
for filename in os.listdir(directory):
try:
with open(os.path.join(directory, filename), 'r') as file:
# Attempt to read the file and add its content to the list
texts.append(file.read())
except Exception as e:
# Print an error message if file loading fails
print(f"Error loading {filename}: {e}")
return texts
# Load documents safely, handling potential errors
safe_docs = load_documents_safe('directory/')
# Print the safely loaded documents
print(safe_docs)
import nltk
import nltk
from nltk import punkt
# Download the punkt tokenizer
nltk.download('punkt')
nltk.download('punkt_tab')
def split_text(text):
# Use nltk to split the text into sentences
return nltk.sent_tokenize(text)
def chunk_by_sentiment(text):
# Split the text into sentences
sentences = nltk.sent_tokenize(text)
chunks = []
current_chunk = []
return chunks
# Sample text for sentiment-based chunking
text = "I love programming. It's fun! I hate bugs."
# Create sentiment-based chunks
chunks = chunk_by_sentiment(text)
# Print the resulting sentiment chunks
print(chunks)
def mock_model(chunk):
# This is a simple mock model for demonstration
# It returns a score based on the length of the chunk
score = 1.0 if len(chunk.split()) >= 3 else 0.0 # Example logic
return {'score': score}
return chunks
def retrieval_chain(user_input):
# Predefined responses for simple retrieval
responses = {
"hi": "Hello!",
"how are you?": "I'm fine, thank you!",
}
# Return response based on user input, or a default message
return responses.get(user_input.lower(), "I don't understand.")
def retrieve(user_input):
# Encode user input into an embedding
input_embedding = model.encode(user_input, convert_to_tensor=True)
# Calculate cosine similarity between input and corpus
cos_sim = util.pytorch_cos_sim(input_embedding, corpus_embeddings)
# Find the index of the best match
best_match = cos_sim.argmax()
return corpus[best_match]
def multi_turn_conversation(user_input):
# Add each user input to the history
history.append(user_input)
# Generate responses for all turns in the conversation history
responses = [retrieval_chain(u) for u in history]
return responses[-1] # Return the latest response
Feel free to reach out if you need further modifications or additional information!
You said:
everthing is great now but you haven't provided the best explanation of the topic in the explanation
add their internal working their architectures and some real life examples
ChatGPT said:
Sure! Let's enhance the explanations with more details about the internal workings, architectures,
and real-life examples for each topic.
You said:
now you have provided only explanation ,so first thing do the changes as i say in this add more
explanation in main topic heading then come to internal working and architecture and real life
examples with 5 programs whose difficulty level increases
ChatGPT said:
Sure! Let's enhance the structure with more detailed explanations under each main topic heading,
followed by internal workings, architectures, real-life examples, and the corresponding programs.
# Pass the output of the first LLM to the second LLM for further generation
output2 = llm2(output1[0]['generated_text'])
def chain_llms(input_text):
# Generate a response using the first LLM
response1 = llm1(input_text, max_length=50)[0]['generated_text']
# Use the output of the first LLM as input for the second LLM
response2 = llm2(response1, max_length=50)[0]['generated_text']
return response2
def conditional_chain(input_text):
# Check if the input is a question
if "?" in input_text:
# Generate a response using the first LLM
return llm1(input_text, max_length=50)[0]['generated_text']
def context_chain(input_text):
# Combine context with user input for more informed responses
full_input = f"{context} {input_text}"
# Use the output from the first LLM as input for the second LLM
input_text = llm2(input_text, max_length=50)[0]['generated_text']
return input_text
import os
def load_documents(directory):
# Initialize an empty list to store document contents
texts = []
return texts
def load_documents_with_metadata(directory):
# Initialize a list to hold documents with metadata
documents = []
# Loop through all files in the directory
for filename in os.listdir(directory):
with open(os.path.join(directory, filename), 'r') as file:
text = file.read()
# Append a dictionary with filename and content to the list
documents.append({'filename': filename, 'content': text})
return documents
def preprocess(text):
# Remove stopwords from the text
stop_words = set(stopwords.words('english'))
return ' '.join([word for word in text.split() if word.lower() not in
stop_words])
def load_and_preprocess(directory):
# Load and preprocess all documents in the specified directory
return [preprocess(open(os.path.join(directory, file)).read()) for file in
os.listdir(directory)]
def load_documents_safe(directory):
# Initialize an empty list for documents
texts = []
return texts
import nltk
def split_text(text):
# Use nltk to split the text into sentences
return nltk.sent_tokenize(text)
return chunks
return chunks
if current_chunk:
chunks.append({'chunk': ' '.join(current_chunk), 'index': len(chunks)})
return chunks
def chunk_by_sentiment(text):
sentences = text.split('. ')
chunks = []
current_chunk = []
if current_chunk:
chunks.append(' '.join(current_chunk))
return chunks
def retrieval_chain(user_input):
# Predefined responses for simple retrieval
responses = {
"hi": "Hello!",
"how are you?": "I'm fine, thank you!",
}
# Return response based on user input, or a default message
return responses.get(user_input.lower(), "I don't understand.")
def retrieve(user_input):
# Encode user input into an embedding
input_embedding = model.encode(user_input, convert_to_tensor=True)
# Calculate cosine similarity between input and corpus
cos_sim = util.pytorch_cos_sim(input_embedding, corpus_embeddings)
# Find the index of the best match
best_match = cos_sim.argmax()
return corpus[best_match]
def rerank_responses(user_input):
# List of possible responses
responses = ["AI is the simulation of human intelligence.", "AI is cool.",
"AI stands for artificial intelligence."]
# Calculate similarity scores for each response
scores = [util.pytorch_cos_sim(model.encode(user_input),
model.encode(r)).item() for r in responses]
# Re-rank responses based on their similarity scores
ranked_responses = [x for _, x in sorted(zip(scores, responses),
reverse=True)]
return ranked_responses
def multi_turn_conversation(user_input):
# Add each user input to the history
history.append(user_input)
# Generate responses for all turns in the conversation history
responses = [retrieval_chain(u) for u in history]
return responses[-1] # Return the latest response
Object 1