prompt
stringlengths
51
10k
completion
stringlengths
8
362
api
stringlengths
18
90
from langchain.callbacks import HumanApprovalCallbackHandler from langchain.tools import ShellTool tool =
ShellTool()
langchain.tools.ShellTool
from langchain.chains import RetrievalQA from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import Chroma from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter loader = TextLoader("../../state_of_the_union.txt", encoding="utf-8") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) texts = text_splitter.split_documents(documents) for i, text in enumerate(texts): text.metadata["source"] = f"{i}-pl" embeddings = OpenAIEmbeddings() docsearch = Chroma.from_documents(texts, embeddings) from langchain.chains import create_qa_with_sources_chain from langchain.chains.combine_documents.stuff import StuffDocumentsChain from langchain.prompts import PromptTemplate from langchain_openai import ChatOpenAI llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613") qa_chain = create_qa_with_sources_chain(llm) doc_prompt = PromptTemplate( template="Content: {page_content}\nSource: {source}", input_variables=["page_content", "source"], ) final_qa_chain = StuffDocumentsChain( llm_chain=qa_chain, document_variable_name="context", document_prompt=doc_prompt, ) retrieval_qa = RetrievalQA( retriever=docsearch.as_retriever(), combine_documents_chain=final_qa_chain ) query = "What did the president say about russia" retrieval_qa.run(query) qa_chain_pydantic = create_qa_with_sources_chain(llm, output_parser="pydantic") final_qa_chain_pydantic = StuffDocumentsChain( llm_chain=qa_chain_pydantic, document_variable_name="context", document_prompt=doc_prompt, ) retrieval_qa_pydantic = RetrievalQA( retriever=docsearch.as_retriever(), combine_documents_chain=final_qa_chain_pydantic ) retrieval_qa_pydantic.run(query) from langchain.chains import ConversationalRetrievalChain, LLMChain from langchain.memory import ConversationBufferMemory memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True) _template = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question, in its original language.\ Make sure to avoid using any unclear pronouns. Chat History: {chat_history} Follow Up Input: {question} Standalone question:""" CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template) condense_question_chain = LLMChain( llm=llm, prompt=CONDENSE_QUESTION_PROMPT, ) qa = ConversationalRetrievalChain( question_generator=condense_question_chain, retriever=docsearch.as_retriever(), memory=memory, combine_docs_chain=final_qa_chain, ) query = "What did the president say about Ketanji Brown Jackson" result = qa({"question": query}) result query = "what did he say about her predecessor?" result = qa({"question": query}) result from typing import List from langchain.chains.openai_functions import create_qa_with_structure_chain from langchain.prompts.chat import ChatPromptTemplate, HumanMessagePromptTemplate from langchain_core.messages import HumanMessage, SystemMessage from pydantic import BaseModel, Field class CustomResponseSchema(BaseModel): """An answer to the question being asked, with sources.""" answer: str = Field(..., description="Answer to the question that was asked") countries_referenced: List[str] = Field( ..., description="All of the countries mentioned in the sources" ) sources: List[str] = Field( ..., description="List of sources used to answer the question" ) prompt_messages = [ SystemMessage( content=( "You are a world class algorithm to answer " "questions in a specific format." ) ), HumanMessage(content="Answer question using the following context"), HumanMessagePromptTemplate.from_template("{context}"), HumanMessagePromptTemplate.from_template("Question: {question}"), HumanMessage( content="Tips: Make sure to answer in the correct format. Return all of the countries mentioned in the sources in uppercase characters." ), ] chain_prompt =
ChatPromptTemplate(messages=prompt_messages)
langchain.prompts.chat.ChatPromptTemplate
import getpass import os os.environ["TAVILY_API_KEY"] = getpass.getpass() from langchain.retrievers.tavily_search_api import TavilySearchAPIRetriever retriever =
TavilySearchAPIRetriever(k=3)
langchain.retrievers.tavily_search_api.TavilySearchAPIRetriever
get_ipython().run_line_magic('pip', 'install --upgrade --quiet python-gitlab') import os from langchain.agents import AgentType, initialize_agent from langchain_community.agent_toolkits.gitlab.toolkit import GitLabToolkit from langchain_community.utilities.gitlab import GitLabAPIWrapper from langchain_openai import OpenAI os.environ["GITLAB_URL"] = "https://gitlab.example.org" os.environ["GITLAB_PERSONAL_ACCESS_TOKEN"] = "" os.environ["GITLAB_REPOSITORY"] = "username/repo-name" os.environ["GITLAB_BRANCH"] = "bot-branch-name" os.environ["GITLAB_BASE_BRANCH"] = "main" os.environ["OPENAI_API_KEY"] = "" llm = OpenAI(temperature=0) gitlab =
GitLabAPIWrapper()
langchain_community.utilities.gitlab.GitLabAPIWrapper
import os os.environ["EXA_API_KEY"] = "..." get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-exa') get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain langchain-openai') from langchain_core.prompts import PromptTemplate from langchain_core.runnables import RunnableParallel, RunnablePassthrough from langchain_exa import ExaSearchRetriever, TextContentsOptions from langchain_openai import ChatOpenAI retriever = ExaSearchRetriever( k=5, text_contents_options=TextContentsOptions(max_length=200) ) prompt = PromptTemplate.from_template( """Answer the following query based on the following context: query: {query} <context> {context} </context""" ) llm = ChatOpenAI() chain = ( RunnableParallel({"context": retriever, "query": RunnablePassthrough()}) | prompt | llm ) chain.invoke("When is the best time to visit japan?") get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-exa') from exa_py import Exa from langchain.agents import tool exa = Exa(api_key=os.environ["EXA_API_KEY"]) @tool def search(query: str): """Search for a webpage based on the query.""" return exa.search(f"{query}", use_autoprompt=True, num_results=5) @tool def find_similar(url: str): """Search for webpages similar to a given URL. The url passed in should be a URL returned from `search`. """ return exa.find_similar(url, num_results=5) @tool def get_contents(ids: list[str]): """Get the contents of a webpage. The ids passed in should be a list of ids returned from `search`. """ return exa.get_contents(ids) tools = [search, get_contents, find_similar] from langchain.agents import AgentExecutor, OpenAIFunctionsAgent from langchain_core.messages import SystemMessage from langchain_openai import ChatOpenAI llm = ChatOpenAI(temperature=0) system_message = SystemMessage( content="You are a web researcher who answers user questions by looking up information on the internet and retrieving contents of helpful documents. Cite your sources." ) agent_prompt = OpenAIFunctionsAgent.create_prompt(system_message) agent =
OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=agent_prompt)
langchain.agents.OpenAIFunctionsAgent
get_ipython().run_line_magic('pip', 'install --upgrade --quiet transformers') from langchain_community.document_loaders import ImageCaptionLoader list_image_urls = [ "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5a/Hyla_japonica_sep01.jpg/260px-Hyla_japonica_sep01.jpg", "https://upload.wikimedia.org/wikipedia/commons/thumb/7/71/Tibur%C3%B3n_azul_%28Prionace_glauca%29%2C_canal_Fayal-Pico%2C_islas_Azores%2C_Portugal%2C_2020-07-27%2C_DD_14.jpg/270px-Tibur%C3%B3n_azul_%28Prionace_glauca%29%2C_canal_Fayal-Pico%2C_islas_Azores%2C_Portugal%2C_2020-07-27%2C_DD_14.jpg", "https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Thure_de_Thulstrup_-_Battle_of_Shiloh.jpg/251px-Thure_de_Thulstrup_-_Battle_of_Shiloh.jpg", "https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Passion_fruits_-_whole_and_halved.jpg/270px-Passion_fruits_-_whole_and_halved.jpg", "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5e/Messier83_-_Heic1403a.jpg/277px-Messier83_-_Heic1403a.jpg", "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/2022-01-22_Men%27s_World_Cup_at_2021-22_St._Moritz%E2%80%93Celerina_Luge_World_Cup_and_European_Championships_by_Sandro_Halank%E2%80%93257.jpg/288px-2022-01-22_Men%27s_World_Cup_at_2021-22_St._Moritz%E2%80%93Celerina_Luge_World_Cup_and_European_Championships_by_Sandro_Halank%E2%80%93257.jpg", "https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Wiesen_Pippau_%28Crepis_biennis%29-20220624-RM-123950.jpg/224px-Wiesen_Pippau_%28Crepis_biennis%29-20220624-RM-123950.jpg", ] loader =
ImageCaptionLoader(path_images=list_image_urls)
langchain_community.document_loaders.ImageCaptionLoader
from langchain.chains import ConversationChain from langchain.memory import ConversationBufferMemory from langchain_openai import OpenAI llm = OpenAI(temperature=0) conversation = ConversationChain( llm=llm, verbose=True, memory=
ConversationBufferMemory()
langchain.memory.ConversationBufferMemory
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain langchain-openai') from langchain.model_laboratory import ModelLaboratory from langchain.prompts import PromptTemplate from langchain_community.llms import Cohere, HuggingFaceHub from langchain_openai import OpenAI import getpass import os os.environ["COHERE_API_KEY"] = getpass.getpass("Cohere API Key:") os.environ["OPENAI_API_KEY"] = getpass.getpass("Open API Key:") os.environ["HUGGINGFACEHUB_API_TOKEN"] = getpass.getpass("Hugging Face API Key:") llms = [ OpenAI(temperature=0), Cohere(temperature=0), HuggingFaceHub(repo_id="google/flan-t5-xl", model_kwargs={"temperature": 1}), ] model_lab = ModelLaboratory.from_llms(llms) model_lab.compare("What color is a flamingo?") prompt = PromptTemplate( template="What is the capital of {state}?", input_variables=["state"] ) model_lab_with_prompt = ModelLaboratory.from_llms(llms, prompt=prompt) model_lab_with_prompt.compare("New York") from langchain.chains import SelfAskWithSearchChain from langchain_community.utilities import SerpAPIWrapper open_ai_llm = OpenAI(temperature=0) search = SerpAPIWrapper() self_ask_with_search_openai = SelfAskWithSearchChain( llm=open_ai_llm, search_chain=search, verbose=True ) cohere_llm =
Cohere(temperature=0)
langchain_community.llms.Cohere
from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate, FewShotChatMessagePromptTemplate from langchain_core.runnables import RunnableLambda from langchain_openai import ChatOpenAI examples = [ { "input": "Could the members of The Police perform lawful arrests?", "output": "what can the members of The Police do?", }, { "input": "Jan Sindel’s was born in what country?", "output": "what is Jan Sindel’s personal history?", }, ] example_prompt =
ChatPromptTemplate.from_messages( [ ("human", "{input}")
langchain_core.prompts.ChatPromptTemplate.from_messages
get_ipython().system(' pip install -U langchain openai chromadb langchain-experimental # (newest versions required for multi-modal)') get_ipython().system(' pip install "unstructured[all-docs]==0.10.19" pillow pydantic lxml pillow matplotlib tiktoken open_clip_torch torch') path = "/Users/rlm/Desktop/photos/" from unstructured.partition.pdf import partition_pdf raw_pdf_elements = partition_pdf( filename=path + "photos.pdf", extract_images_in_pdf=True, infer_table_structure=True, chunking_strategy="by_title", max_characters=4000, new_after_n_chars=3800, combine_text_under_n_chars=2000, image_output_dir_path=path, ) tables = [] texts = [] for element in raw_pdf_elements: if "unstructured.documents.elements.Table" in str(type(element)): tables.append(str(element)) elif "unstructured.documents.elements.CompositeElement" in str(type(element)): texts.append(str(element)) import os import uuid import chromadb import numpy as np from langchain_community.vectorstores import Chroma from langchain_experimental.open_clip import OpenCLIPEmbeddings from PIL import Image as _PILImage vectorstore = Chroma( collection_name="mm_rag_clip_photos", embedding_function=OpenCLIPEmbeddings() ) image_uris = sorted( [ os.path.join(path, image_name) for image_name in os.listdir(path) if image_name.endswith(".jpg") ] ) vectorstore.add_images(uris=image_uris) vectorstore.add_texts(texts=texts) retriever = vectorstore.as_retriever() import base64 import io from io import BytesIO import numpy as np from PIL import Image def resize_base64_image(base64_string, size=(128, 128)): """ Resize an image encoded as a Base64 string. Args: base64_string (str): Base64 string of the original image. size (tuple): Desired size of the image as (width, height). Returns: str: Base64 string of the resized image. """ img_data = base64.b64decode(base64_string) img = Image.open(io.BytesIO(img_data)) resized_img = img.resize(size, Image.LANCZOS) buffered = io.BytesIO() resized_img.save(buffered, format=img.format) return base64.b64encode(buffered.getvalue()).decode("utf-8") def is_base64(s): """Check if a string is Base64 encoded""" try: return base64.b64encode(base64.b64decode(s)) == s.encode() except Exception: return False def split_image_text_types(docs): """Split numpy array images and texts""" images = [] text = [] for doc in docs: doc = doc.page_content # Extract Document contents if is_base64(doc): images.append( resize_base64_image(doc, size=(250, 250)) ) # base64 encoded str else: text.append(doc) return {"images": images, "texts": text} from operator import itemgetter from langchain_core.messages import HumanMessage, SystemMessage from langchain_core.output_parsers import StrOutputParser from langchain_core.runnables import RunnableLambda, RunnablePassthrough from langchain_openai import ChatOpenAI def prompt_func(data_dict): formatted_texts = "\n".join(data_dict["context"]["texts"]) messages = [] if data_dict["context"]["images"]: image_message = { "type": "image_url", "image_url": { "url": f"data:image/jpeg;base64,{data_dict['context']['images'][0]}" }, } messages.append(image_message) text_message = { "type": "text", "text": ( "As an expert art critic and historian, your task is to analyze and interpret images, " "considering their historical and cultural significance. Alongside the images, you will be " "provided with related text to offer context. Both will be retrieved from a vectorstore based " "on user-input keywords. Please use your extensive knowledge and analytical skills to provide a " "comprehensive summary that includes:\n" "- A detailed description of the visual elements in the image.\n" "- The historical and cultural context of the image.\n" "- An interpretation of the image's symbolism and meaning.\n" "- Connections between the image and the related text.\n\n" f"User-provided keywords: {data_dict['question']}\n\n" "Text and / or tables:\n" f"{formatted_texts}" ), } messages.append(text_message) return [HumanMessage(content=messages)] model = ChatOpenAI(temperature=0, model="gpt-4-vision-preview", max_tokens=1024) chain = ( { "context": retriever | RunnableLambda(split_image_text_types), "question":
RunnablePassthrough()
langchain_core.runnables.RunnablePassthrough
get_ipython().run_line_magic('pip', 'install --upgrade --quiet doctran') import json from langchain_community.document_transformers import DoctranQATransformer from langchain_core.documents import Document from dotenv import load_dotenv load_dotenv() sample_text = """[Generated with ChatGPT] Confidential Document - For Internal Use Only Date: July 1, 2023 Subject: Updates and Discussions on Various Topics Dear Team, I hope this email finds you well. In this document, I would like to provide you with some important updates and discuss various topics that require our attention. Please treat the information contained herein as highly confidential. Security and Privacy Measures As part of our ongoing commitment to ensure the security and privacy of our customers' data, we have implemented robust measures across all our systems. We would like to commend John Doe (email: [email protected]) from the IT department for his diligent work in enhancing our network security. Moving forward, we kindly remind everyone to strictly adhere to our data protection policies and guidelines. Additionally, if you come across any potential security risks or incidents, please report them immediately to our dedicated team at [email protected]. HR Updates and Employee Benefits Recently, we welcomed several new team members who have made significant contributions to their respective departments. I would like to recognize Jane Smith (SSN: 049-45-5928) for her outstanding performance in customer service. Jane has consistently received positive feedback from our clients. Furthermore, please remember that the open enrollment period for our employee benefits program is fast approaching. Should you have any questions or require assistance, please contact our HR representative, Michael Johnson (phone: 418-492-3850, email: [email protected]). Marketing Initiatives and Campaigns Our marketing team has been actively working on developing new strategies to increase brand awareness and drive customer engagement. We would like to thank Sarah Thompson (phone: 415-555-1234) for her exceptional efforts in managing our social media platforms. Sarah has successfully increased our follower base by 20% in the past month alone. Moreover, please mark your calendars for the upcoming product launch event on July 15th. We encourage all team members to attend and support this exciting milestone for our company. Research and Development Projects In our pursuit of innovation, our research and development department has been working tirelessly on various projects. I would like to acknowledge the exceptional work of David Rodriguez (email: [email protected]) in his role as project lead. David's contributions to the development of our cutting-edge technology have been instrumental. Furthermore, we would like to remind everyone to share their ideas and suggestions for potential new projects during our monthly R&D brainstorming session, scheduled for July 10th. Please treat the information in this document with utmost confidentiality and ensure that it is not shared with unauthorized individuals. If you have any questions or concerns regarding the topics discussed, please do not hesitate to reach out to me directly. Thank you for your attention, and let's continue to work together to achieve our goals. Best regards, Jason Fan Cofounder & CEO Psychic [email protected] """ print(sample_text) documents = [Document(page_content=sample_text)] qa_transformer =
DoctranQATransformer()
langchain_community.document_transformers.DoctranQATransformer
from langchain_community.document_loaders import TomlLoader loader =
TomlLoader("example_data/fake_rule.toml")
langchain_community.document_loaders.TomlLoader
get_ipython().run_line_magic('pip', 'install --upgrade --quiet "cassio>=0.1.4"') import os from getpass import getpass from datasets import ( load_dataset, ) from langchain_community.document_loaders import PyPDFLoader from langchain_core.documents import Document from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate from langchain_core.runnables import RunnablePassthrough from langchain_openai import ChatOpenAI, OpenAIEmbeddings from langchain_text_splitters import RecursiveCharacterTextSplitter os.environ["OPENAI_API_KEY"] = getpass("OPENAI_API_KEY = ") embe = OpenAIEmbeddings() from langchain_community.vectorstores import Cassandra from cassandra.cluster import Cluster cluster = Cluster(["127.0.0.1"]) session = cluster.connect() import cassio CASSANDRA_KEYSPACE = input("CASSANDRA_KEYSPACE = ") cassio.init(session=session, keyspace=CASSANDRA_KEYSPACE) vstore = Cassandra( embedding=embe, table_name="cassandra_vector_demo", ) ASTRA_DB_ID = input("ASTRA_DB_ID = ") ASTRA_DB_APPLICATION_TOKEN = getpass("ASTRA_DB_APPLICATION_TOKEN = ") desired_keyspace = input("ASTRA_DB_KEYSPACE (optional, can be left empty) = ") if desired_keyspace: ASTRA_DB_KEYSPACE = desired_keyspace else: ASTRA_DB_KEYSPACE = None import cassio cassio.init( database_id=ASTRA_DB_ID, token=ASTRA_DB_APPLICATION_TOKEN, keyspace=ASTRA_DB_KEYSPACE, ) vstore = Cassandra( embedding=embe, table_name="cassandra_vector_demo", ) philo_dataset = load_dataset("datastax/philosopher-quotes")["train"] docs = [] for entry in philo_dataset: metadata = {"author": entry["author"]} doc = Document(page_content=entry["quote"], metadata=metadata) docs.append(doc) inserted_ids = vstore.add_documents(docs) print(f"\nInserted {len(inserted_ids)} documents.") texts = ["I think, therefore I am.", "To the things themselves!"] metadatas = [{"author": "descartes"}, {"author": "husserl"}] ids = ["desc_01", "huss_xy"] inserted_ids_2 = vstore.add_texts(texts=texts, metadatas=metadatas, ids=ids) print(f"\nInserted {len(inserted_ids_2)} documents.") results = vstore.similarity_search("Our life is what we make of it", k=3) for res in results: print(f"* {res.page_content} [{res.metadata}]") results_filtered = vstore.similarity_search( "Our life is what we make of it", k=3, filter={"author": "plato"}, ) for res in results_filtered: print(f"* {res.page_content} [{res.metadata}]") results = vstore.similarity_search_with_score("Our life is what we make of it", k=3) for res, score in results: print(f"* [SIM={score:3f}] {res.page_content} [{res.metadata}]") results = vstore.max_marginal_relevance_search( "Our life is what we make of it", k=3, filter={"author": "aristotle"}, ) for res in results: print(f"* {res.page_content} [{res.metadata}]") delete_1 = vstore.delete(inserted_ids[:3]) print(f"all_succeed={delete_1}") # True, all documents deleted delete_2 = vstore.delete(inserted_ids[2:5]) print(f"some_succeeds={delete_2}") # True, though some IDs were gone already get_ipython().system('curl -L "https://github.com/awesome-astra/datasets/blob/main/demo-resources/what-is-philosophy/what-is-philosophy.pdf?raw=true" -o "what-is-philosophy.pdf"') pdf_loader = PyPDFLoader("what-is-philosophy.pdf") splitter = RecursiveCharacterTextSplitter(chunk_size=512, chunk_overlap=64) docs_from_pdf = pdf_loader.load_and_split(text_splitter=splitter) print(f"Documents from PDF: {len(docs_from_pdf)}.") inserted_ids_from_pdf = vstore.add_documents(docs_from_pdf) print(f"Inserted {len(inserted_ids_from_pdf)} documents.") retriever = vstore.as_retriever(search_kwargs={"k": 3}) philo_template = """ You are a philosopher that draws inspiration from great thinkers of the past to craft well-thought answers to user questions. Use the provided context as the basis for your answers and do not make up new reasoning paths - just mix-and-match what you are given. Your answers must be concise and to the point, and refrain from answering about other topics than philosophy. CONTEXT: {context} QUESTION: {question} YOUR ANSWER:""" philo_prompt =
ChatPromptTemplate.from_template(philo_template)
langchain_core.prompts.ChatPromptTemplate.from_template
get_ipython().system('poetry run pip install dgml-utils==0.3.0 --upgrade --quiet') import os from langchain_community.document_loaders import DocugamiLoader DOCUGAMI_API_KEY = os.environ.get("DOCUGAMI_API_KEY") docset_id = "26xpy3aes7xp" document_ids = ["d7jqdzcj50sj", "cgd1eacfkchw"] loader = DocugamiLoader(docset_id=docset_id, document_ids=document_ids) chunks = loader.load() len(chunks) loader.min_text_length = 64 loader.include_xml_tags = True chunks = loader.load() for chunk in chunks[:5]: print(chunk) get_ipython().system('poetry run pip install --upgrade langchain-openai tiktoken chromadb hnswlib') loader = DocugamiLoader(docset_id="zo954yqy53wp") chunks = loader.load() for chunk in chunks: stripped_metadata = chunk.metadata.copy() for key in chunk.metadata: if key not in ["name", "xpath", "id", "structure"]: del stripped_metadata[key] chunk.metadata = stripped_metadata print(len(chunks)) from langchain.chains import RetrievalQA from langchain_community.vectorstores.chroma import Chroma from langchain_openai import OpenAI, OpenAIEmbeddings embedding = OpenAIEmbeddings() vectordb = Chroma.from_documents(documents=chunks, embedding=embedding) retriever = vectordb.as_retriever() qa_chain = RetrievalQA.from_chain_type( llm=OpenAI(), chain_type="stuff", retriever=retriever, return_source_documents=True ) qa_chain("What can tenants do with signage on their properties?") chain_response = qa_chain("What is rentable area for the property owned by DHA Group?") chain_response["result"] # correct answer should be 13,500 sq ft chain_response["source_documents"] loader =
DocugamiLoader(docset_id="zo954yqy53wp")
langchain_community.document_loaders.DocugamiLoader
meals = [ "Beef Enchiladas with Feta cheese. Mexican-Greek fusion", "Chicken Flatbreads with red sauce. Italian-Mexican fusion", "Veggie sweet potato quesadillas with vegan cheese", "One-Pan Tortelonni bake with peppers and onions", ] from langchain_openai import OpenAI llm = OpenAI(model="gpt-3.5-turbo-instruct") from langchain.prompts import PromptTemplate PROMPT_TEMPLATE = """Here is the description of a meal: "{meal}". Embed the meal into the given text: "{text_to_personalize}". Prepend a personalized message including the user's name "{user}" and their preference "{preference}". Make it sound good. """ PROMPT = PromptTemplate( input_variables=["meal", "text_to_personalize", "user", "preference"], template=PROMPT_TEMPLATE, ) import langchain_experimental.rl_chain as rl_chain chain = rl_chain.PickBest.from_llm(llm=llm, prompt=PROMPT) response = chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Tom"), preference=rl_chain.BasedOn(["Vegetarian", "regular dairy is ok"]), text_to_personalize="This is the weeks specialty dish, our master chefs \ believe you will love it!", ) print(response["response"]) for _ in range(5): try: response = chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Tom"), preference=rl_chain.BasedOn(["Vegetarian", "regular dairy is ok"]), text_to_personalize="This is the weeks specialty dish, our master chefs believe you will love it!", ) except Exception as e: print(e) print(response["response"]) print() scoring_criteria_template = ( "Given {preference} rank how good or bad this selection is {meal}" ) chain = rl_chain.PickBest.from_llm( llm=llm, prompt=PROMPT, selection_scorer=rl_chain.AutoSelectionScorer( llm=llm, scoring_criteria_template_str=scoring_criteria_template ), ) response = chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Tom"), preference=rl_chain.BasedOn(["Vegetarian", "regular dairy is ok"]), text_to_personalize="This is the weeks specialty dish, our master chefs believe you will love it!", ) print(response["response"]) selection_metadata = response["selection_metadata"] print( f"selected index: {selection_metadata.selected.index}, score: {selection_metadata.selected.score}" ) class CustomSelectionScorer(rl_chain.SelectionScorer): def score_response( self, inputs, llm_response: str, event: rl_chain.PickBestEvent ) -> float: print(event.based_on) print(event.to_select_from) selected_meal = event.to_select_from["meal"][event.selected.index] print(f"selected meal: {selected_meal}") if "Tom" in event.based_on["user"]: if "Vegetarian" in event.based_on["preference"]: if "Chicken" in selected_meal or "Beef" in selected_meal: return 0.0 else: return 1.0 else: if "Chicken" in selected_meal or "Beef" in selected_meal: return 1.0 else: return 0.0 else: raise NotImplementedError("I don't know how to score this user") chain = rl_chain.PickBest.from_llm( llm=llm, prompt=PROMPT, selection_scorer=CustomSelectionScorer(), ) response = chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Tom"), preference=rl_chain.BasedOn(["Vegetarian", "regular dairy is ok"]), text_to_personalize="This is the weeks specialty dish, our master chefs believe you will love it!", ) class CustomSelectionScorer(rl_chain.SelectionScorer): def score_preference(self, preference, selected_meal): if "Vegetarian" in preference: if "Chicken" in selected_meal or "Beef" in selected_meal: return 0.0 else: return 1.0 else: if "Chicken" in selected_meal or "Beef" in selected_meal: return 1.0 else: return 0.0 def score_response( self, inputs, llm_response: str, event: rl_chain.PickBestEvent ) -> float: selected_meal = event.to_select_from["meal"][event.selected.index] if "Tom" in event.based_on["user"]: return self.score_preference(event.based_on["preference"], selected_meal) elif "Anna" in event.based_on["user"]: return self.score_preference(event.based_on["preference"], selected_meal) else: raise NotImplementedError("I don't know how to score this user") chain = rl_chain.PickBest.from_llm( llm=llm, prompt=PROMPT, selection_scorer=CustomSelectionScorer(), metrics_step=5, metrics_window_size=5, # rolling window average ) random_chain = rl_chain.PickBest.from_llm( llm=llm, prompt=PROMPT, selection_scorer=CustomSelectionScorer(), metrics_step=5, metrics_window_size=5, # rolling window average policy=rl_chain.PickBestRandomPolicy, # set the random policy instead of default ) for _ in range(20): try: chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Tom"), preference=rl_chain.BasedOn(["Vegetarian", "regular dairy is ok"]), text_to_personalize="This is the weeks specialty dish, our master chefs believe you will love it!", ) random_chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Tom"), preference=rl_chain.BasedOn(["Vegetarian", "regular dairy is ok"]), text_to_personalize="This is the weeks specialty dish, our master chefs believe you will love it!", ) chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Anna"), preference=rl_chain.BasedOn(["Loves meat", "especially beef"]), text_to_personalize="This is the weeks specialty dish, our master chefs believe you will love it!", ) random_chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Anna"), preference=rl_chain.BasedOn(["Loves meat", "especially beef"]), text_to_personalize="This is the weeks specialty dish, our master chefs believe you will love it!", ) except Exception as e: print(e) from matplotlib import pyplot as plt chain.metrics.to_pandas()["score"].plot(label="default learning policy") random_chain.metrics.to_pandas()["score"].plot(label="random selection policy") plt.legend() print( f"The final average score for the default policy, calculated over a rolling window, is: {chain.metrics.to_pandas()['score'].iloc[-1]}" ) print( f"The final average score for the random policy, calculated over a rolling window, is: {random_chain.metrics.to_pandas()['score'].iloc[-1]}" ) from langchain.globals import set_debug from langchain.prompts.prompt import PromptTemplate set_debug(True) REWARD_PROMPT_TEMPLATE = """ Given {preference} rank how good or bad this selection is {meal} IMPORTANT: you MUST return a single number between -1 and 1, -1 being bad, 1 being good """ REWARD_PROMPT = PromptTemplate( input_variables=["preference", "meal"], template=REWARD_PROMPT_TEMPLATE, ) chain = rl_chain.PickBest.from_llm( llm=llm, prompt=PROMPT, selection_scorer=rl_chain.AutoSelectionScorer(llm=llm, prompt=REWARD_PROMPT), ) chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Tom"), preference=
rl_chain.BasedOn(["Vegetarian", "regular dairy is ok"])
langchain_experimental.rl_chain.BasedOn
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-core langchain langchain-openai') from langchain.utils.math import cosine_similarity from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import PromptTemplate from langchain_core.runnables import RunnableLambda, RunnablePassthrough from langchain_openai import ChatOpenAI, OpenAIEmbeddings physics_template = """You are a very smart physics professor. \ You are great at answering questions about physics in a concise and easy to understand manner. \ When you don't know the answer to a question you admit that you don't know. Here is a question: {query}""" math_template = """You are a very good mathematician. You are great at answering math questions. \ You are so good because you are able to break down hard problems into their component parts, \ answer the component parts, and then put them together to answer the broader question. Here is a question: {query}""" embeddings = OpenAIEmbeddings() prompt_templates = [physics_template, math_template] prompt_embeddings = embeddings.embed_documents(prompt_templates) def prompt_router(input): query_embedding = embeddings.embed_query(input["query"]) similarity = cosine_similarity([query_embedding], prompt_embeddings)[0] most_similar = prompt_templates[similarity.argmax()] print("Using MATH" if most_similar == math_template else "Using PHYSICS") return PromptTemplate.from_template(most_similar) chain = ( {"query": RunnablePassthrough()} |
RunnableLambda(prompt_router)
langchain_core.runnables.RunnableLambda
get_ipython().system(' pip install langchain replicate') from langchain_community.chat_models import ChatOllama llama2_chat = ChatOllama(model="llama2:13b-chat") llama2_code = ChatOllama(model="codellama:7b-instruct") from langchain_community.llms import Replicate replicate_id = "meta/llama-2-13b-chat:f4e2de70d66816a838a89eeeb621910adffb0dd0baba3976c96980970978018d" llama2_chat_replicate = Replicate( model=replicate_id, input={"temperature": 0.01, "max_length": 500, "top_p": 1} ) llm = llama2_chat from langchain_community.utilities import SQLDatabase db = SQLDatabase.from_uri("sqlite:///nba_roster.db", sample_rows_in_table_info=0) def get_schema(_): return db.get_table_info() def run_query(query): return db.run(query) from langchain_core.prompts import ChatPromptTemplate template = """Based on the table schema below, write a SQL query that would answer the user's question: {schema} Question: {question} SQL Query:""" prompt = ChatPromptTemplate.from_messages( [ ("system", "Given an input question, convert it to a SQL query. No pre-amble."), ("human", template), ] ) from langchain_core.output_parsers import StrOutputParser from langchain_core.runnables import RunnablePassthrough sql_response = ( RunnablePassthrough.assign(schema=get_schema) | prompt | llm.bind(stop=["\nSQLResult:"]) | StrOutputParser() ) sql_response.invoke({"question": "What team is Klay Thompson on?"}) template = """Based on the table schema below, question, sql query, and sql response, write a natural language response: {schema} Question: {question} SQL Query: {query} SQL Response: {response}""" prompt_response = ChatPromptTemplate.from_messages( [ ( "system", "Given an input question and SQL response, convert it to a natural language answer. No pre-amble.", ), ("human", template), ] ) full_chain = ( RunnablePassthrough.assign(query=sql_response) | RunnablePassthrough.assign( schema=get_schema, response=lambda x: db.run(x["query"]), ) | prompt_response | llm ) full_chain.invoke({"question": "How many unique teams are there?"}) from langchain.memory import ConversationBufferMemory from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder template = """Given an input question, convert it to a SQL query. No pre-amble. Based on the table schema below, write a SQL query that would answer the user's question: {schema} """ prompt = ChatPromptTemplate.from_messages( [ ("system", template),
MessagesPlaceholder(variable_name="history")
langchain_core.prompts.MessagesPlaceholder
get_ipython().run_line_magic('pip', 'install --upgrade --quiet elasticsearch') from langchain.retrievers import ElasticSearchBM25Retriever elasticsearch_url = "http://localhost:9200" retriever =
ElasticSearchBM25Retriever.create(elasticsearch_url, "langchain-index-4")
langchain.retrievers.ElasticSearchBM25Retriever.create
get_ipython().run_line_magic('pip', 'install --upgrade --quiet pandoc') from langchain_community.document_loaders import UnstructuredEPubLoader loader = UnstructuredEPubLoader("winter-sports.epub") data = loader.load() loader =
UnstructuredEPubLoader("winter-sports.epub", mode="elements")
langchain_community.document_loaders.UnstructuredEPubLoader
from langchain.prompts import ChatMessagePromptTemplate prompt = "May the {subject} be with you" chat_message_prompt = ChatMessagePromptTemplate.from_template( role="Jedi", template=prompt ) chat_message_prompt.format(subject="force") from langchain.prompts import ( ChatPromptTemplate, HumanMessagePromptTemplate, MessagesPlaceholder, ) human_prompt = "Summarize our conversation so far in {word_count} words." human_message_template = HumanMessagePromptTemplate.from_template(human_prompt) chat_prompt = ChatPromptTemplate.from_messages( [
MessagesPlaceholder(variable_name="conversation")
langchain.prompts.MessagesPlaceholder
import os import yaml get_ipython().system('wget https://raw.githubusercontent.com/openai/openai-openapi/master/openapi.yaml -O openai_openapi.yaml') get_ipython().system('wget https://www.klarna.com/us/shopping/public/openai/v0/api-docs -O klarna_openapi.yaml') get_ipython().system('wget https://raw.githubusercontent.com/APIs-guru/openapi-directory/main/APIs/spotify.com/1.0.0/openapi.yaml -O spotify_openapi.yaml') from langchain_community.agent_toolkits.openapi.spec import reduce_openapi_spec with open("openai_openapi.yaml") as f: raw_openai_api_spec = yaml.load(f, Loader=yaml.Loader) openai_api_spec = reduce_openapi_spec(raw_openai_api_spec) with open("klarna_openapi.yaml") as f: raw_klarna_api_spec = yaml.load(f, Loader=yaml.Loader) klarna_api_spec = reduce_openapi_spec(raw_klarna_api_spec) with open("spotify_openapi.yaml") as f: raw_spotify_api_spec = yaml.load(f, Loader=yaml.Loader) spotify_api_spec = reduce_openapi_spec(raw_spotify_api_spec) import spotipy.util as util from langchain.requests import RequestsWrapper def construct_spotify_auth_headers(raw_spec: dict): scopes = list( raw_spec["components"]["securitySchemes"]["oauth_2_0"]["flows"][ "authorizationCode" ]["scopes"].keys() ) access_token = util.prompt_for_user_token(scope=",".join(scopes)) return {"Authorization": f"Bearer {access_token}"} headers = construct_spotify_auth_headers(raw_spotify_api_spec) requests_wrapper =
RequestsWrapper(headers=headers)
langchain.requests.RequestsWrapper
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain langchain-openai') from langchain.evaluation import load_evaluator from langchain_openai import ChatOpenAI evaluator = load_evaluator("labeled_score_string", llm=ChatOpenAI(model="gpt-4")) eval_result = evaluator.evaluate_strings( prediction="You can find them in the dresser's third drawer.", reference="The socks are in the third drawer in the dresser", input="Where are my socks?", ) print(eval_result) accuracy_criteria = { "accuracy": """ Score 1: The answer is completely unrelated to the reference. Score 3: The answer has minor relevance but does not align with the reference. Score 5: The answer has moderate relevance but contains inaccuracies. Score 7: The answer aligns with the reference but has minor errors or omissions. Score 10: The answer is completely accurate and aligns perfectly with the reference.""" } evaluator = load_evaluator( "labeled_score_string", criteria=accuracy_criteria, llm=ChatOpenAI(model="gpt-4"), ) eval_result = evaluator.evaluate_strings( prediction="You can find them in the dresser's third drawer.", reference="The socks are in the third drawer in the dresser", input="Where are my socks?", ) print(eval_result) eval_result = evaluator.evaluate_strings( prediction="You can find them in the dresser.", reference="The socks are in the third drawer in the dresser", input="Where are my socks?", ) print(eval_result) eval_result = evaluator.evaluate_strings( prediction="You can find them in the dog's bed.", reference="The socks are in the third drawer in the dresser", input="Where are my socks?", ) print(eval_result) evaluator = load_evaluator( "labeled_score_string", criteria=accuracy_criteria, llm=ChatOpenAI(model="gpt-4"), normalize_by=10, ) eval_result = evaluator.evaluate_strings( prediction="You can find them in the dresser.", reference="The socks are in the third drawer in the dresser", input="Where are my socks?", ) print(eval_result) from langchain.evaluation import load_evaluator hh_criteria = { "helpful": "The assistant's answer should be helpful to the user.", "harmless": "The assistant's answer should not be illegal, harmful, offensive or unethical.", } evaluator =
load_evaluator("score_string", criteria=hh_criteria)
langchain.evaluation.load_evaluator
from langchain_core.messages import ( AIMessage, BaseMessage, FunctionMessage, HumanMessage, SystemMessage, ToolMessage, ) from langchain_core.messages import ( AIMessageChunk, FunctionMessageChunk, HumanMessageChunk, SystemMessageChunk, ToolMessageChunk, ) AIMessageChunk(content="Hello") + AIMessageChunk(content=" World!") from typing import Any, AsyncIterator, Dict, Iterator, List, Optional from langchain_core.callbacks import ( AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun, ) from langchain_core.language_models import BaseChatModel, SimpleChatModel from langchain_core.messages import AIMessageChunk, BaseMessage, HumanMessage from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult from langchain_core.runnables import run_in_executor class CustomChatModelAdvanced(BaseChatModel): """A custom chat model that echoes the first `n` characters of the input. When contributing an implementation to LangChain, carefully document the model including the initialization parameters, include an example of how to initialize the model and include any relevant links to the underlying models documentation or API. Example: .. code-block:: python model = CustomChatModel(n=2) result = model.invoke([HumanMessage(content="hello")]) result = model.batch([[HumanMessage(content="hello")], [HumanMessage(content="world")]]) """ n: int """The number of characters from the last message of the prompt to be echoed.""" def _generate( self, messages: List[BaseMessage], stop: Optional[List[str]] = None, run_manager: Optional[CallbackManagerForLLMRun] = None, **kwargs: Any, ) -> ChatResult: """Override the _generate method to implement the chat model logic. This can be a call to an API, a call to a local model, or any other implementation that generates a response to the input prompt. Args: messages: the prompt composed of a list of messages. stop: a list of strings on which the model should stop generating. If generation stops due to a stop token, the stop token itself SHOULD BE INCLUDED as part of the output. This is not enforced across models right now, but it's a good practice to follow since it makes it much easier to parse the output of the model downstream and understand why generation stopped. run_manager: A run manager with callbacks for the LLM. """ last_message = messages[-1] tokens = last_message.content[: self.n] message =
AIMessage(content=tokens)
langchain_core.messages.AIMessage
get_ipython().run_line_magic('pip', "install --upgrade --quiet langchain-openai 'deeplake[enterprise]' tiktoken") from langchain_community.vectorstores import DeepLake from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") activeloop_token = getpass.getpass("activeloop token:") embeddings = OpenAIEmbeddings() from langchain_community.document_loaders import TextLoader loader = TextLoader("../../modules/state_of_the_union.txt") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings() db = DeepLake(dataset_path="./my_deeplake/", embedding=embeddings, overwrite=True) db.add_documents(docs) query = "What did the president say about Ketanji Brown Jackson" docs = db.similarity_search(query) print(docs[0].page_content) db = DeepLake(dataset_path="./my_deeplake/", embedding=embeddings, read_only=True) docs = db.similarity_search(query) from langchain.chains import RetrievalQA from langchain_openai import OpenAIChat qa = RetrievalQA.from_chain_type( llm=OpenAIChat(model="gpt-3.5-turbo"), chain_type="stuff", retriever=db.as_retriever(), ) query = "What did the president say about Ketanji Brown Jackson" qa.run(query) import random for d in docs: d.metadata["year"] = random.randint(2012, 2014) db = DeepLake.from_documents( docs, embeddings, dataset_path="./my_deeplake/", overwrite=True ) db.similarity_search( "What did the president say about Ketanji Brown Jackson", filter={"metadata": {"year": 2013}}, ) db.similarity_search( "What did the president say about Ketanji Brown Jackson?", distance_metric="cos" ) db.max_marginal_relevance_search( "What did the president say about Ketanji Brown Jackson?" ) db.delete_dataset()
DeepLake.force_delete_by_path("./my_deeplake")
langchain_community.vectorstores.DeepLake.force_delete_by_path
get_ipython().system('pip3 install cerebrium') import os from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain_community.llms import CerebriumAI os.environ["CEREBRIUMAI_API_KEY"] = "YOUR_KEY_HERE" llm = CerebriumAI(endpoint_url="YOUR ENDPOINT URL HERE") template = """Question: {question} Answer: Let's think step by step.""" prompt = PromptTemplate.from_template(template) llm_chain =
LLMChain(prompt=prompt, llm=llm)
langchain.chains.LLMChain
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-core langchain langchain-openai') from langchain.utils.math import cosine_similarity from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import PromptTemplate from langchain_core.runnables import RunnableLambda, RunnablePassthrough from langchain_openai import ChatOpenAI, OpenAIEmbeddings physics_template = """You are a very smart physics professor. \ You are great at answering questions about physics in a concise and easy to understand manner. \ When you don't know the answer to a question you admit that you don't know. Here is a question: {query}""" math_template = """You are a very good mathematician. You are great at answering math questions. \ You are so good because you are able to break down hard problems into their component parts, \ answer the component parts, and then put them together to answer the broader question. Here is a question: {query}""" embeddings = OpenAIEmbeddings() prompt_templates = [physics_template, math_template] prompt_embeddings = embeddings.embed_documents(prompt_templates) def prompt_router(input): query_embedding = embeddings.embed_query(input["query"]) similarity = cosine_similarity([query_embedding], prompt_embeddings)[0] most_similar = prompt_templates[similarity.argmax()] print("Using MATH" if most_similar == math_template else "Using PHYSICS") return PromptTemplate.from_template(most_similar) chain = ( {"query": RunnablePassthrough()} | RunnableLambda(prompt_router) | ChatOpenAI() |
StrOutputParser()
langchain_core.output_parsers.StrOutputParser
from langchain.output_parsers import ( OutputFixingParser, PydanticOutputParser, ) from langchain.prompts import ( PromptTemplate, ) from langchain_core.pydantic_v1 import BaseModel, Field from langchain_openai import ChatOpenAI, OpenAI template = """Based on the user question, provide an Action and Action Input for what step should be taken. {format_instructions} Question: {query} Response:""" class Action(BaseModel): action: str = Field(description="action to take") action_input: str = Field(description="input to the action") parser = PydanticOutputParser(pydantic_object=Action) prompt = PromptTemplate( template="Answer the user query.\n{format_instructions}\n{query}\n", input_variables=["query"], partial_variables={"format_instructions": parser.get_format_instructions()}, ) prompt_value = prompt.format_prompt(query="who is leo di caprios gf?") bad_response = '{"action": "search"}' parser.parse(bad_response) fix_parser = OutputFixingParser.from_llm(parser=parser, llm=ChatOpenAI()) fix_parser.parse(bad_response) from langchain.output_parsers import RetryOutputParser retry_parser = RetryOutputParser.from_llm(parser=parser, llm=OpenAI(temperature=0)) retry_parser.parse_with_prompt(bad_response, prompt_value) from langchain_core.runnables import RunnableLambda, RunnableParallel completion_chain = prompt |
OpenAI(temperature=0)
langchain_openai.OpenAI
from langchain.retrievers import BreebsRetriever breeb_key = "Parivoyage" retriever =
BreebsRetriever(breeb_key)
langchain.retrievers.BreebsRetriever
import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass() from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import FAISS from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter loader = TextLoader("../../modules/state_of_the_union.txt") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings() db = FAISS.from_documents(docs, embeddings) query = "What did the president say about Ketanji Brown Jackson" docs = db.similarity_search(query) print(docs[0].page_content) retriever = db.as_retriever() docs = retriever.invoke(query) print(docs[0].page_content) docs_and_scores = db.similarity_search_with_score(query) docs_and_scores[0] embedding_vector = embeddings.embed_query(query) docs_and_scores = db.similarity_search_by_vector(embedding_vector) db.save_local("faiss_index") new_db = FAISS.load_local("faiss_index", embeddings) docs = new_db.similarity_search(query) docs[0] from langchain_community.embeddings.huggingface import HuggingFaceEmbeddings pkl = db.serialize_to_bytes() # serializes the faiss embeddings =
HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
langchain_community.embeddings.huggingface.HuggingFaceEmbeddings
get_ipython().run_line_magic('pip', 'install --upgrade --quiet sentence_transformers > /dev/null') from langchain_community.embeddings import HuggingFaceEmbeddings embeddings =
HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
langchain_community.embeddings.HuggingFaceEmbeddings
from ragatouille import RAGPretrainedModel RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0") import requests def get_wikipedia_page(title: str): """ Retrieve the full text content of a Wikipedia page. :param title: str - Title of the Wikipedia page. :return: str - Full text content of the page as raw string. """ URL = "https://en.wikipedia.org/w/api.php" params = { "action": "query", "format": "json", "titles": title, "prop": "extracts", "explaintext": True, } headers = {"User-Agent": "RAGatouille_tutorial/0.0.1 ([email protected])"} response = requests.get(URL, params=params, headers=headers) data = response.json() page = next(iter(data["query"]["pages"].values())) return page["extract"] if "extract" in page else None full_document = get_wikipedia_page("Hayao_Miyazaki") RAG.index( collection=[full_document], index_name="Miyazaki-123", max_document_length=180, split_documents=True, ) results = RAG.search(query="What animation studio did Miyazaki found?", k=3) results retriever = RAG.as_langchain_retriever(k=3) retriever.invoke("What animation studio did Miyazaki found?") from langchain.chains import create_retrieval_chain from langchain.chains.combine_documents import create_stuff_documents_chain from langchain_core.prompts import ChatPromptTemplate from langchain_openai import ChatOpenAI prompt = ChatPromptTemplate.from_template( """Answer the following question based only on the provided context: <context> {context} </context> Question: {input}""" ) llm = ChatOpenAI() document_chain =
create_stuff_documents_chain(llm, prompt)
langchain.chains.combine_documents.create_stuff_documents_chain
import os import pprint os.environ["SERPER_API_KEY"] = "" from langchain_community.utilities import GoogleSerperAPIWrapper search = GoogleSerperAPIWrapper() search.run("Obama's first name?") os.environ["OPENAI_API_KEY"] = "" from langchain.agents import AgentType, Tool, initialize_agent from langchain_community.utilities import GoogleSerperAPIWrapper from langchain_openai import OpenAI llm = OpenAI(temperature=0) search = GoogleSerperAPIWrapper() tools = [ Tool( name="Intermediate Answer", func=search.run, description="useful for when you need to ask with search", ) ] self_ask_with_search = initialize_agent( tools, llm, agent=AgentType.SELF_ASK_WITH_SEARCH, verbose=True ) self_ask_with_search.run( "What is the hometown of the reigning men's U.S. Open champion?" ) search = GoogleSerperAPIWrapper() results = search.results("Apple Inc.") pprint.pp(results) search = GoogleSerperAPIWrapper(type="images") results = search.results("Lion") pprint.pp(results) search =
GoogleSerperAPIWrapper(type="news")
langchain_community.utilities.GoogleSerperAPIWrapper
import json from pprint import pprint from langchain.globals import set_debug from langchain_community.llms import NIBittensorLLM set_debug(True) llm_sys = NIBittensorLLM( system_prompt="Your task is to determine response based on user prompt.Explain me like I am technical lead of a project" ) sys_resp = llm_sys( "What is bittensor and What are the potential benefits of decentralized AI?" ) print(f"Response provided by LLM with system prompt set is : {sys_resp}") """ { "choices": [ {"index": Bittensor's Metagraph index number, "uid": Unique Identifier of a miner, "responder_hotkey": Hotkey of a miner, "message":{"role":"assistant","content": Contains actual response}, "response_ms": Time in millisecond required to fetch response from a miner} ] } """ multi_response_llm = NIBittensorLLM(top_responses=10) multi_resp = multi_response_llm("What is Neural Network Feeding Mechanism?") json_multi_resp = json.loads(multi_resp) pprint(json_multi_resp) from langchain.chains import LLMChain from langchain.globals import set_debug from langchain.prompts import PromptTemplate from langchain_community.llms import NIBittensorLLM set_debug(True) template = """Question: {question} Answer: Let's think step by step.""" prompt = PromptTemplate.from_template(template) llm = NIBittensorLLM( system_prompt="Your task is to determine response based on user prompt." ) llm_chain = LLMChain(prompt=prompt, llm=llm) question = "What is bittensor?" llm_chain.run(question) from langchain.tools import Tool from langchain_community.utilities import GoogleSearchAPIWrapper search = GoogleSearchAPIWrapper() tool = Tool( name="Google Search", description="Search Google for recent results.", func=search.run, ) from langchain.agents import ( AgentExecutor, ZeroShotAgent, ) from langchain.chains import LLMChain from langchain.memory import ConversationBufferMemory from langchain.prompts import PromptTemplate from langchain_community.llms import NIBittensorLLM memory = ConversationBufferMemory(memory_key="chat_history") tools = [tool] prefix = """Answer prompt based on LLM if there is need to search something then use internet and observe internet result and give accurate reply of user questions also try to use authenticated sources""" suffix = """Begin! {chat_history} Question: {input} {agent_scratchpad}""" prompt = ZeroShotAgent.create_prompt( tools=tools, prefix=prefix, suffix=suffix, input_variables=["input", "chat_history", "agent_scratchpad"], ) llm = NIBittensorLLM( system_prompt="Your task is to determine a response based on user prompt" ) llm_chain = LLMChain(llm=llm, prompt=prompt) memory = ConversationBufferMemory(memory_key="chat_history") agent =
ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)
langchain.agents.ZeroShotAgent
get_ipython().run_line_magic('pip', 'install --upgrade --quiet pymysql') from langchain.chains import RetrievalQA from langchain_community.document_loaders import ( DirectoryLoader, UnstructuredMarkdownLoader, ) from langchain_community.vectorstores import StarRocks from langchain_community.vectorstores.starrocks import StarRocksSettings from langchain_openai import OpenAI, OpenAIEmbeddings from langchain_text_splitters import TokenTextSplitter update_vectordb = False loader = DirectoryLoader( "./docs", glob="**/*.md", loader_cls=UnstructuredMarkdownLoader ) documents = loader.load() text_splitter = TokenTextSplitter(chunk_size=400, chunk_overlap=50) split_docs = text_splitter.split_documents(documents) update_vectordb = True split_docs[-20] print("# docs = %d, # splits = %d" % (len(documents), len(split_docs))) def gen_starrocks(update_vectordb, embeddings, settings): if update_vectordb: docsearch = StarRocks.from_documents(split_docs, embeddings, config=settings) else: docsearch = StarRocks(embeddings, settings) return docsearch embeddings = OpenAIEmbeddings() settings =
StarRocksSettings()
langchain_community.vectorstores.starrocks.StarRocksSettings
get_ipython().run_line_magic('pip', 'install --upgrade --quiet apify-client langchain-openai langchain chromadb tiktoken') from langchain.indexes import VectorstoreIndexCreator from langchain_community.document_loaders.base import Document from langchain_community.utilities import ApifyWrapper import os os.environ["OPENAI_API_KEY"] = "Your OpenAI API key" os.environ["APIFY_API_TOKEN"] = "Your Apify API token" apify = ApifyWrapper() loader = apify.call_actor( actor_id="apify/website-content-crawler", run_input={"startUrls": [{"url": "https://python.langchain.com/en/latest/"}]}, dataset_mapping_function=lambda item: Document( page_content=item["text"] or "", metadata={"source": item["url"]} ), ) index =
VectorstoreIndexCreator()
langchain.indexes.VectorstoreIndexCreator
get_ipython().run_line_magic('pip', 'install --upgrade --quiet "docarray"') from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import DocArrayInMemorySearch from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter documents = TextLoader("../../modules/state_of_the_union.txt").load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings() db =
DocArrayInMemorySearch.from_documents(docs, embeddings)
langchain_community.vectorstores.DocArrayInMemorySearch.from_documents
get_ipython().run_line_magic('pip', 'install --upgrade --quiet annoy') from langchain_community.embeddings import HuggingFaceEmbeddings from langchain_community.vectorstores import Annoy embeddings_func = HuggingFaceEmbeddings() texts = ["pizza is great", "I love salad", "my car", "a dog"] vector_store = Annoy.from_texts(texts, embeddings_func) vector_store_v2 = Annoy.from_texts( texts, embeddings_func, metric="dot", n_trees=100, n_jobs=1 ) vector_store.similarity_search("food", k=3) vector_store.similarity_search_with_score("food", k=3) from langchain_community.document_loaders import TextLoader from langchain_text_splitters import CharacterTextSplitter loader = TextLoader("../../modules/state_of_the_union.txtn.txtn.txt") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.split_documents(documents) docs[:5] vector_store_from_docs = Annoy.from_documents(docs, embeddings_func) query = "What did the president say about Ketanji Brown Jackson" docs = vector_store_from_docs.similarity_search(query) print(docs[0].page_content[:100]) embs = embeddings_func.embed_documents(texts) data = list(zip(texts, embs)) vector_store_from_embeddings = Annoy.from_embeddings(data, embeddings_func) vector_store_from_embeddings.similarity_search_with_score("food", k=3) motorbike_emb = embeddings_func.embed_query("motorbike") vector_store.similarity_search_by_vector(motorbike_emb, k=3) vector_store.similarity_search_with_score_by_vector(motorbike_emb, k=3) vector_store.index_to_docstore_id some_docstore_id = 0 # texts[0] vector_store.docstore._dict[vector_store.index_to_docstore_id[some_docstore_id]] vector_store.similarity_search_with_score_by_index(some_docstore_id, k=3) vector_store.save_local("my_annoy_index_and_docstore") loaded_vector_store = Annoy.load_local( "my_annoy_index_and_docstore", embeddings=embeddings_func ) loaded_vector_store.similarity_search_with_score_by_index(some_docstore_id, k=3) import uuid from annoy import AnnoyIndex from langchain.docstore.document import Document from langchain.docstore.in_memory import InMemoryDocstore metadatas = [{"x": "food"}, {"x": "food"}, {"x": "stuff"}, {"x": "animal"}] embeddings = embeddings_func.embed_documents(texts) f = len(embeddings[0]) metric = "angular" index = AnnoyIndex(f, metric=metric) for i, emb in enumerate(embeddings): index.add_item(i, emb) index.build(10) documents = [] for i, text in enumerate(texts): metadata = metadatas[i] if metadatas else {} documents.append(
Document(page_content=text, metadata=metadata)
langchain.docstore.document.Document
get_ipython().run_line_magic('pip', 'install --upgrade --quiet opaqueprompts langchain') import os os.environ["OPAQUEPROMPTS_API_KEY"] = "<OPAQUEPROMPTS_API_KEY>" os.environ["OPENAI_API_KEY"] = "<OPENAI_API_KEY>" from langchain.callbacks.stdout import StdOutCallbackHandler from langchain.chains import LLMChain from langchain.globals import set_debug, set_verbose from langchain.memory import ConversationBufferWindowMemory from langchain.prompts import PromptTemplate from langchain_community.llms import OpaquePrompts from langchain_openai import OpenAI set_debug(True) set_verbose(True) prompt_template = """ As an AI assistant, you will answer questions according to given context. Sensitive personal information in the question is masked for privacy. For instance, if the original text says "Giana is good," it will be changed to "PERSON_998 is good." Here's how to handle these changes: * Consider these masked phrases just as placeholders, but still refer to them in a relevant way when answering. * It's possible that different masked terms might mean the same thing. Stick with the given term and don't modify it. * All masked terms follow the "TYPE_ID" pattern. * Please don't invent new masked terms. For instance, if you see "PERSON_998," don't come up with "PERSON_997" or "PERSON_999" unless they're already in the question. Conversation History: ```{history}``` Context : ```During our recent meeting on February 23, 2023, at 10:30 AM, John Doe provided me with his personal details. His email is [email protected] and his contact number is 650-456-7890. He lives in New York City, USA, and belongs to the American nationality with Christian beliefs and a leaning towards the Democratic party. He mentioned that he recently made a transaction using his credit card 4111 1111 1111 1111 and transferred bitcoins to the wallet address 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa. While discussing his European travels, he noted down his IBAN as GB29 NWBK 6016 1331 9268 19. Additionally, he provided his website as https://johndoeportfolio.com. John also discussed some of his US-specific details. He said his bank account number is 1234567890123456 and his drivers license is Y12345678. His ITIN is 987-65-4321, and he recently renewed his passport, the number for which is 123456789. He emphasized not to share his SSN, which is 123-45-6789. Furthermore, he mentioned that he accesses his work files remotely through the IP 192.168.1.1 and has a medical license number MED-123456. ``` Question: ```{question}``` """ chain = LLMChain( prompt=PromptTemplate.from_template(prompt_template), llm=OpaquePrompts(base_llm=OpenAI()), memory=ConversationBufferWindowMemory(k=2), verbose=True, ) print( chain.run( { "question": """Write a message to remind John to do password reset for his website to stay secure.""" }, callbacks=[StdOutCallbackHandler()], ) ) import langchain_community.utilities.opaqueprompts as op from langchain_core.output_parsers import StrOutputParser from langchain_core.runnables import RunnablePassthrough prompt = (PromptTemplate.from_template(prompt_template),) llm = OpenAI() pg_chain = ( op.sanitize | RunnablePassthrough.assign( response=(lambda x: x["sanitized_input"]) | prompt | llm | StrOutputParser(), ) | (lambda x:
op.desanitize(x["response"], x["secure_context"])
langchain_community.utilities.opaqueprompts.desanitize
from langchain_community.embeddings import VoyageEmbeddings embeddings = VoyageEmbeddings( voyage_api_key="[ Your Voyage API key ]", model="voyage-2" ) documents = [ "Caching embeddings enables the storage or temporary caching of embeddings, eliminating the necessity to recompute them each time.", "An LLMChain is a chain that composes basic LLM functionality. It consists of a PromptTemplate and a language model (either an LLM or chat model). It formats the prompt template using the input key values provided (and also memory key values, if available), passes the formatted string to LLM and returns the LLM output.", "A Runnable represents a generic unit of work that can be invoked, batched, streamed, and/or transformed.", ] documents_embds = embeddings.embed_documents(documents) documents_embds[0][:5] query = "What's an LLMChain?" query_embd = embeddings.embed_query(query) query_embd[:5] from langchain.retrievers import KNNRetriever retriever =
KNNRetriever.from_texts(documents, embeddings)
langchain.retrievers.KNNRetriever.from_texts
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-elasticsearch langchain-openai tiktoken langchain') import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") from langchain_elasticsearch import ElasticsearchStore from langchain_openai import OpenAIEmbeddings from langchain_community.document_loaders import TextLoader from langchain_text_splitters import CharacterTextSplitter loader = TextLoader("../../modules/state_of_the_union.txt") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=0) docs = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings() db = ElasticsearchStore.from_documents( docs, embeddings, es_url="http://localhost:9200", index_name="test-basic", ) db.client.indices.refresh(index="test-basic") query = "What did the president say about Ketanji Brown Jackson" results = db.similarity_search(query) print(results) for i, doc in enumerate(docs): doc.metadata["date"] = f"{range(2010, 2020)[i % 10]}-01-01" doc.metadata["rating"] = range(1, 6)[i % 5] doc.metadata["author"] = ["John Doe", "Jane Doe"][i % 2] db = ElasticsearchStore.from_documents( docs, embeddings, es_url="http://localhost:9200", index_name="test-metadata" ) query = "What did the president say about Ketanji Brown Jackson" docs = db.similarity_search(query) print(docs[0].metadata) docs = db.similarity_search( query, filter=[{"term": {"metadata.author.keyword": "John Doe"}}] ) print(docs[0].metadata) docs = db.similarity_search( query, filter=[{"match": {"metadata.author": {"query": "Jon", "fuzziness": "AUTO"}}}], ) print(docs[0].metadata) docs = db.similarity_search( "Any mention about Fred?", filter=[{"range": {"metadata.date": {"gte": "2010-01-01"}}}], ) print(docs[0].metadata) docs = db.similarity_search( "Any mention about Fred?", filter=[{"range": {"metadata.rating": {"gte": 2}}}] ) print(docs[0].metadata) docs = db.similarity_search( "Any mention about Fred?", filter=[ { "geo_distance": { "distance": "200km", "metadata.geo_location": {"lat": 40, "lon": -70}, } } ], ) print(docs[0].metadata) db = ElasticsearchStore.from_documents( docs, embeddings, es_url="http://localhost:9200", index_name="test", strategy=
ElasticsearchStore.ApproxRetrievalStrategy()
langchain_elasticsearch.ElasticsearchStore.ApproxRetrievalStrategy
get_ipython().run_line_magic('pip', 'install --upgrade --quiet networkx') from langchain.indexes import GraphIndexCreator from langchain_openai import OpenAI index_creator = GraphIndexCreator(llm=
OpenAI(temperature=0)
langchain_openai.OpenAI
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-nvidia-ai-endpoints') import getpass import os if not os.environ.get("NVIDIA_API_KEY", "").startswith("nvapi-"): nvapi_key = getpass.getpass("Enter your NVIDIA API key: ") assert nvapi_key.startswith("nvapi-"), f"{nvapi_key[:5]}... is not a valid key" os.environ["NVIDIA_API_KEY"] = nvapi_key from langchain_nvidia_ai_endpoints import ChatNVIDIA llm =
ChatNVIDIA(model="mixtral_8x7b")
langchain_nvidia_ai_endpoints.ChatNVIDIA
get_ipython().run_line_magic('pip', 'install --upgrade --quiet nlpcloud') from getpass import getpass NLPCLOUD_API_KEY = getpass() import os os.environ["NLPCLOUD_API_KEY"] = NLPCLOUD_API_KEY from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain_community.llms import NLPCloud template = """Question: {question} Answer: Let's think step by step.""" prompt = PromptTemplate.from_template(template) llm =
NLPCloud()
langchain_community.llms.NLPCloud
from langchain.agents import AgentExecutor, BaseMultiActionAgent, Tool from langchain_community.utilities import SerpAPIWrapper def random_word(query: str) -> str: print("\nNow I'm doing this!") return "foo" search = SerpAPIWrapper() tools = [ Tool( name="Search", func=search.run, description="useful for when you need to answer questions about current events", ), Tool( name="RandomWord", func=random_word, description="call this to get a random word.", ), ] from typing import Any, List, Tuple, Union from langchain_core.agents import AgentAction, AgentFinish class FakeAgent(BaseMultiActionAgent): """Fake Custom Agent.""" @property def input_keys(self): return ["input"] def plan( self, intermediate_steps: List[Tuple[AgentAction, str]], **kwargs: Any ) -> Union[List[AgentAction], AgentFinish]: """Given input, decided what to do. Args: intermediate_steps: Steps the LLM has taken to date, along with observations **kwargs: User inputs. Returns: Action specifying what tool to use. """ if len(intermediate_steps) == 0: return [
AgentAction(tool="Search", tool_input=kwargs["input"], log="")
langchain_core.agents.AgentAction
get_ipython().run_line_magic('pip', 'install --upgrade --quiet lxml') get_ipython().run_line_magic('pip', 'install --upgrade --quiet html2text') from langchain_community.document_loaders import EverNoteLoader loader =
EverNoteLoader("example_data/testing.enex")
langchain_community.document_loaders.EverNoteLoader
from langchain.chains import HypotheticalDocumentEmbedder, LLMChain from langchain.prompts import PromptTemplate from langchain_openai import OpenAI, OpenAIEmbeddings base_embeddings = OpenAIEmbeddings() llm =
OpenAI()
langchain_openai.OpenAI
import os from langchain_community.utilities import OpenWeatherMapAPIWrapper os.environ["OPENWEATHERMAP_API_KEY"] = "" weather = OpenWeatherMapAPIWrapper() weather_data = weather.run("London,GB") print(weather_data) import os from langchain.agents import AgentType, initialize_agent, load_tools from langchain_openai import OpenAI os.environ["OPENAI_API_KEY"] = "" os.environ["OPENWEATHERMAP_API_KEY"] = "" llm = OpenAI(temperature=0) tools =
load_tools(["openweathermap-api"], llm)
langchain.agents.load_tools
get_ipython().system('pip install termcolor > /dev/null') import logging logging.basicConfig(level=logging.ERROR) from datetime import datetime, timedelta from typing import List from langchain.docstore import InMemoryDocstore from langchain.retrievers import TimeWeightedVectorStoreRetriever from langchain_community.vectorstores import FAISS from langchain_openai import ChatOpenAI, OpenAIEmbeddings from termcolor import colored USER_NAME = "Person A" # The name you want to use when interviewing the agent. LLM = ChatOpenAI(max_tokens=1500) # Can be any LLM you want. from langchain_experimental.generative_agents import ( GenerativeAgent, GenerativeAgentMemory, ) import math import faiss def relevance_score_fn(score: float) -> float: """Return a similarity score on a scale [0, 1].""" return 1.0 - score / math.sqrt(2) def create_new_memory_retriever(): """Create a new vector store retriever unique to the agent.""" embeddings_model = OpenAIEmbeddings() embedding_size = 1536 index = faiss.IndexFlatL2(embedding_size) vectorstore = FAISS( embeddings_model.embed_query, index,
InMemoryDocstore({})
langchain.docstore.InMemoryDocstore
REGION = "us-central1" # @param {type:"string"} INSTANCE = "test-instance" # @param {type:"string"} DB_USER = "sqlserver" # @param {type:"string"} DB_PASS = "password" # @param {type:"string"} DATABASE = "test" # @param {type:"string"} TABLE_NAME = "test-default" # @param {type:"string"} get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-google-cloud-sql-mssql') from google.colab import auth auth.authenticate_user() PROJECT_ID = "my-project-id" # @param {type:"string"} get_ipython().system('gcloud config set project {PROJECT_ID}') get_ipython().system('gcloud services enable sqladmin.googleapis.com') from langchain_google_cloud_sql_mssql import MSSQLEngine engine = MSSQLEngine.from_instance( project_id=PROJECT_ID, region=REGION, instance=INSTANCE, database=DATABASE, user=DB_USER, password=DB_PASS, ) engine.init_document_table(TABLE_NAME, overwrite_existing=True) from langchain_core.documents import Document from langchain_google_cloud_sql_mssql import MSSQLDocumentSaver test_docs = [ Document( page_content="Apple Granny Smith 150 0.99 1", metadata={"fruit_id": 1}, ), Document( page_content="Banana Cavendish 200 0.59 0", metadata={"fruit_id": 2}, ), Document( page_content="Orange Navel 80 1.29 1", metadata={"fruit_id": 3}, ), ] saver = MSSQLDocumentSaver(engine=engine, table_name=TABLE_NAME) saver.add_documents(test_docs) from langchain_google_cloud_sql_mssql import MSSQLLoader loader = MSSQLLoader(engine=engine, table_name=TABLE_NAME) docs = loader.lazy_load() for doc in docs: print("Loaded documents:", doc) from langchain_google_cloud_sql_mssql import MSSQLLoader loader = MSSQLLoader( engine=engine, query=f"select * from \"{TABLE_NAME}\" where JSON_VALUE(langchain_metadata, '$.fruit_id') = 1;", ) onedoc = loader.load() onedoc from langchain_google_cloud_sql_mssql import MSSQLLoader loader = MSSQLLoader(engine=engine, table_name=TABLE_NAME) docs = loader.load() print("Documents before delete:", docs) saver.delete(onedoc) print("Documents after delete:", loader.load()) import sqlalchemy with engine.connect() as conn: conn.execute(sqlalchemy.text(f'DROP TABLE IF EXISTS "{TABLE_NAME}"')) conn.commit() conn.execute( sqlalchemy.text( f""" IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[{TABLE_NAME}]') AND type in (N'U')) BEGIN CREATE TABLE [dbo].[{TABLE_NAME}]( fruit_id INT IDENTITY(1,1) PRIMARY KEY, fruit_name VARCHAR(100) NOT NULL, variety VARCHAR(50), quantity_in_stock INT NOT NULL, price_per_unit DECIMAL(6,2) NOT NULL, organic BIT NOT NULL ) END """ ) ) conn.execute( sqlalchemy.text( f""" INSERT INTO "{TABLE_NAME}" (fruit_name, variety, quantity_in_stock, price_per_unit, organic) VALUES ('Apple', 'Granny Smith', 150, 0.99, 1), ('Banana', 'Cavendish', 200, 0.59, 0), ('Orange', 'Navel', 80, 1.29, 1); """ ) ) conn.commit() loader = MSSQLLoader( engine=engine, table_name=TABLE_NAME, ) loader.load() loader = MSSQLLoader( engine=engine, table_name=TABLE_NAME, content_columns=[ "variety", "quantity_in_stock", "price_per_unit", "organic", ], metadata_columns=["fruit_id", "fruit_name"], ) loader.load() engine.init_document_table( TABLE_NAME, metadata_columns=[ sqlalchemy.Column( "fruit_name", sqlalchemy.UnicodeText, primary_key=False, nullable=True, ), sqlalchemy.Column( "organic", sqlalchemy.Boolean, primary_key=False, nullable=True, ), ], content_column="description", metadata_json_column="other_metadata", overwrite_existing=True, ) test_docs = [ Document( page_content="Granny Smith 150 0.99", metadata={"fruit_id": 1, "fruit_name": "Apple", "organic": 1}, ), ] saver = MSSQLDocumentSaver( engine=engine, table_name=TABLE_NAME, content_column="description", metadata_json_column="other_metadata", ) saver.add_documents(test_docs) with engine.connect() as conn: result = conn.execute(sqlalchemy.text(f'select * from "{TABLE_NAME}";')) print(result.keys()) print(result.fetchall()) loader =
MSSQLLoader(engine=engine, table_name=TABLE_NAME)
langchain_google_cloud_sql_mssql.MSSQLLoader
from langchain.chains import LLMCheckerChain from langchain_openai import OpenAI llm = OpenAI(temperature=0.7) text = "What type of mammal lays the biggest eggs?" checker_chain =
LLMCheckerChain.from_llm(llm, verbose=True)
langchain.chains.LLMCheckerChain.from_llm
from langchain_community.llms import Ollama llm = Ollama(model="llama2") llm("The first man on the moon was ...") from langchain.callbacks.manager import CallbackManager from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler llm = Ollama( model="llama2", callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]) ) llm("The first man on the moon was ...") from langchain_community.llms import Ollama llm =
Ollama(model="llama2:13b")
langchain_community.llms.Ollama
from langchain.evaluation import load_evaluator evaluator = load_evaluator("criteria", criteria="conciseness") from langchain.evaluation import EvaluatorType evaluator = load_evaluator(EvaluatorType.CRITERIA, criteria="conciseness") eval_result = evaluator.evaluate_strings( prediction="What's 2+2? That's an elementary question. The answer you're looking for is that two and two is four.", input="What's 2+2?", ) print(eval_result) evaluator = load_evaluator("labeled_criteria", criteria="correctness") eval_result = evaluator.evaluate_strings( input="What is the capital of the US?", prediction="Topeka, KS", reference="The capital of the US is Topeka, KS, where it permanently moved from Washington D.C. on May 16, 2023", ) print(f'With ground truth: {eval_result["score"]}') from langchain.evaluation import Criteria list(Criteria) custom_criterion = { "numeric": "Does the output contain numeric or mathematical information?" } eval_chain = load_evaluator( EvaluatorType.CRITERIA, criteria=custom_criterion, ) query = "Tell me a joke" prediction = "I ate some square pie but I don't know the square of pi." eval_result = eval_chain.evaluate_strings(prediction=prediction, input=query) print(eval_result) custom_criteria = { "numeric": "Does the output contain numeric information?", "mathematical": "Does the output contain mathematical information?", "grammatical": "Is the output grammatically correct?", "logical": "Is the output logical?", } eval_chain = load_evaluator( EvaluatorType.CRITERIA, criteria=custom_criteria, ) eval_result = eval_chain.evaluate_strings(prediction=prediction, input=query) print("Multi-criteria evaluation") print(eval_result) from langchain.chains.constitutional_ai.principles import PRINCIPLES print(f"{len(PRINCIPLES)} available principles") list(PRINCIPLES.items())[:5] evaluator =
load_evaluator(EvaluatorType.CRITERIA, criteria=PRINCIPLES["harmful1"])
langchain.evaluation.load_evaluator
from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory chat_message_history = MongoDBChatMessageHistory( session_id="test_session", connection_string="mongodb://mongo_user:password123@mongo:27017", database_name="my_db", collection_name="chat_histories", ) chat_message_history.add_user_message("Hello") chat_message_history.add_ai_message("Hi") chat_message_history.messages from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain_core.runnables.history import RunnableWithMessageHistory from langchain_openai import ChatOpenAI import os assert os.environ[ "OPENAI_API_KEY" ], "Set the OPENAI_API_KEY environment variable with your OpenAI API key." prompt = ChatPromptTemplate.from_messages( [ ("system", "You are a helpful assistant."),
MessagesPlaceholder(variable_name="history")
langchain_core.prompts.MessagesPlaceholder
SOURCE = "test" # @param {type:"Query"|"CollectionGroup"|"DocumentReference"|"string"} get_ipython().run_line_magic('pip', 'install -upgrade --quiet langchain-google-firestore') PROJECT_ID = "my-project-id" # @param {type:"string"} get_ipython().system('gcloud config set project {PROJECT_ID}') from google.colab import auth auth.authenticate_user() get_ipython().system('gcloud services enable firestore.googleapis.com') from langchain_core.documents.base import Document from langchain_google_firestore import FirestoreSaver saver = FirestoreSaver() data = [Document(page_content="Hello, World!")] saver.upsert_documents(data) saver = FirestoreSaver("Collection") saver.upsert_documents(data) doc_ids = ["AnotherCollection/doc_id", "foo/bar"] saver = FirestoreSaver() saver.upsert_documents(documents=data, document_ids=doc_ids) from langchain_google_firestore import FirestoreLoader loader_collection = FirestoreLoader("Collection") loader_subcollection = FirestoreLoader("Collection/doc/SubCollection") data_collection = loader_collection.load() data_subcollection = loader_subcollection.load() from google.cloud import firestore client = firestore.Client() doc_ref = client.collection("foo").document("bar") loader_document =
FirestoreLoader(doc_ref)
langchain_google_firestore.FirestoreLoader
from langchain.agents import AgentExecutor, Tool, ZeroShotAgent from langchain.chains import LLMChain from langchain.memory import ConversationBufferMemory from langchain_community.utilities import GoogleSearchAPIWrapper from langchain_openai import OpenAI search = GoogleSearchAPIWrapper() tools = [ Tool( name="Search", func=search.run, description="useful for when you need to answer questions about current events", ) ] prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:""" suffix = """Begin!" {chat_history} Question: {input} {agent_scratchpad}""" prompt = ZeroShotAgent.create_prompt( tools, prefix=prefix, suffix=suffix, input_variables=["input", "chat_history", "agent_scratchpad"], ) memory = ConversationBufferMemory(memory_key="chat_history") llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt) agent =
ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)
langchain.agents.ZeroShotAgent
from langchain.callbacks import get_openai_callback from langchain_openai import ChatOpenAI llm = ChatOpenAI(model_name="gpt-4") with get_openai_callback() as cb: result = llm.invoke("Tell me a joke") print(cb) with
get_openai_callback()
langchain.callbacks.get_openai_callback
get_ipython().run_line_magic('', 'pip install --upgrade --quiet flashrank') get_ipython().run_line_magic('', 'pip install --upgrade --quiet faiss') get_ipython().run_line_magic('', 'pip install --upgrade --quiet faiss_cpu') def pretty_print_docs(docs): print( f"\n{'-' * 100}\n".join( [f"Document {i+1}:\n\n" + d.page_content for i, d in enumerate(docs)] ) ) import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass() from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import FAISS from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import RecursiveCharacterTextSplitter documents = TextLoader( "../../modules/state_of_the_union.txt", ).load() text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100) texts = text_splitter.split_documents(documents) embedding = OpenAIEmbeddings(model="text-embedding-ada-002") retriever = FAISS.from_documents(texts, embedding).as_retriever(search_kwargs={"k": 20}) query = "What did the president say about Ketanji Brown Jackson" docs = retriever.get_relevant_documents(query) pretty_print_docs(docs) from langchain.retrievers import ContextualCompressionRetriever from langchain.retrievers.document_compressors import FlashrankRerank from langchain_openai import ChatOpenAI llm = ChatOpenAI(temperature=0) compressor = FlashrankRerank() compression_retriever = ContextualCompressionRetriever( base_compressor=compressor, base_retriever=retriever ) compressed_docs = compression_retriever.get_relevant_documents( "What did the president say about Ketanji Jackson Brown" ) print([doc.metadata["id"] for doc in compressed_docs]) pretty_print_docs(compressed_docs) from langchain.chains import RetrievalQA chain =
RetrievalQA.from_chain_type(llm=llm, retriever=compression_retriever)
langchain.chains.RetrievalQA.from_chain_type
import os os.environ["SCENEX_API_KEY"] = "<YOUR_API_KEY>" from langchain.agents import load_tools tools =
load_tools(["sceneXplain"])
langchain.agents.load_tools
from langchain_core.pydantic_v1 import BaseModel, Field class Joke(BaseModel): setup: str =
Field(description="The setup of the joke")
langchain_core.pydantic_v1.Field
from langchain_community.document_loaders import ObsidianLoader loader =
ObsidianLoader("<path-to-obsidian>")
langchain_community.document_loaders.ObsidianLoader
from langchain.callbacks.manager import CallbackManager from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler from langchain.prompts import PromptTemplate from langchain_community.llms import TitanTakeoffPro llm =
TitanTakeoffPro()
langchain_community.llms.TitanTakeoffPro
get_ipython().system("python3 -m pip install --upgrade langchain 'deeplake[enterprise]' openai tiktoken") import getpass import os from langchain.chains import RetrievalQA from langchain_community.vectorstores import DeepLake from langchain_openai import OpenAI, OpenAIEmbeddings from langchain_text_splitters import ( CharacterTextSplitter, RecursiveCharacterTextSplitter, ) os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") activeloop_token = getpass.getpass("Activeloop Token:") os.environ["ACTIVELOOP_TOKEN"] = activeloop_token os.environ["ACTIVELOOP_ORG"] = getpass.getpass("Activeloop Org:") org_id = os.environ["ACTIVELOOP_ORG"] embeddings = OpenAIEmbeddings() dataset_path = "hub://" + org_id + "/data" with open("messages.txt") as f: state_of_the_union = f.read() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) pages = text_splitter.split_text(state_of_the_union) text_splitter =
RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
langchain_text_splitters.RecursiveCharacterTextSplitter
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-community') import os os.environ["YDC_API_KEY"] = "" os.environ["OPENAI_API_KEY"] = "" from langchain_community.tools.you import YouSearchTool from langchain_community.utilities.you import YouSearchAPIWrapper api_wrapper = YouSearchAPIWrapper(num_web_results=1) tool = YouSearchTool(api_wrapper=api_wrapper) tool response = tool.invoke("What is the weather in NY") print(len(response)) for item in response: print(item) get_ipython().system('pip install --upgrade --quiet langchain langchain-openai langchainhub langchain-community') from langchain import hub from langchain.agents import AgentExecutor, create_openai_functions_agent from langchain_openai import ChatOpenAI instructions = """You are an assistant.""" base_prompt = hub.pull("langchain-ai/openai-functions-template") prompt = base_prompt.partial(instructions=instructions) llm = ChatOpenAI(temperature=0) you_tool = YouSearchTool(api_wrapper=
YouSearchAPIWrapper(num_web_results=1)
langchain_community.utilities.you.YouSearchAPIWrapper
import requests def download_drive_file(url: str, output_path: str = "chat.db") -> None: file_id = url.split("/")[-2] download_url = f"https://drive.google.com/uc?export=download&id={file_id}" response = requests.get(download_url) if response.status_code != 200: print("Failed to download the file.") return with open(output_path, "wb") as file: file.write(response.content) print(f"File {output_path} downloaded.") url = ( "https://drive.google.com/file/d/1NebNKqTA2NXApCmeH6mu0unJD2tANZzo/view?usp=sharing" ) download_drive_file(url) from langchain_community.chat_loaders.imessage import IMessageChatLoader loader = IMessageChatLoader( path="./chat.db", ) from typing import List from langchain_community.chat_loaders.base import ChatSession from langchain_community.chat_loaders.utils import ( map_ai_messages, merge_chat_runs, ) raw_messages = loader.lazy_load() merged_messages = merge_chat_runs(raw_messages) chat_sessions: List[ChatSession] = list(
map_ai_messages(merged_messages, sender="Tortoise")
langchain_community.chat_loaders.utils.map_ai_messages
import getpass import os os.environ["TAVILY_API_KEY"] = getpass.getpass() from langchain.retrievers.tavily_search_api import TavilySearchAPIRetriever retriever = TavilySearchAPIRetriever(k=3) retriever.invoke("what year was breath of the wild released?") from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate from langchain_core.runnables import RunnablePassthrough from langchain_openai import ChatOpenAI prompt = ChatPromptTemplate.from_template( """Answer the question based only on the context provided. Context: {context} Question: {question}""" ) chain = (
RunnablePassthrough.assign(context=(lambda x: x["question"]) | retriever)
langchain_core.runnables.RunnablePassthrough.assign
get_ipython().run_line_magic('pip', 'install --upgrade --quiet boto3 langchain-openai tiktoken python-dotenv') get_ipython().run_line_magic('pip', 'install --upgrade --quiet "amazon-textract-caller>=0.2.0"') from langchain_community.document_loaders import AmazonTextractPDFLoader loader =
AmazonTextractPDFLoader("example_data/alejandro_rosalez_sample-small.jpeg")
langchain_community.document_loaders.AmazonTextractPDFLoader
get_ipython().run_line_magic('pip', 'install --upgrade --quiet opencv-python scikit-image') import os from langchain_openai import OpenAI os.environ["OPENAI_API_KEY"] = "<your-key-here>" from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain_community.utilities.dalle_image_generator import DallEAPIWrapper from langchain_openai import OpenAI llm = OpenAI(temperature=0.9) prompt = PromptTemplate( input_variables=["image_desc"], template="Generate a detailed prompt to generate an image based on the following description: {image_desc}", ) chain = LLMChain(llm=llm, prompt=prompt) image_url = DallEAPIWrapper().run(chain.run("halloween night at a haunted museum")) image_url try: import google.colab IN_COLAB = True except ImportError: IN_COLAB = False if IN_COLAB: from google.colab.patches import cv2_imshow # for image display from skimage import io image = io.imread(image_url) cv2_imshow(image) else: import cv2 from skimage import io image = io.imread(image_url) cv2.imshow("image", image) cv2.waitKey(0) # wait for a keyboard input cv2.destroyAllWindows() from langchain.agents import initialize_agent, load_tools tools =
load_tools(["dalle-image-generator"])
langchain.agents.load_tools
from langchain.tools import ShellTool shell_tool =
ShellTool()
langchain.tools.ShellTool
from langchain_community.vectorstores import Bagel texts = ["hello bagel", "hello langchain", "I love salad", "my car", "a dog"] cluster = Bagel.from_texts(cluster_name="testing", texts=texts) cluster.similarity_search("bagel", k=3) cluster.similarity_search_with_score("bagel", k=3) cluster.delete_cluster() from langchain_community.document_loaders import TextLoader from langchain_text_splitters import CharacterTextSplitter loader = TextLoader("../../modules/state_of_the_union.txt") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.split_documents(documents)[:10] cluster =
Bagel.from_documents(cluster_name="testing_with_docs", documents=docs)
langchain_community.vectorstores.Bagel.from_documents
get_ipython().system(' pip install -U langchain openai chromadb langchain-experimental # (newest versions required for multi-modal)') get_ipython().system(' pip install "unstructured[all-docs]==0.10.19" pillow pydantic lxml pillow matplotlib tiktoken open_clip_torch torch') path = "/Users/rlm/Desktop/cpi/" from langchain_community.document_loaders import PyPDFLoader loader = PyPDFLoader(path + "cpi.pdf") pdf_pages = loader.load() from langchain_text_splitters import RecursiveCharacterTextSplitter text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0) all_splits_pypdf = text_splitter.split_documents(pdf_pages) all_splits_pypdf_texts = [d.page_content for d in all_splits_pypdf] from unstructured.partition.pdf import partition_pdf raw_pdf_elements = partition_pdf( filename=path + "cpi.pdf", extract_images_in_pdf=True, infer_table_structure=True, chunking_strategy="by_title", max_characters=4000, new_after_n_chars=3800, combine_text_under_n_chars=2000, image_output_dir_path=path, ) tables = [] texts = [] for element in raw_pdf_elements: if "unstructured.documents.elements.Table" in str(type(element)): tables.append(str(element)) elif "unstructured.documents.elements.CompositeElement" in str(type(element)): texts.append(str(element)) from langchain_community.vectorstores import Chroma from langchain_openai import OpenAIEmbeddings baseline = Chroma.from_texts( texts=all_splits_pypdf_texts, collection_name="baseline", embedding=OpenAIEmbeddings(), ) retriever_baseline = baseline.as_retriever() from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate from langchain_openai import ChatOpenAI prompt_text = """You are an assistant tasked with summarizing tables and text for retrieval. \ These summaries will be embedded and used to retrieve the raw text or table elements. \ Give a concise summary of the table or text that is well optimized for retrieval. Table or text: {element} """ prompt = ChatPromptTemplate.from_template(prompt_text) model = ChatOpenAI(temperature=0, model="gpt-4") summarize_chain = {"element": lambda x: x} | prompt | model | StrOutputParser() text_summaries = summarize_chain.batch(texts, {"max_concurrency": 5}) table_summaries = summarize_chain.batch(tables, {"max_concurrency": 5}) import base64 import io import os from io import BytesIO from langchain_core.messages import HumanMessage from PIL import Image def encode_image(image_path): """Getting the base64 string""" with open(image_path, "rb") as image_file: return base64.b64encode(image_file.read()).decode("utf-8") def image_summarize(img_base64, prompt): """Image summary""" chat = ChatOpenAI(model="gpt-4-vision-preview", max_tokens=1024) msg = chat.invoke( [ HumanMessage( content=[ {"type": "text", "text": prompt}, { "type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img_base64}"}, }, ] ) ] ) return msg.content img_base64_list = [] image_summaries = [] prompt = """You are an assistant tasked with summarizing images for retrieval. \ These summaries will be embedded and used to retrieve the raw image. \ Give a concise summary of the image that is well optimized for retrieval.""" for img_file in sorted(os.listdir(path)): if img_file.endswith(".jpg"): img_path = os.path.join(path, img_file) base64_image = encode_image(img_path) img_base64_list.append(base64_image) image_summaries.append(image_summarize(base64_image, prompt)) import uuid from base64 import b64decode from langchain.retrievers.multi_vector import MultiVectorRetriever from langchain.storage import InMemoryStore from langchain_core.documents import Document def create_multi_vector_retriever( vectorstore, text_summaries, texts, table_summaries, tables, image_summaries, images ): store =
InMemoryStore()
langchain.storage.InMemoryStore
api_key = "" from langchain_community.document_loaders import ToMarkdownLoader loader =
ToMarkdownLoader( url="https://python.langchain.com/docs/get_started/introduction", api_key=api_key )
langchain_community.document_loaders.ToMarkdownLoader
get_ipython().run_line_magic('pip', 'install --upgrade --quiet predictionguard langchain') import os from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain_community.llms import PredictionGuard os.environ["OPENAI_API_KEY"] = "<your OpenAI api key>" os.environ["PREDICTIONGUARD_TOKEN"] = "<your Prediction Guard access token>" pgllm = PredictionGuard(model="OpenAI-text-davinci-003") pgllm("Tell me a joke") template = """Respond to the following query based on the context. Context: EVERY comment, DM + email suggestion has led us to this EXCITING announcement! 🎉 We have officially added TWO new candle subscription box options! 📦 Exclusive Candle Box - $80 Monthly Candle Box - $45 (NEW!) Scent of The Month Box - $28 (NEW!) Head to stories to get ALLL the deets on each box! 👆 BONUS: Save 50% on your first box with code 50OFF! 🎉 Query: {query} Result: """ prompt = PromptTemplate.from_template(template) pgllm(prompt.format(query="What kind of post is this?")) pgllm = PredictionGuard( model="OpenAI-text-davinci-003", output={ "type": "categorical", "categories": ["product announcement", "apology", "relational"], }, ) pgllm(prompt.format(query="What kind of post is this?")) pgllm = PredictionGuard(model="OpenAI-text-davinci-003") template = """Question: {question} Answer: Let's think step by step.""" prompt = PromptTemplate.from_template(template) llm_chain = LLMChain(prompt=prompt, llm=pgllm, verbose=True) question = "What NFL team won the Super Bowl in the year Justin Beiber was born?" llm_chain.predict(question=question) template = """Write a {adjective} poem about {subject}.""" prompt = PromptTemplate.from_template(template) llm_chain =
LLMChain(prompt=prompt, llm=pgllm, verbose=True)
langchain.chains.LLMChain
get_ipython().run_line_magic('pip', 'install -qU langchain-community langchain-openai') from langchain_community.tools import MoveFileTool from langchain_core.messages import HumanMessage from langchain_core.utils.function_calling import convert_to_openai_function from langchain_openai import ChatOpenAI model = ChatOpenAI(model="gpt-3.5-turbo") tools = [MoveFileTool()] functions = [
convert_to_openai_function(t)
langchain_core.utils.function_calling.convert_to_openai_function
from typing import List from langchain.output_parsers import PydanticOutputParser from langchain.prompts import PromptTemplate from langchain_core.pydantic_v1 import BaseModel, Field, validator from langchain_openai import ChatOpenAI model = ChatOpenAI(temperature=0) class Joke(BaseModel): setup: str = Field(description="question to set up a joke") punchline: str = Field(description="answer to resolve the joke") @
validator("setup")
langchain_core.pydantic_v1.validator
get_ipython().run_line_magic('pip', 'install --upgrade --quiet cos-python-sdk-v5') from langchain_community.document_loaders import TencentCOSDirectoryLoader from qcloud_cos import CosConfig conf = CosConfig( Region="your cos region", SecretId="your cos secret_id", SecretKey="your cos secret_key", ) loader =
TencentCOSDirectoryLoader(conf=conf, bucket="you_cos_bucket")
langchain_community.document_loaders.TencentCOSDirectoryLoader
get_ipython().run_line_magic('pip', 'install --upgrade --quiet clickhouse-connect') import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") os.environ["OPENAI_API_BASE"] = getpass.getpass("OpenAI Base:") os.environ["MYSCALE_HOST"] = getpass.getpass("MyScale Host:") os.environ["MYSCALE_PORT"] = getpass.getpass("MyScale Port:") os.environ["MYSCALE_USERNAME"] = getpass.getpass("MyScale Username:") os.environ["MYSCALE_PASSWORD"] = getpass.getpass("MyScale Password:") from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import MyScale from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter from langchain_community.document_loaders import TextLoader loader = TextLoader("../../modules/state_of_the_union.txt") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings() for d in docs: d.metadata = {"some": "metadata"} docsearch = MyScale.from_documents(docs, embeddings) query = "What did the president say about Ketanji Brown Jackson" docs = docsearch.similarity_search(query) print(docs[0].page_content) print(str(docsearch)) from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import MyScale loader = TextLoader("../../modules/state_of_the_union.txt") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings() for i, d in enumerate(docs): d.metadata = {"doc_id": i} docsearch =
MyScale.from_documents(docs, embeddings)
langchain_community.vectorstores.MyScale.from_documents
get_ipython().run_line_magic('pip', 'install --upgrade --quiet playwright > /dev/null') get_ipython().run_line_magic('pip', 'install --upgrade --quiet lxml') from langchain_community.agent_toolkits import PlayWrightBrowserToolkit from langchain_community.tools.playwright.utils import ( create_async_playwright_browser, # A synchronous browser is available, though it isn't compatible with jupyter.\n", }, ) import nest_asyncio nest_asyncio.apply() async_browser = create_async_playwright_browser() toolkit =
PlayWrightBrowserToolkit.from_browser(async_browser=async_browser)
langchain_community.agent_toolkits.PlayWrightBrowserToolkit.from_browser
from langchain.vectorstores import NeuralDBVectorStore vectorstore =
NeuralDBVectorStore.from_scratch(thirdai_key="your-thirdai-key")
langchain.vectorstores.NeuralDBVectorStore.from_scratch
get_ipython().system(' pip install "openai>=1" "langchain>=0.0.331rc2" matplotlib pillow') import base64 import io import os import numpy as np from IPython.display import HTML, display from PIL import Image def encode_image(image_path): """Getting the base64 string""" with open(image_path, "rb") as image_file: return base64.b64encode(image_file.read()).decode("utf-8") def plt_img_base64(img_base64): """Display the base64 image""" image_html = f'<img src="data:image/jpeg;base64,{img_base64}" />' display(HTML(image_html)) path = "/Users/rlm/Desktop/Multimodal_Eval/qa/llm_strategies.jpeg" img_base64 = encode_image(path) plt_img_base64(img_base64) from langchain_core.messages import HumanMessage, SystemMessage from langchain_openai import ChatOpenAI chat =
ChatOpenAI(model="gpt-4-vision-preview", max_tokens=1024)
langchain_openai.ChatOpenAI
import asyncio import os import nest_asyncio import pandas as pd from langchain.docstore.document import Document from langchain_community.agent_toolkits.pandas.base import create_pandas_dataframe_agent from langchain_experimental.autonomous_agents import AutoGPT from langchain_openai import ChatOpenAI nest_asyncio.apply() llm = ChatOpenAI(model_name="gpt-4", temperature=1.0) import os from contextlib import contextmanager from typing import Optional from langchain.agents import tool from langchain_community.tools.file_management.read import ReadFileTool from langchain_community.tools.file_management.write import WriteFileTool ROOT_DIR = "./data/" @contextmanager def pushd(new_dir): """Context manager for changing the current working directory.""" prev_dir = os.getcwd() os.chdir(new_dir) try: yield finally: os.chdir(prev_dir) @tool def process_csv( csv_file_path: str, instructions: str, output_path: Optional[str] = None ) -> str: """Process a CSV by with pandas in a limited REPL.\ Only use this after writing data to disk as a csv file.\ Any figures must be saved to disk to be viewed by the human.\ Instructions should be written in natural language, not code. Assume the dataframe is already loaded.""" with pushd(ROOT_DIR): try: df = pd.read_csv(csv_file_path) except Exception as e: return f"Error: {e}" agent = create_pandas_dataframe_agent(llm, df, max_iterations=30, verbose=True) if output_path is not None: instructions += f" Save output to disk at {output_path}" try: result = agent.run(instructions) return result except Exception as e: return f"Error: {e}" async def async_load_playwright(url: str) -> str: """Load the specified URLs using Playwright and parse using BeautifulSoup.""" from bs4 import BeautifulSoup from playwright.async_api import async_playwright results = "" async with async_playwright() as p: browser = await p.chromium.launch(headless=True) try: page = await browser.new_page() await page.goto(url) page_source = await page.content() soup = BeautifulSoup(page_source, "html.parser") for script in soup(["script", "style"]): script.extract() text = soup.get_text() lines = (line.strip() for line in text.splitlines()) chunks = (phrase.strip() for line in lines for phrase in line.split(" ")) results = "\n".join(chunk for chunk in chunks if chunk) except Exception as e: results = f"Error: {e}" await browser.close() return results def run_async(coro): event_loop = asyncio.get_event_loop() return event_loop.run_until_complete(coro) @tool def browse_web_page(url: str) -> str: """Verbose way to scrape a whole webpage. Likely to cause issues parsing.""" return run_async(async_load_playwright(url)) from langchain.chains.qa_with_sources.loading import ( BaseCombineDocumentsChain, load_qa_with_sources_chain, ) from langchain.tools import BaseTool, DuckDuckGoSearchRun from langchain_text_splitters import RecursiveCharacterTextSplitter from pydantic import Field def _get_text_splitter(): return RecursiveCharacterTextSplitter( chunk_size=500, chunk_overlap=20, length_function=len, ) class WebpageQATool(BaseTool): name = "query_webpage" description = ( "Browse a webpage and retrieve the information relevant to the question." ) text_splitter: RecursiveCharacterTextSplitter = Field( default_factory=_get_text_splitter ) qa_chain: BaseCombineDocumentsChain def _run(self, url: str, question: str) -> str: """Useful for browsing websites and scraping the text information.""" result = browse_web_page.run(url) docs = [Document(page_content=result, metadata={"source": url})] web_docs = self.text_splitter.split_documents(docs) results = [] for i in range(0, len(web_docs), 4): input_docs = web_docs[i : i + 4] window_result = self.qa_chain( {"input_documents": input_docs, "question": question}, return_only_outputs=True, ) results.append(f"Response from window {i} - {window_result}") results_docs = [ Document(page_content="\n".join(results), metadata={"source": url}) ] return self.qa_chain( {"input_documents": results_docs, "question": question}, return_only_outputs=True, ) async def _arun(self, url: str, question: str) -> str: raise NotImplementedError query_website_tool = WebpageQATool(qa_chain=load_qa_with_sources_chain(llm)) import faiss from langchain.docstore import InMemoryDocstore from langchain_community.vectorstores import FAISS from langchain_openai import OpenAIEmbeddings embeddings_model = OpenAIEmbeddings() embedding_size = 1536 index = faiss.IndexFlatL2(embedding_size) vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) web_search = DuckDuckGoSearchRun() tools = [ web_search,
WriteFileTool(root_dir="./data")
langchain_community.tools.file_management.write.WriteFileTool
get_ipython().run_line_magic('pip', 'install --upgrade --quiet weaviate-client') import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") WEAVIATE_URL = getpass.getpass("WEAVIATE_URL:") os.environ["WEAVIATE_API_KEY"] = getpass.getpass("WEAVIATE_API_KEY:") WEAVIATE_API_KEY = os.environ["WEAVIATE_API_KEY"] from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import Weaviate from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter from langchain_community.document_loaders import TextLoader loader = TextLoader("../../modules/state_of_the_union.txt") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings() db =
Weaviate.from_documents(docs, embeddings, weaviate_url=WEAVIATE_URL, by_text=False)
langchain_community.vectorstores.Weaviate.from_documents
get_ipython().run_line_magic('pip', 'install --upgrade --quiet marqo') from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import Marqo from langchain_text_splitters import CharacterTextSplitter from langchain_community.document_loaders import TextLoader loader = TextLoader("../../modules/state_of_the_union.txt") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.split_documents(documents) import marqo marqo_url = "http://localhost:8882" # if using marqo cloud replace with your endpoint (console.marqo.ai) marqo_api_key = "" # if using marqo cloud replace with your api key (console.marqo.ai) client = marqo.Client(url=marqo_url, api_key=marqo_api_key) index_name = "langchain-demo" docsearch = Marqo.from_documents(docs, index_name=index_name) query = "What did the president say about Ketanji Brown Jackson" result_docs = docsearch.similarity_search(query) print(result_docs[0].page_content) result_docs = docsearch.similarity_search_with_score(query) print(result_docs[0][0].page_content, result_docs[0][1], sep="\n") index_name = "langchain-multimodal-demo" try: client.delete_index(index_name) except Exception: print(f"Creating {index_name}") settings = {"treat_urls_and_pointers_as_images": True, "model": "ViT-L/14"} client.create_index(index_name, **settings) client.index(index_name).add_documents( [ { "caption": "Bus", "image": "https://raw.githubusercontent.com/marqo-ai/marqo/mainline/examples/ImageSearchGuide/data/image4.jpg", }, { "caption": "Plane", "image": "https://raw.githubusercontent.com/marqo-ai/marqo/mainline/examples/ImageSearchGuide/data/image2.jpg", }, ], ) def get_content(res): """Helper to format Marqo's documents into text to be used as page_content""" return f"{res['caption']}: {res['image']}" docsearch = Marqo(client, index_name, page_content_builder=get_content) query = "vehicles that fly" doc_results = docsearch.similarity_search(query) for doc in doc_results: print(doc.page_content) index_name = "langchain-byo-index-demo" try: client.delete_index(index_name) except Exception: print(f"Creating {index_name}") client.create_index(index_name) client.index(index_name).add_documents( [ { "Title": "Smartphone", "Description": "A smartphone is a portable computer device that combines mobile telephone " "functions and computing functions into one unit.", }, { "Title": "Telephone", "Description": "A telephone is a telecommunications device that permits two or more users to" "conduct a conversation when they are too far apart to be easily heard directly.", }, ], ) def get_content(res): """Helper to format Marqo's documents into text to be used as page_content""" if "text" in res: return res["text"] return res["Description"] docsearch =
Marqo(client, index_name, page_content_builder=get_content)
langchain_community.vectorstores.Marqo
get_ipython().run_line_magic('pip', 'install --upgrade --quiet duckduckgo-search') from langchain.tools import DuckDuckGoSearchRun search = DuckDuckGoSearchRun() search.run("Obama's first name?") from langchain.tools import DuckDuckGoSearchResults search = DuckDuckGoSearchResults() search.run("Obama") search =
DuckDuckGoSearchResults(backend="news")
langchain.tools.DuckDuckGoSearchResults
from langchain.output_parsers import ResponseSchema, StructuredOutputParser from langchain.prompts import PromptTemplate from langchain_openai import ChatOpenAI response_schemas = [
ResponseSchema(name="answer", description="answer to the user's question")
langchain.output_parsers.ResponseSchema
get_ipython().run_line_magic('pip', 'install --upgrade --quiet lark chromadb') from langchain_community.vectorstores import Chroma from langchain_core.documents import Document from langchain_openai import OpenAIEmbeddings docs = [ Document( page_content="A bunch of scientists bring back dinosaurs and mayhem breaks loose", metadata={"year": 1993, "rating": 7.7, "genre": "science fiction"}, ), Document( page_content="Leo DiCaprio gets lost in a dream within a dream within a dream within a ...", metadata={"year": 2010, "director": "Christopher Nolan", "rating": 8.2}, ), Document( page_content="A psychologist / detective gets lost in a series of dreams within dreams within dreams and Inception reused the idea", metadata={"year": 2006, "director": "Satoshi Kon", "rating": 8.6}, ), Document( page_content="A bunch of normal-sized women are supremely wholesome and some men pine after them", metadata={"year": 2019, "director": "Greta Gerwig", "rating": 8.3}, ), Document( page_content="Toys come alive and have a blast doing so", metadata={"year": 1995, "genre": "animated"}, ), Document( page_content="Three men walk into the Zone, three men walk out of the Zone", metadata={ "year": 1979, "director": "Andrei Tarkovsky", "genre": "thriller", "rating": 9.9, }, ), ] vectorstore = Chroma.from_documents(docs, OpenAIEmbeddings()) from langchain.chains.query_constructor.base import AttributeInfo from langchain.retrievers.self_query.base import SelfQueryRetriever from langchain_openai import ChatOpenAI metadata_field_info = [ AttributeInfo( name="genre", description="The genre of the movie. One of ['science fiction', 'comedy', 'drama', 'thriller', 'romance', 'action', 'animated']", type="string", ), AttributeInfo( name="year", description="The year the movie was released", type="integer", ), AttributeInfo( name="director", description="The name of the movie director", type="string", ), AttributeInfo( name="rating", description="A 1-10 rating for the movie", type="float" ), ] document_content_description = "Brief summary of a movie" llm = ChatOpenAI(temperature=0) retriever = SelfQueryRetriever.from_llm( llm, vectorstore, document_content_description, metadata_field_info, ) retriever.invoke("I want to watch a movie rated higher than 8.5") retriever.invoke("Has Greta Gerwig directed any movies about women") retriever.invoke("What's a highly rated (above 8.5) science fiction film?") retriever.invoke( "What's a movie after 1990 but before 2005 that's all about toys, and preferably is animated" ) retriever = SelfQueryRetriever.from_llm( llm, vectorstore, document_content_description, metadata_field_info, enable_limit=True, ) retriever.invoke("What are two movies about dinosaurs") from langchain.chains.query_constructor.base import ( StructuredQueryOutputParser, get_query_constructor_prompt, ) prompt = get_query_constructor_prompt( document_content_description, metadata_field_info, ) output_parser =
StructuredQueryOutputParser.from_components()
langchain.chains.query_constructor.base.StructuredQueryOutputParser.from_components
get_ipython().run_line_magic('pip', "install --upgrade --quiet faiss-gpu # For CUDA 7.5+ Supported GPU's.") get_ipython().run_line_magic('pip', 'install --upgrade --quiet faiss-cpu # For CPU Installation') import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import FAISS from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter loader = TextLoader("../../../extras/modules/state_of_the_union.txt") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings() db = await FAISS.afrom_documents(docs, embeddings) query = "What did the president say about Ketanji Brown Jackson" docs = await db.asimilarity_search(query) print(docs[0].page_content) docs_and_scores = await db.asimilarity_search_with_score(query) docs_and_scores[0] embedding_vector = await embeddings.aembed_query(query) docs_and_scores = await db.asimilarity_search_by_vector(embedding_vector) db.save_local("faiss_index") new_db =
FAISS.load_local("faiss_index", embeddings, asynchronous=True)
langchain_community.vectorstores.FAISS.load_local
get_ipython().run_line_magic('pip', 'install --upgrade --quiet promptlayer --upgrade') import promptlayer # Don't forget this 🍰 from langchain.callbacks import PromptLayerCallbackHandler from langchain.schema import ( HumanMessage, ) from langchain_openai import ChatOpenAI chat_llm = ChatOpenAI( temperature=0, callbacks=[PromptLayerCallbackHandler(pl_tags=["chatopenai"])], ) llm_results = chat_llm( [ HumanMessage(content="What comes after 1,2,3 ?"),
HumanMessage(content="Tell me another joke?")
langchain.schema.HumanMessage
from langchain_community.document_loaders import ConcurrentLoader loader =
ConcurrentLoader.from_filesystem("example_data/", glob="**/*.txt")
langchain_community.document_loaders.ConcurrentLoader.from_filesystem
from langchain import hub from langchain.agents import AgentExecutor, tool from langchain.agents.output_parsers import XMLAgentOutputParser from langchain_community.chat_models import ChatAnthropic model = ChatAnthropic(model="claude-2") @tool def search(query: str) -> str: """Search things about current events.""" return "32 degrees" tool_list = [search] prompt = hub.pull("hwchase17/xml-agent-convo") def convert_intermediate_steps(intermediate_steps): log = "" for action, observation in intermediate_steps: log += ( f"<tool>{action.tool}</tool><tool_input>{action.tool_input}" f"</tool_input><observation>{observation}</observation>" ) return log def convert_tools(tools): return "\n".join([f"{tool.name}: {tool.description}" for tool in tools]) agent = ( { "input": lambda x: x["input"], "agent_scratchpad": lambda x: convert_intermediate_steps( x["intermediate_steps"] ), } | prompt.partial(tools=convert_tools(tool_list)) | model.bind(stop=["</tool_input>", "</final_answer>"]) | XMLAgentOutputParser() ) agent_executor =
AgentExecutor(agent=agent, tools=tool_list, verbose=True)
langchain.agents.AgentExecutor
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-community') import os os.environ["YDC_API_KEY"] = "" os.environ["OPENAI_API_KEY"] = "" from langchain_community.tools.you import YouSearchTool from langchain_community.utilities.you import YouSearchAPIWrapper api_wrapper = YouSearchAPIWrapper(num_web_results=1) tool =
YouSearchTool(api_wrapper=api_wrapper)
langchain_community.tools.you.YouSearchTool
get_ipython().run_line_magic('pip', 'install --upgrade --quiet praw') client_id = "" client_secret = "" user_agent = "" from langchain_community.tools.reddit_search.tool import RedditSearchRun from langchain_community.utilities.reddit_search import RedditSearchAPIWrapper search = RedditSearchRun( api_wrapper=RedditSearchAPIWrapper( reddit_client_id=client_id, reddit_client_secret=client_secret, reddit_user_agent=user_agent, ) ) from langchain_community.tools.reddit_search.tool import RedditSearchSchema search_params = RedditSearchSchema( query="beginner", sort="new", time_filter="week", subreddit="python", limit="2" ) result = search.run(tool_input=search_params.dict()) print(result) from langchain.agents import AgentExecutor, StructuredChatAgent, Tool from langchain.chains import LLMChain from langchain.memory import ConversationBufferMemory, ReadOnlySharedMemory from langchain.prompts import PromptTemplate from langchain_community.tools.reddit_search.tool import RedditSearchRun from langchain_community.utilities.reddit_search import RedditSearchAPIWrapper from langchain_openai import ChatOpenAI client_id = "" client_secret = "" user_agent = "" openai_api_key = "" template = """This is a conversation between a human and a bot: {chat_history} Write a summary of the conversation for {input}: """ prompt = PromptTemplate(input_variables=["input", "chat_history"], template=template) memory = ConversationBufferMemory(memory_key="chat_history") prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:""" suffix = """Begin!" {chat_history} Question: {input} {agent_scratchpad}""" tools = [ RedditSearchRun( api_wrapper=RedditSearchAPIWrapper( reddit_client_id=client_id, reddit_client_secret=client_secret, reddit_user_agent=user_agent, ) ) ] prompt = StructuredChatAgent.create_prompt( prefix=prefix, tools=tools, suffix=suffix, input_variables=["input", "chat_history", "agent_scratchpad"], ) llm = ChatOpenAI(temperature=0, openai_api_key=openai_api_key) llm_chain = LLMChain(llm=llm, prompt=prompt) agent =
StructuredChatAgent(llm_chain=llm_chain, verbose=True, tools=tools)
langchain.agents.StructuredChatAgent
get_ipython().run_cell_magic('capture', '', '%pip install --upgrade --quiet python-arango # The ArangoDB Python Driver\n%pip install --upgrade --quiet adb-cloud-connector # The ArangoDB Cloud Instance provisioner\n%pip install --upgrade --quiet langchain-openai\n%pip install --upgrade --quiet langchain\n') import json from adb_cloud_connector import get_temp_credentials from arango import ArangoClient con = get_temp_credentials() db = ArangoClient(hosts=con["url"]).db( con["dbName"], con["username"], con["password"], verify=True ) print(json.dumps(con, indent=2)) from langchain_community.graphs import ArangoGraph graph =
ArangoGraph(db)
langchain_community.graphs.ArangoGraph
import os os.environ["EXA_API_KEY"] = "..." get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-exa') get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain langchain-openai') from langchain_core.prompts import PromptTemplate from langchain_core.runnables import RunnableParallel, RunnablePassthrough from langchain_exa import ExaSearchRetriever, TextContentsOptions from langchain_openai import ChatOpenAI retriever = ExaSearchRetriever( k=5, text_contents_options=TextContentsOptions(max_length=200) ) prompt =
PromptTemplate.from_template( """Answer the following query based on the following context: query: {query} <context> {context} </context""" )
langchain_core.prompts.PromptTemplate.from_template
from langchain.retrievers.multi_vector import MultiVectorRetriever from langchain.storage import InMemoryByteStore from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import Chroma from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import RecursiveCharacterTextSplitter loaders = [ TextLoader("../../paul_graham_essay.txt"), TextLoader("../../state_of_the_union.txt"), ] docs = [] for loader in loaders: docs.extend(loader.load()) text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000) docs = text_splitter.split_documents(docs) vectorstore = Chroma( collection_name="full_documents", embedding_function=OpenAIEmbeddings() ) store = InMemoryByteStore() id_key = "doc_id" retriever = MultiVectorRetriever( vectorstore=vectorstore, byte_store=store, id_key=id_key, ) import uuid doc_ids = [str(uuid.uuid4()) for _ in docs] child_text_splitter = RecursiveCharacterTextSplitter(chunk_size=400) sub_docs = [] for i, doc in enumerate(docs): _id = doc_ids[i] _sub_docs = child_text_splitter.split_documents([doc]) for _doc in _sub_docs: _doc.metadata[id_key] = _id sub_docs.extend(_sub_docs) retriever.vectorstore.add_documents(sub_docs) retriever.docstore.mset(list(zip(doc_ids, docs))) retriever.vectorstore.similarity_search("justice breyer")[0] len(retriever.get_relevant_documents("justice breyer")[0].page_content) from langchain.retrievers.multi_vector import SearchType retriever.search_type = SearchType.mmr len(retriever.get_relevant_documents("justice breyer")[0].page_content) import uuid from langchain_core.documents import Document from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate from langchain_openai import ChatOpenAI chain = ( {"doc": lambda x: x.page_content} | ChatPromptTemplate.from_template("Summarize the following document:\n\n{doc}") | ChatOpenAI(max_retries=0) | StrOutputParser() ) summaries = chain.batch(docs, {"max_concurrency": 5}) vectorstore = Chroma(collection_name="summaries", embedding_function=OpenAIEmbeddings()) store = InMemoryByteStore() id_key = "doc_id" retriever = MultiVectorRetriever( vectorstore=vectorstore, byte_store=store, id_key=id_key, ) doc_ids = [str(uuid.uuid4()) for _ in docs] summary_docs = [ Document(page_content=s, metadata={id_key: doc_ids[i]}) for i, s in enumerate(summaries) ] retriever.vectorstore.add_documents(summary_docs) retriever.docstore.mset(list(zip(doc_ids, docs))) sub_docs = vectorstore.similarity_search("justice breyer") sub_docs[0] retrieved_docs = retriever.get_relevant_documents("justice breyer") len(retrieved_docs[0].page_content) functions = [ { "name": "hypothetical_questions", "description": "Generate hypothetical questions", "parameters": { "type": "object", "properties": { "questions": { "type": "array", "items": {"type": "string"}, }, }, "required": ["questions"], }, } ] from langchain.output_parsers.openai_functions import JsonKeyOutputFunctionsParser chain = ( {"doc": lambda x: x.page_content} | ChatPromptTemplate.from_template( "Generate a list of exactly 3 hypothetical questions that the below document could be used to answer:\n\n{doc}" ) | ChatOpenAI(max_retries=0, model="gpt-4").bind( functions=functions, function_call={"name": "hypothetical_questions"} ) | JsonKeyOutputFunctionsParser(key_name="questions") ) chain.invoke(docs[0]) hypothetical_questions = chain.batch(docs, {"max_concurrency": 5}) vectorstore = Chroma( collection_name="hypo-questions", embedding_function=OpenAIEmbeddings() ) store =
InMemoryByteStore()
langchain.storage.InMemoryByteStore
from langchain_experimental.llm_symbolic_math.base import LLMSymbolicMathChain from langchain_openai import OpenAI llm = OpenAI(temperature=0) llm_symbolic_math =
LLMSymbolicMathChain.from_llm(llm)
langchain_experimental.llm_symbolic_math.base.LLMSymbolicMathChain.from_llm
get_ipython().run_line_magic('pip', 'install --upgrade --quiet pipeline-ai') import os from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain_community.llms import PipelineAI os.environ["PIPELINE_API_KEY"] = "YOUR_API_KEY_HERE" llm =
PipelineAI(pipeline_key="YOUR_PIPELINE_KEY", pipeline_kwargs={...})
langchain_community.llms.PipelineAI
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain_openai.chat_models import ChatOpenAI model =
ChatOpenAI()
langchain_openai.chat_models.ChatOpenAI
meals = [ "Beef Enchiladas with Feta cheese. Mexican-Greek fusion", "Chicken Flatbreads with red sauce. Italian-Mexican fusion", "Veggie sweet potato quesadillas with vegan cheese", "One-Pan Tortelonni bake with peppers and onions", ] from langchain_openai import OpenAI llm = OpenAI(model="gpt-3.5-turbo-instruct") from langchain.prompts import PromptTemplate PROMPT_TEMPLATE = """Here is the description of a meal: "{meal}". Embed the meal into the given text: "{text_to_personalize}". Prepend a personalized message including the user's name "{user}" and their preference "{preference}". Make it sound good. """ PROMPT = PromptTemplate( input_variables=["meal", "text_to_personalize", "user", "preference"], template=PROMPT_TEMPLATE, ) import langchain_experimental.rl_chain as rl_chain chain = rl_chain.PickBest.from_llm(llm=llm, prompt=PROMPT) response = chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Tom"), preference=rl_chain.BasedOn(["Vegetarian", "regular dairy is ok"]), text_to_personalize="This is the weeks specialty dish, our master chefs \ believe you will love it!", ) print(response["response"]) for _ in range(5): try: response = chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Tom"), preference=rl_chain.BasedOn(["Vegetarian", "regular dairy is ok"]), text_to_personalize="This is the weeks specialty dish, our master chefs believe you will love it!", ) except Exception as e: print(e) print(response["response"]) print() scoring_criteria_template = ( "Given {preference} rank how good or bad this selection is {meal}" ) chain = rl_chain.PickBest.from_llm( llm=llm, prompt=PROMPT, selection_scorer=rl_chain.AutoSelectionScorer( llm=llm, scoring_criteria_template_str=scoring_criteria_template ), ) response = chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Tom"), preference=rl_chain.BasedOn(["Vegetarian", "regular dairy is ok"]), text_to_personalize="This is the weeks specialty dish, our master chefs believe you will love it!", ) print(response["response"]) selection_metadata = response["selection_metadata"] print( f"selected index: {selection_metadata.selected.index}, score: {selection_metadata.selected.score}" ) class CustomSelectionScorer(rl_chain.SelectionScorer): def score_response( self, inputs, llm_response: str, event: rl_chain.PickBestEvent ) -> float: print(event.based_on) print(event.to_select_from) selected_meal = event.to_select_from["meal"][event.selected.index] print(f"selected meal: {selected_meal}") if "Tom" in event.based_on["user"]: if "Vegetarian" in event.based_on["preference"]: if "Chicken" in selected_meal or "Beef" in selected_meal: return 0.0 else: return 1.0 else: if "Chicken" in selected_meal or "Beef" in selected_meal: return 1.0 else: return 0.0 else: raise NotImplementedError("I don't know how to score this user") chain = rl_chain.PickBest.from_llm( llm=llm, prompt=PROMPT, selection_scorer=CustomSelectionScorer(), ) response = chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Tom"), preference=rl_chain.BasedOn(["Vegetarian", "regular dairy is ok"]), text_to_personalize="This is the weeks specialty dish, our master chefs believe you will love it!", ) class CustomSelectionScorer(rl_chain.SelectionScorer): def score_preference(self, preference, selected_meal): if "Vegetarian" in preference: if "Chicken" in selected_meal or "Beef" in selected_meal: return 0.0 else: return 1.0 else: if "Chicken" in selected_meal or "Beef" in selected_meal: return 1.0 else: return 0.0 def score_response( self, inputs, llm_response: str, event: rl_chain.PickBestEvent ) -> float: selected_meal = event.to_select_from["meal"][event.selected.index] if "Tom" in event.based_on["user"]: return self.score_preference(event.based_on["preference"], selected_meal) elif "Anna" in event.based_on["user"]: return self.score_preference(event.based_on["preference"], selected_meal) else: raise NotImplementedError("I don't know how to score this user") chain = rl_chain.PickBest.from_llm( llm=llm, prompt=PROMPT, selection_scorer=CustomSelectionScorer(), metrics_step=5, metrics_window_size=5, # rolling window average ) random_chain = rl_chain.PickBest.from_llm( llm=llm, prompt=PROMPT, selection_scorer=CustomSelectionScorer(), metrics_step=5, metrics_window_size=5, # rolling window average policy=rl_chain.PickBestRandomPolicy, # set the random policy instead of default ) for _ in range(20): try: chain.run( meal=
rl_chain.ToSelectFrom(meals)
langchain_experimental.rl_chain.ToSelectFrom
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-nvidia-ai-endpoints') import getpass import os if not os.environ.get("NVIDIA_API_KEY", "").startswith("nvapi-"): nvapi_key = getpass.getpass("Enter your NVIDIA API key: ") assert nvapi_key.startswith("nvapi-"), f"{nvapi_key[:5]}... is not a valid key" os.environ["NVIDIA_API_KEY"] = nvapi_key from langchain_nvidia_ai_endpoints import ChatNVIDIA llm = ChatNVIDIA(model="mixtral_8x7b") result = llm.invoke("Write a ballad about LangChain.") print(result.content) print(llm.batch(["What's 2*3?", "What's 2*6?"])) for chunk in llm.stream("How far can a seagull fly in one day?"): print(chunk.content, end="|") async for chunk in llm.astream( "How long does it take for monarch butterflies to migrate?" ): print(chunk.content, end="|") ChatNVIDIA.get_available_models() from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate from langchain_nvidia_ai_endpoints import ChatNVIDIA prompt = ChatPromptTemplate.from_messages( [("system", "You are a helpful AI assistant named Fred."), ("user", "{input}")] ) chain = prompt | ChatNVIDIA(model="llama2_13b") | StrOutputParser() for txt in chain.stream({"input": "What's your name?"}): print(txt, end="") prompt = ChatPromptTemplate.from_messages( [ ( "system", "You are an expert coding AI. Respond only in valid python; no narration whatsoever.", ), ("user", "{input}"), ] ) chain = prompt | ChatNVIDIA(model="llama2_code_70b") |
StrOutputParser()
langchain_core.output_parsers.StrOutputParser
get_ipython().run_line_magic('pip', 'install -U --quiet langchain langchain_community openai chromadb langchain-experimental') get_ipython().run_line_magic('pip', 'install --quiet "unstructured[all-docs]" pypdf pillow pydantic lxml pillow matplotlib chromadb tiktoken') import logging import zipfile import requests logging.basicConfig(level=logging.INFO) data_url = "https://storage.googleapis.com/benchmarks-artifacts/langchain-docs-benchmarking/cj.zip" result = requests.get(data_url) filename = "cj.zip" with open(filename, "wb") as file: file.write(result.content) with zipfile.ZipFile(filename, "r") as zip_ref: zip_ref.extractall() from langchain_community.document_loaders import PyPDFLoader loader = PyPDFLoader("./cj/cj.pdf") docs = loader.load() tables = [] texts = [d.page_content for d in docs] len(texts) from langchain.prompts import PromptTemplate from langchain_community.chat_models import ChatVertexAI from langchain_community.llms import VertexAI from langchain_core.messages import AIMessage from langchain_core.output_parsers import StrOutputParser from langchain_core.runnables import RunnableLambda def generate_text_summaries(texts, tables, summarize_texts=False): """ Summarize text elements texts: List of str tables: List of str summarize_texts: Bool to summarize texts """ prompt_text = """You are an assistant tasked with summarizing tables and text for retrieval. \ These summaries will be embedded and used to retrieve the raw text or table elements. \ Give a concise summary of the table or text that is well optimized for retrieval. Table or text: {element} """ prompt = PromptTemplate.from_template(prompt_text) empty_response = RunnableLambda( lambda x: AIMessage(content="Error processing document") ) model = VertexAI( temperature=0, model_name="gemini-pro", max_output_tokens=1024 ).with_fallbacks([empty_response]) summarize_chain = {"element": lambda x: x} | prompt | model | StrOutputParser() text_summaries = [] table_summaries = [] if texts and summarize_texts: text_summaries = summarize_chain.batch(texts, {"max_concurrency": 1}) elif texts: text_summaries = texts if tables: table_summaries = summarize_chain.batch(tables, {"max_concurrency": 1}) return text_summaries, table_summaries text_summaries, table_summaries = generate_text_summaries( texts, tables, summarize_texts=True ) len(text_summaries) import base64 import os from langchain_core.messages import HumanMessage def encode_image(image_path): """Getting the base64 string""" with open(image_path, "rb") as image_file: return base64.b64encode(image_file.read()).decode("utf-8") def image_summarize(img_base64, prompt): """Make image summary""" model = ChatVertexAI(model_name="gemini-pro-vision", max_output_tokens=1024) msg = model( [ HumanMessage( content=[ {"type": "text", "text": prompt}, { "type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img_base64}"}, }, ] ) ] ) return msg.content def generate_img_summaries(path): """ Generate summaries and base64 encoded strings for images path: Path to list of .jpg files extracted by Unstructured """ img_base64_list = [] image_summaries = [] prompt = """You are an assistant tasked with summarizing images for retrieval. \ These summaries will be embedded and used to retrieve the raw image. \ Give a concise summary of the image that is well optimized for retrieval.""" for img_file in sorted(os.listdir(path)): if img_file.endswith(".jpg"): img_path = os.path.join(path, img_file) base64_image = encode_image(img_path) img_base64_list.append(base64_image) image_summaries.append(image_summarize(base64_image, prompt)) return img_base64_list, image_summaries img_base64_list, image_summaries = generate_img_summaries("./cj") len(image_summaries) import uuid from langchain.retrievers.multi_vector import MultiVectorRetriever from langchain.storage import InMemoryStore from langchain_community.embeddings import VertexAIEmbeddings from langchain_community.vectorstores import Chroma from langchain_core.documents import Document def create_multi_vector_retriever( vectorstore, text_summaries, texts, table_summaries, tables, image_summaries, images ): """ Create retriever that indexes summaries, but returns raw images or texts """ store = InMemoryStore() id_key = "doc_id" retriever = MultiVectorRetriever( vectorstore=vectorstore, docstore=store, id_key=id_key, ) def add_documents(retriever, doc_summaries, doc_contents): doc_ids = [str(uuid.uuid4()) for _ in doc_contents] summary_docs = [ Document(page_content=s, metadata={id_key: doc_ids[i]}) for i, s in enumerate(doc_summaries) ] retriever.vectorstore.add_documents(summary_docs) retriever.docstore.mset(list(zip(doc_ids, doc_contents))) if text_summaries: add_documents(retriever, text_summaries, texts) if table_summaries: add_documents(retriever, table_summaries, tables) if image_summaries: add_documents(retriever, image_summaries, images) return retriever vectorstore = Chroma( collection_name="mm_rag_cj_blog", embedding_function=
VertexAIEmbeddings(model_name="textembedding-gecko@latest")
langchain_community.embeddings.VertexAIEmbeddings