0% found this document useful (0 votes)
209 views1 page

Langchain Onepager

The document discusses using large language models for tasks like summarization, question answering, and conversation. It presents different techniques for fine-tuning models and setting up prompts, templates, and memory. Various tools and components are introduced for building natural language interfaces with language models.

Uploaded by

Natik Talibov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
209 views1 page

Langchain Onepager

The document discusses using large language models for tasks like summarization, question answering, and conversation. It presents different techniques for fine-tuning models and setting up prompts, templates, and memory. Various tools and components are introduced for building natural language interfaces with language models.

Uploaded by

Natik Talibov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

LangChain onepager Fine-tuning Models

from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training


Setup Memory
from langchain.memory import ConversationBufferMemory
Memory

import langchain pretrained_model = AutoModelForCausalLM.from_pretrained(...)


pretrained_model.gradient_checkpointing_enable()
memory = ConversationBufferMemory(memory_key="chat_history")
# Setup predefined memories
model = prepare_model_for_kbit_training(pretrained_model) memory.chat_memory.add_user_message("Hi!")
# Specify LoRa configuration memory.chat_memory.add_ai_message("Welcome! How can I help you?")
config = LoraConfig(r=16, lora_alpha=32, lora_dropout=0.05, bias="none", memory_variables = memory.load_memory_variables({...})
target_modules=["query_key_value"], task_type="CAUSAL_LM") # Add response to memory
model = get_peft_model(model, config) memory.add_ai_message(chat_response.content)
# Set training parameters
trainer = transformers.Trainer(
model=model, train_dataset=train_dataset, Chains Chains
args=transformers.TrainingArguments( from langchain.chains import ConversationChain, summarize, question_answering
num_train_epochs=10, per_device_train_batch_size=8, ...), from langchain.schema import StrOutputParser
Calling OpenAI large language model Models data_collator=transformers.DataCollatorForLanguageModeling(tokenizer)) # Templates for summarizing customer feedback and drafting email response
model.config.use_cache = False feedback_summary_prompt = PromptTemplate.from_template(
from langchain.llms import OpenAI trainer.train() """You are a customer service manager. Summarize the customer feedback.
llm = OpenAI( model_name="text-davinci-003", temperature=0.01) Customer Feedback: {feedback}
llm("Suggest 3 bday gifts for a data scientist") Summary:""")
>>> 1. A subscription to a data science magazine
>>> 2. A set of data science books Prompt Templates Prompts email_prompt = PromptTemplate.from_template(
from langchain.prompts import PromptTemplate """You are a customer service representative. Given the summary of
>>> 3. A data science-themed mug or t-shirt customer feedback, it is your job to write a professional email response.
# Define the template for SEO description
Feedback Summary: {summary}
Conversation schemas: History and Instructions template = "Act as an SEO expert. Provide a SEO description for {product}"
Email Response:""" )
from langchain.chat_models import ChatOpenAI # Create the prompt template
feedback_chain = feedback_summary_prompt | llm | StrOutputParser()
from langchain.schema import HumanMessage,AIMessage,SystemMessage prompt = PromptTemplate(input_variables=["product"], template=template) summary_chain = ({"summary": feedback_chain} | email_prompt | llm | StrOutputParser())
chat = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.01) # Pass in an input to return a formatted prompt summary_chain.invoke({"feedback": "Incorrect item has arrived"})
conversation_history = [ formatted_prompt = prompt.format(product="Electric Scooter")
HumanMessage(content="Suggest 3 bday gifts for a data scientist"), llm(formatted_prompt)
AIMessage(content="What is your price range?"), >>> The Electric Scooter is the perfect way to get around town quickly ... # Predefined chains: summarization and Q&A
HumanMessage(content="Under 100$") ] formatted_prompt = prompt.format(product="Perpetuum Mobile") chain = summarize.load_summarize_chain(llm, chain_type="stuff")
llm(formatted_prompt) chain.run(loaded_documents)
chat(conversation_history).content chain = question_answering.load_qa_chain(llm, chain_type="stuff")
>>> 1. A data science book: Consider gifting a popular and highly ... >>> Perpetuum Mobile is an innovative product that provides a ...
chain.run(input_documents=loaded_documents, question = <input>)
>>> 2. Data visualization tool: A data scientist often deals with .... # Use memory
>>> 3. Subscription to a data science platform: Give them access to .... from langchain.prompts import FewShotPromptTemplate conversation=ConversationChain(llm=llm,memory=ConversationBufferMemory())
system_instruction = SystemMessage(content = """You work as an assistant # Define three examples for the 3-shot learning conversation.run("Name the tallest mountain in the world") >>> Everest
in an electronics store. Your income depends on the items you sold""") examples = [ conversation.run("How high is it?") >>> 8848 m
user_message = HumanMessage(content="3 bday gifts for a data scientist") {"email_text": "Win a free iPhone!", "category": "Spam"},
chat([system_instruction, user_message]).content {"email_text": "Next Sprint Planning Meeting.", "category": "Meetings"},
>>> 1. Laptop: A high-performance laptop with a powerful processor .... {"email_text": "Version 2.1 of Y is now live", "category": "Project Updates"}] Tools Agents and Tools
>>> 2. External Hard Drive: Data scientists deal with large datasets .... # Create a PromptTemplate for classifying emails from langchain.agents import load_tools
>>> 3. Data Science Books: Books related to data science can be .... prompt_template = PromptTemplate(template="Classify the email: tools = load_tools(["serpapi", "llm-math", ...], llm=llm)
{email_text}/n{category}", input_variables=["email_text", "category"])
from langchain.tools import StructuredTool, BaseTool
Open-source models # Create a FewShotPromptTemplate using PromptTemplate and examples def multiply_two_numbers(a: float, b: float) -> float:
from auto_gptq import AutoGPTQForCausalLM, BaseQuantizeConfig few_shot_prompt = FewShotPromptTemplate(example_prompt = """multiply two numbers"""
from transformers import AutoTokenizer, AutoModelForCausalLM prompt_template, examples = examples, suffix = "Classify the email: return a * b
{email_text}", input_variables=["email_text"]) multiplier_tool = StructuredTool.from_function(multiply_two_numbers)
model_name = "TheBloke/llama-2-13B-Guanaco-QLoRA-GPTQ"
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
# Initialize the AutoGPTQForCausalLM model with appropriate parameters Agents
model = AutoGPTQForCausalLM.from_quantized( Document loaders Indexes
model_name, use_safetensors=True, trust_remote_code=True, from langchain.document_loaders import csv_loader, DirectoryLoader, from langchain.agents import initialize_agent, AgentType, BaseSingleActionAgent
device_map="auto", quantize_config=None ) WebBaseLoader, JSONLoader, UnstructuredPDFLoader, ....... agent = initialize_agent(
# Tokenize the query and convert to CUDA tensor loader = DirectoryLoader('../', glob="**/*.md") tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION )
input_ids = tokenizer(query, return_tensors="pt").input_ids.cuda() loader = csv_loader.CSVLoader(...) agent.run({"input": "How old would Harry Potter be when Daniel
# Generate text using the model with specified settings loader = WebBaseLoader(...) Radcliffe was born?"}) >>>9
output = model.generate(inputs=input_ids, temperature=0.1) loader = JSONLoader(...) # create own agents and tools
loader = UnstructuredPDFLoader(...) class UnitConversionTool(BaseTool):
loaded_documents = loader.load() name = "Unit Conversion Tool"
Text generation parameters description = "Converts American units to International units"
The temperature parameter affects the randomness of the token generation def _run(self, text: str):
Top-k sampling limits token generation to the top k most likely at each step Retrievers and Vectorstores def miles_to_km(match):
Top-p (nucleus) sampling limits token generation to cumulative probability p miles = float(match.group(1))
from langchain.text_splitter import RecursiveCharacterTextSplitter return f"{miles * 1.60934:.2f} km"
The length of generated tokens can be specified by max_tokens parameter from langchain.vectorstores import FAISS, Chroma, Pinecone, ...
llm = OpenAI(temperature=0.5, top_k=10, top_p=0.75, max_tokens=50) return re.sub(r'\b(\d+(\.\d+)?)\s*(miles|mile)\b', miles_to_km, text)
# Split docs into texts def _arun(self, text: str):
splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=50) raise NotImplementedError("No async yet")
Quantization texts = splitter.split_documents(loaded_documents) agent = initialize_agent(
from transformers import BitsAndBytesConfig # Embed your texts and store them in a vectorstore agent='chat-conversational-react-description',
# Configure BitsAndBytesConfig for 4-bit quantization db = FAISS.from_documents(texts, embeddings) tools=[UnitConversionTool()],
bnb_config = BitsAndBytesConfig( db = FAISS.from_texts(["some_string_abc", "some_string_xyz"], embeddings) llm=llm,
load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16, # Perform similarity search memory=memory v1.1.0 ─ 10.11.2023
bnb_4bit_quant_type="nf4", bnb_4bit_use_double_quant=True) db.similarity_search(query) ) latest version (click) →
model_4bit = AutoModelForCausalLM.from_pretrained( # Initialize retriever and ask for relevant documents back agent.run("five miles")
retriever = db.as_retriever() by Ivan Reznikov
model_name_or_path, quantization_config=bnb_config, >>> 8.05 kilometers
device_map="auto", trust_remote_code=True) docs = retriever.get_relevant_documents(some_query) medium.com/@ivanreznikov
linkedin.com/in/reznikovivan

You might also like