prompt
stringlengths
43
25.9k
completion
stringlengths
7
362
api
stringlengths
18
90
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)
langchain_community.vectorstores.DeepLake
import runhouse as rh from langchain_community.embeddings import ( SelfHostedEmbeddings, SelfHostedHuggingFaceEmbeddings, SelfHostedHuggingFaceInstructEmbeddings, ) gpu = rh.cluster(name="rh-a10x", instance_type="A100:1", use_spot=False) embeddings = SelfHostedHuggingFaceEmbeddings(hardware=gpu) text = "This is a test document." query_result = embeddings.embed_query(text) embeddings =
SelfHostedHuggingFaceInstructEmbeddings(hardware=gpu)
langchain_community.embeddings.SelfHostedHuggingFaceInstructEmbeddings
get_ipython().run_line_magic('pip', 'install --upgrade --quiet tiktoken langchain-openai python-dotenv datasets langchain deeplake beautifulsoup4 html2text ragas') ORG_ID = "..." import getpass import os from langchain.chains import RetrievalQA from langchain.vectorstores.deeplake import DeepLake from langchain_openai import OpenAIChat, OpenAIEmbeddings os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter your OpenAI API token: ") os.environ["ACTIVELOOP_TOKEN"] = getpass.getpass( "Enter your ActiveLoop API token: " ) # Get your API token from https://app.activeloop.ai, click on your profile picture in the top right corner, and select "API Tokens" token = os.getenv("ACTIVELOOP_TOKEN") openai_embeddings = OpenAIEmbeddings() db = DeepLake( dataset_path=f"hub://{ORG_ID}/deeplake-docs-deepmemory", # org_id stands for your username or organization from activeloop embedding=openai_embeddings, runtime={"tensor_db": True}, token=token, read_only=False, ) from urllib.parse import urljoin import requests from bs4 import BeautifulSoup def get_all_links(url): response = requests.get(url) if response.status_code != 200: print(f"Failed to retrieve the page: {url}") return [] soup = BeautifulSoup(response.content, "html.parser") links = [ urljoin(url, a["href"]) for a in soup.find_all("a", href=True) if a["href"] ] return links base_url = "https://docs.deeplake.ai/en/latest/" all_links = get_all_links(base_url) from langchain.document_loaders import AsyncHtmlLoader loader = AsyncHtmlLoader(all_links) docs = loader.load() from langchain.document_transformers import Html2TextTransformer html2text =
Html2TextTransformer()
langchain.document_transformers.Html2TextTransformer
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() 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 multi_vector_img = Chroma( collection_name="multi_vector_img", embedding_function=OpenAIEmbeddings() ) retriever_multi_vector_img = create_multi_vector_retriever( multi_vector_img, text_summaries, texts, table_summaries, tables, image_summaries, img_base64_list, ) query = "What percentage of CPI is dedicated to Housing, and how does it compare to the combined percentage of Medical Care, Apparel, and Other Goods and Services?" suffix_for_images = " Include any pie charts, graphs, or tables." docs = retriever_multi_vector_img.get_relevant_documents(query + suffix_for_images) from IPython.display import HTML, display def plt_img_base64(img_base64): image_html = f'<img src="data:image/jpeg;base64,{img_base64}" />' display(HTML(image_html)) plt_img_base64(docs[1]) multi_vector_text = Chroma( collection_name="multi_vector_text", embedding_function=OpenAIEmbeddings() ) retriever_multi_vector_img_summary = create_multi_vector_retriever( multi_vector_text, text_summaries, texts, table_summaries, tables, image_summaries, image_summaries, ) from langchain_experimental.open_clip import OpenCLIPEmbeddings multimodal_embd = Chroma( collection_name="multimodal_embd", embedding_function=OpenCLIPEmbeddings() ) image_uris = sorted( [ os.path.join(path, image_name) for image_name in os.listdir(path) if image_name.endswith(".jpg") ] ) if image_uris: multimodal_embd.add_images(uris=image_uris) if texts: multimodal_embd.add_texts(texts=texts) if tables: multimodal_embd.add_texts(texts=tables) retriever_multimodal_embd = multimodal_embd.as_retriever() from operator import itemgetter from langchain_core.runnables import RunnablePassthrough template = """Answer the question based only on the following context, which can include text and tables: {context} Question: {question} """ rag_prompt_text = ChatPromptTemplate.from_template(template) def text_rag_chain(retriever): """RAG chain""" model = ChatOpenAI(temperature=0, model="gpt-4") chain = ( {"context": retriever, "question": RunnablePassthrough()} | rag_prompt_text | model | StrOutputParser() ) return chain import re from langchain_core.documents import Document from langchain_core.runnables import RunnableLambda def looks_like_base64(sb): """Check if the string looks like base64.""" return re.match("^[A-Za-z0-9+/]+[=]{0,2}$", sb) is not None def is_image_data(b64data): """Check if the base64 data is an image by looking at the start of the data.""" image_signatures = { b"\xFF\xD8\xFF": "jpg", b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A": "png", b"\x47\x49\x46\x38": "gif", b"\x52\x49\x46\x46": "webp", } try: header = base64.b64decode(b64data)[:8] # Decode and get the first 8 bytes for sig, format in image_signatures.items(): if header.startswith(sig): return True return False except Exception: return False def split_image_text_types(docs): """Split base64-encoded images and texts.""" b64_images = [] texts = [] for doc in docs: if isinstance(doc, Document): doc = doc.page_content if looks_like_base64(doc) and is_image_data(doc): b64_images.append(doc) else: texts.append(doc) return {"images": b64_images, "texts": texts} def img_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": ( "Answer the question based only on the provided context, which can include text, tables, and image(s). " "If an image is provided, analyze it carefully to help answer the question.\n" f"User-provided question / keywords: {data_dict['question']}\n\n" "Text and / or tables:\n" f"{formatted_texts}" ), } messages.append(text_message) return [HumanMessage(content=messages)] def multi_modal_rag_chain(retriever): """Multi-modal RAG chain""" model = ChatOpenAI(temperature=0, model="gpt-4-vision-preview", max_tokens=1024) chain = ( { "context": retriever | RunnableLambda(split_image_text_types), "question": RunnablePassthrough(), } | RunnableLambda(img_prompt_func) | model |
StrOutputParser()
langchain_core.output_parsers.StrOutputParser
import os from langchain.chains import ConversationalRetrievalChain from langchain_community.vectorstores import Vectara from langchain_openai import OpenAI from langchain_community.document_loaders import TextLoader loader = TextLoader("state_of_the_union.txt") documents = loader.load() vectara = Vectara.from_documents(documents, embedding=None) from langchain.memory import ConversationBufferMemory memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True) openai_api_key = os.environ["OPENAI_API_KEY"] llm = OpenAI(openai_api_key=openai_api_key, temperature=0) retriever = vectara.as_retriever() d = retriever.get_relevant_documents( "What did the president say about Ketanji Brown Jackson", k=2 ) print(d) bot = ConversationalRetrievalChain.from_llm( llm, retriever, memory=memory, verbose=False ) query = "What did the president say about Ketanji Brown Jackson" result = bot.invoke({"question": query}) result["answer"] query = "Did he mention who she suceeded" result = bot.invoke({"question": query}) result["answer"] bot = ConversationalRetrievalChain.from_llm( OpenAI(temperature=0), vectara.as_retriever() ) chat_history = [] query = "What did the president say about Ketanji Brown Jackson" result = bot.invoke({"question": query, "chat_history": chat_history}) result["answer"] chat_history = [(query, result["answer"])] query = "Did he mention who she suceeded" result = bot.invoke({"question": query, "chat_history": chat_history}) result["answer"] bot = ConversationalRetrievalChain.from_llm( llm, vectara.as_retriever(), return_source_documents=True ) chat_history = [] query = "What did the president say about Ketanji Brown Jackson" result = bot.invoke({"question": query, "chat_history": chat_history}) result["source_documents"][0] from langchain.chains import LLMChain from langchain.chains.conversational_retrieval.prompts import CONDENSE_QUESTION_PROMPT from langchain.chains.question_answering import load_qa_chain question_generator = LLMChain(llm=llm, prompt=CONDENSE_QUESTION_PROMPT) doc_chain = load_qa_chain(llm, chain_type="map_reduce") chain = ConversationalRetrievalChain( retriever=vectara.as_retriever(), question_generator=question_generator, combine_docs_chain=doc_chain, ) chat_history = [] query = "What did the president say about Ketanji Brown Jackson" result = chain({"question": query, "chat_history": chat_history}) result["answer"] from langchain.chains.qa_with_sources import load_qa_with_sources_chain question_generator =
LLMChain(llm=llm, prompt=CONDENSE_QUESTION_PROMPT)
langchain.chains.llm.LLMChain
import logging from langchain.retrievers import RePhraseQueryRetriever from langchain_community.document_loaders import WebBaseLoader from langchain_community.vectorstores import Chroma from langchain_openai import ChatOpenAI, OpenAIEmbeddings from langchain_text_splitters import RecursiveCharacterTextSplitter logging.basicConfig() logging.getLogger("langchain.retrievers.re_phraser").setLevel(logging.INFO) loader = WebBaseLoader("https://lilianweng.github.io/posts/2023-06-23-agent/") data = loader.load() text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0) all_splits = text_splitter.split_documents(data) vectorstore = Chroma.from_documents(documents=all_splits, embedding=OpenAIEmbeddings()) llm =
ChatOpenAI(temperature=0)
langchain_openai.ChatOpenAI
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()
langchain_core.output_parsers.StrOutputParser
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}"), ("ai", "{output}"), ] ) few_shot_prompt = FewShotChatMessagePromptTemplate( example_prompt=example_prompt, examples=examples, ) prompt = ChatPromptTemplate.from_messages( [ ( "system", """You are an expert at world knowledge. Your task is to step back and paraphrase a question to a more generic step-back question, which is easier to answer. Here are a few examples:""", ), few_shot_prompt, ("user", "{question}"), ] ) question_gen = prompt | ChatOpenAI(temperature=0) | StrOutputParser() question = "was chatgpt around while trump was president?" question_gen.invoke({"question": question}) from langchain_community.utilities import DuckDuckGoSearchAPIWrapper search =
DuckDuckGoSearchAPIWrapper(max_results=4)
langchain_community.utilities.DuckDuckGoSearchAPIWrapper
get_ipython().run_line_magic('pip', 'install --upgrade --quiet aim') get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain') get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-openai') get_ipython().run_line_magic('pip', 'install --upgrade --quiet google-search-results') import os from datetime import datetime from langchain.callbacks import AimCallbackHandler, StdOutCallbackHandler from langchain_openai import OpenAI os.environ["OPENAI_API_KEY"] = "..." os.environ["SERPAPI_API_KEY"] = "..." session_group = datetime.now().strftime("%m.%d.%Y_%H.%M.%S") aim_callback = AimCallbackHandler( repo=".", experiment_name="scenario 1: OpenAI LLM", ) callbacks = [StdOutCallbackHandler(), aim_callback] llm = OpenAI(temperature=0, callbacks=callbacks) llm_result = llm.generate(["Tell me a joke", "Tell me a poem"] * 3) aim_callback.flush_tracker( langchain_asset=llm, experiment_name="scenario 2: Chain with multiple SubChains on multiple generations", ) from langchain.chains import LLMChain from langchain.prompts import PromptTemplate template = """You are a playwright. Given the title of play, it is your job to write a synopsis for that title. Title: {title} Playwright: This is a synopsis for the above play:""" prompt_template =
PromptTemplate(input_variables=["title"], template=template)
langchain.prompts.PromptTemplate
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") search.run("Obama") from langchain_community.utilities import DuckDuckGoSearchAPIWrapper wrapper = DuckDuckGoSearchAPIWrapper(region="de-de", time="d", max_results=2) search =
DuckDuckGoSearchResults(api_wrapper=wrapper, source="news")
langchain.tools.DuckDuckGoSearchResults
get_ipython().run_line_magic('pip', 'install --upgrade --quiet text-generation transformers google-search-results numexpr langchainhub sentencepiece jinja2') import os from langchain_community.llms import HuggingFaceTextGenInference ENDPOINT_URL = "<YOUR_ENDPOINT_URL_HERE>" HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN") llm = HuggingFaceTextGenInference( inference_server_url=ENDPOINT_URL, max_new_tokens=512, top_k=50, temperature=0.1, repetition_penalty=1.03, server_kwargs={ "headers": { "Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json", } }, ) from langchain_community.llms import HuggingFaceEndpoint ENDPOINT_URL = "<YOUR_ENDPOINT_URL_HERE>" llm = HuggingFaceEndpoint( endpoint_url=ENDPOINT_URL, task="text-generation", model_kwargs={ "max_new_tokens": 512, "top_k": 50, "temperature": 0.1, "repetition_penalty": 1.03, }, ) from langchain_community.llms import HuggingFaceHub llm = HuggingFaceHub( repo_id="HuggingFaceH4/zephyr-7b-beta", task="text-generation", model_kwargs={ "max_new_tokens": 512, "top_k": 30, "temperature": 0.1, "repetition_penalty": 1.03, }, ) from langchain.schema import ( HumanMessage, SystemMessage, ) from langchain_community.chat_models.huggingface import ChatHuggingFace messages = [
SystemMessage(content="You're a helpful assistant")
langchain.schema.SystemMessage
from langchain.agents import AgentExecutor, Tool, ZeroShotAgent from langchain.chains import LLMChain from langchain.memory import ConversationBufferMemory, ReadOnlySharedMemory from langchain.prompts import PromptTemplate from langchain_community.utilities import GoogleSearchAPIWrapper from langchain_openai import OpenAI 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") readonlymemory =
ReadOnlySharedMemory(memory=memory)
langchain.memory.ReadOnlySharedMemory
from getpass import getpass from langchain_community.document_loaders.larksuite import LarkSuiteDocLoader DOMAIN = input("larksuite domain") ACCESS_TOKEN = getpass("larksuite tenant_access_token or user_access_token") DOCUMENT_ID = input("larksuite document id") from pprint import pprint larksuite_loader = LarkSuiteDocLoader(DOMAIN, ACCESS_TOKEN, DOCUMENT_ID) docs = larksuite_loader.load() pprint(docs) from langchain.chains.summarize import load_summarize_chain from langchain_community.llms.fake import FakeListLLM llm = FakeListLLM() chain =
load_summarize_chain(llm, chain_type="map_reduce")
langchain.chains.summarize.load_summarize_chain
import asyncio from typing import Any, Dict, List from langchain.callbacks.base import AsyncCallbackHandler, BaseCallbackHandler from langchain_core.messages import HumanMessage, LLMResult from langchain_openai import ChatOpenAI class MyCustomSyncHandler(BaseCallbackHandler): def on_llm_new_token(self, token: str, **kwargs) -> None: print(f"Sync handler being called in a `thread_pool_executor`: token: {token}") class MyCustomAsyncHandler(AsyncCallbackHandler): """Async callback handler that can be used to handle callbacks from langchain.""" async def on_llm_start( self, serialized: Dict[str, Any], prompts: List[str], **kwargs: Any ) -> None: """Run when chain starts running.""" print("zzzz....") await asyncio.sleep(0.3) class_name = serialized["name"] print("Hi! I just woke up. Your llm is starting") async def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None: """Run when chain ends running.""" print("zzzz....") await asyncio.sleep(0.3) print("Hi! I just woke up. Your llm is ending") chat = ChatOpenAI( max_tokens=25, streaming=True, callbacks=[MyCustomSyncHandler(), MyCustomAsyncHandler()], ) await chat.agenerate([[
HumanMessage(content="Tell me a joke")
langchain_core.messages.HumanMessage
get_ipython().run_line_magic('pip', 'install --upgrade --quiet xata langchain-openai tiktoken langchain') import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") api_key = getpass.getpass("Xata API key: ") db_url = input("Xata database URL (copy it from your DB settings):") from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores.xata import XataVectorStore 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()
langchain_openai.OpenAIEmbeddings
get_ipython().run_line_magic('pip', 'install --upgrade --quiet pymilvus') 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 Milvus 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() vector_db = Milvus.from_documents( docs, embeddings, connection_args={"host": "127.0.0.1", "port": "19530"}, ) query = "What did the president say about Ketanji Brown Jackson" docs = vector_db.similarity_search(query) docs[0].page_content vector_db = Milvus.from_documents( docs, embeddings, collection_name="collection_1", connection_args={"host": "127.0.0.1", "port": "19530"}, ) vector_db = Milvus( embeddings, connection_args={"host": "127.0.0.1", "port": "19530"}, collection_name="collection_1", ) from langchain_core.documents import Document docs = [ Document(page_content="i worked at kensho", metadata={"namespace": "harrison"}), Document(page_content="i worked at facebook", metadata={"namespace": "ankush"}), ] vectorstore = Milvus.from_documents( docs, embeddings, connection_args={"host": "127.0.0.1", "port": "19530"}, drop_old=True, partition_key_field="namespace", # Use the "namespace" field as the partition key ) vectorstore.as_retriever( search_kwargs={"expr": 'namespace == "ankush"'} ).get_relevant_documents("where did i work?") vectorstore.as_retriever( search_kwargs={"expr": 'namespace == "harrison"'} ).get_relevant_documents("where did i work?") from langchain.docstore.document import Document docs = [ Document(page_content="foo", metadata={"id": 1}), Document(page_content="bar", metadata={"id": 2}), Document(page_content="baz", metadata={"id": 3}), ] vector_db = Milvus.from_documents( docs, embeddings, connection_args={"host": "127.0.0.1", "port": "19530"}, ) expr = "id in [1,2]" pks = vector_db.get_pks(expr) result = vector_db.delete(pks) new_docs = [
Document(page_content="new_foo", metadata={"id": 1})
langchain.docstore.document.Document
from langchain.agents import Tool from langchain.chains import RetrievalQA from langchain_community.document_loaders import PyPDFLoader from langchain_community.vectorstores import FAISS from langchain_openai import ChatOpenAI, OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter from pydantic import BaseModel, Field class DocumentInput(BaseModel): question: str = Field() llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613") tools = [] files = [ { "name": "alphabet-earnings", "path": "/Users/harrisonchase/Downloads/2023Q1_alphabet_earnings_release.pdf", }, { "name": "tesla-earnings", "path": "/Users/harrisonchase/Downloads/TSLA-Q1-2023-Update.pdf", }, ] for file in files: loader =
PyPDFLoader(file["path"])
langchain_community.document_loaders.PyPDFLoader
import os os.environ["LANGCHAIN_WANDB_TRACING"] = "true" os.environ["WANDB_PROJECT"] = "langchain-tracing" from langchain.agents import AgentType, initialize_agent, load_tools from langchain.callbacks import wandb_tracing_enabled from langchain_openai import OpenAI llm = OpenAI(temperature=0) tools =
load_tools(["llm-math"], llm=llm)
langchain.agents.load_tools
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-core databricks-vectorsearch langchain-openai tiktoken') import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") from langchain_community.document_loaders import TextLoader 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)
langchain_text_splitters.CharacterTextSplitter
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain langchain-openai') import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass() from langchain_core.tools import tool @tool def complex_tool(int_arg: int, float_arg: float, dict_arg: dict) -> int: """Do something complex with a complex tool.""" return int_arg * float_arg from langchain_openai import ChatOpenAI model = ChatOpenAI(model="gpt-3.5-turbo", temperature=0) model_with_tools = model.bind_tools( [complex_tool], tool_choice="complex_tool", ) from operator import itemgetter from langchain.output_parsers import JsonOutputKeyToolsParser from langchain_core.runnables import Runnable, RunnableLambda, RunnablePassthrough chain = ( model_with_tools | JsonOutputKeyToolsParser(key_name="complex_tool", return_single=True) | complex_tool ) chain.invoke( "use complex tool. the args are 5, 2.1, empty dictionary. don't forget dict_arg" ) from typing import Any from langchain_core.runnables import RunnableConfig def try_except_tool(tool_args: dict, config: RunnableConfig) -> Runnable: try: complex_tool.invoke(tool_args, config=config) except Exception as e: return f"Calling tool with arguments:\n\n{tool_args}\n\nraised the following error:\n\n{type(e)}: {e}" chain = ( model_with_tools | JsonOutputKeyToolsParser(key_name="complex_tool", return_single=True) | try_except_tool ) print( chain.invoke( "use complex tool. the args are 5, 2.1, empty dictionary. don't forget dict_arg" ) ) chain = ( model_with_tools | JsonOutputKeyToolsParser(key_name="complex_tool", return_single=True) | complex_tool ) better_model = ChatOpenAI(model="gpt-4-1106-preview", temperature=0).bind_tools( [complex_tool], tool_choice="complex_tool" ) better_chain = ( better_model | JsonOutputKeyToolsParser(key_name="complex_tool", return_single=True) | complex_tool ) chain_with_fallback = chain.with_fallbacks([better_chain]) chain_with_fallback.invoke( "use complex tool. the args are 5, 2.1, empty dictionary. don't forget dict_arg" ) import json from typing import Any from langchain_core.messages import AIMessage, HumanMessage, ToolMessage from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain_core.runnables import RunnablePassthrough class CustomToolException(Exception): """Custom LangChain tool exception.""" def __init__(self, tool_call: dict, exception: Exception) -> None: super().__init__() self.tool_call = tool_call self.exception = exception def tool_custom_exception(tool_call: dict, config: RunnableConfig) -> Runnable: try: return complex_tool.invoke(tool_call["args"], config=config) except Exception as e: raise CustomToolException(tool_call, e) def exception_to_messages(inputs: dict) -> dict: exception = inputs.pop("exception") tool_call = { "type": "function", "function": { "name": "complex_tool", "arguments": json.dumps(exception.tool_call["args"]), }, "id": exception.tool_call["id"], } messages = [ AIMessage(content="", additional_kwargs={"tool_calls": [tool_call]}), ToolMessage(tool_call_id=tool_call["id"], content=str(exception.exception)), HumanMessage( content="The last tool calls raised exceptions. Try calling the tools again with corrected arguments." ), ] inputs["last_output"] = messages return inputs prompt = ChatPromptTemplate.from_messages( [("human", "{input}"),
MessagesPlaceholder("last_output", optional=True)
langchain_core.prompts.MessagesPlaceholder
get_ipython().system('pip install boto3') from langchain_experimental.recommenders import AmazonPersonalize recommender_arn = "<insert_arn>" client = AmazonPersonalize( credentials_profile_name="default", region_name="us-west-2", recommender_arn=recommender_arn, ) client.get_recommendations(user_id="1") from langchain.llms.bedrock import Bedrock from langchain_experimental.recommenders import AmazonPersonalizeChain bedrock_llm = Bedrock(model_id="anthropic.claude-v2", region_name="us-west-2") chain = AmazonPersonalizeChain.from_llm( llm=bedrock_llm, client=client, return_direct=False ) response = chain({"user_id": "1"}) print(response) from langchain.prompts.prompt import PromptTemplate RANDOM_PROMPT_QUERY = """ You are a skilled publicist. Write a high-converting marketing email advertising several movies available in a video-on-demand streaming platform next week, given the movie and user information below. Your email will leverage the power of storytelling and persuasive language. The movies to recommend and their information is contained in the <movie> tag. All movies in the <movie> tag must be recommended. Give a summary of the movies and why the human should watch them. Put the email between <email> tags. <movie> {result} </movie> Assistant: """ RANDOM_PROMPT = PromptTemplate(input_variables=["result"], template=RANDOM_PROMPT_QUERY) chain = AmazonPersonalizeChain.from_llm( llm=bedrock_llm, client=client, return_direct=False, prompt_template=RANDOM_PROMPT ) chain.run({"user_id": "1", "item_id": "234"}) from langchain.chains import LLMChain, SequentialChain RANDOM_PROMPT_QUERY_2 = """ You are a skilled publicist. Write a high-converting marketing email advertising several movies available in a video-on-demand streaming platform next week, given the movie and user information below. Your email will leverage the power of storytelling and persuasive language. You want the email to impress the user, so make it appealing to them. The movies to recommend and their information is contained in the <movie> tag. All movies in the <movie> tag must be recommended. Give a summary of the movies and why the human should watch them. Put the email between <email> tags. <movie> {result} </movie> Assistant: """ RANDOM_PROMPT_2 = PromptTemplate( input_variables=["result"], template=RANDOM_PROMPT_QUERY_2 ) personalize_chain_instance = AmazonPersonalizeChain.from_llm( llm=bedrock_llm, client=client, return_direct=True ) random_chain_instance =
LLMChain(llm=bedrock_llm, prompt=RANDOM_PROMPT_2)
langchain.chains.LLMChain
from langchain_community.document_loaders import UnstructuredURLLoader urls = [ "https://www.understandingwar.org/backgrounder/russian-offensive-campaign-assessment-february-8-2023", "https://www.understandingwar.org/backgrounder/russian-offensive-campaign-assessment-february-9-2023", ] loader =
UnstructuredURLLoader(urls=urls)
langchain_community.document_loaders.UnstructuredURLLoader
from langchain_community.chat_models.llama_edge import LlamaEdgeChatService from langchain_core.messages import HumanMessage, SystemMessage service_url = "https://b008-54-186-154-209.ngrok-free.app" chat = LlamaEdgeChatService(service_url=service_url) system_message = SystemMessage(content="You are an AI assistant") user_message = HumanMessage(content="What is the capital of France?") messages = [system_message, user_message] response = chat(messages) print(f"[Bot] {response.content}") service_url = "https://b008-54-186-154-209.ngrok-free.app" chat =
LlamaEdgeChatService(service_url=service_url, streaming=True)
langchain_community.chat_models.llama_edge.LlamaEdgeChatService
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() query_constructor = prompt | llm | output_parser print(prompt.format(query="dummy question")) query_constructor.invoke( { "query": "What are some sci-fi movies from the 90's directed by Luc Besson about taxi drivers" } ) from langchain.retrievers.self_query.chroma import ChromaTranslator retriever = SelfQueryRetriever( query_constructor=query_constructor, vectorstore=vectorstore, structured_query_translator=
ChromaTranslator()
langchain.retrievers.self_query.chroma.ChromaTranslator
get_ipython().run_line_magic('pip', 'install --upgrade --quiet wikipedia') from langchain.tools import WikipediaQueryRun from langchain_community.utilities import WikipediaAPIWrapper wikipedia = WikipediaQueryRun(api_wrapper=
WikipediaAPIWrapper()
langchain_community.utilities.WikipediaAPIWrapper
from langchain.chains import LLMSummarizationCheckerChain from langchain_openai import OpenAI llm = OpenAI(temperature=0) checker_chain =
LLMSummarizationCheckerChain.from_llm(llm, verbose=True, max_checks=2)
langchain.chains.LLMSummarizationCheckerChain.from_llm
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 johndoe@example.com 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)
langchain.memory.ConversationBufferWindowMemory
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")
langchain_community.utilities.GoogleSerperAPIWrapper
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 -qU langchain langchain-openai langchain-anthropic langchain-community wikipedia') import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass() os.environ["ANTHROPIC_API_KEY"] = getpass.getpass() from langchain_community.retrievers import WikipediaRetriever from langchain_core.prompts import ChatPromptTemplate from langchain_openai import ChatOpenAI llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0) wiki = WikipediaRetriever(top_k_results=6, doc_content_chars_max=2000) prompt = ChatPromptTemplate.from_messages( [ ( "system", "You're a helpful AI assistant. Given a user question and some Wikipedia article snippets, answer the user question. If none of the articles answer the question, just say you don't know.\n\nHere are the Wikipedia articles:{context}", ), ("human", "{question}"), ] ) prompt.pretty_print() from operator import itemgetter from typing import List from langchain_core.documents import Document from langchain_core.output_parsers import StrOutputParser from langchain_core.runnables import ( RunnableLambda, RunnableParallel, RunnablePassthrough, ) def format_docs(docs: List[Document]) -> str: """Convert Documents to a single string.:""" formatted = [ f"Article Title: {doc.metadata['title']}\nArticle Snippet: {doc.page_content}" for doc in docs ] return "\n\n" + "\n\n".join(formatted) format = itemgetter("docs") | RunnableLambda(format_docs) answer = prompt | llm | StrOutputParser() chain = ( RunnableParallel(question=RunnablePassthrough(), docs=wiki) .assign(context=format) .assign(answer=answer) .pick(["answer", "docs"]) ) chain.invoke("How fast are cheetahs?") from langchain_core.pydantic_v1 import BaseModel, Field class cited_answer(BaseModel): """Answer the user question based only on the given sources, and cite the sources used.""" answer: str = Field( ..., description="The answer to the user question, which is based only on the given sources.", ) citations: List[int] = Field( ..., description="The integer IDs of the SPECIFIC sources which justify the answer.", ) llm_with_tool = llm.bind_tools( [cited_answer], tool_choice="cited_answer", ) example_q = """What Brian's height? Source: 1 Information: Suzy is 6'2" Source: 2 Information: Jeremiah is blonde Source: 3 Information: Brian is 3 inches shorted than Suzy""" llm_with_tool.invoke(example_q) from langchain.output_parsers.openai_tools import JsonOutputKeyToolsParser output_parser = JsonOutputKeyToolsParser(key_name="cited_answer", return_single=True) (llm_with_tool | output_parser).invoke(example_q) def format_docs_with_id(docs: List[Document]) -> str: formatted = [ f"Source ID: {i}\nArticle Title: {doc.metadata['title']}\nArticle Snippet: {doc.page_content}" for i, doc in enumerate(docs) ] return "\n\n" + "\n\n".join(formatted) format_1 = itemgetter("docs") | RunnableLambda(format_docs_with_id) answer_1 = prompt | llm_with_tool | output_parser chain_1 = ( RunnableParallel(question=RunnablePassthrough(), docs=wiki) .assign(context=format_1) .assign(cited_answer=answer_1) .pick(["cited_answer", "docs"]) ) chain_1.invoke("How fast are cheetahs?") class Citation(BaseModel): source_id: int = Field( ..., description="The integer ID of a SPECIFIC source which justifies the answer.", ) quote: str = Field( ..., description="The VERBATIM quote from the specified source that justifies the answer.", ) class quoted_answer(BaseModel): """Answer the user question based only on the given sources, and cite the sources used.""" answer: str = Field( ..., description="The answer to the user question, which is based only on the given sources.", ) citations: List[Citation] = Field( ..., description="Citations from the given sources that justify the answer." ) output_parser_2 = JsonOutputKeyToolsParser(key_name="quoted_answer", return_single=True) llm_with_tool_2 = llm.bind_tools( [quoted_answer], tool_choice="quoted_answer", ) format_2 = itemgetter("docs") | RunnableLambda(format_docs_with_id) answer_2 = prompt | llm_with_tool_2 | output_parser_2 chain_2 = ( RunnableParallel(question=RunnablePassthrough(), docs=wiki) .assign(context=format_2) .assign(quoted_answer=answer_2) .pick(["quoted_answer", "docs"]) ) chain_2.invoke("How fast are cheetahs?") from langchain_anthropic import ChatAnthropicMessages anthropic = ChatAnthropicMessages(model_name="claude-instant-1.2") system = """You're a helpful AI assistant. Given a user question and some Wikipedia article snippets, \ answer the user question and provide citations. If none of the articles answer the question, just say you don't know. Remember, you must return both an answer and citations. A citation consists of a VERBATIM quote that \ justifies the answer and the ID of the quote article. Return a citation for every quote across all articles \ that justify the answer. Use the following format for your final output: <cited_answer> <answer></answer> <citations> <citation><source_id></source_id><quote></quote></citation> <citation><source_id></source_id><quote></quote></citation> ... </citations> </cited_answer> Here are the Wikipedia articles:{context}""" prompt_3 = ChatPromptTemplate.from_messages( [("system", system), ("human", "{question}")] ) from langchain_core.output_parsers import XMLOutputParser def format_docs_xml(docs: List[Document]) -> str: formatted = [] for i, doc in enumerate(docs): doc_str = f"""\ <source id=\"{i}\"> <title>{doc.metadata['title']}</title> <article_snippet>{doc.page_content}</article_snippet> </source>""" formatted.append(doc_str) return "\n\n<sources>" + "\n".join(formatted) + "</sources>" format_3 = itemgetter("docs") | RunnableLambda(format_docs_xml) answer_3 = prompt_3 | anthropic | XMLOutputParser() | itemgetter("cited_answer") chain_3 = ( RunnableParallel(question=RunnablePassthrough(), docs=wiki) .assign(context=format_3) .assign(cited_answer=answer_3) .pick(["cited_answer", "docs"]) ) chain_3.invoke("How fast are cheetahs?") from langchain.retrievers.document_compressors import EmbeddingsFilter from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import RecursiveCharacterTextSplitter splitter = RecursiveCharacterTextSplitter( chunk_size=400, chunk_overlap=0, separators=["\n\n", "\n", ".", " "], keep_separator=False, ) compressor = EmbeddingsFilter(embeddings=OpenAIEmbeddings(), k=10) def split_and_filter(input) -> List[Document]: docs = input["docs"] question = input["question"] split_docs = splitter.split_documents(docs) stateful_docs = compressor.compress_documents(split_docs, question) return [stateful_doc for stateful_doc in stateful_docs] retrieve = ( RunnableParallel(question=RunnablePassthrough(), docs=wiki) | split_and_filter ) docs = retrieve.invoke("How fast are cheetahs?") for doc in docs: print(doc.page_content) print("\n\n") chain_4 = ( RunnableParallel(question=
RunnablePassthrough()
langchain_core.runnables.RunnablePassthrough
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain langchain-community langchainhub gpt4all chromadb') from langchain_community.document_loaders import WebBaseLoader from langchain_text_splitters import RecursiveCharacterTextSplitter loader = WebBaseLoader("https://lilianweng.github.io/posts/2023-06-23-agent/") data = loader.load() text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0) all_splits = text_splitter.split_documents(data) from langchain_community.embeddings import GPT4AllEmbeddings from langchain_community.vectorstores import Chroma vectorstore = Chroma.from_documents(documents=all_splits, embedding=
GPT4AllEmbeddings()
langchain_community.embeddings.GPT4AllEmbeddings
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain langchain-openai') from operator import itemgetter from langchain.memory import ConversationBufferMemory from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain_core.runnables import RunnableLambda, RunnablePassthrough from langchain_openai import ChatOpenAI model = ChatOpenAI() prompt = ChatPromptTemplate.from_messages( [ ("system", "You are a helpful chatbot"),
MessagesPlaceholder(variable_name="history")
langchain_core.prompts.MessagesPlaceholder
import os from langchain_openai import OpenAI from lemonai import execute_workflow """ Load all relevant API Keys and Access Tokens into your environment variables """ os.environ["OPENAI_API_KEY"] = "*INSERT OPENAI API KEY HERE*" os.environ["AIRTABLE_ACCESS_TOKEN"] = "*INSERT AIRTABLE TOKEN HERE*" hackernews_username = "*INSERT HACKERNEWS USERNAME HERE*" airtable_base_id = "*INSERT BASE ID HERE*" airtable_table_id = "*INSERT TABLE ID HERE*" """ Define your instruction to be given to your LLM """ prompt = f"""Read information from Hackernews for user {hackernews_username} and then write the results to Airtable (baseId: {airtable_base_id}, tableId: {airtable_table_id}). Only write the fields "username", "karma" and "created_at_i". Please make sure that Airtable does NOT automatically convert the field types. """ """ Use the Lemon AI execute_workflow wrapper to run your Langchain agent in combination with Lemon AI """ model =
OpenAI(temperature=0)
langchain_openai.OpenAI
from langchain.chains import LLMMathChain from langchain_community.utilities import DuckDuckGoSearchAPIWrapper from langchain_core.tools import Tool from langchain_experimental.plan_and_execute import ( PlanAndExecute, load_agent_executor, load_chat_planner, ) from langchain_openai import ChatOpenAI, OpenAI search = DuckDuckGoSearchAPIWrapper() llm =
OpenAI(temperature=0)
langchain_openai.OpenAI
from typing import Optional from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain_experimental.autonomous_agents import BabyAGI from langchain_openai import OpenAI, OpenAIEmbeddings get_ipython().run_line_magic('pip', 'install faiss-cpu > /dev/null') get_ipython().run_line_magic('pip', 'install google-search-results > /dev/null') from langchain.docstore import InMemoryDocstore from langchain_community.vectorstores import FAISS embeddings_model = OpenAIEmbeddings() import faiss embedding_size = 1536 index = faiss.IndexFlatL2(embedding_size) vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) from langchain.agents import AgentExecutor, Tool, ZeroShotAgent from langchain.chains import LLMChain from langchain_community.utilities import SerpAPIWrapper from langchain_openai import OpenAI todo_prompt = PromptTemplate.from_template( "You are a planner who is an expert at coming up with a todo list for a given objective. Come up with a todo list for this objective: {objective}" ) todo_chain = LLMChain(llm=OpenAI(temperature=0), prompt=todo_prompt) search = SerpAPIWrapper() tools = [ Tool( name="Search", func=search.run, description="useful for when you need to answer questions about current events", ), Tool( name="TODO", func=todo_chain.run, description="useful for when you need to come up with todo lists. Input: an objective to create a todo list for. Output: a todo list for that objective. Please be very clear what the objective is!", ), ] prefix = """You are an AI who performs one task based on the following objective: {objective}. Take into account these previously completed tasks: {context}.""" suffix = """Question: {task} {agent_scratchpad}""" prompt = ZeroShotAgent.create_prompt( tools, prefix=prefix, suffix=suffix, input_variables=["objective", "task", "context", "agent_scratchpad"], ) llm = OpenAI(temperature=0) llm_chain =
LLMChain(llm=llm, prompt=prompt)
langchain.chains.LLMChain
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain langchain_openai') import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass("Input your OpenAI API key:") tidb_connection_string_template = "mysql+pymysql://<USER>:<PASSWORD>@<HOST>:4000/<DB>?ssl_ca=/etc/ssl/cert.pem&ssl_verify_cert=true&ssl_verify_identity=true" tidb_password = getpass.getpass("Input your TiDB password:") tidb_connection_string = tidb_connection_string_template.replace( "<PASSWORD>", tidb_password ) from datetime import datetime from langchain_community.chat_message_histories import TiDBChatMessageHistory history = TiDBChatMessageHistory( connection_string=tidb_connection_string, session_id="code_gen", earliest_time=datetime.utcnow(), # Optional to set earliest_time to load messages after this time point. ) history.add_user_message("How's our feature going?") history.add_ai_message( "It's going well. We are working on testing now. It will be released in Feb." ) history.messages from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain_openai import ChatOpenAI prompt = ChatPromptTemplate.from_messages( [ ( "system", "You're an assistant who's good at coding. You're helping a startup build", ),
MessagesPlaceholder(variable_name="history")
langchain_core.prompts.MessagesPlaceholder
get_ipython().run_line_magic('pip', 'install --upgrade --quiet boto3 nltk') get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain_experimental') get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain pydantic') import os import boto3 comprehend_client = boto3.client("comprehend", region_name="us-east-1") from langchain_experimental.comprehend_moderation import AmazonComprehendModerationChain comprehend_moderation = AmazonComprehendModerationChain( client=comprehend_client, verbose=True, # optional ) from langchain.prompts import PromptTemplate from langchain_community.llms.fake import FakeListLLM from langchain_experimental.comprehend_moderation.base_moderation_exceptions import ( ModerationPiiError, ) template = """Question: {question} Answer:""" prompt = PromptTemplate.from_template(template) responses = [ "Final Answer: A credit card number looks like 1289-2321-1123-2387. A fake SSN number looks like 323-22-9980. John Doe's phone number is (999)253-9876.", "Final Answer: This is a really <expletive> way of constructing a birdhouse. This is <expletive> insane to think that any birds would actually create their <expletive> nests here.", ] llm = FakeListLLM(responses=responses) chain = ( prompt | comprehend_moderation | {"input": (lambda x: x["output"]) | llm} | comprehend_moderation ) try: response = chain.invoke( { "question": "A sample SSN number looks like this 123-22-3345. Can you give me some more samples?" } ) except ModerationPiiError as e: print(str(e)) else: print(response["output"]) from langchain_experimental.comprehend_moderation import ( BaseModerationConfig, ModerationPiiConfig, ModerationPromptSafetyConfig, ModerationToxicityConfig, ) pii_config = ModerationPiiConfig(labels=["SSN"], redact=True, mask_character="X") toxicity_config = ModerationToxicityConfig(threshold=0.5) prompt_safety_config = ModerationPromptSafetyConfig(threshold=0.5) moderation_config = BaseModerationConfig( filters=[pii_config, toxicity_config, prompt_safety_config] ) comp_moderation_with_config = AmazonComprehendModerationChain( moderation_config=moderation_config, # specify the configuration client=comprehend_client, # optionally pass the Boto3 Client verbose=True, ) from langchain.prompts import PromptTemplate from langchain_community.llms.fake import FakeListLLM template = """Question: {question} Answer:""" prompt = PromptTemplate.from_template(template) responses = [ "Final Answer: A credit card number looks like 1289-2321-1123-2387. A fake SSN number looks like 323-22-9980. John Doe's phone number is (999)253-9876.", "Final Answer: This is a really <expletive> way of constructing a birdhouse. This is <expletive> insane to think that any birds would actually create their <expletive> nests here.", ] llm = FakeListLLM(responses=responses) chain = ( prompt | comp_moderation_with_config | {"input": (lambda x: x["output"]) | llm} | comp_moderation_with_config ) try: response = chain.invoke( { "question": "A sample SSN number looks like this 123-45-7890. Can you give me some more samples?" } ) except Exception as e: print(str(e)) else: print(response["output"]) from langchain_experimental.comprehend_moderation import BaseModerationCallbackHandler class MyModCallback(BaseModerationCallbackHandler): async def on_after_pii(self, output_beacon, unique_id): import json moderation_type = output_beacon["moderation_type"] chain_id = output_beacon["moderation_chain_id"] with open(f"output-{moderation_type}-{chain_id}.json", "w") as file: data = {"beacon_data": output_beacon, "unique_id": unique_id} json.dump(data, file) """ async def on_after_toxicity(self, output_beacon, unique_id): pass async def on_after_prompt_safety(self, output_beacon, unique_id): pass """ my_callback = MyModCallback() pii_config = ModerationPiiConfig(labels=["SSN"], redact=True, mask_character="X") toxicity_config = ModerationToxicityConfig(threshold=0.5) moderation_config = BaseModerationConfig(filters=[pii_config, toxicity_config]) comp_moderation_with_config = AmazonComprehendModerationChain( moderation_config=moderation_config, # specify the configuration client=comprehend_client, # optionally pass the Boto3 Client unique_id="john.doe@email.com", # A unique ID moderation_callback=my_callback, # BaseModerationCallbackHandler verbose=True, ) from langchain.prompts import PromptTemplate from langchain_community.llms.fake import FakeListLLM template = """Question: {question} Answer:""" prompt = PromptTemplate.from_template(template) responses = [ "Final Answer: A credit card number looks like 1289-2321-1123-2387. A fake SSN number looks like 323-22-9980. John Doe's phone number is (999)253-9876.", "Final Answer: This is a really <expletive> way of constructing a birdhouse. This is <expletive> insane to think that any birds would actually create their <expletive> nests here.", ] llm = FakeListLLM(responses=responses) chain = ( prompt | comp_moderation_with_config | {"input": (lambda x: x["output"]) | llm} | comp_moderation_with_config ) try: response = chain.invoke( { "question": "A sample SSN number looks like this 123-456-7890. Can you give me some more samples?" } ) except Exception as e: print(str(e)) else: print(response["output"]) get_ipython().run_line_magic('pip', 'install --upgrade --quiet huggingface_hub') import os os.environ["HUGGINGFACEHUB_API_TOKEN"] = "<YOUR HF TOKEN HERE>" repo_id = "google/flan-t5-xxl" from langchain.prompts import PromptTemplate from langchain_community.llms import HuggingFaceHub template = """{question}""" prompt = PromptTemplate.from_template(template) llm = HuggingFaceHub( repo_id=repo_id, model_kwargs={"temperature": 0.5, "max_length": 256} ) pii_config = ModerationPiiConfig( labels=["SSN", "CREDIT_DEBIT_NUMBER"], redact=True, mask_character="X" ) toxicity_config = ModerationToxicityConfig(threshold=0.5) prompt_safety_config = ModerationPromptSafetyConfig(threshold=0.8) moderation_config_1 = BaseModerationConfig( filters=[pii_config, toxicity_config, prompt_safety_config] ) moderation_config_2 = BaseModerationConfig(filters=[pii_config]) amazon_comp_moderation = AmazonComprehendModerationChain( moderation_config=moderation_config_1, client=comprehend_client, moderation_callback=my_callback, verbose=True, ) amazon_comp_moderation_out = AmazonComprehendModerationChain( moderation_config=moderation_config_2, client=comprehend_client, verbose=True ) chain = ( prompt | amazon_comp_moderation | {"input": (lambda x: x["output"]) | llm} | amazon_comp_moderation_out ) try: response = chain.invoke( { "question": """What is John Doe's address, phone number and SSN from the following text? John Doe, a resident of 1234 Elm Street in Springfield, recently celebrated his birthday on January 1st. Turning 43 this year, John reflected on the years gone by. He often shares memories of his younger days with his close friends through calls on his phone, (555) 123-4567. Meanwhile, during a casual evening, he received an email at johndoe@example.com reminding him of an old acquaintance's reunion. As he navigated through some old documents, he stumbled upon a paper that listed his SSN as 123-45-6789, reminding him to store it in a safer place. """ } ) except Exception as e: print(str(e)) else: print(response["output"]) endpoint_name = "<SAGEMAKER_ENDPOINT_NAME>" # replace with your SageMaker Endpoint name region = "<REGION>" # replace with your SageMaker Endpoint region import json from langchain.prompts import PromptTemplate from langchain_community.llms import SagemakerEndpoint from langchain_community.llms.sagemaker_endpoint import LLMContentHandler class ContentHandler(LLMContentHandler): content_type = "application/json" accepts = "application/json" def transform_input(self, prompt: str, model_kwargs: dict) -> bytes: input_str = json.dumps({"text_inputs": prompt, **model_kwargs}) return input_str.encode("utf-8") def transform_output(self, output: bytes) -> str: response_json = json.loads(output.read().decode("utf-8")) return response_json["generated_texts"][0] content_handler = ContentHandler() template = """From the following 'Document', precisely answer the 'Question'. Do not add any spurious information in your answer. Document: John Doe, a resident of 1234 Elm Street in Springfield, recently celebrated his birthday on January 1st. Turning 43 this year, John reflected on the years gone by. He often shares memories of his younger days with his close friends through calls on his phone, (555) 123-4567. Meanwhile, during a casual evening, he received an email at johndoe@example.com reminding him of an old acquaintance's reunion. As he navigated through some old documents, he stumbled upon a paper that listed his SSN as 123-45-6789, reminding him to store it in a safer place. Question: {question} Answer: """ llm_prompt = PromptTemplate.from_template(template) llm = SagemakerEndpoint( endpoint_name=endpoint_name, region_name=region, model_kwargs={ "temperature": 0.95, "max_length": 200, "num_return_sequences": 3, "top_k": 50, "top_p": 0.95, "do_sample": True, }, content_handler=content_handler, ) pii_config = ModerationPiiConfig(labels=["SSN"], redact=True, mask_character="X") toxicity_config =
ModerationToxicityConfig(threshold=0.5)
langchain_experimental.comprehend_moderation.ModerationToxicityConfig
get_ipython().run_line_magic('pip', 'install --upgrade --quiet sagemaker') get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-openai') get_ipython().run_line_magic('pip', 'install --upgrade --quiet google-search-results') import os os.environ["OPENAI_API_KEY"] = "<ADD-KEY-HERE>" os.environ["SERPAPI_API_KEY"] = "<ADD-KEY-HERE>" from langchain.agents import initialize_agent, load_tools from langchain.callbacks import SageMakerCallbackHandler from langchain.chains import LLMChain, SimpleSequentialChain from langchain.prompts import PromptTemplate from langchain_openai import OpenAI from sagemaker.analytics import ExperimentAnalytics from sagemaker.experiments.run import Run from sagemaker.session import Session HPARAMS = { "temperature": 0.1, "model_name": "gpt-3.5-turbo-instruct", } BUCKET_NAME = None EXPERIMENT_NAME = "langchain-sagemaker-tracker" session = Session(default_bucket=BUCKET_NAME) RUN_NAME = "run-scenario-1" PROMPT_TEMPLATE = "tell me a joke about {topic}" INPUT_VARIABLES = {"topic": "fish"} with Run( experiment_name=EXPERIMENT_NAME, run_name=RUN_NAME, sagemaker_session=session ) as run: sagemaker_callback = SageMakerCallbackHandler(run) llm = OpenAI(callbacks=[sagemaker_callback], **HPARAMS) prompt = PromptTemplate.from_template(template=PROMPT_TEMPLATE) chain = LLMChain(llm=llm, prompt=prompt, callbacks=[sagemaker_callback]) chain.run(**INPUT_VARIABLES) sagemaker_callback.flush_tracker() RUN_NAME = "run-scenario-2" PROMPT_TEMPLATE_1 = """You are a playwright. Given the title of play, it is your job to write a synopsis for that title. Title: {title} Playwright: This is a synopsis for the above play:""" PROMPT_TEMPLATE_2 = """You are a play critic from the New York Times. Given the synopsis of play, it is your job to write a review for that play. Play Synopsis: {synopsis} Review from a New York Times play critic of the above play:""" INPUT_VARIABLES = { "input": "documentary about good video games that push the boundary of game design" } with Run( experiment_name=EXPERIMENT_NAME, run_name=RUN_NAME, sagemaker_session=session ) as run: sagemaker_callback =
SageMakerCallbackHandler(run)
langchain.callbacks.SageMakerCallbackHandler
from langchain_experimental.llm_bash.base import LLMBashChain from langchain_openai import OpenAI llm = OpenAI(temperature=0) text = "Please write a bash script that prints 'Hello World' to the console." bash_chain = LLMBashChain.from_llm(llm, verbose=True) bash_chain.run(text) from langchain.prompts.prompt import PromptTemplate from langchain_experimental.llm_bash.prompt import BashOutputParser _PROMPT_TEMPLATE = """If someone asks you to perform a task, your job is to come up with a series of bash commands that will perform the task. There is no need to put "#!/bin/bash" in your answer. Make sure to reason step by step, using this format: Question: "copy the files in the directory named 'target' into a new directory at the same level as target called 'myNewDirectory'" I need to take the following actions: - List all files in the directory - Create a new directory - Copy the files from the first directory into the second directory ```bash ls mkdir myNewDirectory cp -r target/* myNewDirectory ``` Do not use 'echo' when writing the script. That is the format. Begin! Question: {question}""" PROMPT = PromptTemplate( input_variables=["question"], template=_PROMPT_TEMPLATE, output_parser=BashOutputParser(), ) bash_chain = LLMBashChain.from_llm(llm, prompt=PROMPT, verbose=True) text = "Please write a bash script that prints 'Hello World' to the console." bash_chain.run(text) from langchain_experimental.llm_bash.bash import BashProcess persistent_process =
BashProcess(persistent=True)
langchain_experimental.llm_bash.bash.BashProcess
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().system(' pip install langchain unstructured[all-docs] pydantic lxml langchainhub') get_ipython().system(' brew install tesseract') get_ipython().system(' brew install poppler') path = "/Users/rlm/Desktop/Papers/LLaMA2/" from typing import Any from pydantic import BaseModel from unstructured.partition.pdf import partition_pdf raw_pdf_elements = partition_pdf( filename=path + "LLaMA2.pdf", extract_images_in_pdf=False, 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, ) category_counts = {} for element in raw_pdf_elements: category = str(type(element)) if category in category_counts: category_counts[category] += 1 else: category_counts[category] = 1 unique_categories = set(category_counts.keys()) category_counts class Element(BaseModel): type: str text: Any categorized_elements = [] for element in raw_pdf_elements: if "unstructured.documents.elements.Table" in str(type(element)): categorized_elements.append(Element(type="table", text=str(element))) elif "unstructured.documents.elements.CompositeElement" in str(type(element)): categorized_elements.append(Element(type="text", text=str(element))) table_elements = [e for e in categorized_elements if e.type == "table"] print(len(table_elements)) text_elements = [e for e in categorized_elements if e.type == "text"] print(len(text_elements)) 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. \ Give a concise summary of the table or text. Table or text chunk: {element} """ prompt = ChatPromptTemplate.from_template(prompt_text) model = ChatOpenAI(temperature=0, model="gpt-4") summarize_chain = {"element": lambda x: x} | prompt | model | StrOutputParser() tables = [i.text for i in table_elements] table_summaries = summarize_chain.batch(tables, {"max_concurrency": 5}) texts = [i.text for i in text_elements] text_summaries = summarize_chain.batch(texts, {"max_concurrency": 5}) import uuid from langchain.retrievers.multi_vector import MultiVectorRetriever from langchain.storage import InMemoryStore from langchain_community.vectorstores import Chroma from langchain_core.documents import Document from langchain_openai import OpenAIEmbeddings vectorstore = Chroma(collection_name="summaries", embedding_function=OpenAIEmbeddings()) store =
InMemoryStore()
langchain.storage.InMemoryStore
get_ipython().run_line_magic('pip', 'install -qU langchain langchain-openai langchain-anthropic langchain-community wikipedia') import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass() os.environ["ANTHROPIC_API_KEY"] = getpass.getpass() from langchain_community.retrievers import WikipediaRetriever from langchain_core.prompts import ChatPromptTemplate from langchain_openai import ChatOpenAI llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0) wiki =
WikipediaRetriever(top_k_results=6, doc_content_chars_max=2000)
langchain_community.retrievers.WikipediaRetriever
from langchain_community.llms import Baseten mistral =
Baseten(model="MODEL_ID", deployment="production")
langchain_community.llms.Baseten
from langchain_community.utilities import DuckDuckGoSearchAPIWrapper from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate from langchain_core.runnables import RunnablePassthrough from langchain_openai import ChatOpenAI template = """Answer the users question based only on the following context: <context> {context} </context> Question: {question} """ prompt = ChatPromptTemplate.from_template(template) model = ChatOpenAI(temperature=0) search = DuckDuckGoSearchAPIWrapper() def retriever(query): return search.run(query) chain = ( {"context": retriever, "question": RunnablePassthrough()} | prompt | model | StrOutputParser() ) simple_query = "what is langchain?" chain.invoke(simple_query) distracted_query = "man that sam bankman fried trial was crazy! what is langchain?" chain.invoke(distracted_query) retriever(distracted_query) template = """Provide a better search query for \ web search engine to answer the given question, end \ the queries with ’**’. Question: \ {x} Answer:""" rewrite_prompt = ChatPromptTemplate.from_template(template) from langchain import hub rewrite_prompt = hub.pull("langchain-ai/rewrite") print(rewrite_prompt.template) def _parse(text): return text.strip("**") rewriter = rewrite_prompt | ChatOpenAI(temperature=0) | StrOutputParser() | _parse rewriter.invoke({"x": distracted_query}) rewrite_retrieve_read_chain = ( { "context": {"x": RunnablePassthrough()} | rewriter | retriever, "question": RunnablePassthrough(), } | prompt | model |
StrOutputParser()
langchain_core.output_parsers.StrOutputParser
get_ipython().run_line_magic('pip', 'install --upgrade --quiet comet_ml langchain langchain-openai google-search-results spacy textstat pandas') get_ipython().system('{sys.executable} -m spacy download en_core_web_sm') import comet_ml comet_ml.init(project_name="comet-example-langchain") import os os.environ["OPENAI_API_KEY"] = "..." os.environ["SERPAPI_API_KEY"] = "..." from langchain.callbacks import CometCallbackHandler, StdOutCallbackHandler from langchain_openai import OpenAI comet_callback = CometCallbackHandler( project_name="comet-example-langchain", complexity_metrics=True, stream_logs=True, tags=["llm"], visualizations=["dep"], ) callbacks = [StdOutCallbackHandler(), comet_callback] llm = OpenAI(temperature=0.9, callbacks=callbacks, verbose=True) llm_result = llm.generate(["Tell me a joke", "Tell me a poem", "Tell me a fact"] * 3) print("LLM result", llm_result) comet_callback.flush_tracker(llm, finish=True) from langchain.callbacks import CometCallbackHandler, StdOutCallbackHandler from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain_openai import OpenAI comet_callback = CometCallbackHandler( complexity_metrics=True, project_name="comet-example-langchain", stream_logs=True, tags=["synopsis-chain"], ) callbacks = [StdOutCallbackHandler(), comet_callback] llm = OpenAI(temperature=0.9, callbacks=callbacks) template = """You are a playwright. Given the title of play, it is your job to write a synopsis for that title. Title: {title} Playwright: This is a synopsis for the above play:""" prompt_template = PromptTemplate(input_variables=["title"], template=template) synopsis_chain = LLMChain(llm=llm, prompt=prompt_template, callbacks=callbacks) test_prompts = [{"title": "Documentary about Bigfoot in Paris"}] print(synopsis_chain.apply(test_prompts)) comet_callback.flush_tracker(synopsis_chain, finish=True) from langchain.agents import initialize_agent, load_tools from langchain.callbacks import CometCallbackHandler, StdOutCallbackHandler from langchain_openai import OpenAI comet_callback = CometCallbackHandler( project_name="comet-example-langchain", complexity_metrics=True, stream_logs=True, tags=["agent"], ) callbacks = [StdOutCallbackHandler(), comet_callback] llm = OpenAI(temperature=0.9, callbacks=callbacks) tools = load_tools(["serpapi", "llm-math"], llm=llm, callbacks=callbacks) agent = initialize_agent( tools, llm, agent="zero-shot-react-description", callbacks=callbacks, verbose=True, ) agent.run( "Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?" ) comet_callback.flush_tracker(agent, finish=True) get_ipython().run_line_magic('pip', 'install --upgrade --quiet rouge-score') from langchain.callbacks import CometCallbackHandler, StdOutCallbackHandler from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain_openai import OpenAI from rouge_score import rouge_scorer class Rouge: def __init__(self, reference): self.reference = reference self.scorer = rouge_scorer.RougeScorer(["rougeLsum"], use_stemmer=True) def compute_metric(self, generation, prompt_idx, gen_idx): prediction = generation.text results = self.scorer.score(target=self.reference, prediction=prediction) return { "rougeLsum_score": results["rougeLsum"].fmeasure, "reference": self.reference, } reference = """ The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building. It was the first structure to reach a height of 300 metres. It is now taller than the Chrysler Building in New York City by 5.2 metres (17 ft) Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France . """ rouge_score = Rouge(reference=reference) template = """Given the following article, it is your job to write a summary. Article: {article} Summary: This is the summary for the above article:""" prompt_template =
PromptTemplate(input_variables=["article"], template=template)
langchain.prompts.PromptTemplate
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain langchain-openai argilla') import os os.environ["ARGILLA_API_URL"] = "..." os.environ["ARGILLA_API_KEY"] = "..." os.environ["OPENAI_API_KEY"] = "..." import argilla as rg from packaging.version import parse as parse_version if parse_version(rg.__version__) < parse_version("1.8.0"): raise RuntimeError( "`FeedbackDataset` is only available in Argilla v1.8.0 or higher, please " "upgrade `argilla` as `pip install argilla --upgrade`." ) dataset = rg.FeedbackDataset( fields=[ rg.TextField(name="prompt"), rg.TextField(name="response"), ], questions=[ rg.RatingQuestion( name="response-rating", description="How would you rate the quality of the response?", values=[1, 2, 3, 4, 5], required=True, ), rg.TextQuestion( name="response-feedback", description="What feedback do you have for the response?", required=False, ), ], guidelines="You're asked to rate the quality of the response and provide feedback.", ) rg.init( api_url=os.environ["ARGILLA_API_URL"], api_key=os.environ["ARGILLA_API_KEY"], ) dataset.push_to_argilla("langchain-dataset") from langchain.callbacks import ArgillaCallbackHandler argilla_callback = ArgillaCallbackHandler( dataset_name="langchain-dataset", api_url=os.environ["ARGILLA_API_URL"], api_key=os.environ["ARGILLA_API_KEY"], ) from langchain.callbacks import ArgillaCallbackHandler, StdOutCallbackHandler from langchain_openai import OpenAI argilla_callback = ArgillaCallbackHandler( dataset_name="langchain-dataset", api_url=os.environ["ARGILLA_API_URL"], api_key=os.environ["ARGILLA_API_KEY"], ) callbacks = [StdOutCallbackHandler(), argilla_callback] llm = OpenAI(temperature=0.9, callbacks=callbacks) llm.generate(["Tell me a joke", "Tell me a poem"] * 3) from langchain.callbacks import ArgillaCallbackHandler, StdOutCallbackHandler from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain_openai import OpenAI argilla_callback = ArgillaCallbackHandler( dataset_name="langchain-dataset", api_url=os.environ["ARGILLA_API_URL"], api_key=os.environ["ARGILLA_API_KEY"], ) callbacks = [
StdOutCallbackHandler()
langchain.callbacks.StdOutCallbackHandler
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") documents = loader.load() documents from langchain_community.document_loaders import AmazonTextractPDFLoader loader =
AmazonTextractPDFLoader( "https://amazon-textract-public-content.s3.us-east-2.amazonaws.com/langchain/alejandro_rosalez_sample_1.jpg" )
langchain_community.document_loaders.AmazonTextractPDFLoader
import getpass import os os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY") or getpass.getpass( "OpenAI API Key:" ) from langchain.sql_database import SQLDatabase from langchain_openai import ChatOpenAI CONNECTION_STRING = "postgresql+psycopg2://postgres:test@localhost:5432/vectordb" # Replace with your own db = SQLDatabase.from_uri(CONNECTION_STRING) from langchain_openai import OpenAIEmbeddings embeddings_model = OpenAIEmbeddings() tracks = db.run('SELECT "Name" FROM "Track"') song_titles = [s[0] for s in eval(tracks)] title_embeddings = embeddings_model.embed_documents(song_titles) len(title_embeddings) from tqdm import tqdm for i in tqdm(range(len(title_embeddings))): title = song_titles[i].replace("'", "''") embedding = title_embeddings[i] sql_command = ( f'UPDATE "Track" SET "embeddings" = ARRAY{embedding} WHERE "Name" =' + f"'{title}'" ) db.run(sql_command) embeded_title = embeddings_model.embed_query("hope about the future") query = ( 'SELECT "Track"."Name" FROM "Track" WHERE "Track"."embeddings" IS NOT NULL ORDER BY "embeddings" <-> ' + f"'{embeded_title}' LIMIT 5" ) db.run(query) def get_schema(_): return db.get_table_info() def run_query(query): return db.run(query) from langchain_core.prompts import ChatPromptTemplate template = """You are a Postgres expert. Given an input question, first create a syntactically correct Postgres query to run, then look at the results of the query and return the answer to the input question. Unless the user specifies in the question a specific number of examples to obtain, query for at most 5 results using the LIMIT clause as per Postgres. You can order the results to return the most informative data in the database. Never query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in double quotes (") to denote them as delimited identifiers. Pay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table. Pay attention to use date('now') function to get the current date, if the question involves "today". You can use an extra extension which allows you to run semantic similarity using <-> operator on tables containing columns named "embeddings". <-> operator can ONLY be used on embeddings columns. The embeddings value for a given row typically represents the semantic meaning of that row. The vector represents an embedding representation of the question, given below. Do NOT fill in the vector values directly, but rather specify a `[search_word]` placeholder, which should contain the word that would be embedded for filtering. For example, if the user asks for songs about 'the feeling of loneliness' the query could be: 'SELECT "[whatever_table_name]"."SongName" FROM "[whatever_table_name]" ORDER BY "embeddings" <-> '[loneliness]' LIMIT 5' Use the following format: Question: <Question here> SQLQuery: <SQL Query to run> SQLResult: <Result of the SQLQuery> Answer: <Final answer here> Only use the following tables: {schema} """ prompt = ChatPromptTemplate.from_messages( [("system", template), ("human", "{question}")] ) from langchain_core.output_parsers import StrOutputParser from langchain_core.runnables import RunnablePassthrough from langchain_openai import ChatOpenAI db = SQLDatabase.from_uri( CONNECTION_STRING ) # We reconnect to db so the new columns are loaded as well. llm = ChatOpenAI(model_name="gpt-4", temperature=0) sql_query_chain = (
RunnablePassthrough.assign(schema=get_schema)
langchain_core.runnables.RunnablePassthrough.assign
from langchain.retrievers import ParentDocumentRetriever from langchain.storage import InMemoryStore 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()) child_splitter = RecursiveCharacterTextSplitter(chunk_size=400) vectorstore = Chroma( collection_name="full_documents", embedding_function=OpenAIEmbeddings() ) store = InMemoryStore() retriever = ParentDocumentRetriever( vectorstore=vectorstore, docstore=store, child_splitter=child_splitter, ) retriever.add_documents(docs, ids=None) list(store.yield_keys()) sub_docs = vectorstore.similarity_search("justice breyer") print(sub_docs[0].page_content) retrieved_docs = retriever.get_relevant_documents("justice breyer") len(retrieved_docs[0].page_content) parent_splitter = RecursiveCharacterTextSplitter(chunk_size=2000) child_splitter = RecursiveCharacterTextSplitter(chunk_size=400) vectorstore = Chroma( collection_name="split_parents", embedding_function=
OpenAIEmbeddings()
langchain_openai.OpenAIEmbeddings
get_ipython().run_line_magic('pip', 'install --upgrade --quiet pymongo') import os CONNECTION_STRING = "YOUR_CONNECTION_STRING" INDEX_NAME = "izzy-test-index" NAMESPACE = "izzy_test_db.izzy_test_collection" DB_NAME, COLLECTION_NAME = NAMESPACE.split(".") os.environ["OPENAI_API_TYPE"] = "azure" os.environ["OPENAI_API_VERSION"] = "2023-05-15" os.environ[ "OPENAI_API_BASE" ] = "YOUR_OPEN_AI_ENDPOINT" # https://example.openai.azure.com/ os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY" os.environ[ "OPENAI_EMBEDDINGS_DEPLOYMENT" ] = "smart-agent-embedding-ada" # the deployment name for the embedding model os.environ["OPENAI_EMBEDDINGS_MODEL_NAME"] = "text-embedding-ada-002" # the model name from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores.azure_cosmos_db import ( AzureCosmosDBVectorSearch, CosmosDBSimilarityType, CosmosDBVectorSearchType, ) from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter SOURCE_FILE_NAME = "../../modules/state_of_the_union.txt" loader =
TextLoader(SOURCE_FILE_NAME)
langchain_community.document_loaders.TextLoader
from typing import Optional from langchain_experimental.autonomous_agents import BabyAGI from langchain_openai import OpenAI, OpenAIEmbeddings from langchain.docstore import InMemoryDocstore from langchain_community.vectorstores import FAISS embeddings_model =
OpenAIEmbeddings()
langchain_openai.OpenAIEmbeddings
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-robocorp') from langchain.agents import AgentExecutor, OpenAIFunctionsAgent from langchain_core.messages import SystemMessage from langchain_openai import ChatOpenAI from langchain_robocorp import ActionServerToolkit llm = ChatOpenAI(model="gpt-4", temperature=0) toolkit = ActionServerToolkit(url="http://localhost:8080", report_trace=True) tools = toolkit.get_tools() system_message =
SystemMessage(content="You are a helpful assistant")
langchain_core.messages.SystemMessage
get_ipython().run_line_magic('pip', 'install --upgrade --quiet airbyte-source-stripe') from langchain_community.document_loaders.airbyte import AirbyteStripeLoader config = { } loader = AirbyteStripeLoader( config=config, stream_name="invoices" ) # check the documentation linked above for a list of all streams docs = loader.load() docs_iterator = loader.lazy_load() from langchain.docstore.document import Document def handle_record(record, id): return
Document(page_content=record.data["title"], metadata=record.data)
langchain.docstore.document.Document
from langchain_community.tools.edenai import ( EdenAiExplicitImageTool, EdenAiObjectDetectionTool, EdenAiParsingIDTool, EdenAiParsingInvoiceTool, EdenAiSpeechToTextTool, EdenAiTextModerationTool, EdenAiTextToSpeechTool, ) from langchain.agents import AgentType, initialize_agent from langchain_community.llms import EdenAI llm = EdenAI( feature="text", provider="openai", params={"temperature": 0.2, "max_tokens": 250} ) tools = [ EdenAiTextModerationTool(providers=["openai"], language="en"), EdenAiObjectDetectionTool(providers=["google", "api4ai"]),
EdenAiTextToSpeechTool(providers=["amazon"], language="en", voice="MALE")
langchain_community.tools.edenai.EdenAiTextToSpeechTool
get_ipython().run_line_magic('pip', 'install --upgrade --quiet dingodb') get_ipython().run_line_magic('pip', 'install --upgrade --quiet git+https://git@github.com/dingodb/pydingo.git') 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 Dingo 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()
langchain_openai.OpenAIEmbeddings
get_ipython().run_line_magic('pip', 'install -qU chromadb langchain langchain-community langchain-openai') from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import Chroma from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import RecursiveCharacterTextSplitter loader = TextLoader("../../state_of_the_union.txt") documents = loader.load() text_splitter =
RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
langchain_text_splitters.RecursiveCharacterTextSplitter
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()
langchain_community.utilities.dalle_image_generator.DallEAPIWrapper
from langchain.chains import LLMChain from langchain.memory import ConversationBufferMemory from langchain.prompts import PromptTemplate from langchain_openai import OpenAI template = """You are a chatbot having a conversation with a human. {chat_history} Human: {human_input} Chatbot:""" prompt = PromptTemplate( input_variables=["chat_history", "human_input"], template=template ) memory = ConversationBufferMemory(memory_key="chat_history") llm =
OpenAI()
langchain_openai.OpenAI
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)
langchain_experimental.rl_chain.ToSelectFrom
from typing import Callable, List from langchain.memory import ConversationBufferMemory from langchain.schema import ( AIMessage, HumanMessage, SystemMessage, ) from langchain_openai import ChatOpenAI from langchain.agents import AgentType, initialize_agent, load_tools class DialogueAgent: def __init__( self, name: str, system_message: SystemMessage, model: ChatOpenAI, ) -> None: self.name = name self.system_message = system_message self.model = model self.prefix = f"{self.name}: " self.reset() def reset(self): self.message_history = ["Here is the conversation so far."] def send(self) -> str: """ Applies the chatmodel to the message history and returns the message string """ message = self.model( [ self.system_message, HumanMessage(content="\n".join(self.message_history + [self.prefix])), ] ) return message.content def receive(self, name: str, message: str) -> None: """ Concatenates {message} spoken by {name} into message history """ self.message_history.append(f"{name}: {message}") class DialogueSimulator: def __init__( self, agents: List[DialogueAgent], selection_function: Callable[[int, List[DialogueAgent]], int], ) -> None: self.agents = agents self._step = 0 self.select_next_speaker = selection_function def reset(self): for agent in self.agents: agent.reset() def inject(self, name: str, message: str): """ Initiates the conversation with a {message} from {name} """ for agent in self.agents: agent.receive(name, message) self._step += 1 def step(self) -> tuple[str, str]: speaker_idx = self.select_next_speaker(self._step, self.agents) speaker = self.agents[speaker_idx] message = speaker.send() for receiver in self.agents: receiver.receive(speaker.name, message) self._step += 1 return speaker.name, message class DialogueAgentWithTools(DialogueAgent): def __init__( self, name: str, system_message: SystemMessage, model: ChatOpenAI, tool_names: List[str], **tool_kwargs, ) -> None: super().__init__(name, system_message, model) self.tools = load_tools(tool_names, **tool_kwargs) def send(self) -> str: """ Applies the chatmodel to the message history and returns the message string """ agent_chain = initialize_agent( self.tools, self.model, agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory=ConversationBufferMemory( memory_key="chat_history", return_messages=True ), ) message = AIMessage( content=agent_chain.run( input="\n".join( [self.system_message.content] + self.message_history + [self.prefix] ) ) ) return message.content names = { "AI accelerationist": ["arxiv", "ddg-search", "wikipedia"], "AI alarmist": ["arxiv", "ddg-search", "wikipedia"], } topic = "The current impact of automation and artificial intelligence on employment" word_limit = 50 # word limit for task brainstorming conversation_description = f"""Here is the topic of conversation: {topic} The participants are: {', '.join(names.keys())}""" agent_descriptor_system_message = SystemMessage( content="You can add detail to the description of the conversation participant." ) def generate_agent_description(name): agent_specifier_prompt = [ agent_descriptor_system_message, HumanMessage( content=f"""{conversation_description} Please reply with a creative description of {name}, in {word_limit} words or less. Speak directly to {name}. Give them a point of view. Do not add anything else.""" ), ] agent_description = ChatOpenAI(temperature=1.0)(agent_specifier_prompt).content return agent_description agent_descriptions = {name: generate_agent_description(name) for name in names} for name, description in agent_descriptions.items(): print(description) def generate_system_message(name, description, tools): return f"""{conversation_description} Your name is {name}. Your description is as follows: {description} Your goal is to persuade your conversation partner of your point of view. DO look up information with your tool to refute your partner's claims. DO cite your sources. DO NOT fabricate fake citations. DO NOT cite any source that you did not look up. Do not add anything else. Stop speaking the moment you finish speaking from your perspective. """ agent_system_messages = { name: generate_system_message(name, description, tools) for (name, tools), description in zip(names.items(), agent_descriptions.values()) } for name, system_message in agent_system_messages.items(): print(name) print(system_message) topic_specifier_prompt = [
SystemMessage(content="You can make a topic more specific.")
langchain.schema.SystemMessage
get_ipython().run_line_magic('pip', 'install --upgrade --quiet titan-iris') from langchain_community.llms import TitanTakeoff llm = TitanTakeoff( base_url="http://localhost:8000", generate_max_length=128, temperature=1.0 ) prompt = "What is the largest planet in the solar system?" llm(prompt) from langchain.callbacks.manager import CallbackManager from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler llm = TitanTakeoff( callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]), streaming=True ) prompt = "What is the capital of France?" llm(prompt) from langchain.chains import LLMChain from langchain.prompts import PromptTemplate llm = TitanTakeoff() template = "What is the capital of {country}" prompt =
PromptTemplate.from_template(template)
langchain.prompts.PromptTemplate.from_template
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) llm =
ChatOpenAI()
langchain_openai.ChatOpenAI
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")
langchain_openai.ChatOpenAI
get_ipython().run_line_magic('pip', 'install --upgrade --quiet rank_bm25') from langchain.retrievers import BM25Retriever retriever = BM25Retriever.from_texts(["foo", "bar", "world", "hello", "foo bar"]) from langchain_core.documents import Document retriever = BM25Retriever.from_documents( [ Document(page_content="foo"), Document(page_content="bar"), Document(page_content="world"), Document(page_content="hello"),
Document(page_content="foo bar")
langchain_core.documents.Document
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() for txt in chain.stream({"input": "How do I solve this fizz buzz problem?"}): print(txt, end="") from langchain_nvidia_ai_endpoints import ChatNVIDIA llm = ChatNVIDIA(model="nemotron_steerlm_8b") complex_result = llm.invoke( "What's a PB&J?", labels={"creativity": 0, "complexity": 3, "verbosity": 0} ) print("Un-creative\n") print(complex_result.content) print("\n\nCreative\n") creative_result = llm.invoke( "What's a PB&J?", labels={"creativity": 9, "complexity": 3, "verbosity": 9} ) print(creative_result.content) 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="nemotron_steerlm_8b").bind( labels={"creativity": 9, "complexity": 0, "verbosity": 9} ) |
StrOutputParser()
langchain_core.output_parsers.StrOutputParser
get_ipython().system(' docker run -d -p 8123:8123 -p9000:9000 --name langchain-clickhouse-server --ulimit nofile=262144:262144 clickhouse/clickhouse-server:23.4.2.11') get_ipython().run_line_magic('pip', 'install --upgrade --quiet clickhouse-connect') import getpass import os if not os.environ["OPENAI_API_KEY"]: os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") from langchain_community.vectorstores import Clickhouse, ClickhouseSettings 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"} settings = ClickhouseSettings(table="clickhouse_vector_search_example") docsearch = Clickhouse.from_documents(docs, embeddings, config=settings) query = "What did the president say about Ketanji Brown Jackson" docs = docsearch.similarity_search(query) print(docs[0].page_content) print(str(docsearch)) print(f"Clickhouse Table DDL:\n\n{docsearch.schema}") from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import Clickhouse, ClickhouseSettings loader =
TextLoader("../../modules/state_of_the_union.txt")
langchain_community.document_loaders.TextLoader
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)
langchain.prompts.PromptTemplate.from_template
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()
langchain_openai.OpenAIEmbeddings
from langchain.indexes import SQLRecordManager, index from langchain_core.documents import Document from langchain_elasticsearch import ElasticsearchStore from langchain_openai import OpenAIEmbeddings collection_name = "test_index" embedding = OpenAIEmbeddings() vectorstore = ElasticsearchStore( es_url="http://localhost:9200", index_name="test_index", embedding=embedding ) namespace = f"elasticsearch/{collection_name}" record_manager = SQLRecordManager( namespace, db_url="sqlite:///record_manager_cache.sql" ) record_manager.create_schema() doc1 = Document(page_content="kitty", metadata={"source": "kitty.txt"}) doc2 = Document(page_content="doggy", metadata={"source": "doggy.txt"}) def _clear(): """Hacky helper method to clear content. See the `full` mode section to to understand why it works."""
index([], record_manager, vectorstore, cleanup="full", source_id_key="source")
langchain.indexes.index
get_ipython().run_line_magic('pip', 'install --upgrade --quiet google-api-python-client google-auth-httplib2 google-auth-oauthlib') folder_id = "root" get_ipython().run_line_magic('pip', 'install --upgrade --quiet unstructured') from langchain_community.tools.google_drive.tool import GoogleDriveSearchTool from langchain_community.utilities.google_drive import GoogleDriveAPIWrapper tool = GoogleDriveSearchTool( api_wrapper=GoogleDriveAPIWrapper( folder_id=folder_id, num_results=2, template="gdrive-query-in-folder", # Search in the body of documents ) ) import logging logging.basicConfig(level=logging.INFO) tool.run("machine learning") tool.description from langchain.agents import load_tools tools = load_tools( ["google-drive-search"], folder_id=folder_id, template="gdrive-query-in-folder", ) from langchain.agents import AgentType, initialize_agent from langchain_openai import OpenAI llm =
OpenAI(temperature=0)
langchain_openai.OpenAI
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}"), ("ai", "{output}"), ] ) few_shot_prompt = FewShotChatMessagePromptTemplate( example_prompt=example_prompt, examples=examples, ) prompt = ChatPromptTemplate.from_messages( [ ( "system", """You are an expert at world knowledge. Your task is to step back and paraphrase a question to a more generic step-back question, which is easier to answer. Here are a few examples:""", ), few_shot_prompt, ("user", "{question}"), ] ) question_gen = prompt | ChatOpenAI(temperature=0) | StrOutputParser() question = "was chatgpt around while trump was president?" question_gen.invoke({"question": question}) from langchain_community.utilities import DuckDuckGoSearchAPIWrapper search = DuckDuckGoSearchAPIWrapper(max_results=4) def retriever(query): return search.run(query) retriever(question) retriever(question_gen.invoke({"question": question})) from langchain import hub response_prompt = hub.pull("langchain-ai/stepback-answer") chain = ( { "normal_context": RunnableLambda(lambda x: x["question"]) | retriever, "step_back_context": question_gen | retriever, "question": lambda x: x["question"], } | response_prompt | ChatOpenAI(temperature=0) | StrOutputParser() ) chain.invoke({"question": question}) response_prompt_template = """You are an expert of world knowledge. I am going to ask you a question. Your response should be comprehensive and not contradicted with the following context if they are relevant. Otherwise, ignore them if they are not relevant. {normal_context} Original Question: {question} Answer:""" response_prompt = ChatPromptTemplate.from_template(response_prompt_template) chain = ( { "normal_context": RunnableLambda(lambda x: x["question"]) | retriever, "question": lambda x: x["question"], } | response_prompt |
ChatOpenAI(temperature=0)
langchain_openai.ChatOpenAI
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)
langchain.chains.LLMChain
get_ipython().run_line_magic('load_ext', 'autoreload') get_ipython().run_line_magic('autoreload', '2') get_ipython().system('poetry run pip install replicate') from getpass import getpass REPLICATE_API_TOKEN = getpass() import os os.environ["REPLICATE_API_TOKEN"] = REPLICATE_API_TOKEN from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain_community.llms import Replicate llm = Replicate( model="a16z-infra/llama13b-v2-chat:df7690f1994d94e96ad9d568eac121aecf50684a0b0963b25a41cc40061269e5", model_kwargs={"temperature": 0.75, "max_length": 500, "top_p": 1}, ) prompt = """ User: Answer the following yes/no question by reasoning step by step. Can a dog drive a car? Assistant: """ llm(prompt) llm = Replicate( model="replicate/dolly-v2-12b:ef0e1aefc61f8e096ebe4db6b2bacc297daf2ef6899f0f7e001ec445893500e5" ) prompt = """ Answer the following yes/no question by reasoning step by step. Can a dog drive a car? """ llm(prompt) text2image = Replicate( model="stability-ai/stable-diffusion:db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf", model_kwargs={"image_dimensions": "512x512"}, ) image_output = text2image("A cat riding a motorcycle by Picasso") image_output get_ipython().system('poetry run pip install Pillow') from io import BytesIO import requests from PIL import Image response = requests.get(image_output) img = Image.open(BytesIO(response.content)) img from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler llm = Replicate( streaming=True, callbacks=[StreamingStdOutCallbackHandler()], model="a16z-infra/llama13b-v2-chat:df7690f1994d94e96ad9d568eac121aecf50684a0b0963b25a41cc40061269e5", model_kwargs={"temperature": 0.75, "max_length": 500, "top_p": 1}, ) prompt = """ User: Answer the following yes/no question by reasoning step by step. Can a dog drive a car? Assistant: """ _ = llm(prompt) import time llm = Replicate( model="a16z-infra/llama13b-v2-chat:df7690f1994d94e96ad9d568eac121aecf50684a0b0963b25a41cc40061269e5", model_kwargs={"temperature": 0.01, "max_length": 500, "top_p": 1}, ) prompt = """ User: What is the best way to learn python? Assistant: """ start_time = time.perf_counter() raw_output = llm(prompt) # raw output, no stop end_time = time.perf_counter() print(f"Raw output:\n {raw_output}") print(f"Raw output runtime: {end_time - start_time} seconds") start_time = time.perf_counter() stopped_output = llm(prompt, stop=["\n\n"]) # stop on double newlines end_time = time.perf_counter() print(f"Stopped output:\n {stopped_output}") print(f"Stopped output runtime: {end_time - start_time} seconds") from langchain.chains import SimpleSequentialChain dolly_llm = Replicate( model="replicate/dolly-v2-12b:ef0e1aefc61f8e096ebe4db6b2bacc297daf2ef6899f0f7e001ec445893500e5" ) text2image = Replicate( model="stability-ai/stable-diffusion:db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf" ) prompt = PromptTemplate( input_variables=["product"], template="What is a good name for a company that makes {product}?", ) chain = LLMChain(llm=dolly_llm, prompt=prompt) second_prompt = PromptTemplate( input_variables=["company_name"], template="Write a description of a logo for this company: {company_name}", ) chain_two = LLMChain(llm=dolly_llm, prompt=second_prompt) third_prompt = PromptTemplate( input_variables=["company_logo_description"], template="{company_logo_description}", ) chain_three =
LLMChain(llm=text2image, prompt=third_prompt)
langchain.chains.LLMChain
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 langchain langchain-openai') import os import uuid uid = uuid.uuid4().hex[:6] project_name = f"Run Fine-tuning Walkthrough {uid}" os.environ["LANGCHAIN_TRACING_V2"] = "true" os.environ["LANGCHAIN_API_KEY"] = "YOUR API KEY" os.environ["LANGCHAIN_PROJECT"] = project_name from enum import Enum from langchain_core.pydantic_v1 import BaseModel, Field class Operation(Enum): add = "+" subtract = "-" multiply = "*" divide = "/" class Calculator(BaseModel): """A calculator function""" num1: float num2: float operation: Operation = Field(..., description="+,-,*,/") def calculate(self): if self.operation == Operation.add: return self.num1 + self.num2 elif self.operation == Operation.subtract: return self.num1 - self.num2 elif self.operation == Operation.multiply: return self.num1 * self.num2 elif self.operation == Operation.divide: if self.num2 != 0: return self.num1 / self.num2 else: return "Cannot divide by zero" from pprint import pprint from langchain.utils.openai_functions import convert_pydantic_to_openai_function from langchain_core.pydantic_v1 import BaseModel openai_function_def = convert_pydantic_to_openai_function(Calculator) pprint(openai_function_def) from langchain.output_parsers.openai_functions import PydanticOutputFunctionsParser from langchain_core.prompts import ChatPromptTemplate from langchain_openai import ChatOpenAI prompt = ChatPromptTemplate.from_messages( [ ("system", "You are an accounting assistant."), ("user", "{input}"), ] ) chain = ( prompt | ChatOpenAI().bind(functions=[openai_function_def]) |
PydanticOutputFunctionsParser(pydantic_schema=Calculator)
langchain.output_parsers.openai_functions.PydanticOutputFunctionsParser
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain label-studio label-studio-sdk langchain-openai') import os os.environ["LABEL_STUDIO_URL"] = "<YOUR-LABEL-STUDIO-URL>" # e.g. http://localhost:8080 os.environ["LABEL_STUDIO_API_KEY"] = "<YOUR-LABEL-STUDIO-API-KEY>" os.environ["OPENAI_API_KEY"] = "<YOUR-OPENAI-API-KEY>" from langchain.callbacks import LabelStudioCallbackHandler from langchain_openai import OpenAI llm = OpenAI( temperature=0, callbacks=[
LabelStudioCallbackHandler(project_name="My Project")
langchain.callbacks.LabelStudioCallbackHandler
get_ipython().run_line_magic('pip', 'install --upgrade --quiet runhouse') import runhouse as rh from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain_community.llms import SelfHostedHuggingFaceLLM, SelfHostedPipeline gpu = rh.cluster(name="rh-a10x", instance_type="A100:1", use_spot=False) template = """Question: {question} Answer: Let's think step by step.""" prompt =
PromptTemplate.from_template(template)
langchain.prompts.PromptTemplate.from_template
from langchain_community.utils.openai_functions import ( convert_pydantic_to_openai_function, ) from langchain_core.prompts import ChatPromptTemplate from langchain_core.pydantic_v1 import BaseModel, Field, validator from langchain_openai import ChatOpenAI class Joke(BaseModel): """Joke to tell user.""" setup: str = Field(description="question to set up a joke") punchline: str = Field(description="answer to resolve the joke") openai_functions = [convert_pydantic_to_openai_function(Joke)] model = ChatOpenAI(temperature=0) prompt = ChatPromptTemplate.from_messages( [("system", "You are helpful assistant"), ("user", "{input}")] ) from langchain.output_parsers.openai_functions import JsonOutputFunctionsParser parser =
JsonOutputFunctionsParser()
langchain.output_parsers.openai_functions.JsonOutputFunctionsParser
get_ipython().system('pip install --upgrade langchain langchain-google-vertexai') project: str = "PUT_YOUR_PROJECT_ID_HERE" # @param {type:"string"} endpoint_id: str = "PUT_YOUR_ENDPOINT_ID_HERE" # @param {type:"string"} location: str = "PUT_YOUR_ENDPOINT_LOCAtION_HERE" # @param {type:"string"} from langchain_google_vertexai import ( GemmaChatVertexAIModelGarden, GemmaVertexAIModelGarden, ) llm = GemmaVertexAIModelGarden( endpoint_id=endpoint_id, project=project, location=location, ) output = llm.invoke("What is the meaning of life?") print(output) from langchain_core.messages import HumanMessage llm = GemmaChatVertexAIModelGarden( endpoint_id=endpoint_id, project=project, location=location, ) message1 = HumanMessage(content="How much is 2+2?") answer1 = llm.invoke([message1]) print(answer1) message2 = HumanMessage(content="How much is 3+3?") answer2 = llm.invoke([message1, answer1, message2]) print(answer2) answer1 = llm.invoke([message1], parse_response=True) print(answer1) answer2 = llm.invoke([message1, answer1, message2], parse_response=True) print(answer2) get_ipython().system('mkdir -p ~/.kaggle && cp kaggle.json ~/.kaggle/kaggle.json') get_ipython().system('pip install keras>=3 keras_nlp') from langchain_google_vertexai import GemmaLocalKaggle keras_backend: str = "jax" # @param {type:"string"} model_name: str = "gemma_2b_en" # @param {type:"string"} llm = GemmaLocalKaggle(model_name=model_name, keras_backend=keras_backend) output = llm.invoke("What is the meaning of life?", max_tokens=30) print(output) from langchain_google_vertexai import GemmaChatLocalKaggle keras_backend: str = "jax" # @param {type:"string"} model_name: str = "gemma_2b_en" # @param {type:"string"} llm = GemmaChatLocalKaggle(model_name=model_name, keras_backend=keras_backend) from langchain_core.messages import HumanMessage message1 =
HumanMessage(content="Hi! Who are you?")
langchain_core.messages.HumanMessage
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain langsmith langchainhub --quiet') get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-openai tiktoken pandas duckduckgo-search --quiet') import os from uuid import uuid4 unique_id = uuid4().hex[0:8] os.environ["LANGCHAIN_TRACING_V2"] = "true" os.environ["LANGCHAIN_PROJECT"] = f"Tracing Walkthrough - {unique_id}" os.environ["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com" os.environ["LANGCHAIN_API_KEY"] = "<YOUR-API-KEY>" # Update to your API key os.environ["OPENAI_API_KEY"] = "<YOUR-OPENAI-API-KEY>" from langsmith import Client client = Client() from langchain import hub from langchain.agents import AgentExecutor from langchain.agents.format_scratchpad.openai_tools import ( format_to_openai_tool_messages, ) from langchain.agents.output_parsers.openai_tools import OpenAIToolsAgentOutputParser from langchain_community.tools import DuckDuckGoSearchResults from langchain_openai import ChatOpenAI prompt = hub.pull("wfh/langsmith-agent-prompt:5d466cbc") llm = ChatOpenAI( model="gpt-3.5-turbo-16k", temperature=0, ) tools = [ DuckDuckGoSearchResults( name="duck_duck_go" ), # General internet search using DuckDuckGo ] llm_with_tools = llm.bind_tools(tools) runnable_agent = ( { "input": lambda x: x["input"], "agent_scratchpad": lambda x: format_to_openai_tool_messages( x["intermediate_steps"] ), } | prompt | llm_with_tools |
OpenAIToolsAgentOutputParser()
langchain.agents.output_parsers.openai_tools.OpenAIToolsAgentOutputParser
get_ipython().run_line_magic('pip', 'install -qU langchain langchain-openai langchain-anthropic langchain-community wikipedia') import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass() os.environ["ANTHROPIC_API_KEY"] = getpass.getpass() from langchain_community.retrievers import WikipediaRetriever from langchain_core.prompts import ChatPromptTemplate from langchain_openai import ChatOpenAI llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0) wiki = WikipediaRetriever(top_k_results=6, doc_content_chars_max=2000) prompt = ChatPromptTemplate.from_messages( [ ( "system", "You're a helpful AI assistant. Given a user question and some Wikipedia article snippets, answer the user question. If none of the articles answer the question, just say you don't know.\n\nHere are the Wikipedia articles:{context}", ), ("human", "{question}"), ] ) prompt.pretty_print() from operator import itemgetter from typing import List from langchain_core.documents import Document from langchain_core.output_parsers import StrOutputParser from langchain_core.runnables import ( RunnableLambda, RunnableParallel, RunnablePassthrough, ) def format_docs(docs: List[Document]) -> str: """Convert Documents to a single string.:""" formatted = [ f"Article Title: {doc.metadata['title']}\nArticle Snippet: {doc.page_content}" for doc in docs ] return "\n\n" + "\n\n".join(formatted) format = itemgetter("docs") | RunnableLambda(format_docs) answer = prompt | llm | StrOutputParser() chain = ( RunnableParallel(question=
RunnablePassthrough()
langchain_core.runnables.RunnablePassthrough
from langchain.chains import LLMSummarizationCheckerChain from langchain_openai import OpenAI llm = OpenAI(temperature=0) checker_chain = LLMSummarizationCheckerChain.from_llm(llm, verbose=True, max_checks=2) text = """ Your 9-year old might like these recent discoveries made by The James Webb Space Telescope (JWST): • In 2023, The JWST spotted a number of galaxies nicknamed "green peas." They were given this name because they are small, round, and green, like peas. • The telescope captured images of galaxies that are over 13 billion years old. This means that the light from these galaxies has been traveling for over 13 billion years to reach us. • JWST took the very first pictures of a planet outside of our own solar system. These distant worlds are called "exoplanets." Exo means "from outside." These discoveries can spark a child's imagination about the infinite wonders of the universe.""" checker_chain.run(text) from langchain.chains import LLMSummarizationCheckerChain from langchain_openai import OpenAI llm = OpenAI(temperature=0) checker_chain = LLMSummarizationCheckerChain.from_llm(llm, verbose=True, max_checks=3) text = "The Greenland Sea is an outlying portion of the Arctic Ocean located between Iceland, Norway, the Svalbard archipelago and Greenland. It has an area of 465,000 square miles and is one of five oceans in the world, alongside the Pacific Ocean, Atlantic Ocean, Indian Ocean, and the Southern Ocean. It is the smallest of the five oceans and is covered almost entirely by water, some of which is frozen in the form of glaciers and icebergs. The sea is named after the island of Greenland, and is the Arctic Ocean's main outlet to the Atlantic. It is often frozen over so navigation is limited, and is considered the northern branch of the Norwegian Sea." checker_chain.run(text) from langchain.chains import LLMSummarizationCheckerChain from langchain_openai import OpenAI llm = OpenAI(temperature=0) checker_chain =
LLMSummarizationCheckerChain.from_llm(llm, max_checks=3, verbose=True)
langchain.chains.LLMSummarizationCheckerChain.from_llm
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)
langchain_openai.ChatOpenAI
get_ipython().run_line_magic('pip', 'install --upgrade --quiet airbyte-source-zendesk-support') from langchain_community.document_loaders.airbyte import AirbyteZendeskSupportLoader config = { } loader = AirbyteZendeskSupportLoader( config=config, stream_name="tickets" ) # check the documentation linked above for a list of all streams docs = loader.load() docs_iterator = loader.lazy_load() from langchain.docstore.document import Document def handle_record(record, id): return
Document(page_content=record.data["title"], metadata=record.data)
langchain.docstore.document.Document
get_ipython().run_line_magic('pip', 'install --upgrade --quiet pgvector') get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-openai') get_ipython().run_line_magic('pip', 'install --upgrade --quiet psycopg2-binary') get_ipython().run_line_magic('pip', 'install --upgrade --quiet tiktoken') import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") from dotenv import load_dotenv load_dotenv() from langchain.docstore.document import Document from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores.pgvector import PGVector 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)
langchain_text_splitters.CharacterTextSplitter
from langchain_community.graphs import NeptuneGraph host = "<neptune-host>" port = 8182 use_https = True graph = NeptuneGraph(host=host, port=port, use_https=use_https) from langchain.chains import NeptuneOpenCypherQAChain from langchain_openai import ChatOpenAI llm =
ChatOpenAI(temperature=0, model="gpt-4")
langchain_openai.ChatOpenAI
from langchain.retrievers import KNNRetriever from langchain_openai import OpenAIEmbeddings retriever = KNNRetriever.from_texts( ["foo", "bar", "world", "hello", "foo bar"],
OpenAIEmbeddings()
langchain_openai.OpenAIEmbeddings
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({})
langchain.docstore.InMemoryDocstore
from langchain_openai import OpenAIEmbeddings from langchain_pinecone import PineconeVectorStore all_documents = { "doc1": "Climate change and economic impact.", "doc2": "Public health concerns due to climate change.", "doc3": "Climate change: A social perspective.", "doc4": "Technological solutions to climate change.", "doc5": "Policy changes needed to combat climate change.", "doc6": "Climate change and its impact on biodiversity.", "doc7": "Climate change: The science and models.", "doc8": "Global warming: A subset of climate change.", "doc9": "How climate change affects daily weather.", "doc10": "The history of climate change activism.", } vectorstore = PineconeVectorStore.from_texts( list(all_documents.values()),
OpenAIEmbeddings()
langchain_openai.OpenAIEmbeddings
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain fleet-context langchain-openai pandas faiss-cpu # faiss-gpu for CUDA supported GPU') from operator import itemgetter from typing import Any, Optional, Type import pandas as pd from langchain.retrievers import MultiVectorRetriever from langchain_community.vectorstores import FAISS from langchain_core.documents import Document from langchain_core.stores import BaseStore from langchain_core.vectorstores import VectorStore from langchain_openai import OpenAIEmbeddings def load_fleet_retriever( df: pd.DataFrame, *, vectorstore_cls: Type[VectorStore] = FAISS, docstore: Optional[BaseStore] = None, **kwargs: Any, ): vectorstore = _populate_vectorstore(df, vectorstore_cls) if docstore is None: return vectorstore.as_retriever(**kwargs) else: _populate_docstore(df, docstore) return MultiVectorRetriever( vectorstore=vectorstore, docstore=docstore, id_key="parent", **kwargs ) def _populate_vectorstore( df: pd.DataFrame, vectorstore_cls: Type[VectorStore], ) -> VectorStore: if not hasattr(vectorstore_cls, "from_embeddings"): raise ValueError( f"Incompatible vector store class {vectorstore_cls}." "Must implement `from_embeddings` class method." ) texts_embeddings = [] metadatas = [] for _, row in df.iterrows(): texts_embeddings.append((row.metadata["text"], row["dense_embeddings"])) metadatas.append(row.metadata) return vectorstore_cls.from_embeddings( texts_embeddings, OpenAIEmbeddings(model="text-embedding-ada-002"), metadatas=metadatas, ) def _populate_docstore(df: pd.DataFrame, docstore: BaseStore) -> None: parent_docs = [] df = df.copy() df["parent"] = df.metadata.apply(itemgetter("parent")) for parent_id, group in df.groupby("parent"): sorted_group = group.iloc[ group.metadata.apply(itemgetter("section_index")).argsort() ] text = "".join(sorted_group.metadata.apply(itemgetter("text"))) metadata = { k: sorted_group.iloc[0].metadata[k] for k in ("title", "type", "url") } text = metadata["title"] + "\n" + text metadata["id"] = parent_id parent_docs.append(
Document(page_content=text, metadata=metadata)
langchain_core.documents.Document
from getpass import getpass MOSAICML_API_TOKEN = getpass() import os os.environ["MOSAICML_API_TOKEN"] = MOSAICML_API_TOKEN from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain_community.llms import MosaicML template = """Question: {question}""" prompt = PromptTemplate.from_template(template) llm = MosaicML(inject_instruction_format=True, model_kwargs={"max_new_tokens": 128}) llm_chain =
LLMChain(prompt=prompt, llm=llm)
langchain.chains.LLMChain
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)
langchain_community.agent_toolkits.openapi.spec.reduce_openapi_spec
from langchain.chains import GraphCypherQAChain from langchain_community.graphs import Neo4jGraph from langchain_openai import ChatOpenAI graph = Neo4jGraph( url="bolt://localhost:7687", username="neo4j", password="pleaseletmein" ) graph.query( """ MERGE (m:Movie {name:"Top Gun"}) WITH m UNWIND ["Tom Cruise", "Val Kilmer", "Anthony Edwards", "Meg Ryan"] AS actor MERGE (a:Actor {name:actor}) MERGE (a)-[:ACTED_IN]->(m) """ ) graph.refresh_schema() print(graph.schema) chain = GraphCypherQAChain.from_llm( ChatOpenAI(temperature=0), graph=graph, verbose=True ) chain.run("Who played in Top Gun?") chain = GraphCypherQAChain.from_llm( ChatOpenAI(temperature=0), graph=graph, verbose=True, top_k=2 ) chain.run("Who played in Top Gun?") chain = GraphCypherQAChain.from_llm( ChatOpenAI(temperature=0), graph=graph, verbose=True, return_intermediate_steps=True ) result = chain("Who played in Top Gun?") print(f"Intermediate steps: {result['intermediate_steps']}") print(f"Final answer: {result['result']}") chain = GraphCypherQAChain.from_llm( ChatOpenAI(temperature=0), graph=graph, verbose=True, return_direct=True ) chain.run("Who played in Top Gun?") from langchain.prompts.prompt import PromptTemplate CYPHER_GENERATION_TEMPLATE = """Task:Generate Cypher statement to query a graph database. Instructions: Use only the provided relationship types and properties in the schema. Do not use any other relationship types or properties that are not provided. Schema: {schema} Note: Do not include any explanations or apologies in your responses. Do not respond to any questions that might ask anything else than for you to construct a Cypher statement. Do not include any text except the generated Cypher statement. Examples: Here are a few examples of generated Cypher statements for particular questions: MATCH (m:Movie {{title:"Top Gun"}})<-[:ACTED_IN]-() RETURN count(*) AS numberOfActors The question is: {question}""" CYPHER_GENERATION_PROMPT = PromptTemplate( input_variables=["schema", "question"], template=CYPHER_GENERATION_TEMPLATE ) chain = GraphCypherQAChain.from_llm( ChatOpenAI(temperature=0), graph=graph, verbose=True, cypher_prompt=CYPHER_GENERATION_PROMPT, ) chain.run("How many people played in Top Gun?") chain = GraphCypherQAChain.from_llm( graph=graph, cypher_llm=ChatOpenAI(temperature=0, model="gpt-3.5-turbo"), qa_llm=ChatOpenAI(temperature=0, model="gpt-3.5-turbo-16k"), verbose=True, ) chain.run("Who played in Top Gun?") chain = GraphCypherQAChain.from_llm( graph=graph, cypher_llm=ChatOpenAI(temperature=0, model="gpt-3.5-turbo"), qa_llm=
ChatOpenAI(temperature=0, model="gpt-3.5-turbo-16k")
langchain_openai.ChatOpenAI
get_ipython().run_line_magic('pip', 'install --upgrade --quiet supabase') import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") os.environ["SUPABASE_URL"] = getpass.getpass("Supabase URL:") os.environ["SUPABASE_SERVICE_KEY"] = getpass.getpass("Supabase Service Key:") from dotenv import load_dotenv load_dotenv() import os from langchain_community.vectorstores import SupabaseVectorStore from langchain_openai import OpenAIEmbeddings from supabase.client import Client, create_client supabase_url = os.environ.get("SUPABASE_URL") supabase_key = os.environ.get("SUPABASE_SERVICE_KEY") supabase: Client = create_client(supabase_url, supabase_key) embeddings =
OpenAIEmbeddings()
langchain_openai.OpenAIEmbeddings
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) docsearch.add_texts(["This is a document that is about elephants"]) query = "modern communications devices" doc_results = docsearch.similarity_search(query) print(doc_results[0].page_content) query = "elephants" doc_results = docsearch.similarity_search(query, page_content_builder=get_content) print(doc_results[0].page_content) query = {"communications devices": 1.0} doc_results = docsearch.similarity_search(query) print(doc_results[0].page_content) query = {"communications devices": 1.0, "technology post 2000": -1.0} doc_results = docsearch.similarity_search(query) print(doc_results[0].page_content) import getpass import os from langchain.chains import RetrievalQAWithSourcesChain from langchain_openai import OpenAI os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") with open("../../modules/state_of_the_union.txt") as f: state_of_the_union = f.read() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) texts = text_splitter.split_text(state_of_the_union) index_name = "langchain-qa-with-retrieval" docsearch = Marqo.from_documents(docs, index_name=index_name) chain = RetrievalQAWithSourcesChain.from_chain_type(
OpenAI(temperature=0)
langchain_openai.OpenAI
get_ipython().run_line_magic('pip', 'install laser_encoders') from langchain_community.embeddings.laser import LaserEmbeddings embeddings =
LaserEmbeddings(lang="eng_Latn")
langchain_community.embeddings.laser.LaserEmbeddings
get_ipython().run_line_magic('pip', 'install -qU langchain-text-splitters') from langchain_text_splitters import HTMLHeaderTextSplitter html_string = """ <!DOCTYPE html> <html> <body> <div> <h1>Foo</h1> <p>Some intro text about Foo.</p> <div> <h2>Bar main section</h2> <p>Some intro text about Bar.</p> <h3>Bar subsection 1</h3> <p>Some text about the first subtopic of Bar.</p> <h3>Bar subsection 2</h3> <p>Some text about the second subtopic of Bar.</p> </div> <div> <h2>Baz</h2> <p>Some text about Baz</p> </div> <br> <p>Some concluding text about Foo</p> </div> </body> </html> """ headers_to_split_on = [ ("h1", "Header 1"), ("h2", "Header 2"), ("h3", "Header 3"), ] html_splitter = HTMLHeaderTextSplitter(headers_to_split_on=headers_to_split_on) html_header_splits = html_splitter.split_text(html_string) html_header_splits from langchain_text_splitters import RecursiveCharacterTextSplitter url = "https://plato.stanford.edu/entries/goedel/" headers_to_split_on = [ ("h1", "Header 1"), ("h2", "Header 2"), ("h3", "Header 3"), ("h4", "Header 4"), ] html_splitter =
HTMLHeaderTextSplitter(headers_to_split_on=headers_to_split_on)
langchain_text_splitters.HTMLHeaderTextSplitter
get_ipython().run_line_magic('pip', 'install --upgrade --quiet usearch') 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 USearch from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter from langchain_community.document_loaders import TextLoader loader =
TextLoader("../../../extras/modules/state_of_the_union.txt")
langchain_community.document_loaders.TextLoader
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"), ("human", "{question}"), ] ) memory =
ConversationBufferMemory(return_messages=True)
langchain.memory.ConversationBufferMemory
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)
langchain_openai.OpenAI