Datasets:

Modalities:
Text
Formats:
parquet
Libraries:
Datasets
pandas
prompt
stringlengths
43
25.9k
completion
stringlengths
7
362
api
stringlengths
18
90
from langchain_community.document_loaders import IFixitLoader loader = IFixitLoader("https://www.ifixit.com/Teardown/Banana+Teardown/811") data = loader.load() data loader = IFixitLoader( "https://www.ifixit.com/Answers/View/318583/My+iPhone+6+is+typing+and+opening+apps+by+itself" ) data = loader.load() data loader = IFixitLoader("https://www.ifixit.com/Device/Standard_iPad") data = loader.load() data data =
IFixitLoader.load_suggestions("Banana")
langchain_community.document_loaders.IFixitLoader.load_suggestions
get_ipython().run_line_magic('pip', 'install --upgrade --quiet rockset') import os import rockset ROCKSET_API_KEY = os.environ.get( "ROCKSET_API_KEY" ) # Verify ROCKSET_API_KEY environment variable ROCKSET_API_SERVER = rockset.Regions.usw2a1 # Verify Rockset region rockset_client = rockset.RocksetClient(ROCKSET_API_SERVER, ROCKSET_API_KEY) COLLECTION_NAME = "langchain_demo" TEXT_KEY = "description" EMBEDDING_KEY = "description_embedding" from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import Rockset 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 "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()
langchain_openai.OpenAIEmbeddings
from langchain.agents import AgentType, initialize_agent from langchain.chains import LLMMathChain from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import Tool from langchain_openai import ChatOpenAI get_ipython().run_line_magic('pip', 'install --upgrade --quiet numexpr') llm = ChatOpenAI(temperature=0, model="gpt-4") llm_math_chain = LLMMathChain.from_llm(llm=llm, verbose=True) primes = {998: 7901, 999: 7907, 1000: 7919} class CalculatorInput(BaseModel): question: str = Field() class PrimeInput(BaseModel): n: int = Field() def is_prime(n: int) -> bool: if n <= 1 or (n % 2 == 0 and n > 2): return False for i in range(3, int(n**0.5) + 1, 2): if n % i == 0: return False return True def get_prime(n: int, primes: dict = primes) -> str: return str(primes.get(int(n))) async def aget_prime(n: int, primes: dict = primes) -> str: return str(primes.get(int(n))) tools = [ Tool( name="GetPrime", func=get_prime, description="A tool that returns the `n`th prime number", args_schema=PrimeInput, coroutine=aget_prime, ), Tool.from_function( func=llm_math_chain.run, name="Calculator", description="Useful for when you need to compute mathematical expressions", args_schema=CalculatorInput, coroutine=llm_math_chain.arun, ), ] from langchain import hub prompt = hub.pull("hwchase17/openai-functions-agent") from langchain.agents import create_openai_functions_agent agent =
create_openai_functions_agent(llm, tools, prompt)
langchain.agents.create_openai_functions_agent
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 psycopg2-binary') get_ipython().run_line_magic('pip', 'install --upgrade --quiet tiktoken') YBUSER = "[SANDBOX USER]" YBPASSWORD = "[SANDBOX PASSWORD]" YBDATABASE = "[SANDBOX_DATABASE]" YBHOST = "trialsandbox.sandbox.aws.yellowbrickcloud.com" OPENAI_API_KEY = "[OPENAI API KEY]" import os import pathlib import re import sys import urllib.parse as urlparse from getpass import getpass import psycopg2 from IPython.display import Markdown, display from langchain.chains import LLMChain, RetrievalQAWithSourcesChain from langchain.docstore.document import Document from langchain_community.vectorstores import Yellowbrick from langchain_openai import ChatOpenAI, OpenAIEmbeddings from langchain_text_splitters import RecursiveCharacterTextSplitter yellowbrick_connection_string = ( f"postgres://{urlparse.quote(YBUSER)}:{YBPASSWORD}@{YBHOST}:5432/{YBDATABASE}" ) YB_DOC_DATABASE = "sample_data" YB_DOC_TABLE = "yellowbrick_documentation" embedding_table = "my_embeddings" os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY from langchain.prompts.chat import ( ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, ) system_template = """If you don't know the answer, Make up your best guess.""" messages = [ SystemMessagePromptTemplate.from_template(system_template), HumanMessagePromptTemplate.from_template("{question}"), ] prompt = ChatPromptTemplate.from_messages(messages) chain_type_kwargs = {"prompt": prompt} llm = ChatOpenAI( model_name="gpt-3.5-turbo", # Modify model_name if you have access to GPT-4 temperature=0, max_tokens=256, ) chain = LLMChain( llm=llm, prompt=prompt, verbose=False, ) def print_result_simple(query): result = chain(query) output_text = f"""### Question: {query} {result['text']} """ display(Markdown(output_text)) print_result_simple("How many databases can be in a Yellowbrick Instance?") print_result_simple("What's an easy way to add users in bulk to Yellowbrick?") try: conn = psycopg2.connect(yellowbrick_connection_string) except psycopg2.Error as e: print(f"Error connecting to the database: {e}") exit(1) cursor = conn.cursor() create_table_query = f""" CREATE TABLE if not exists {embedding_table} ( id uuid, embedding_id integer, text character varying(60000), metadata character varying(1024), embedding double precision ) DISTRIBUTE ON (id); truncate table {embedding_table}; """ try: cursor.execute(create_table_query) print(f"Table '{embedding_table}' created successfully!") except psycopg2.Error as e: print(f"Error creating table: {e}") conn.rollback() conn.commit() cursor.close() conn.close() yellowbrick_doc_connection_string = ( f"postgres://{urlparse.quote(YBUSER)}:{YBPASSWORD}@{YBHOST}:5432/{YB_DOC_DATABASE}" ) conn = psycopg2.connect(yellowbrick_doc_connection_string) cursor = conn.cursor() query = f"SELECT path, document FROM {YB_DOC_TABLE}" cursor.execute(query) yellowbrick_documents = cursor.fetchall() print(f"Extracted {len(yellowbrick_documents)} documents successfully!") cursor.close() conn.close() DOCUMENT_BASE_URL = "https://docs.yellowbrick.com/6.7.1/" # Actual URL separator = "\n## " # This separator assumes Markdown docs from the repo uses ### as logical main header most of the time chunk_size_limit = 2000 max_chunk_overlap = 200 documents = [ Document( page_content=document[1], metadata={"source": DOCUMENT_BASE_URL + document[0].replace(".md", ".html")}, ) for document in yellowbrick_documents ] text_splitter = RecursiveCharacterTextSplitter( chunk_size=chunk_size_limit, chunk_overlap=max_chunk_overlap, separators=[separator, "\nn", "\n", ",", " ", ""], ) split_docs = text_splitter.split_documents(documents) docs_text = [doc.page_content for doc in split_docs] embeddings = OpenAIEmbeddings() vector_store = Yellowbrick.from_documents( documents=split_docs, embedding=embeddings, connection_string=yellowbrick_connection_string, table=embedding_table, ) print(f"Created vector store with {len(documents)} documents") system_template = """Use the following pieces of context to answer the users question. Take note of the sources and include them in the answer in the format: "SOURCES: source1 source2", use "SOURCES" in capital letters regardless of the number of sources. If you don't know the answer, just say that "I don't know", don't try to make up an answer. ---------------- {summaries}""" messages = [ SystemMessagePromptTemplate.from_template(system_template),
HumanMessagePromptTemplate.from_template("{question}")
langchain.prompts.chat.HumanMessagePromptTemplate.from_template
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain nuclia') from langchain_community.vectorstores.nucliadb import NucliaDB API_KEY = "YOUR_API_KEY" ndb = NucliaDB(knowledge_box="YOUR_KB_ID", local=False, api_key=API_KEY) from langchain_community.vectorstores.nucliadb import NucliaDB ndb =
NucliaDB(knowledge_box="YOUR_KB_ID", local=True, backend="http://my-local-server")
langchain_community.vectorstores.nucliadb.NucliaDB
get_ipython().system(' pip install -U langchain openai chromadb langchain-experimental # (newest versions required for multi-modal)') get_ipython().system(' pip install "unstructured[all-docs]==0.10.19" pillow pydantic lxml pillow matplotlib tiktoken open_clip_torch torch') path = "/Users/rlm/Desktop/photos/" from unstructured.partition.pdf import partition_pdf raw_pdf_elements = partition_pdf( filename=path + "photos.pdf", extract_images_in_pdf=True, infer_table_structure=True, chunking_strategy="by_title", max_characters=4000, new_after_n_chars=3800, combine_text_under_n_chars=2000, image_output_dir_path=path, ) tables = [] texts = [] for element in raw_pdf_elements: if "unstructured.documents.elements.Table" in str(type(element)): tables.append(str(element)) elif "unstructured.documents.elements.CompositeElement" in str(type(element)): texts.append(str(element)) import os import uuid import chromadb import numpy as np from langchain_community.vectorstores import Chroma from langchain_experimental.open_clip import OpenCLIPEmbeddings from PIL import Image as _PILImage vectorstore = Chroma( collection_name="mm_rag_clip_photos", embedding_function=OpenCLIPEmbeddings() ) image_uris = sorted( [ os.path.join(path, image_name) for image_name in os.listdir(path) if image_name.endswith(".jpg") ] ) vectorstore.add_images(uris=image_uris) vectorstore.add_texts(texts=texts) retriever = vectorstore.as_retriever() import base64 import io from io import BytesIO import numpy as np from PIL import Image def resize_base64_image(base64_string, size=(128, 128)): """ Resize an image encoded as a Base64 string. Args: base64_string (str): Base64 string of the original image. size (tuple): Desired size of the image as (width, height). Returns: str: Base64 string of the resized image. """ img_data = base64.b64decode(base64_string) img = Image.open(io.BytesIO(img_data)) resized_img = img.resize(size, Image.LANCZOS) buffered = io.BytesIO() resized_img.save(buffered, format=img.format) return base64.b64encode(buffered.getvalue()).decode("utf-8") def is_base64(s): """Check if a string is Base64 encoded""" try: return base64.b64encode(base64.b64decode(s)) == s.encode() except Exception: return False def split_image_text_types(docs): """Split numpy array images and texts""" images = [] text = [] for doc in docs: doc = doc.page_content # Extract Document contents if is_base64(doc): images.append( resize_base64_image(doc, size=(250, 250)) ) # base64 encoded str else: text.append(doc) return {"images": images, "texts": text} from operator import itemgetter from langchain_core.messages import HumanMessage, SystemMessage from langchain_core.output_parsers import StrOutputParser from langchain_core.runnables import RunnableLambda, RunnablePassthrough from langchain_openai import ChatOpenAI def prompt_func(data_dict): formatted_texts = "\n".join(data_dict["context"]["texts"]) messages = [] if data_dict["context"]["images"]: image_message = { "type": "image_url", "image_url": { "url": f"data:image/jpeg;base64,{data_dict['context']['images'][0]}" }, } messages.append(image_message) text_message = { "type": "text", "text": ( "As an expert art critic and historian, your task is to analyze and interpret images, " "considering their historical and cultural significance. Alongside the images, you will be " "provided with related text to offer context. Both will be retrieved from a vectorstore based " "on user-input keywords. Please use your extensive knowledge and analytical skills to provide a " "comprehensive summary that includes:\n" "- A detailed description of the visual elements in the image.\n" "- The historical and cultural context of the image.\n" "- An interpretation of the image's symbolism and meaning.\n" "- Connections between the image and the related text.\n\n" f"User-provided keywords: {data_dict['question']}\n\n" "Text and / or tables:\n" f"{formatted_texts}" ), } messages.append(text_message) return [HumanMessage(content=messages)] model = ChatOpenAI(temperature=0, model="gpt-4-vision-preview", max_tokens=1024) chain = ( { "context": retriever |
RunnableLambda(split_image_text_types)
langchain_core.runnables.RunnableLambda
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(), docs=retrieve) .assign(context=format) .assign(answer=answer) .pick(["answer", "docs"]) ) chain_4.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 annotated_answer(BaseModel): """Annotate the answer to the user question with quote citations that justify the answer.""" citations: List[Citation] = Field( ..., description="Citations from the given sources that justify the answer." ) llm_with_tools_5 = llm.bind_tools( [annotated_answer], tool_choice="annotated_answer", ) from langchain_core.prompts import MessagesPlaceholder prompt_5 = 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}"),
MessagesPlaceholder("chat_history", optional=True)
langchain_core.prompts.MessagesPlaceholder
from langchain.chains import RetrievalQA from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import Chroma from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter loader = TextLoader("../../state_of_the_union.txt", encoding="utf-8") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) texts = text_splitter.split_documents(documents) for i, text in enumerate(texts): text.metadata["source"] = f"{i}-pl" embeddings = OpenAIEmbeddings() docsearch = Chroma.from_documents(texts, embeddings) from langchain.chains import create_qa_with_sources_chain from langchain.chains.combine_documents.stuff import StuffDocumentsChain from langchain.prompts import PromptTemplate from langchain_openai import ChatOpenAI llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613") qa_chain = create_qa_with_sources_chain(llm) doc_prompt = PromptTemplate( template="Content: {page_content}\nSource: {source}", input_variables=["page_content", "source"], ) final_qa_chain = StuffDocumentsChain( llm_chain=qa_chain, document_variable_name="context", document_prompt=doc_prompt, ) retrieval_qa = RetrievalQA( retriever=docsearch.as_retriever(), combine_documents_chain=final_qa_chain ) query = "What did the president say about russia" retrieval_qa.run(query) qa_chain_pydantic = create_qa_with_sources_chain(llm, output_parser="pydantic") final_qa_chain_pydantic = StuffDocumentsChain( llm_chain=qa_chain_pydantic, document_variable_name="context", document_prompt=doc_prompt, ) retrieval_qa_pydantic = RetrievalQA( retriever=docsearch.as_retriever(), combine_documents_chain=final_qa_chain_pydantic ) retrieval_qa_pydantic.run(query) from langchain.chains import ConversationalRetrievalChain, LLMChain from langchain.memory import ConversationBufferMemory memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True) _template = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question, in its original language.\ Make sure to avoid using any unclear pronouns. Chat History: {chat_history} Follow Up Input: {question} Standalone question:""" CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template) condense_question_chain = LLMChain( llm=llm, prompt=CONDENSE_QUESTION_PROMPT, ) qa = ConversationalRetrievalChain( question_generator=condense_question_chain, retriever=docsearch.as_retriever(), memory=memory, combine_docs_chain=final_qa_chain, ) query = "What did the president say about Ketanji Brown Jackson" result = qa({"question": query}) result query = "what did he say about her predecessor?" result = qa({"question": query}) result from typing import List from langchain.chains.openai_functions import create_qa_with_structure_chain from langchain.prompts.chat import ChatPromptTemplate, HumanMessagePromptTemplate from langchain_core.messages import HumanMessage, SystemMessage from pydantic import BaseModel, Field class CustomResponseSchema(BaseModel): """An answer to the question being asked, with sources.""" answer: str = Field(..., description="Answer to the question that was asked") countries_referenced: List[str] = Field( ..., description="All of the countries mentioned in the sources" ) sources: List[str] = Field( ..., description="List of sources used to answer the question" ) prompt_messages = [ SystemMessage( content=( "You are a world class algorithm to answer " "questions in a specific format." ) ), HumanMessage(content="Answer question using the following context"), HumanMessagePromptTemplate.from_template("{context}"),
HumanMessagePromptTemplate.from_template("Question: {question}")
langchain.prompts.chat.HumanMessagePromptTemplate.from_template
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()) question = "What are the approaches to Task Decomposition?" docs = vectorstore.similarity_search(question) len(docs) docs[0] get_ipython().run_line_magic('pip', 'install --upgrade --quiet llama-cpp-python') get_ipython().system(' CMAKE_ARGS="-DLLAMA_METAL=on" FORCE_CMAKE=1 /Users/rlm/miniforge3/envs/llama/bin/pip install -U llama-cpp-python --no-cache-dir') from langchain_community.llms import LlamaCpp n_gpu_layers = 1 # Metal set to 1 is enough. n_batch = 512 # Should be between 1 and n_ctx, consider the amount of RAM of your Apple Silicon Chip. llm = LlamaCpp( model_path="/Users/rlm/Desktop/Code/llama.cpp/models/llama-2-13b-chat.ggufv3.q4_0.bin", n_gpu_layers=n_gpu_layers, n_batch=n_batch, n_ctx=2048, f16_kv=True, # MUST set to True, otherwise you will run into problem after a couple of calls verbose=True, ) llm.invoke("Simulate a rap battle between Stephen Colbert and John Oliver") from langchain_community.llms import GPT4All gpt4all = GPT4All( model="/Users/rlm/Desktop/Code/gpt4all/models/nous-hermes-13b.ggmlv3.q4_0.bin", max_tokens=2048, ) from langchain_community.llms.llamafile import Llamafile llamafile = Llamafile() llamafile.invoke("Here is my grandmother's beloved recipe for spaghetti and meatballs:") from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import PromptTemplate prompt = PromptTemplate.from_template( "Summarize the main themes in these retrieved docs: {docs}" ) def format_docs(docs): return "\n\n".join(doc.page_content for doc in docs) chain = {"docs": format_docs} | prompt | llm | StrOutputParser() question = "What are the approaches to Task Decomposition?" docs = vectorstore.similarity_search(question) chain.invoke(docs) from langchain import hub rag_prompt = hub.pull("rlm/rag-prompt") rag_prompt.messages from langchain_core.runnables import RunnablePassthrough, RunnablePick chain = ( RunnablePassthrough.assign(context=
RunnablePick("context")
langchain_core.runnables.RunnablePick
get_ipython().run_line_magic('pip', 'install --upgrade --quiet tigrisdb openapi-schema-pydantic langchain-openai tiktoken') import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") os.environ["TIGRIS_PROJECT"] = getpass.getpass("Tigris Project Name:") os.environ["TIGRIS_CLIENT_ID"] = getpass.getpass("Tigris Client Id:") os.environ["TIGRIS_CLIENT_SECRET"] = getpass.getpass("Tigris Client Secret:") from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import Tigris from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter loader =
TextLoader("../../../state_of_the_union.txt")
langchain_community.document_loaders.TextLoader
import os os.environ["GOOGLE_CSE_ID"] = "" os.environ["GOOGLE_API_KEY"] = "" from langchain.tools import Tool from langchain_community.utilities import GoogleSearchAPIWrapper search =
GoogleSearchAPIWrapper()
langchain_community.utilities.GoogleSearchAPIWrapper
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)
langchain.agents.load_tools
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")
langchain_openai.ChatOpenAI
URL = "" # Your Fiddler instance URL, Make sure to include the full URL (including https://). For example: https://demo.fiddler.ai ORG_NAME = "" AUTH_TOKEN = "" # Your Fiddler instance auth token PROJECT_NAME = "" MODEL_NAME = "" # Model name in Fiddler from langchain_community.callbacks.fiddler_callback import FiddlerCallbackHandler fiddler_handler = FiddlerCallbackHandler( url=URL, org=ORG_NAME, project=PROJECT_NAME, model=MODEL_NAME, api_key=AUTH_TOKEN, ) from langchain_core.output_parsers import StrOutputParser from langchain_openai import OpenAI llm = OpenAI(temperature=0, streaming=True, callbacks=[fiddler_handler]) output_parser =
StrOutputParser()
langchain_core.output_parsers.StrOutputParser
get_ipython().run_line_magic('pip', 'install --upgrade --quiet nlpcloud') from getpass import getpass NLPCLOUD_API_KEY = getpass() import os os.environ["NLPCLOUD_API_KEY"] = NLPCLOUD_API_KEY from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain_community.llms import NLPCloud template = """Question: {question} Answer: Let's think step by step.""" prompt = PromptTemplate.from_template(template) llm =
NLPCloud()
langchain_community.llms.NLPCloud
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()
langchain_core.runnables.RunnablePassthrough
get_ipython().run_line_magic('pip', 'install --upgrade --quiet unstructured') from langchain_community.document_loaders import UnstructuredEmailLoader loader = UnstructuredEmailLoader("example_data/fake-email.eml") data = loader.load() data loader = UnstructuredEmailLoader("example_data/fake-email.eml", mode="elements") data = loader.load() data[0] loader = UnstructuredEmailLoader( "example_data/fake-email.eml", mode="elements", process_attachments=True, ) data = loader.load() data[0] get_ipython().run_line_magic('pip', 'install --upgrade --quiet extract_msg') from langchain_community.document_loaders import OutlookMessageLoader loader =
OutlookMessageLoader("example_data/fake-email.msg")
langchain_community.document_loaders.OutlookMessageLoader
get_ipython().run_line_magic('pip', 'install --upgrade --quiet opaqueprompts langchain') import os os.environ["OPAQUEPROMPTS_API_KEY"] = "<OPAQUEPROMPTS_API_KEY>" os.environ["OPENAI_API_KEY"] = "<OPENAI_API_KEY>" from langchain.callbacks.stdout import StdOutCallbackHandler from langchain.chains import LLMChain from langchain.globals import set_debug, set_verbose from langchain.memory import ConversationBufferWindowMemory from langchain.prompts import PromptTemplate from langchain_community.llms import OpaquePrompts from langchain_openai import OpenAI set_debug(True) set_verbose(True) prompt_template = """ As an AI assistant, you will answer questions according to given context. Sensitive personal information in the question is masked for privacy. For instance, if the original text says "Giana is good," it will be changed to "PERSON_998 is good." Here's how to handle these changes: * Consider these masked phrases just as placeholders, but still refer to them in a relevant way when answering. * It's possible that different masked terms might mean the same thing. Stick with the given term and don't modify it. * All masked terms follow the "TYPE_ID" pattern. * Please don't invent new masked terms. For instance, if you see "PERSON_998," don't come up with "PERSON_997" or "PERSON_999" unless they're already in the question. Conversation History: ```{history}``` Context : ```During our recent meeting on February 23, 2023, at 10:30 AM, John Doe provided me with his personal details. His email is [email protected] and his contact number is 650-456-7890. He lives in New York City, USA, and belongs to the American nationality with Christian beliefs and a leaning towards the Democratic party. He mentioned that he recently made a transaction using his credit card 4111 1111 1111 1111 and transferred bitcoins to the wallet address 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa. While discussing his European travels, he noted down his IBAN as GB29 NWBK 6016 1331 9268 19. Additionally, he provided his website as https://johndoeportfolio.com. John also discussed some of his US-specific details. He said his bank account number is 1234567890123456 and his drivers license is Y12345678. His ITIN is 987-65-4321, and he recently renewed his passport, the number for which is 123456789. He emphasized not to share his SSN, which is 123-45-6789. Furthermore, he mentioned that he accesses his work files remotely through the IP 192.168.1.1 and has a medical license number MED-123456. ``` Question: ```{question}``` """ chain = LLMChain( prompt=PromptTemplate.from_template(prompt_template), llm=OpaquePrompts(base_llm=
OpenAI()
langchain_openai.OpenAI
get_ipython().run_line_magic('pip', 'install --upgrade --quiet sentence_transformers') from langchain_community.embeddings import HuggingFaceEmbeddings embeddings =
HuggingFaceEmbeddings()
langchain_community.embeddings.HuggingFaceEmbeddings
from langchain.agents import AgentType, initialize_agent from langchain.requests import Requests from langchain_community.agent_toolkits import NLAToolkit from langchain_openai import OpenAI llm = OpenAI( temperature=0, max_tokens=700, model_name="gpt-3.5-turbo-instruct" ) # You can swap between different core LLM's here. speak_toolkit = NLAToolkit.from_llm_and_url(llm, "https://api.speak.com/openapi.yaml") klarna_toolkit = NLAToolkit.from_llm_and_url( llm, "https://www.klarna.com/us/shopping/public/openai/v0/api-docs/" ) openapi_format_instructions = """Use the following format: Question: the input question you must answer Thought: you should always think about what to do Action: the action to take, should be one of [{tool_names}] Action Input: what to instruct the AI Action representative. Observation: The Agent's response ... (this Thought/Action/Action Input/Observation can repeat N times) Thought: I now know the final answer. User can't see any of my observations, API responses, links, or tools. Final Answer: the final answer to the original input question with the right amount of detail When responding with your Final Answer, remember that the person you are responding to CANNOT see any of your Thought/Action/Action Input/Observations, so if there is any relevant information there you need to include it explicitly in your response.""" natural_language_tools = speak_toolkit.get_tools() + klarna_toolkit.get_tools() mrkl = initialize_agent( natural_language_tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, agent_kwargs={"format_instructions": openapi_format_instructions}, ) mrkl.run( "I have an end of year party for my Italian class and have to buy some Italian clothes for it" ) spoonacular_api_key = "" # Copy from the API Console requests =
Requests(headers={"x-api-key": spoonacular_api_key})
langchain.requests.Requests
from langchain.callbacks import get_openai_callback from langchain_openai import ChatOpenAI llm = ChatOpenAI(model_name="gpt-4") with get_openai_callback() as cb: result = llm.invoke("Tell me a joke") print(cb) with get_openai_callback() as cb: result = llm.invoke("Tell me a joke") result2 = llm.invoke("Tell me a joke") print(cb.total_tokens) from langchain.agents import AgentType, initialize_agent, load_tools from langchain_openai import OpenAI tools = load_tools(["serpapi", "llm-math"], llm=llm) agent =
initialize_agent(tools, llm, agent=AgentType.OPENAI_FUNCTIONS, verbose=True)
langchain.agents.initialize_agent
import os import pprint os.environ["SERPER_API_KEY"] = "" from langchain_community.utilities import GoogleSerperAPIWrapper search =
GoogleSerperAPIWrapper()
langchain_community.utilities.GoogleSerperAPIWrapper
from langchain.callbacks import HumanApprovalCallbackHandler from langchain.tools import ShellTool tool = ShellTool() print(tool.run("echo Hello World!")) tool = ShellTool(callbacks=[
HumanApprovalCallbackHandler()
langchain.callbacks.HumanApprovalCallbackHandler
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)
langchain.prompts.PromptTemplate
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain sentence_transformers') from langchain_community.embeddings import HuggingFaceEmbeddings embeddings = HuggingFaceEmbeddings() text = "This is a test document." query_result = embeddings.embed_query(text) query_result[:3] doc_result = embeddings.embed_documents([text]) import getpass inference_api_key = getpass.getpass("Enter your HF Inference API Key:\n\n") from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings embeddings = HuggingFaceInferenceAPIEmbeddings( api_key=inference_api_key, model_name="sentence-transformers/all-MiniLM-l6-v2" ) query_result = embeddings.embed_query(text) query_result[:3] get_ipython().system('pip install huggingface_hub') from langchain_community.embeddings import HuggingFaceHubEmbeddings embeddings =
HuggingFaceHubEmbeddings()
langchain_community.embeddings.HuggingFaceHubEmbeddings
get_ipython().run_line_magic('pip', 'install --upgrade --quiet trubrics') import os os.environ["TRUBRICS_EMAIL"] = "***@***" os.environ["TRUBRICS_PASSWORD"] = "***" os.environ["OPENAI_API_KEY"] = "sk-***" from langchain.callbacks import TrubricsCallbackHandler from langchain_openai import OpenAI llm = OpenAI(callbacks=[TrubricsCallbackHandler()]) res = llm.generate(["Tell me a joke", "Write me a poem"]) print("--> GPT's joke: ", res.generations[0][0].text) print() print("--> GPT's poem: ", res.generations[1][0].text) from langchain.callbacks import TrubricsCallbackHandler from langchain_core.messages import HumanMessage, SystemMessage from langchain_openai import ChatOpenAI chat_llm = ChatOpenAI( callbacks=[ TrubricsCallbackHandler( project="default", tags=["chat model"], user_id="user-id-1234", some_metadata={"hello": [1, 2]}, ) ] ) chat_res = chat_llm( [
SystemMessage(content="Every answer of yours must be about OpenAI.")
langchain_core.messages.SystemMessage
from langchain_community.document_loaders.blob_loaders.youtube_audio import ( YoutubeAudioLoader, ) from langchain_community.document_loaders.generic import GenericLoader from langchain_community.document_loaders.parsers import ( OpenAIWhisperParser, OpenAIWhisperParserLocal, ) get_ipython().run_line_magic('pip', 'install --upgrade --quiet yt_dlp') get_ipython().run_line_magic('pip', 'install --upgrade --quiet pydub') get_ipython().run_line_magic('pip', 'install --upgrade --quiet librosa') local = False urls = ["https://youtu.be/kCc8FmEb1nY", "https://youtu.be/VMj-3S1tku0"] save_dir = "~/Downloads/YouTube" if local: loader = GenericLoader( YoutubeAudioLoader(urls, save_dir), OpenAIWhisperParserLocal() ) else: loader = GenericLoader(YoutubeAudioLoader(urls, save_dir), OpenAIWhisperParser()) docs = loader.load() docs[0].page_content[0:500] from langchain.chains import RetrievalQA from langchain_community.vectorstores import FAISS from langchain_openai import ChatOpenAI, OpenAIEmbeddings from langchain_text_splitters import RecursiveCharacterTextSplitter combined_docs = [doc.page_content for doc in docs] text = " ".join(combined_docs) text_splitter = RecursiveCharacterTextSplitter(chunk_size=1500, chunk_overlap=150) splits = text_splitter.split_text(text) embeddings = OpenAIEmbeddings() vectordb = FAISS.from_texts(splits, embeddings) qa_chain = RetrievalQA.from_chain_type( llm=
ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
langchain_openai.ChatOpenAI
from langchain_community.document_loaders import HuggingFaceDatasetLoader dataset_name = "imdb" page_content_column = "text" loader = HuggingFaceDatasetLoader(dataset_name, page_content_column) data = loader.load() data[:15] from langchain.indexes import VectorstoreIndexCreator from langchain_community.document_loaders.hugging_face_dataset import ( HuggingFaceDatasetLoader, ) dataset_name = "tweet_eval" page_content_column = "text" name = "stance_climate" loader = HuggingFaceDatasetLoader(dataset_name, page_content_column, name) index =
VectorstoreIndexCreator()
langchain.indexes.VectorstoreIndexCreator
get_ipython().run_line_magic('pip', 'install -U --quiet langchain langchain_community openai chromadb langchain-experimental') get_ipython().run_line_magic('pip', 'install --quiet "unstructured[all-docs]" pypdf pillow pydantic lxml pillow matplotlib chromadb tiktoken') import logging import zipfile import requests logging.basicConfig(level=logging.INFO) data_url = "https://storage.googleapis.com/benchmarks-artifacts/langchain-docs-benchmarking/cj.zip" result = requests.get(data_url) filename = "cj.zip" with open(filename, "wb") as file: file.write(result.content) with zipfile.ZipFile(filename, "r") as zip_ref: zip_ref.extractall() from langchain_community.document_loaders import PyPDFLoader loader = PyPDFLoader("./cj/cj.pdf") docs = loader.load() tables = [] texts = [d.page_content for d in docs] len(texts) from langchain.prompts import PromptTemplate from langchain_community.chat_models import ChatVertexAI from langchain_community.llms import VertexAI from langchain_core.messages import AIMessage from langchain_core.output_parsers import StrOutputParser from langchain_core.runnables import RunnableLambda def generate_text_summaries(texts, tables, summarize_texts=False): """ Summarize text elements texts: List of str tables: List of str summarize_texts: Bool to summarize texts """ prompt_text = """You are an assistant tasked with summarizing tables and text for retrieval. \ These summaries will be embedded and used to retrieve the raw text or table elements. \ Give a concise summary of the table or text that is well optimized for retrieval. Table or text: {element} """ prompt = PromptTemplate.from_template(prompt_text) empty_response = RunnableLambda( lambda x: AIMessage(content="Error processing document") ) model = VertexAI( temperature=0, model_name="gemini-pro", max_output_tokens=1024 ).with_fallbacks([empty_response]) summarize_chain = {"element": lambda x: x} | prompt | model | StrOutputParser() text_summaries = [] table_summaries = [] if texts and summarize_texts: text_summaries = summarize_chain.batch(texts, {"max_concurrency": 1}) elif texts: text_summaries = texts if tables: table_summaries = summarize_chain.batch(tables, {"max_concurrency": 1}) return text_summaries, table_summaries text_summaries, table_summaries = generate_text_summaries( texts, tables, summarize_texts=True ) len(text_summaries) import base64 import os from langchain_core.messages import HumanMessage def encode_image(image_path): """Getting the base64 string""" with open(image_path, "rb") as image_file: return base64.b64encode(image_file.read()).decode("utf-8") def image_summarize(img_base64, prompt): """Make image summary""" model =
ChatVertexAI(model_name="gemini-pro-vision", max_output_tokens=1024)
langchain_community.chat_models.ChatVertexAI
get_ipython().run_line_magic('pip', 'install -qU langchain-text-splitters') from langchain_text_splitters import ( Language, RecursiveCharacterTextSplitter, ) [e.value for e in Language]
RecursiveCharacterTextSplitter.get_separators_for_language(Language.PYTHON)
langchain_text_splitters.RecursiveCharacterTextSplitter.get_separators_for_language
get_ipython().system('pip install -U oci') from langchain_community.llms import OCIGenAI llm = OCIGenAI( model_id="MY_MODEL", service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com", compartment_id="MY_OCID", ) response = llm.invoke("Tell me one fact about earth", temperature=0.7) print(response) from langchain.chains import LLMChain from langchain_core.prompts import PromptTemplate llm = OCIGenAI( model_id="MY_MODEL", service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com", compartment_id="MY_OCID", auth_type="SECURITY_TOKEN", auth_profile="MY_PROFILE", # replace with your profile name model_kwargs={"temperature": 0.7, "top_p": 0.75, "max_tokens": 200}, ) prompt = PromptTemplate(input_variables=["query"], template="{query}") llm_chain =
LLMChain(llm=llm, prompt=prompt)
langchain.chains.LLMChain
import os from langchain.agents import AgentType, initialize_agent, load_tools from langchain_community.utilities import Portkey from langchain_openai import OpenAI os.environ["OPENAI_API_KEY"] = "<OPENAI_API_KEY>" PORTKEY_API_KEY = "<PORTKEY_API_KEY>" # Paste your Portkey API Key here TRACE_ID = "portkey_langchain_demo" # Set trace id here headers = Portkey.Config( api_key=PORTKEY_API_KEY, trace_id=TRACE_ID, ) llm = OpenAI(temperature=0, headers=headers) tools = load_tools(["serpapi", "llm-math"], llm=llm) agent = initialize_agent( tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True ) agent.run( "What was the high temperature in SF yesterday in Fahrenheit? What is that number raised to the .023 power?" ) headers = Portkey.Config( api_key="<PORTKEY_API_KEY>", cache="semantic", cache_force_refresh="True", cache_age=1729, retry_count=5, trace_id="langchain_agent", environment="production", user="john", organisation="acme", prompt="Frost", ) llm =
OpenAI(temperature=0.9, headers=headers)
langchain_openai.OpenAI
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain langchain-openai') from langchain_community.chat_models import ChatAnthropic from langchain_openai import ChatOpenAI from unittest.mock import patch import httpx from openai import RateLimitError request = httpx.Request("GET", "/") response = httpx.Response(200, request=request) error = RateLimitError("rate limit", response=response, body="") openai_llm = ChatOpenAI(max_retries=0) anthropic_llm = ChatAnthropic() llm = openai_llm.with_fallbacks([anthropic_llm]) with patch("openai.resources.chat.completions.Completions.create", side_effect=error): try: print(openai_llm.invoke("Why did the chicken cross the road?")) except RateLimitError: print("Hit error") with patch("openai.resources.chat.completions.Completions.create", side_effect=error): try: print(llm.invoke("Why did the chicken cross the road?")) except RateLimitError: print("Hit error") from langchain_core.prompts import ChatPromptTemplate prompt = ChatPromptTemplate.from_messages( [ ( "system", "You're a nice assistant who always includes a compliment in your response", ), ("human", "Why did the {animal} cross the road"), ] ) chain = prompt | llm with patch("openai.resources.chat.completions.Completions.create", side_effect=error): try: print(chain.invoke({"animal": "kangaroo"})) except RateLimitError: print("Hit error") from langchain_core.output_parsers import StrOutputParser chat_prompt = ChatPromptTemplate.from_messages( [ ( "system", "You're a nice assistant who always includes a compliment in your response", ), ("human", "Why did the {animal} cross the road"), ] ) chat_model = ChatOpenAI(model_name="gpt-fake") bad_chain = chat_prompt | chat_model | StrOutputParser() from langchain.prompts import PromptTemplate from langchain_openai import OpenAI prompt_template = """Instructions: You should always include a compliment in your response. Question: Why did the {animal} cross the road?""" prompt =
PromptTemplate.from_template(prompt_template)
langchain.prompts.PromptTemplate.from_template
from typing import List from langchain.prompts.chat import ( HumanMessagePromptTemplate, SystemMessagePromptTemplate, ) from langchain.schema import ( AIMessage, BaseMessage, HumanMessage, SystemMessage, ) from langchain_openai import ChatOpenAI class CAMELAgent: def __init__( self, system_message: SystemMessage, model: ChatOpenAI, ) -> None: self.system_message = system_message self.model = model self.init_messages() def reset(self) -> None: self.init_messages() return self.stored_messages def init_messages(self) -> None: self.stored_messages = [self.system_message] def update_messages(self, message: BaseMessage) -> List[BaseMessage]: self.stored_messages.append(message) return self.stored_messages def step( self, input_message: HumanMessage, ) -> AIMessage: messages = self.update_messages(input_message) output_message = self.model(messages) self.update_messages(output_message) return output_message import os os.environ["OPENAI_API_KEY"] = "" assistant_role_name = "Python Programmer" user_role_name = "Stock Trader" task = "Develop a trading bot for the stock market" word_limit = 50 # word limit for task brainstorming task_specifier_sys_msg =
SystemMessage(content="You can make a task more specific.")
langchain.schema.SystemMessage
from langchain.prompts import PromptTemplate from langchain_core.output_parsers import StrOutputParser from langchain_core.prompt_values import PromptValue from langchain_openai import ChatOpenAI short_context_model = ChatOpenAI(model="gpt-3.5-turbo") long_context_model = ChatOpenAI(model="gpt-3.5-turbo-16k") def get_context_length(prompt: PromptValue): messages = prompt.to_messages() tokens = short_context_model.get_num_tokens_from_messages(messages) return tokens prompt =
PromptTemplate.from_template("Summarize this passage: {context}")
langchain.prompts.PromptTemplate.from_template
get_ipython().run_line_magic('pip', 'install --upgrade --quiet cos-python-sdk-v5') from langchain_community.document_loaders import TencentCOSDirectoryLoader from qcloud_cos import CosConfig conf = CosConfig( Region="your cos region", SecretId="your cos secret_id", SecretKey="your cos secret_key", ) loader =
TencentCOSDirectoryLoader(conf=conf, bucket="you_cos_bucket")
langchain_community.document_loaders.TencentCOSDirectoryLoader
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain langchain-openai') from langchain.evaluation import load_evaluator evaluator = load_evaluator("trajectory") import subprocess from urllib.parse import urlparse from langchain.agents import AgentType, initialize_agent from langchain.tools import tool from langchain_openai import ChatOpenAI from pydantic import HttpUrl @tool def ping(url: HttpUrl, return_error: bool) -> str: """Ping the fully specified url. Must include https:// in the url.""" hostname = urlparse(str(url)).netloc completed_process = subprocess.run( ["ping", "-c", "1", hostname], capture_output=True, text=True ) output = completed_process.stdout if return_error and completed_process.returncode != 0: return completed_process.stderr return output @tool def trace_route(url: HttpUrl, return_error: bool) -> str: """Trace the route to the specified url. Must include https:// in the url.""" hostname = urlparse(str(url)).netloc completed_process = subprocess.run( ["traceroute", hostname], capture_output=True, text=True ) output = completed_process.stdout if return_error and completed_process.returncode != 0: return completed_process.stderr return output llm = ChatOpenAI(model="gpt-3.5-turbo-0613", temperature=0) agent = initialize_agent( llm=llm, tools=[ping, trace_route], agent=AgentType.OPENAI_MULTI_FUNCTIONS, return_intermediate_steps=True, # IMPORTANT! ) result = agent("What's the latency like for https://langchain.com?") evaluation_result = evaluator.evaluate_agent_trajectory( prediction=result["output"], input=result["input"], agent_trajectory=result["intermediate_steps"], ) evaluation_result get_ipython().run_line_magic('pip', 'install --upgrade --quiet anthropic') from langchain_community.chat_models import ChatAnthropic eval_llm =
ChatAnthropic(temperature=0)
langchain_community.chat_models.ChatAnthropic
from langchain.globals import set_llm_cache from langchain_openai import OpenAI llm = OpenAI(model_name="gpt-3.5-turbo-instruct", n=2, best_of=2) get_ipython().run_cell_magic('time', '', 'from langchain.cache import InMemoryCache\n\nset_llm_cache(InMemoryCache())\n\n# The first time, it is not yet in cache, so it should take longer\nllm.predict("Tell me a joke")\n') get_ipython().run_cell_magic('time', '', '# The second time it is, so it goes faster\nllm.predict("Tell me a joke")\n') get_ipython().system('rm .langchain.db') from langchain.cache import SQLiteCache set_llm_cache(
SQLiteCache(database_path=".langchain.db")
langchain.cache.SQLiteCache
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")
langchain.llms.bedrock.Bedrock
from typing import Any, Dict, List from langchain.chains import ConversationChain from langchain.schema import BaseMemory from langchain_openai import OpenAI from pydantic import BaseModel get_ipython().run_line_magic('pip', 'install --upgrade --quiet spacy') import spacy nlp = spacy.load("en_core_web_lg") class SpacyEntityMemory(BaseMemory, BaseModel): """Memory class for storing information about entities.""" entities: dict = {} memory_key: str = "entities" def clear(self): self.entities = {} @property def memory_variables(self) -> List[str]: """Define the variables we are providing to the prompt.""" return [self.memory_key] def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, str]: """Load the memory variables, in this case the entity key.""" doc = nlp(inputs[list(inputs.keys())[0]]) entities = [ self.entities[str(ent)] for ent in doc.ents if str(ent) in self.entities ] return {self.memory_key: "\n".join(entities)} def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None: """Save context from this conversation to buffer.""" text = inputs[list(inputs.keys())[0]] doc = nlp(text) for ent in doc.ents: ent_str = str(ent) if ent_str in self.entities: self.entities[ent_str] += f"\n{text}" else: self.entities[ent_str] = text from langchain.prompts.prompt import PromptTemplate template = """The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. You are provided with information about entities the Human mentions, if relevant. Relevant entity information: {entities} Conversation: Human: {input} AI:""" prompt =
PromptTemplate(input_variables=["entities", "input"], template=template)
langchain.prompts.prompt.PromptTemplate
from langchain.agents import load_tools requests_tools =
load_tools(["requests_all"])
langchain.agents.load_tools
get_ipython().system(' pip install langchain unstructured[all-docs] pydantic lxml') path = "/Users/rlm/Desktop/Papers/LLaVA/" from typing import Any from pydantic import BaseModel from unstructured.partition.pdf import partition_pdf raw_pdf_elements = partition_pdf( filename=path + "LLaVA.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, ) 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() texts = [i.text for i in text_elements] text_summaries = summarize_chain.batch(texts, {"max_concurrency": 5}) tables = [i.text for i in table_elements] table_summaries = summarize_chain.batch(tables, {"max_concurrency": 5}) get_ipython().run_cell_magic('bash', '', '\n# Define the directory containing the images\nIMG_DIR=~/Desktop/Papers/LLaVA/\n\n# Loop through each image in the directory\nfor img in "${IMG_DIR}"*.jpg; do\n # Extract the base name of the image without extension\n base_name=$(basename "$img" .jpg)\n\n # Define the output file name based on the image name\n output_file="${IMG_DIR}${base_name}.txt"\n\n # Execute the command and save the output to the defined output file\n /Users/rlm/Desktop/Code/llama.cpp/bin/llava -m ../models/llava-7b/ggml-model-q5_k.gguf --mmproj ../models/llava-7b/mmproj-model-f16.gguf --temp 0.1 -p "Describe the image in detail. Be specific about graphs, such as bar plots." --image "$img" > "$output_file"\n\ndone\n') import glob import os file_paths = glob.glob(os.path.expanduser(os.path.join(path, "*.txt"))) img_summaries = [] for file_path in file_paths: with open(file_path, "r") as file: img_summaries.append(file.read()) logging_header = "clip_model_load: total allocated memory: 201.27 MB\n\n" cleaned_img_summary = [s.split(logging_header, 1)[1].strip() for s in img_summaries] 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()
langchain_openai.OpenAIEmbeddings
from langchain_core.pydantic_v1 import BaseModel, Field class Joke(BaseModel): setup: str = Field(description="The setup of the joke") punchline: str =
Field(description="The punchline to the joke")
langchain_core.pydantic_v1.Field
from langchain.memory import ConversationKGMemory from langchain_openai import OpenAI llm = OpenAI(temperature=0) memory = ConversationKGMemory(llm=llm) memory.save_context({"input": "say hi to sam"}, {"output": "who is sam"}) memory.save_context({"input": "sam is a friend"}, {"output": "okay"}) memory.load_memory_variables({"input": "who is sam"}) memory = ConversationKGMemory(llm=llm, return_messages=True) memory.save_context({"input": "say hi to sam"}, {"output": "who is sam"}) memory.save_context({"input": "sam is a friend"}, {"output": "okay"}) memory.load_memory_variables({"input": "who is sam"}) memory.get_current_entities("what's Sams favorite color?") memory.get_knowledge_triplets("her favorite color is red") llm =
OpenAI(temperature=0)
langchain_openai.OpenAI
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()
langchain_core.runnables.RunnablePassthrough
get_ipython().run_line_magic('pip', 'install --upgrade --quiet html2text') from langchain_community.document_loaders import AsyncHtmlLoader urls = ["https://www.espn.com", "https://lilianweng.github.io/posts/2023-06-23-agent/"] loader = AsyncHtmlLoader(urls) docs = loader.load() from langchain_community.document_transformers import Html2TextTransformer urls = ["https://www.espn.com", "https://lilianweng.github.io/posts/2023-06-23-agent/"] html2text =
Html2TextTransformer()
langchain_community.document_transformers.Html2TextTransformer
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" ) documents = loader.load() documents import boto3 textract_client = boto3.client("textract", region_name="us-east-2") file_path = "s3://amazon-textract-public-content/langchain/layout-parser-paper.pdf" loader = AmazonTextractPDFLoader(file_path, client=textract_client) documents = loader.load() len(documents) import os os.environ["OPENAI_API_KEY"] = "your-OpenAI-API-key" from langchain.chains.question_answering import load_qa_chain from langchain_openai import OpenAI chain = load_qa_chain(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"])
langchain_experimental.rl_chain.BasedOn
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)
langchain_experimental.rl_chain.ToSelectFrom
from typing import List from langchain.output_parsers import PydanticOutputParser from langchain.prompts import PromptTemplate from langchain_core.pydantic_v1 import BaseModel, Field, validator from langchain_openai import ChatOpenAI model = ChatOpenAI(temperature=0) class Joke(BaseModel): setup: str = Field(description="question to set up a joke") punchline: str = Field(description="answer to resolve the joke") @
validator("setup")
langchain_core.pydantic_v1.validator
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) llm_math_chain = LLMMathChain.from_llm(llm=llm, verbose=True) tools = [ Tool( name="Search", func=search.run, description="useful for when you need to answer questions about current events", ), Tool( name="Calculator", func=llm_math_chain.run, description="useful for when you need to answer questions about math", ), ] model =
ChatOpenAI(temperature=0)
langchain_openai.ChatOpenAI
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"])
langchain_community.tools.edenai.EdenAiObjectDetectionTool
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") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.split_documents(documents) embeddings =
OpenAIEmbeddings()
langchain_openai.OpenAIEmbeddings
import os os.environ["OPENAI_API_KEY"] = "..." from langchain.prompts import PromptTemplate from langchain_experimental.smart_llm import SmartLLMChain from langchain_openai import ChatOpenAI hard_question = "I have a 12 liter jug and a 6 liter jug. I want to measure 6 liters. How do I do it?" prompt = PromptTemplate.from_template(hard_question) llm = ChatOpenAI(temperature=0, model_name="gpt-4") chain = SmartLLMChain(llm=llm, prompt=prompt, n_ideas=3, verbose=True) chain.invoke({}) chain = SmartLLMChain( ideation_llm=
ChatOpenAI(temperature=0.9, model_name="gpt-4")
langchain_openai.ChatOpenAI
get_ipython().run_line_magic('pip', 'install --upgrade --quiet llama-cpp-python') get_ipython().system('CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 pip install llama-cpp-python') get_ipython().system('CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 pip install --upgrade --force-reinstall llama-cpp-python --no-cache-dir') get_ipython().system('CMAKE_ARGS="-DLLAMA_METAL=on" FORCE_CMAKE=1 pip install llama-cpp-python') get_ipython().system('CMAKE_ARGS="-DLLAMA_METAL=on" FORCE_CMAKE=1 pip install --upgrade --force-reinstall llama-cpp-python --no-cache-dir') get_ipython().system('python -m pip install -e . --force-reinstall --no-cache-dir') from langchain.callbacks.manager import CallbackManager from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain_community.llms import LlamaCpp template = """Question: {question} Answer: Let's work this out in a step by step way to be sure we have the right answer.""" prompt =
PromptTemplate.from_template(template)
langchain.prompts.PromptTemplate.from_template
from langchain_community.document_loaders import AcreomLoader loader =
AcreomLoader("<path-to-acreom-vault>", collect_metadata=False)
langchain_community.document_loaders.AcreomLoader
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()
langchain_openai.OpenAIEmbeddings
get_ipython().system('pip3 install tcvectordb') from langchain_community.document_loaders import TextLoader from langchain_community.embeddings.fake import FakeEmbeddings from langchain_community.vectorstores import TencentVectorDB from langchain_community.vectorstores.tencentvectordb import ConnectionParams 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 =
FakeEmbeddings(size=128)
langchain_community.embeddings.fake.FakeEmbeddings
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-core langchain-experimental langchain-openai') from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ( ChatPromptTemplate, ) from langchain_experimental.utilities import PythonREPL from langchain_openai import ChatOpenAI template = """Write some python code to solve the user's problem. Return only python code in Markdown format, e.g.: ```python .... ```""" prompt = ChatPromptTemplate.from_messages([("system", template), ("human", "{input}")]) model = ChatOpenAI() def _sanitize_output(text: str): _, after = text.split("```python") return after.split("```")[0] chain = prompt | model | StrOutputParser() | _sanitize_output |
PythonREPL()
langchain_experimental.utilities.PythonREPL
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}")
langchain_core.prompts.ChatPromptTemplate.from_messages
from langchain.agents import AgentType, initialize_agent, load_tools from langchain_openai import OpenAI llm = OpenAI(temperature=0) tools =
load_tools(["google-serper"], llm=llm)
langchain.agents.load_tools
from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate, FewShotChatMessagePromptTemplate from langchain_core.runnables import RunnableLambda from langchain_openai import ChatOpenAI examples = [ { "input": "Could the members of The Police perform lawful arrests?", "output": "what can the members of The Police do?", }, { "input": "Jan Sindel’s was born in what country?", "output": "what is Jan Sindel’s personal history?", }, ] example_prompt =
ChatPromptTemplate.from_messages( [ ("human", "{input}")
langchain_core.prompts.ChatPromptTemplate.from_messages
get_ipython().run_line_magic('pip', 'install --upgrade --quiet lxml') get_ipython().run_line_magic('pip', 'install --upgrade --quiet html2text') from langchain_community.document_loaders import EverNoteLoader loader = EverNoteLoader("example_data/testing.enex") loader.load() loader =
EverNoteLoader("example_data/testing.enex", load_single_document=False)
langchain_community.document_loaders.EverNoteLoader
import os os.environ["SEARCHAPI_API_KEY"] = "" from langchain_community.utilities import SearchApiAPIWrapper search =
SearchApiAPIWrapper()
langchain_community.utilities.SearchApiAPIWrapper
import os os.environ["SCENEX_API_KEY"] = "<YOUR_API_KEY>" from langchain.agents import load_tools tools =
load_tools(["sceneXplain"])
langchain.agents.load_tools
import os os.environ["OCTOAI_API_TOKEN"] = "OCTOAI_API_TOKEN" os.environ["ENDPOINT_URL"] = "https://text.octoai.run/v1/chat/completions" from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain_community.llms.octoai_endpoint import OctoAIEndpoint template = """Below is an instruction that describes a task. Write a response that appropriately completes the request.\n Instruction:\n{question}\n Response: """ prompt = PromptTemplate.from_template(template) llm = OctoAIEndpoint( model_kwargs={ "model": "llama-2-13b-chat-fp16", "max_tokens": 128, "presence_penalty": 0, "temperature": 0.1, "top_p": 0.9, "messages": [ { "role": "system", "content": "You are a helpful assistant. Keep your responses limited to one short paragraph if possible.", }, ], }, ) question = "Who was leonardo davinci?" llm_chain =
LLMChain(prompt=prompt, llm=llm)
langchain.chains.LLMChain
from langchain_community.document_loaders import WebBaseLoader loader = WebBaseLoader("https://www.espn.com/") data = loader.load() data """ import requests from bs4 import BeautifulSoup html_doc = requests.get("{INSERT_NEW_URL_HERE}") soup = BeautifulSoup(html_doc.text, 'html.parser') """ loader = WebBaseLoader(["https://www.espn.com/", "https://google.com"]) docs = loader.load() docs get_ipython().run_line_magic('pip', 'install --upgrade --quiet nest_asyncio') import nest_asyncio nest_asyncio.apply() loader =
WebBaseLoader(["https://www.espn.com/", "https://google.com"])
langchain_community.document_loaders.WebBaseLoader
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) comet_callback = CometCallbackHandler( project_name="comet-example-langchain", complexity_metrics=False, stream_logs=True, tags=["custom_metrics"], custom_metrics=rouge_score.compute_metric, ) callbacks = [StdOutCallbackHandler(), comet_callback] llm =
OpenAI(temperature=0.9)
langchain_openai.OpenAI
get_ipython().run_line_magic('pip', 'install --upgrade --quiet wandb') get_ipython().run_line_magic('pip', 'install --upgrade --quiet pandas') get_ipython().run_line_magic('pip', 'install --upgrade --quiet textstat') get_ipython().run_line_magic('pip', 'install --upgrade --quiet spacy') get_ipython().system('python -m spacy download en_core_web_sm') import os os.environ["WANDB_API_KEY"] = "" from datetime import datetime from langchain.callbacks import StdOutCallbackHandler, WandbCallbackHandler from langchain_openai import OpenAI """Main function. This function is used to try the callback handler. Scenarios: 1. OpenAI LLM 2. Chain with multiple SubChains on multiple generations 3. Agent with Tools """ session_group = datetime.now().strftime("%m.%d.%Y_%H.%M.%S") wandb_callback = WandbCallbackHandler( job_type="inference", project="langchain_callback_demo", group=f"minimal_{session_group}", name="llm", tags=["test"], ) callbacks = [StdOutCallbackHandler(), wandb_callback] llm = OpenAI(temperature=0, callbacks=callbacks) llm_result = llm.generate(["Tell me a joke", "Tell me a poem"] * 3) wandb_callback.flush_tracker(llm, name="simple_sequential") 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) synopsis_chain = LLMChain(llm=llm, prompt=prompt_template, callbacks=callbacks) test_prompts = [ { "title": "documentary about good video games that push the boundary of game design" }, {"title": "cocaine bear vs heroin wolf"}, {"title": "the best in class mlops tooling"}, ] synopsis_chain.apply(test_prompts) wandb_callback.flush_tracker(synopsis_chain, name="agent") from langchain.agents import AgentType, initialize_agent, load_tools tools =
load_tools(["serpapi", "llm-math"], llm=llm)
langchain.agents.load_tools
from langchain.chains import RetrievalQAWithSourcesChain from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores.jaguar import Jaguar from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate from langchain_core.runnables import RunnablePassthrough from langchain_openai import ChatOpenAI, OpenAI, OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter """ Load a text file into a set of documents """ loader = TextLoader("../../modules/state_of_the_union.txt") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=300) docs = text_splitter.split_documents(documents) """ Instantiate a Jaguar vector store """ url = "http://192.168.5.88:8080/fwww/" embeddings =
OpenAIEmbeddings()
langchain_openai.OpenAIEmbeddings
get_ipython().run_line_magic('pip', 'install --upgrade --quiet hologres-vector') from langchain_community.vectorstores import Hologres 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)
langchain_text_splitters.CharacterTextSplitter
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain langchain-openai') from langchain.evaluation import load_evaluator eval_chain =
load_evaluator("pairwise_string")
langchain.evaluation.load_evaluator
get_ipython().run_line_magic('pip', 'install --upgrade --quiet atlassian-python-api') from langchain_community.document_loaders import ConfluenceLoader loader = ConfluenceLoader( url="https://yoursite.atlassian.com/wiki", username="me", api_key="12345" ) documents = loader.load(space_key="SPACE", include_attachments=True, limit=50) from langchain_community.document_loaders import ConfluenceLoader loader =
ConfluenceLoader(url="https://yoursite.atlassian.com/wiki", token="12345")
langchain_community.document_loaders.ConfluenceLoader
get_ipython().run_line_magic('pip', 'install -upgrade --quiet langchain-google-memorystore-redis') PROJECT_ID = "my-project-id" # @param {type:"string"} get_ipython().system('gcloud config set project {PROJECT_ID}') from google.colab import auth auth.authenticate_user() import redis from langchain_google_memorystore_redis import ( DistanceStrategy, HNSWConfig, RedisVectorStore, ) redis_client = redis.from_url("redis://127.0.0.1:6379") index_config = HNSWConfig( name="my_vector_index", distance_strategy=DistanceStrategy.COSINE, vector_size=128 ) RedisVectorStore.init_index(client=redis_client, index_config=index_config) from langchain_community.document_loaders import TextLoader from langchain_text_splitters import CharacterTextSplitter loader =
TextLoader("./state_of_the_union.txt")
langchain_community.document_loaders.TextLoader
get_ipython().run_line_magic('pip', 'install --upgrade --quiet arxiv') from langchain import hub from langchain.agents import AgentExecutor, create_react_agent, load_tools from langchain_openai import ChatOpenAI llm = ChatOpenAI(temperature=0.0) tools = load_tools( ["arxiv"], ) prompt =
hub.pull("hwchase17/react")
langchain.hub.pull
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) summary_chain = LLMChain( llm=OpenAI(), prompt=prompt, verbose=True, memory=readonlymemory, # use the read-only memory to prevent the tool from modifying the memory ) search = GoogleSearchAPIWrapper() tools = [ Tool( name="Search", func=search.run, description="useful for when you need to answer questions about current events", ), Tool( name="Summary", func=summary_chain.run, description="useful for when you summarize a conversation. The input to this tool should be a string, representing who will read this summary.", ), ] prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:""" suffix = """Begin!" {chat_history} Question: {input} {agent_scratchpad}""" prompt = ZeroShotAgent.create_prompt( tools, prefix=prefix, suffix=suffix, input_variables=["input", "chat_history", "agent_scratchpad"], ) llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt) agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True) agent_chain = AgentExecutor.from_agent_and_tools( agent=agent, tools=tools, verbose=True, memory=memory ) agent_chain.run(input="What is ChatGPT?") agent_chain.run(input="Who developed it?") agent_chain.run( input="Thanks. Summarize the conversation, for my daughter 5 years old." ) print(agent_chain.memory.buffer) 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") summary_chain = LLMChain( llm=OpenAI(), prompt=prompt, verbose=True, memory=memory, # <--- this is the only change ) search = GoogleSearchAPIWrapper() tools = [ Tool( name="Search", func=search.run, description="useful for when you need to answer questions about current events", ), Tool( name="Summary", func=summary_chain.run, description="useful for when you summarize a conversation. The input to this tool should be a string, representing who will read this summary.", ), ] prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:""" suffix = """Begin!" {chat_history} Question: {input} {agent_scratchpad}""" prompt = ZeroShotAgent.create_prompt( tools, prefix=prefix, suffix=suffix, input_variables=["input", "chat_history", "agent_scratchpad"], ) llm_chain = LLMChain(llm=
OpenAI(temperature=0)
langchain_openai.OpenAI
import os from langchain_community.utilities import OpenWeatherMapAPIWrapper os.environ["OPENWEATHERMAP_API_KEY"] = "" weather = OpenWeatherMapAPIWrapper() weather_data = weather.run("London,GB") print(weather_data) import os from langchain.agents import AgentType, initialize_agent, load_tools from langchain_openai import OpenAI os.environ["OPENAI_API_KEY"] = "" os.environ["OPENWEATHERMAP_API_KEY"] = "" llm = OpenAI(temperature=0) tools =
load_tools(["openweathermap-api"], llm)
langchain.agents.load_tools
get_ipython().run_line_magic('pip', 'install --upgrade --quiet arxiv') from langchain import hub from langchain.agents import AgentExecutor, create_react_agent, load_tools from langchain_openai import ChatOpenAI llm = ChatOpenAI(temperature=0.0) tools = load_tools( ["arxiv"], ) prompt = hub.pull("hwchase17/react") agent = create_react_agent(llm, tools, prompt) agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True) agent_executor.invoke( { "input": "What's the paper 1605.08386 about?", } ) from langchain_community.utilities import ArxivAPIWrapper arxiv =
ArxivAPIWrapper()
langchain_community.utilities.ArxivAPIWrapper
get_ipython().run_line_magic('pip', 'install --upgrade --quiet pymysql') from langchain.chains import RetrievalQA from langchain_community.document_loaders import ( DirectoryLoader, UnstructuredMarkdownLoader, ) from langchain_community.vectorstores import StarRocks from langchain_community.vectorstores.starrocks import StarRocksSettings from langchain_openai import OpenAI, OpenAIEmbeddings from langchain_text_splitters import TokenTextSplitter update_vectordb = False loader = DirectoryLoader( "./docs", glob="**/*.md", loader_cls=UnstructuredMarkdownLoader ) documents = loader.load() text_splitter = TokenTextSplitter(chunk_size=400, chunk_overlap=50) split_docs = text_splitter.split_documents(documents) update_vectordb = True split_docs[-20] print("# docs = %d, # splits = %d" % (len(documents), len(split_docs))) def gen_starrocks(update_vectordb, embeddings, settings): if update_vectordb: docsearch = StarRocks.from_documents(split_docs, embeddings, config=settings) else: docsearch = StarRocks(embeddings, settings) return docsearch embeddings = OpenAIEmbeddings() settings = StarRocksSettings() settings.port = 41003 settings.host = "127.0.0.1" settings.username = "root" settings.password = "" settings.database = "zya" docsearch = gen_starrocks(update_vectordb, embeddings, settings) print(docsearch) update_vectordb = False llm =
OpenAI()
langchain_openai.OpenAI
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)
langchain_openai.ChatOpenAI
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()) question = "What are the approaches to Task Decomposition?" docs = vectorstore.similarity_search(question) len(docs) docs[0] get_ipython().run_line_magic('pip', 'install --upgrade --quiet llama-cpp-python') get_ipython().system(' CMAKE_ARGS="-DLLAMA_METAL=on" FORCE_CMAKE=1 /Users/rlm/miniforge3/envs/llama/bin/pip install -U llama-cpp-python --no-cache-dir') from langchain_community.llms import LlamaCpp n_gpu_layers = 1 # Metal set to 1 is enough. n_batch = 512 # Should be between 1 and n_ctx, consider the amount of RAM of your Apple Silicon Chip. llm = LlamaCpp( model_path="/Users/rlm/Desktop/Code/llama.cpp/models/llama-2-13b-chat.ggufv3.q4_0.bin", n_gpu_layers=n_gpu_layers, n_batch=n_batch, n_ctx=2048, f16_kv=True, # MUST set to True, otherwise you will run into problem after a couple of calls verbose=True, ) llm.invoke("Simulate a rap battle between Stephen Colbert and John Oliver") from langchain_community.llms import GPT4All gpt4all = GPT4All( model="/Users/rlm/Desktop/Code/gpt4all/models/nous-hermes-13b.ggmlv3.q4_0.bin", max_tokens=2048, ) from langchain_community.llms.llamafile import Llamafile llamafile = Llamafile() llamafile.invoke("Here is my grandmother's beloved recipe for spaghetti and meatballs:") from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import PromptTemplate prompt = PromptTemplate.from_template( "Summarize the main themes in these retrieved docs: {docs}" ) def format_docs(docs): return "\n\n".join(doc.page_content for doc in docs) chain = {"docs": format_docs} | prompt | llm | StrOutputParser() question = "What are the approaches to Task Decomposition?" docs = vectorstore.similarity_search(question) chain.invoke(docs) from langchain import hub rag_prompt = hub.pull("rlm/rag-prompt") rag_prompt.messages from langchain_core.runnables import RunnablePassthrough, RunnablePick chain = ( RunnablePassthrough.assign(context=RunnablePick("context") | format_docs) | rag_prompt | llm |
StrOutputParser()
langchain_core.output_parsers.StrOutputParser
get_ipython().run_line_magic('pip', "install --upgrade --quiet langchain-openai 'deeplake[enterprise]' tiktoken") from langchain_community.vectorstores import DeepLake from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") activeloop_token = getpass.getpass("activeloop token:") embeddings = OpenAIEmbeddings() from langchain_community.document_loaders import TextLoader loader = TextLoader("../../modules/state_of_the_union.txt") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings() db = DeepLake(dataset_path="./my_deeplake/", embedding=embeddings, overwrite=True) db.add_documents(docs) query = "What did the president say about Ketanji Brown Jackson" docs = db.similarity_search(query) print(docs[0].page_content) db = DeepLake(dataset_path="./my_deeplake/", embedding=embeddings, read_only=True) docs = db.similarity_search(query) from langchain.chains import RetrievalQA from langchain_openai import OpenAIChat qa = RetrievalQA.from_chain_type( llm=OpenAIChat(model="gpt-3.5-turbo"), chain_type="stuff", retriever=db.as_retriever(), ) query = "What did the president say about Ketanji Brown Jackson" qa.run(query) import random for d in docs: d.metadata["year"] = random.randint(2012, 2014) db = DeepLake.from_documents( docs, embeddings, dataset_path="./my_deeplake/", overwrite=True ) db.similarity_search( "What did the president say about Ketanji Brown Jackson", filter={"metadata": {"year": 2013}}, ) db.similarity_search( "What did the president say about Ketanji Brown Jackson?", distance_metric="cos" ) db.max_marginal_relevance_search( "What did the president say about Ketanji Brown Jackson?" ) db.delete_dataset() DeepLake.force_delete_by_path("./my_deeplake") os.environ["ACTIVELOOP_TOKEN"] = activeloop_token username = "<USERNAME_OR_ORG>" # your username on app.activeloop.ai dataset_path = f"hub://{username}/langchain_testing_python" # could be also ./local/path (much faster locally), s3://bucket/path/to/dataset, gcs://path/to/dataset, etc. docs = text_splitter.split_documents(documents) embedding =
OpenAIEmbeddings()
langchain_openai.OpenAIEmbeddings
get_ipython().run_line_magic('pip', 'install --upgrade --quiet transformers huggingface_hub > /dev/null') from langchain.agents import load_huggingface_tool tool =
load_huggingface_tool("lysandre/hf-model-downloads")
langchain.agents.load_huggingface_tool
SOURCE = "test" # @param {type:"Query"|"CollectionGroup"|"DocumentReference"|"string"} get_ipython().run_line_magic('pip', 'install -upgrade --quiet langchain-google-datastore') PROJECT_ID = "my-project-id" # @param {type:"string"} get_ipython().system('gcloud config set project {PROJECT_ID}') from google.colab import auth auth.authenticate_user() get_ipython().system('gcloud services enable datastore.googleapis.com') from langchain_core.documents import Document from langchain_google_datastore import DatastoreSaver data = [Document(page_content="Hello, World!")] saver = DatastoreSaver() saver.upsert_documents(data) saver = DatastoreSaver("Collection") saver.upsert_documents(data) doc_ids = ["AnotherCollection/doc_id", "foo/bar"] saver = DatastoreSaver() saver.upsert_documents(documents=data, document_ids=doc_ids) from langchain_google_datastore import DatastoreLoader loader_collection = DatastoreLoader("Collection") loader_subcollection = DatastoreLoader("Collection/doc/SubCollection") data_collection = loader_collection.load() data_subcollection = loader_subcollection.load() from google.cloud import datastore client = datastore.Client() doc_ref = client.collection("foo").document("bar") loader_document = DatastoreLoader(doc_ref) data = loader_document.load() from google.cloud.datastore import CollectionGroup, FieldFilter, Query col_ref = client.collection("col_group") collection_group = CollectionGroup(col_ref) loader_group = DatastoreLoader(collection_group) col_ref = client.collection("collection") query = col_ref.where(filter=FieldFilter("region", "==", "west_coast")) loader_query = DatastoreLoader(query) saver =
DatastoreSaver()
langchain_google_datastore.DatastoreSaver
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() docs_transformed = html2text.transform_documents(docs) from langchain_text_splitters import RecursiveCharacterTextSplitter chunk_size = 4096 docs_new = [] text_splitter = RecursiveCharacterTextSplitter( chunk_size=chunk_size, ) for doc in docs_transformed: if len(doc.page_content) < chunk_size: docs_new.append(doc) else: docs = text_splitter.create_documents([doc.page_content]) docs_new.extend(docs) docs = db.add_documents(docs_new) from typing import List from langchain.chains.openai_functions import ( create_structured_output_chain, ) from langchain_core.messages import HumanMessage, SystemMessage from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate from langchain_openai import ChatOpenAI from pydantic import BaseModel, Field docs = db.vectorstore.dataset.text.data(fetch_chunks=True, aslist=True)["value"] ids = db.vectorstore.dataset.id.data(fetch_chunks=True, aslist=True)["value"] llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0) class Questions(BaseModel): """Identifying information about a person.""" question: str = Field(..., description="Questions about text") prompt_msgs = [ SystemMessage( content="You are a world class expert for generating questions based on provided context. \ You make sure the question can be answered by the text." ), HumanMessagePromptTemplate.from_template( "Use the given text to generate a question from the following input: {input}" ), HumanMessage(content="Tips: Make sure to answer in the correct format"), ] prompt = ChatPromptTemplate(messages=prompt_msgs) chain =
create_structured_output_chain(Questions, llm, prompt, verbose=True)
langchain.chains.openai_functions.create_structured_output_chain
get_ipython().system('pip install gymnasium') import tenacity from langchain.output_parsers import RegexParser from langchain.schema import ( HumanMessage, SystemMessage, ) class GymnasiumAgent: @classmethod def get_docs(cls, env): return env.unwrapped.__doc__ def __init__(self, model, env): self.model = model self.env = env self.docs = self.get_docs(env) self.instructions = """ Your goal is to maximize your return, i.e. the sum of the rewards you receive. I will give you an observation, reward, terminiation flag, truncation flag, and the return so far, formatted as: Observation: <observation> Reward: <reward> Termination: <termination> Truncation: <truncation> Return: <sum_of_rewards> You will respond with an action, formatted as: Action: <action> where you replace <action> with your actual action. Do nothing else but return the action. """ self.action_parser = RegexParser( regex=r"Action: (.*)", output_keys=["action"], default_output_key="action" ) self.message_history = [] self.ret = 0 def random_action(self): action = self.env.action_space.sample() return action def reset(self): self.message_history = [ SystemMessage(content=self.docs),
SystemMessage(content=self.instructions)
langchain.schema.SystemMessage
import os os.environ["BING_SUBSCRIPTION_KEY"] = "<key>" os.environ["BING_SEARCH_URL"] = "https://api.bing.microsoft.com/v7.0/search" from langchain_community.utilities import BingSearchAPIWrapper search = BingSearchAPIWrapper() search.run("python") search = BingSearchAPIWrapper(k=1) search.run("python") search =
BingSearchAPIWrapper()
langchain_community.utilities.BingSearchAPIWrapper
from langchain.docstore.document import Document text = "..... put the text you copy pasted here......" doc =
Document(page_content=text)
langchain.docstore.document.Document
URL = "" # Your Fiddler instance URL, Make sure to include the full URL (including https://). For example: https://demo.fiddler.ai ORG_NAME = "" AUTH_TOKEN = "" # Your Fiddler instance auth token PROJECT_NAME = "" MODEL_NAME = "" # Model name in Fiddler from langchain_community.callbacks.fiddler_callback import FiddlerCallbackHandler fiddler_handler = FiddlerCallbackHandler( url=URL, org=ORG_NAME, project=PROJECT_NAME, model=MODEL_NAME, api_key=AUTH_TOKEN, ) from langchain_core.output_parsers import StrOutputParser from langchain_openai import OpenAI llm = OpenAI(temperature=0, streaming=True, callbacks=[fiddler_handler]) output_parser = StrOutputParser() chain = llm | output_parser chain.invoke("How far is moon from earth?") chain.invoke("What is the temperature on Mars?") chain.invoke("How much is 2 + 200000?") chain.invoke("Which movie won the oscars this year?") chain.invoke("Can you write me a poem about insomnia?") chain.invoke("How are you doing today?") chain.invoke("What is the meaning of life?") from langchain.prompts import ( ChatPromptTemplate, FewShotChatMessagePromptTemplate, ) examples = [ {"input": "2+2", "output": "4"}, {"input": "2+3", "output": "5"}, ] example_prompt = ChatPromptTemplate.from_messages( [ ("human", "{input}"), ("ai", "{output}"), ] ) few_shot_prompt = FewShotChatMessagePromptTemplate( example_prompt=example_prompt, examples=examples, ) final_prompt =
ChatPromptTemplate.from_messages( [ ("system", "You are a wondrous wizard of math.")
langchain.prompts.ChatPromptTemplate.from_messages
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) data = loader.load() from langchain_community.document_loaders import SeleniumURLLoader urls = [ "https://www.youtube.com/watch?v=dQw4w9WgXcQ", "https://goo.gl/maps/NDSHwePEyaHMFGwh8", ] loader =
SeleniumURLLoader(urls=urls)
langchain_community.document_loaders.SeleniumURLLoader
get_ipython().run_line_magic('pip', 'install --upgrade --quiet ain-py') import os os.environ["AIN_BLOCKCHAIN_ACCOUNT_PRIVATE_KEY"] = "" import os from ain.account import Account if os.environ.get("AIN_BLOCKCHAIN_ACCOUNT_PRIVATE_KEY", None): account = Account(os.environ["AIN_BLOCKCHAIN_ACCOUNT_PRIVATE_KEY"]) else: account = Account.create() os.environ["AIN_BLOCKCHAIN_ACCOUNT_PRIVATE_KEY"] = account.private_key print( f""" address: {account.address} private_key: {account.private_key} """ ) from langchain_community.agent_toolkits.ainetwork.toolkit import AINetworkToolkit toolkit = AINetworkToolkit() tools = toolkit.get_tools() address = tools[0].interface.wallet.defaultAccount.address from langchain.agents import AgentType, initialize_agent from langchain_openai import ChatOpenAI llm =
ChatOpenAI(temperature=0)
langchain_openai.ChatOpenAI
get_ipython().run_line_magic('pip', 'install --upgrade --quiet ipython-ngql') get_ipython().run_line_magic('load_ext', 'ngql') get_ipython().run_line_magic('ngql', '--address 127.0.0.1 --port 9669 --user root --password nebula') get_ipython().run_line_magic('ngql', 'CREATE SPACE IF NOT EXISTS langchain(partition_num=1, replica_factor=1, vid_type=fixed_string(128));') get_ipython().run_line_magic('ngql', 'USE langchain;') get_ipython().run_cell_magic('ngql', '', 'CREATE TAG IF NOT EXISTS movie(name string);\nCREATE TAG IF NOT EXISTS person(name string, birthdate string);\nCREATE EDGE IF NOT EXISTS acted_in();\nCREATE TAG INDEX IF NOT EXISTS person_index ON person(name(128));\nCREATE TAG INDEX IF NOT EXISTS movie_index ON movie(name(128));\n') get_ipython().run_cell_magic('ngql', '', 'INSERT VERTEX person(name, birthdate) VALUES "Al Pacino":("Al Pacino", "1940-04-25");\nINSERT VERTEX movie(name) VALUES "The Godfather II":("The Godfather II");\nINSERT VERTEX movie(name) VALUES "The Godfather Coda: The Death of Michael Corleone":("The Godfather Coda: The Death of Michael Corleone");\nINSERT EDGE acted_in() VALUES "Al Pacino"->"The Godfather II":();\nINSERT EDGE acted_in() VALUES "Al Pacino"->"The Godfather Coda: The Death of Michael Corleone":();\n') from langchain.chains import NebulaGraphQAChain from langchain_community.graphs import NebulaGraph from langchain_openai import ChatOpenAI graph = NebulaGraph( space="langchain", username="root", password="nebula", address="127.0.0.1", port=9669, session_pool_size=30, ) print(graph.get_schema) chain = NebulaGraphQAChain.from_llm(
ChatOpenAI(temperature=0)
langchain_openai.ChatOpenAI
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain langchain-experimental langchain-openai neo4j wikipedia') from langchain_experimental.graph_transformers.diffbot import DiffbotGraphTransformer diffbot_api_key = "DIFFBOT_API_KEY" diffbot_nlp =
DiffbotGraphTransformer(diffbot_api_key=diffbot_api_key)
langchain_experimental.graph_transformers.diffbot.DiffbotGraphTransformer
get_ipython().run_line_magic('pip', 'install --upgrade --quiet timescale-vector') get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-openai') get_ipython().run_line_magic('pip', 'install --upgrade --quiet tiktoken') import os from dotenv import find_dotenv, load_dotenv _ = load_dotenv(find_dotenv()) OPENAI_API_KEY = os.environ["OPENAI_API_KEY"] from typing import Tuple from datetime import datetime, timedelta from langchain.docstore.document import Document from langchain_community.document_loaders import TextLoader from langchain_community.document_loaders.json_loader import JSONLoader from langchain_community.vectorstores.timescalevector import TimescaleVector from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter loader = TextLoader("../../../extras/modules/state_of_the_union.txt") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings() SERVICE_URL = os.environ["TIMESCALE_SERVICE_URL"] COLLECTION_NAME = "state_of_the_union_test" db = TimescaleVector.from_documents( embedding=embeddings, documents=docs, collection_name=COLLECTION_NAME, service_url=SERVICE_URL, ) query = "What did the president say about Ketanji Brown Jackson" docs_with_score = db.similarity_search_with_score(query) for doc, score in docs_with_score: print("-" * 80) print("Score: ", score) print(doc.page_content) print("-" * 80) retriever = db.as_retriever() print(retriever) from langchain_openai import ChatOpenAI llm = ChatOpenAI(temperature=0.1, model="gpt-3.5-turbo-16k") from langchain.chains import RetrievalQA qa_stuff = RetrievalQA.from_chain_type( llm=llm, chain_type="stuff", retriever=retriever, verbose=True, ) query = "What did the president say about Ketanji Brown Jackson?" response = qa_stuff.run(query) print(response) from timescale_vector import client def create_uuid(date_string: str): if date_string is None: return None time_format = "%a %b %d %H:%M:%S %Y %z" datetime_obj = datetime.strptime(date_string, time_format) uuid = client.uuid_from_time(datetime_obj) return str(uuid) def split_name(input_string: str) -> Tuple[str, str]: if input_string is None: return None, None start = input_string.find("<") end = input_string.find(">") name = input_string[:start].strip() email = input_string[start + 1 : end].strip() return name, email def create_date(input_string: str) -> datetime: if input_string is None: return None month_dict = { "Jan": "01", "Feb": "02", "Mar": "03", "Apr": "04", "May": "05", "Jun": "06", "Jul": "07", "Aug": "08", "Sep": "09", "Oct": "10", "Nov": "11", "Dec": "12", } components = input_string.split() day = components[2] month = month_dict[components[1]] year = components[4] time = components[3] timezone_offset_minutes = int(components[5]) # Convert the offset to minutes timezone_hours = timezone_offset_minutes // 60 # Calculate the hours timezone_minutes = timezone_offset_minutes % 60 # Calculate the remaining minutes timestamp_tz_str = ( f"{year}-{month}-{day} {time}+{timezone_hours:02}{timezone_minutes:02}" ) return timestamp_tz_str def extract_metadata(record: dict, metadata: dict) -> dict: record_name, record_email = split_name(record["author"]) metadata["id"] = create_uuid(record["date"]) metadata["date"] = create_date(record["date"]) metadata["author_name"] = record_name metadata["author_email"] = record_email metadata["commit_hash"] = record["commit"] return metadata get_ipython().system('curl -O https://s3.amazonaws.com/assets.timescale.com/ai/ts_git_log.json') FILE_PATH = "../../../../../ts_git_log.json" loader = JSONLoader( file_path=FILE_PATH, jq_schema=".commit_history[]", text_content=False, metadata_func=extract_metadata, ) documents = loader.load() documents = [doc for doc in documents if doc.metadata["date"] is not None] print(documents[0]) NUM_RECORDS = 500 documents = documents[:NUM_RECORDS] text_splitter = CharacterTextSplitter( chunk_size=1000, chunk_overlap=200, ) docs = text_splitter.split_documents(documents) COLLECTION_NAME = "timescale_commits" embeddings = OpenAIEmbeddings() db = TimescaleVector.from_documents( embedding=embeddings, ids=[doc.metadata["id"] for doc in docs], documents=docs, collection_name=COLLECTION_NAME, service_url=SERVICE_URL, time_partition_interval=timedelta(days=7), ) start_dt = datetime(2023, 8, 1, 22, 10, 35) # Start date = 1 August 2023, 22:10:35 end_dt = datetime(2023, 8, 30, 22, 10, 35) # End date = 30 August 2023, 22:10:35 td = timedelta(days=7) # Time delta = 7 days query = "What's new with TimescaleDB functions?" docs_with_score = db.similarity_search_with_score( query, start_date=start_dt, end_date=end_dt ) for doc, score in docs_with_score: print("-" * 80) print("Score: ", score) print("Date: ", doc.metadata["date"]) print(doc.page_content) print("-" * 80) docs_with_score = db.similarity_search_with_score( query, start_date=start_dt, time_delta=td ) for doc, score in docs_with_score: print("-" * 80) print("Score: ", score) print("Date: ", doc.metadata["date"]) print(doc.page_content) print("-" * 80) docs_with_score = db.similarity_search_with_score(query, end_date=end_dt, time_delta=td) for doc, score in docs_with_score: print("-" * 80) print("Score: ", score) print("Date: ", doc.metadata["date"]) print(doc.page_content) print("-" * 80) docs_with_score = db.similarity_search_with_score(query, start_date=start_dt) for doc, score in docs_with_score: print("-" * 80) print("Score: ", score) print("Date: ", doc.metadata["date"]) print(doc.page_content) print("-" * 80) docs_with_score = db.similarity_search_with_score(query, end_date=end_dt) for doc, score in docs_with_score: print("-" * 80) print("Score: ", score) print("Date: ", doc.metadata["date"]) print(doc.page_content) print("-" * 80) retriever = db.as_retriever(search_kwargs={"start_date": start_dt, "end_date": end_dt}) from langchain_openai import ChatOpenAI llm = ChatOpenAI(temperature=0.1, model="gpt-3.5-turbo-16k") from langchain.chains import RetrievalQA qa_stuff = RetrievalQA.from_chain_type( llm=llm, chain_type="stuff", retriever=retriever, verbose=True, ) query = ( "What's new with the timescaledb functions? Tell me when these changes were made." ) response = qa_stuff.run(query) print(response) COLLECTION_NAME = "timescale_commits" embeddings = OpenAIEmbeddings() db = TimescaleVector( collection_name=COLLECTION_NAME, service_url=SERVICE_URL, embedding_function=embeddings, ) db.create_index() db.drop_index() db.create_index(index_type="tsv", max_alpha=1.0, num_neighbors=50) db.drop_index() db.create_index(index_type="hnsw", m=16, ef_construction=64) db.drop_index() db.create_index(index_type="ivfflat", num_lists=20, num_records=1000) db.drop_index() db.create_index() COLLECTION_NAME = "timescale_commits" vectorstore = TimescaleVector( embedding_function=
OpenAIEmbeddings()
langchain_openai.OpenAIEmbeddings
get_ipython().run_line_magic('pip', 'install --upgrade --quiet gradio_tools') from gradio_tools.tools import StableDiffusionTool local_file_path = StableDiffusionTool().langchain.run( "Please create a photo of a dog riding a skateboard" ) local_file_path from PIL import Image im = Image.open(local_file_path) from IPython.display import display display(im) from gradio_tools.tools import ( ImageCaptioningTool, StableDiffusionPromptGeneratorTool, StableDiffusionTool, TextToVideoTool, ) from langchain.agents import initialize_agent from langchain.memory import ConversationBufferMemory from langchain_openai import OpenAI llm =
OpenAI(temperature=0)
langchain_openai.OpenAI
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-text-splitters tiktoken') with open("../../state_of_the_union.txt") as f: state_of_the_union = f.read() from langchain_text_splitters import CharacterTextSplitter text_splitter = CharacterTextSplitter.from_tiktoken_encoder( chunk_size=100, chunk_overlap=0 ) texts = text_splitter.split_text(state_of_the_union) print(texts[0]) from langchain_text_splitters import TokenTextSplitter text_splitter = TokenTextSplitter(chunk_size=10, chunk_overlap=0) texts = text_splitter.split_text(state_of_the_union) print(texts[0]) get_ipython().run_line_magic('pip', 'install --upgrade --quiet spacy') with open("../../state_of_the_union.txt") as f: state_of_the_union = f.read() from langchain_text_splitters import SpacyTextSplitter text_splitter = SpacyTextSplitter(chunk_size=1000) texts = text_splitter.split_text(state_of_the_union) print(texts[0]) from langchain_text_splitters import SentenceTransformersTokenTextSplitter splitter = SentenceTransformersTokenTextSplitter(chunk_overlap=0) text = "Lorem " count_start_and_stop_tokens = 2 text_token_count = splitter.count_tokens(text=text) - count_start_and_stop_tokens print(text_token_count) token_multiplier = splitter.maximum_tokens_per_chunk // text_token_count + 1 text_to_split = text * token_multiplier print(f"tokens in text to split: {splitter.count_tokens(text=text_to_split)}") text_chunks = splitter.split_text(text=text_to_split) print(text_chunks[1]) with open("../../state_of_the_union.txt") as f: state_of_the_union = f.read() from langchain_text_splitters import NLTKTextSplitter text_splitter = NLTKTextSplitter(chunk_size=1000) texts = text_splitter.split_text(state_of_the_union) print(texts[0]) with open("./your_korean_doc.txt") as f: korean_document = f.read() from langchain_text_splitters import KonlpyTextSplitter text_splitter =
KonlpyTextSplitter()
langchain_text_splitters.KonlpyTextSplitter
meals = [ "Beef Enchiladas with Feta cheese. Mexican-Greek fusion", "Chicken Flatbreads with red sauce. Italian-Mexican fusion", "Veggie sweet potato quesadillas with vegan cheese", "One-Pan Tortelonni bake with peppers and onions", ] from langchain_openai import OpenAI llm = OpenAI(model="gpt-3.5-turbo-instruct") from langchain.prompts import PromptTemplate PROMPT_TEMPLATE = """Here is the description of a meal: "{meal}". Embed the meal into the given text: "{text_to_personalize}". Prepend a personalized message including the user's name "{user}" and their preference "{preference}". Make it sound good. """ PROMPT = PromptTemplate( input_variables=["meal", "text_to_personalize", "user", "preference"], template=PROMPT_TEMPLATE, ) import langchain_experimental.rl_chain as rl_chain chain = rl_chain.PickBest.from_llm(llm=llm, prompt=PROMPT) response = chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Tom"), preference=rl_chain.BasedOn(["Vegetarian", "regular dairy is ok"]), text_to_personalize="This is the weeks specialty dish, our master chefs \ believe you will love it!", ) print(response["response"]) for _ in range(5): try: response = chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Tom"), preference=rl_chain.BasedOn(["Vegetarian", "regular dairy is ok"]), text_to_personalize="This is the weeks specialty dish, our master chefs believe you will love it!", ) except Exception as e: print(e) print(response["response"]) print() scoring_criteria_template = ( "Given {preference} rank how good or bad this selection is {meal}" ) chain = rl_chain.PickBest.from_llm( llm=llm, prompt=PROMPT, selection_scorer=rl_chain.AutoSelectionScorer( llm=llm, scoring_criteria_template_str=scoring_criteria_template ), ) response = chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Tom"), preference=rl_chain.BasedOn(["Vegetarian", "regular dairy is ok"]), text_to_personalize="This is the weeks specialty dish, our master chefs believe you will love it!", ) print(response["response"]) selection_metadata = response["selection_metadata"] print( f"selected index: {selection_metadata.selected.index}, score: {selection_metadata.selected.score}" ) class CustomSelectionScorer(rl_chain.SelectionScorer): def score_response( self, inputs, llm_response: str, event: rl_chain.PickBestEvent ) -> float: print(event.based_on) print(event.to_select_from) selected_meal = event.to_select_from["meal"][event.selected.index] print(f"selected meal: {selected_meal}") if "Tom" in event.based_on["user"]: if "Vegetarian" in event.based_on["preference"]: if "Chicken" in selected_meal or "Beef" in selected_meal: return 0.0 else: return 1.0 else: if "Chicken" in selected_meal or "Beef" in selected_meal: return 1.0 else: return 0.0 else: raise NotImplementedError("I don't know how to score this user") chain = rl_chain.PickBest.from_llm( llm=llm, prompt=PROMPT, selection_scorer=CustomSelectionScorer(), ) response = chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Tom"), preference=rl_chain.BasedOn(["Vegetarian", "regular dairy is ok"]), text_to_personalize="This is the weeks specialty dish, our master chefs believe you will love it!", ) class CustomSelectionScorer(rl_chain.SelectionScorer): def score_preference(self, preference, selected_meal): if "Vegetarian" in preference: if "Chicken" in selected_meal or "Beef" in selected_meal: return 0.0 else: return 1.0 else: if "Chicken" in selected_meal or "Beef" in selected_meal: return 1.0 else: return 0.0 def score_response( self, inputs, llm_response: str, event: rl_chain.PickBestEvent ) -> float: selected_meal = event.to_select_from["meal"][event.selected.index] if "Tom" in event.based_on["user"]: return self.score_preference(event.based_on["preference"], selected_meal) elif "Anna" in event.based_on["user"]: return self.score_preference(event.based_on["preference"], selected_meal) else: raise NotImplementedError("I don't know how to score this user") chain = rl_chain.PickBest.from_llm( llm=llm, prompt=PROMPT, selection_scorer=CustomSelectionScorer(), metrics_step=5, metrics_window_size=5, # rolling window average ) random_chain = rl_chain.PickBest.from_llm( llm=llm, prompt=PROMPT, selection_scorer=CustomSelectionScorer(), metrics_step=5, metrics_window_size=5, # rolling window average policy=rl_chain.PickBestRandomPolicy, # set the random policy instead of default ) for _ in range(20): try: chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Tom"), preference=rl_chain.BasedOn(["Vegetarian", "regular dairy is ok"]), text_to_personalize="This is the weeks specialty dish, our master chefs believe you will love it!", ) random_chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Tom"), preference=rl_chain.BasedOn(["Vegetarian", "regular dairy is ok"]), text_to_personalize="This is the weeks specialty dish, our master chefs believe you will love it!", ) chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Anna"), preference=rl_chain.BasedOn(["Loves meat", "especially beef"]), text_to_personalize="This is the weeks specialty dish, our master chefs believe you will love it!", ) random_chain.run( meal=rl_chain.ToSelectFrom(meals), user=rl_chain.BasedOn("Anna"), preference=rl_chain.BasedOn(["Loves meat", "especially beef"]), text_to_personalize="This is the weeks specialty dish, our master chefs believe you will love it!", ) except Exception as e: print(e) from matplotlib import pyplot as plt chain.metrics.to_pandas()["score"].plot(label="default learning policy") random_chain.metrics.to_pandas()["score"].plot(label="random selection policy") plt.legend() print( f"The final average score for the default policy, calculated over a rolling window, is: {chain.metrics.to_pandas()['score'].iloc[-1]}" ) print( f"The final average score for the random policy, calculated over a rolling window, is: {random_chain.metrics.to_pandas()['score'].iloc[-1]}" ) from langchain.globals import set_debug from langchain.prompts.prompt import PromptTemplate set_debug(True) REWARD_PROMPT_TEMPLATE = """ Given {preference} rank how good or bad this selection is {meal} IMPORTANT: you MUST return a single number between -1 and 1, -1 being bad, 1 being good """ REWARD_PROMPT = PromptTemplate( input_variables=["preference", "meal"], template=REWARD_PROMPT_TEMPLATE, ) chain = rl_chain.PickBest.from_llm( llm=llm, prompt=PROMPT, selection_scorer=rl_chain.AutoSelectionScorer(llm=llm, prompt=REWARD_PROMPT), ) chain.run( meal=rl_chain.ToSelectFrom(meals), user=
rl_chain.BasedOn("Tom")
langchain_experimental.rl_chain.BasedOn
get_ipython().run_line_magic('pip', 'install --upgrade --quiet lark opensearch-py') import getpass import os from langchain_community.vectorstores import OpenSearchVectorSearch from langchain_core.documents import Document from langchain_openai import OpenAIEmbeddings os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") embeddings = 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, "rating": 9.9, "director": "Andrei Tarkovsky", "genre": "science fiction", }, ), ] vectorstore = OpenSearchVectorSearch.from_documents( docs, embeddings, index_name="opensearch-self-query-demo", opensearch_url="http://localhost:9200", ) from langchain.chains.query_constructor.base import AttributeInfo from langchain.retrievers.self_query.base import SelfQueryRetriever from langchain_openai import OpenAI metadata_field_info = [ AttributeInfo( name="genre", description="The genre of the movie", type="string or list[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 =
OpenAI(temperature=0)
langchain_openai.OpenAI
from langchain.pydantic_v1 import BaseModel, Field from langchain.tools import BaseTool, StructuredTool, tool @tool def search(query: str) -> str: """Look up things online.""" return "LangChain" print(search.name) print(search.description) print(search.args) @tool def multiply(a: int, b: int) -> int: """Multiply two numbers.""" return a * b print(multiply.name) print(multiply.description) print(multiply.args) class SearchInput(BaseModel): query: str = Field(description="should be a search query") @tool("search-tool", args_schema=SearchInput, return_direct=True) def search(query: str) -> str: """Look up things online.""" return "LangChain" print(search.name) print(search.description) print(search.args) print(search.return_direct) from typing import Optional, Type from langchain.callbacks.manager import ( AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) class SearchInput(BaseModel): query: str =
Field(description="should be a search query")
langchain.pydantic_v1.Field
README.md exists but content is empty. Use the Edit dataset card button to edit it.
Downloads last month
53
Edit dataset card

Collection including luna-code/langchain