prompt
stringlengths
43
25.9k
completion
stringlengths
7
362
api
stringlengths
18
90
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-pinecone langchain-openai langchain') from langchain_community.document_loaders import TextLoader from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter loader =
TextLoader("../../modules/state_of_the_union.txt")
langchain_community.document_loaders.TextLoader
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain label-studio label-studio-sdk langchain-openai') import os os.environ["LABEL_STUDIO_URL"] = "<YOUR-LABEL-STUDIO-URL>" # e.g. http://localhost:8080 os.environ["LABEL_STUDIO_API_KEY"] = "<YOUR-LABEL-STUDIO-API-KEY>" os.environ["OPENAI_API_KEY"] = "<YOUR-OPENAI-API-KEY>" from langchain.callbacks import LabelStudioCallbackHandler from langchain_openai import OpenAI llm = OpenAI( temperature=0, callbacks=[LabelStudioCallbackHandler(project_name="My Project")] ) print(llm("Tell me a joke")) from langchain.callbacks import LabelStudioCallbackHandler from langchain_core.messages import HumanMessage, SystemMessage from langchain_openai import ChatOpenAI chat_llm = ChatOpenAI( callbacks=[ LabelStudioCallbackHandler( mode="chat", project_name="New Project with Chat", ) ] ) llm_results = chat_llm( [ SystemMessage(content="Always use a lot of emojis"), HumanMessage(content="Tell me a joke"), ] ) ls =
LabelStudioCallbackHandler( project_config=""" <View> <Text name="prompt" value="$prompt"/> <TextArea name="response" toName="prompt"/> <TextArea name="user_feedback" toName="prompt"/> <Rating name="rating" toName="prompt"/> <Choices name="sentiment" toName="prompt"> <Choice value="Positive"/> <Choice value="Negative"/> </Choices> </View> """ )
langchain.callbacks.LabelStudioCallbackHandler
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")
langchain_openai.ChatOpenAI
from langchain.chains import GraphCypherQAChain from langchain_community.graphs import Neo4jGraph from langchain_openai import ChatOpenAI graph = Neo4jGraph( url="bolt://localhost:7687", username="neo4j", password="pleaseletmein" ) graph.query( """ MERGE (m:Movie {name:"Top Gun"}) WITH m UNWIND ["Tom Cruise", "Val Kilmer", "Anthony Edwards", "Meg Ryan"] AS actor MERGE (a:Actor {name:actor}) MERGE (a)-[:ACTED_IN]->(m) """ ) graph.refresh_schema() print(graph.schema) chain = GraphCypherQAChain.from_llm( ChatOpenAI(temperature=0), graph=graph, verbose=True ) chain.run("Who played in Top Gun?") chain = GraphCypherQAChain.from_llm( ChatOpenAI(temperature=0), graph=graph, verbose=True, top_k=2 ) chain.run("Who played in Top Gun?") chain = GraphCypherQAChain.from_llm( ChatOpenAI(temperature=0), graph=graph, verbose=True, return_intermediate_steps=True ) result = chain("Who played in Top Gun?") print(f"Intermediate steps: {result['intermediate_steps']}") print(f"Final answer: {result['result']}") chain = GraphCypherQAChain.from_llm( ChatOpenAI(temperature=0), graph=graph, verbose=True, return_direct=True ) chain.run("Who played in Top Gun?") from langchain.prompts.prompt import PromptTemplate CYPHER_GENERATION_TEMPLATE = """Task:Generate Cypher statement to query a graph database. Instructions: Use only the provided relationship types and properties in the schema. Do not use any other relationship types or properties that are not provided. Schema: {schema} Note: Do not include any explanations or apologies in your responses. Do not respond to any questions that might ask anything else than for you to construct a Cypher statement. Do not include any text except the generated Cypher statement. Examples: Here are a few examples of generated Cypher statements for particular questions: MATCH (m:Movie {{title:"Top Gun"}})<-[:ACTED_IN]-() RETURN count(*) AS numberOfActors The question is: {question}""" CYPHER_GENERATION_PROMPT = PromptTemplate( input_variables=["schema", "question"], template=CYPHER_GENERATION_TEMPLATE ) chain = GraphCypherQAChain.from_llm( ChatOpenAI(temperature=0), graph=graph, verbose=True, cypher_prompt=CYPHER_GENERATION_PROMPT, ) chain.run("How many people played in Top Gun?") chain = GraphCypherQAChain.from_llm( graph=graph, cypher_llm=
ChatOpenAI(temperature=0, model="gpt-3.5-turbo")
langchain_openai.ChatOpenAI
from langchain_community.document_loaders import HNLoader loader =
HNLoader("https://news.ycombinator.com/item?id=34817881")
langchain_community.document_loaders.HNLoader
from langchain.chains import FalkorDBQAChain from langchain_community.graphs import FalkorDBGraph from langchain_openai import ChatOpenAI graph = FalkorDBGraph(database="movies") graph.query( """ CREATE (al:Person {name: 'Al Pacino', birthDate: '1940-04-25'}), (robert:Person {name: 'Robert De Niro', birthDate: '1943-08-17'}), (tom:Person {name: 'Tom Cruise', birthDate: '1962-07-3'}), (val:Person {name: 'Val Kilmer', birthDate: '1959-12-31'}), (anthony:Person {name: 'Anthony Edwards', birthDate: '1962-7-19'}), (meg:Person {name: 'Meg Ryan', birthDate: '1961-11-19'}), (god1:Movie {title: 'The Godfather'}), (god2:Movie {title: 'The Godfather: Part II'}), (god3:Movie {title: 'The Godfather Coda: The Death of Michael Corleone'}), (top:Movie {title: 'Top Gun'}), (al)-[:ACTED_IN]->(god1), (al)-[:ACTED_IN]->(god2), (al)-[:ACTED_IN]->(god3), (robert)-[:ACTED_IN]->(god2), (tom)-[:ACTED_IN]->(top), (val)-[:ACTED_IN]->(top), (anthony)-[:ACTED_IN]->(top), (meg)-[:ACTED_IN]->(top) """ ) graph.refresh_schema() print(graph.schema) import os os.environ["OPENAI_API_KEY"] = "API_KEY_HERE" chain = FalkorDBQAChain.from_llm(
ChatOpenAI(temperature=0)
langchain_openai.ChatOpenAI
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), prompt=prompt) agent =
ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)
langchain.agents.ZeroShotAgent
get_ipython().run_line_magic('pip', 'install --upgrade --quiet amadeus > /dev/null') import os os.environ["AMADEUS_CLIENT_ID"] = "CLIENT_ID" os.environ["AMADEUS_CLIENT_SECRET"] = "CLIENT_SECRET" os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY" from langchain_community.agent_toolkits.amadeus.toolkit import AmadeusToolkit toolkit = AmadeusToolkit() tools = toolkit.get_tools() from langchain_community.llms import HuggingFaceHub os.environ["HUGGINGFACEHUB_API_TOKEN"] = "YOUR_HF_API_TOKEN" llm = HuggingFaceHub( repo_id="tiiuae/falcon-7b-instruct", model_kwargs={"temperature": 0.5, "max_length": 64}, ) toolkit_hf = AmadeusToolkit(llm=llm) from langchain import hub from langchain.agents import AgentExecutor, create_react_agent from langchain.agents.output_parsers import ReActJsonSingleInputOutputParser from langchain.tools.render import render_text_description_and_args from langchain_openai import ChatOpenAI llm = ChatOpenAI(temperature=0) prompt =
hub.pull("hwchase17/react-json")
langchain.hub.pull
get_ipython().run_line_magic('pip', 'install --upgrade --quiet gigachat') import os from getpass import getpass os.environ["GIGACHAT_CREDENTIALS"] = getpass() from langchain_community.chat_models import GigaChat chat =
GigaChat(verify_ssl_certs=False)
langchain_community.chat_models.GigaChat
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-google-spanner') from google.colab import auth auth.authenticate_user() PROJECT_ID = "my-project-id" # @param {type:"string"} get_ipython().system('gcloud config set project {PROJECT_ID}') get_ipython().system('gcloud services enable spanner.googleapis.com') INSTANCE = "my-instance" # @param {type: "string"} DATABASE = "my-database" # @param {type: "string"} TABLE_NAME = "vectors_search_data" # @param {type: "string"} from langchain_google_spanner import SecondaryIndex, SpannerVectorStore, TableColumn SpannerVectorStore.init_vector_store_table( instance_id=INSTANCE, database_id=DATABASE, table_name=TABLE_NAME, id_column="row_id", metadata_columns=[
TableColumn(name="metadata", type="JSON", is_null=True)
langchain_google_spanner.TableColumn
from langchain.agents import AgentExecutor, Tool, ZeroShotAgent from langchain.chains import LLMChain from langchain.memory import ConversationBufferMemory from langchain_community.chat_message_histories import RedisChatMessageHistory from langchain_community.utilities import GoogleSearchAPIWrapper from langchain_openai import OpenAI search = GoogleSearchAPIWrapper() tools = [ Tool( name="Search", func=search.run, description="useful for when you need to answer questions about current events", ) ] prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:""" suffix = """Begin!" {chat_history} Question: {input} {agent_scratchpad}""" prompt = ZeroShotAgent.create_prompt( tools, prefix=prefix, suffix=suffix, input_variables=["input", "chat_history", "agent_scratchpad"], ) message_history = RedisChatMessageHistory( url="redis://localhost:6379/0", ttl=600, session_id="my-session" ) memory = ConversationBufferMemory( memory_key="chat_history", chat_memory=message_history ) llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt) agent =
ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)
langchain.agents.ZeroShotAgent
SOURCE = "test" # @param {type:"Query"|"CollectionGroup"|"DocumentReference"|"string"} get_ipython().run_line_magic('pip', 'install -upgrade --quiet langchain-google-firestore') PROJECT_ID = "my-project-id" # @param {type:"string"} get_ipython().system('gcloud config set project {PROJECT_ID}') from google.colab import auth auth.authenticate_user() get_ipython().system('gcloud services enable firestore.googleapis.com') from langchain_core.documents.base import Document from langchain_google_firestore import FirestoreSaver saver = FirestoreSaver() data = [Document(page_content="Hello, World!")] saver.upsert_documents(data) saver = FirestoreSaver("Collection") saver.upsert_documents(data) doc_ids = ["AnotherCollection/doc_id", "foo/bar"] saver = FirestoreSaver() saver.upsert_documents(documents=data, document_ids=doc_ids) from langchain_google_firestore import FirestoreLoader loader_collection = FirestoreLoader("Collection") loader_subcollection = FirestoreLoader("Collection/doc/SubCollection") data_collection = loader_collection.load() data_subcollection = loader_subcollection.load() from google.cloud import firestore client = firestore.Client() doc_ref = client.collection("foo").document("bar") loader_document = FirestoreLoader(doc_ref) data = loader_document.load() from google.cloud.firestore import CollectionGroup, FieldFilter, Query col_ref = client.collection("col_group") collection_group = CollectionGroup(col_ref) loader_group = FirestoreLoader(collection_group) col_ref = client.collection("collection") query = col_ref.where(filter=FieldFilter("region", "==", "west_coast")) loader_query = FirestoreLoader(query) saver =
FirestoreSaver()
langchain_google_firestore.FirestoreSaver
get_ipython().system(' pip install langchain unstructured[all-docs] pydantic lxml langchainhub') get_ipython().system(' brew install tesseract') get_ipython().system(' brew install poppler') path = "/Users/rlm/Desktop/Papers/LLaMA2/" from typing import Any from pydantic import BaseModel from unstructured.partition.pdf import partition_pdf raw_pdf_elements = partition_pdf( filename=path + "LLaMA2.pdf", extract_images_in_pdf=False, infer_table_structure=True, chunking_strategy="by_title", max_characters=4000, new_after_n_chars=3800, combine_text_under_n_chars=2000, image_output_dir_path=path, ) category_counts = {} for element in raw_pdf_elements: category = str(type(element)) if category in category_counts: category_counts[category] += 1 else: category_counts[category] = 1 unique_categories = set(category_counts.keys()) category_counts class Element(BaseModel): type: str text: Any categorized_elements = [] for element in raw_pdf_elements: if "unstructured.documents.elements.Table" in str(type(element)): categorized_elements.append(Element(type="table", text=str(element))) elif "unstructured.documents.elements.CompositeElement" in str(type(element)): categorized_elements.append(Element(type="text", text=str(element))) table_elements = [e for e in categorized_elements if e.type == "table"] print(len(table_elements)) text_elements = [e for e in categorized_elements if e.type == "text"] print(len(text_elements)) from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate from langchain_openai import ChatOpenAI prompt_text = """You are an assistant tasked with summarizing tables and text. \ Give a concise summary of the table or text. Table or text chunk: {element} """ prompt = ChatPromptTemplate.from_template(prompt_text) model = ChatOpenAI(temperature=0, model="gpt-4") summarize_chain = {"element": lambda x: x} | prompt | model | StrOutputParser() tables = [i.text for i in table_elements] table_summaries = summarize_chain.batch(tables, {"max_concurrency": 5}) texts = [i.text for i in text_elements] text_summaries = summarize_chain.batch(texts, {"max_concurrency": 5}) import uuid from langchain.retrievers.multi_vector import MultiVectorRetriever from langchain.storage import InMemoryStore from langchain_community.vectorstores import Chroma from langchain_core.documents import Document from langchain_openai import OpenAIEmbeddings vectorstore = Chroma(collection_name="summaries", embedding_function=
OpenAIEmbeddings()
langchain_openai.OpenAIEmbeddings
from langchain.output_parsers import DatetimeOutputParser from langchain.prompts import PromptTemplate from langchain_openai import OpenAI output_parser = DatetimeOutputParser() template = """Answer the users question: {question} {format_instructions}""" prompt = PromptTemplate.from_template( template, partial_variables={"format_instructions": output_parser.get_format_instructions()}, ) prompt chain = prompt |
OpenAI()
langchain_openai.OpenAI
get_ipython().run_line_magic('pip', 'install --upgrade --quiet rank_bm25') from langchain.retrievers import BM25Retriever retriever = BM25Retriever.from_texts(["foo", "bar", "world", "hello", "foo bar"]) from langchain_core.documents import Document retriever = BM25Retriever.from_documents( [ Document(page_content="foo"), Document(page_content="bar"),
Document(page_content="world")
langchain_core.documents.Document
get_ipython().run_line_magic('pip', 'install --upgrade --quiet pyspark') from pyspark.sql import SparkSession spark = SparkSession.builder.getOrCreate() df = spark.read.csv("example_data/mlb_teams_2012.csv", header=True) from langchain_community.document_loaders import PySparkDataFrameLoader loader =
PySparkDataFrameLoader(spark, df, page_content_column="Team")
langchain_community.document_loaders.PySparkDataFrameLoader
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") def question_ends_with_question_mark(cls, field): if field[-1] != "?": raise ValueError("Badly formed question!") return field joke_query = "Tell me a joke." parser = PydanticOutputParser(pydantic_object=Joke) prompt = PromptTemplate( template="Answer the user query.\n{format_instructions}\n{query}\n", input_variables=["query"], partial_variables={"format_instructions": parser.get_format_instructions()}, ) chain = prompt | model | parser chain.invoke({"query": joke_query}) class Actor(BaseModel): name: str = Field(description="name of an actor") film_names: List[str] = Field(description="list of names of films they starred in") actor_query = "Generate the filmography for a random actor." parser =
PydanticOutputParser(pydantic_object=Actor)
langchain.output_parsers.PydanticOutputParser
get_ipython().run_line_magic('pip', 'install --upgrade --quiet cohere') get_ipython().run_line_magic('pip', 'install --upgrade --quiet faiss') get_ipython().run_line_magic('pip', 'install --upgrade --quiet faiss-cpu') import getpass import os os.environ["COHERE_API_KEY"] = getpass.getpass("Cohere API Key:") def pretty_print_docs(docs): print( f"\n{'-' * 100}\n".join( [f"Document {i+1}:\n\n" + d.page_content for i, d in enumerate(docs)] ) ) from langchain_community.document_loaders import TextLoader from langchain_community.embeddings import CohereEmbeddings from langchain_community.vectorstores import FAISS from langchain_text_splitters import RecursiveCharacterTextSplitter documents = TextLoader("../../modules/state_of_the_union.txt").load() text_splitter =
RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
langchain_text_splitters.RecursiveCharacterTextSplitter
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]" pillow pydantic lxml pillow matplotlib chromadb tiktoken') from langchain_text_splitters import CharacterTextSplitter from unstructured.partition.pdf import partition_pdf def extract_pdf_elements(path, fname): """ Extract images, tables, and chunk text from a PDF file. path: File path, which is used to dump images (.jpg) fname: File name """ return partition_pdf( filename=path + fname, extract_images_in_pdf=False, infer_table_structure=True, chunking_strategy="by_title", max_characters=4000, new_after_n_chars=3800, combine_text_under_n_chars=2000, image_output_dir_path=path, ) def categorize_elements(raw_pdf_elements): """ Categorize extracted elements from a PDF into tables and texts. raw_pdf_elements: List of unstructured.documents.elements """ 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)) return texts, tables fpath = "/Users/rlm/Desktop/cj/" fname = "cj.pdf" raw_pdf_elements = extract_pdf_elements(fpath, fname) texts, tables = categorize_elements(raw_pdf_elements) text_splitter = CharacterTextSplitter.from_tiktoken_encoder( chunk_size=4000, chunk_overlap=0 ) joined_texts = " ".join(texts) texts_4k_token = text_splitter.split_text(joined_texts) from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate from langchain_openai import ChatOpenAI 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 = ChatPromptTemplate.from_template(prompt_text) model = ChatOpenAI(temperature=0, model="gpt-4") 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": 5}) elif texts: text_summaries = texts if tables: table_summaries = summarize_chain.batch(tables, {"max_concurrency": 5}) return text_summaries, table_summaries text_summaries, table_summaries = generate_text_summaries( texts_4k_token, tables, summarize_texts=True ) 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""" 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 def generate_img_summaries(path): """ Generate summaries and base64 encoded strings for images path: Path to list of .jpg files extracted by Unstructured """ img_base64_list = [] image_summaries = [] prompt = """You are an assistant tasked with summarizing images for retrieval. \ These summaries will be embedded and used to retrieve the raw image. \ Give a concise summary of the image that is well optimized for retrieval.""" for img_file in sorted(os.listdir(path)): if img_file.endswith(".jpg"): img_path = os.path.join(path, img_file) base64_image = encode_image(img_path) img_base64_list.append(base64_image) image_summaries.append(image_summarize(base64_image, prompt)) return img_base64_list, image_summaries img_base64_list, image_summaries = generate_img_summaries(fpath) 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 def create_multi_vector_retriever( vectorstore, text_summaries, texts, table_summaries, tables, image_summaries, images ): """ Create retriever that indexes summaries, but returns raw images or texts """ store = InMemoryStore() id_key = "doc_id" retriever = MultiVectorRetriever( vectorstore=vectorstore, docstore=store, id_key=id_key, ) def add_documents(retriever, doc_summaries, doc_contents): doc_ids = [str(uuid.uuid4()) for _ in doc_contents] summary_docs = [ Document(page_content=s, metadata={id_key: doc_ids[i]}) for i, s in enumerate(doc_summaries) ] retriever.vectorstore.add_documents(summary_docs) retriever.docstore.mset(list(zip(doc_ids, doc_contents))) if text_summaries: add_documents(retriever, text_summaries, texts) if table_summaries: add_documents(retriever, table_summaries, tables) if image_summaries: add_documents(retriever, image_summaries, images) return retriever vectorstore = Chroma( collection_name="mm_rag_cj_blog", embedding_function=OpenAIEmbeddings() ) retriever_multi_vector_img = create_multi_vector_retriever( vectorstore, text_summaries, texts, table_summaries, tables, image_summaries, img_base64_list, ) import io import re from IPython.display import HTML, display from langchain_core.runnables import RunnableLambda, RunnablePassthrough from PIL import Image def plt_img_base64(img_base64): """Disply base64 encoded string as image""" image_html = f'<img src="data:image/jpeg;base64,{img_base64}" />' display(HTML(image_html)) 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 resize_base64_image(base64_string, size=(128, 128)): """ Resize an image encoded as a Base64 string """ 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 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): doc = resize_base64_image(doc, size=(1300, 600)) b64_images.append(doc) else: texts.append(doc) return {"images": b64_images, "texts": texts} def img_prompt_func(data_dict): """ Join the context into a single string """ formatted_texts = "\n".join(data_dict["context"]["texts"]) messages = [] if data_dict["context"]["images"]: for image in data_dict["context"]["images"]: image_message = { "type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image}"}, } messages.append(image_message) text_message = { "type": "text", "text": ( "You are financial analyst tasking with providing investment advice.\n" "You will be given a mixed of text, tables, and image(s) usually of charts or graphs.\n" "Use this information to provide investment advice related to the user question. \n" f"User-provided question: {data_dict['question']}\n\n" "Text and / or tables:\n" f"{formatted_texts}" ), } messages.append(text_message) return [HumanMessage(content=messages)] def multi_modal_rag_chain(retriever): """ Multi-modal RAG chain """ model = ChatOpenAI(temperature=0, model="gpt-4-vision-preview", max_tokens=1024) chain = ( { "context": retriever | RunnableLambda(split_image_text_types), "question": RunnablePassthrough(), } | RunnableLambda(img_prompt_func) | model |
StrOutputParser()
langchain_core.output_parsers.StrOutputParser
get_ipython().run_line_magic('pip', 'install --upgrade --quiet pymilvus') import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import Milvus from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter from langchain_community.document_loaders import TextLoader loader = TextLoader("../../modules/state_of_the_union.txt") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings() vector_db = Milvus.from_documents( docs, embeddings, connection_args={"host": "127.0.0.1", "port": "19530"}, ) query = "What did the president say about Ketanji Brown Jackson" docs = vector_db.similarity_search(query) docs[0].page_content vector_db = Milvus.from_documents( docs, embeddings, collection_name="collection_1", connection_args={"host": "127.0.0.1", "port": "19530"}, ) vector_db = Milvus( embeddings, connection_args={"host": "127.0.0.1", "port": "19530"}, collection_name="collection_1", ) from langchain_core.documents import Document docs = [
Document(page_content="i worked at kensho", metadata={"namespace": "harrison"})
langchain.docstore.document.Document
import os os.environ["SCENEX_API_KEY"] = "<YOUR_API_KEY>" from langchain.agents import load_tools tools = load_tools(["sceneXplain"]) from langchain.tools import SceneXplainTool tool = SceneXplainTool() from langchain.agents import initialize_agent from langchain.memory import ConversationBufferMemory from langchain_openai import OpenAI llm = OpenAI(temperature=0) memory =
ConversationBufferMemory(memory_key="chat_history")
langchain.memory.ConversationBufferMemory
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()
langchain_community.utilities.GoogleSearchAPIWrapper
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()
langchain_core.output_parsers.XMLOutputParser
get_ipython().run_line_magic('pip', 'install --upgrade --quiet praw') client_id = "" client_secret = "" user_agent = "" from langchain_community.tools.reddit_search.tool import RedditSearchRun from langchain_community.utilities.reddit_search import RedditSearchAPIWrapper search = RedditSearchRun( api_wrapper=RedditSearchAPIWrapper( reddit_client_id=client_id, reddit_client_secret=client_secret, reddit_user_agent=user_agent, ) ) from langchain_community.tools.reddit_search.tool import RedditSearchSchema search_params = RedditSearchSchema( query="beginner", sort="new", time_filter="week", subreddit="python", limit="2" ) result = search.run(tool_input=search_params.dict()) print(result) from langchain.agents import AgentExecutor, StructuredChatAgent, Tool from langchain.chains import LLMChain from langchain.memory import ConversationBufferMemory, ReadOnlySharedMemory from langchain.prompts import PromptTemplate from langchain_community.tools.reddit_search.tool import RedditSearchRun from langchain_community.utilities.reddit_search import RedditSearchAPIWrapper from langchain_openai import ChatOpenAI client_id = "" client_secret = "" user_agent = "" openai_api_key = "" template = """This is a conversation between a human and a bot: {chat_history} Write a summary of the conversation for {input}: """ prompt = PromptTemplate(input_variables=["input", "chat_history"], template=template) memory = ConversationBufferMemory(memory_key="chat_history") prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:""" suffix = """Begin!" {chat_history} Question: {input} {agent_scratchpad}""" tools = [ RedditSearchRun( api_wrapper=RedditSearchAPIWrapper( reddit_client_id=client_id, reddit_client_secret=client_secret, reddit_user_agent=user_agent, ) ) ] prompt = StructuredChatAgent.create_prompt( prefix=prefix, tools=tools, suffix=suffix, input_variables=["input", "chat_history", "agent_scratchpad"], ) llm = ChatOpenAI(temperature=0, openai_api_key=openai_api_key) llm_chain = LLMChain(llm=llm, prompt=prompt) agent =
StructuredChatAgent(llm_chain=llm_chain, verbose=True, tools=tools)
langchain.agents.StructuredChatAgent
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)
langchain_experimental.rl_chain.ToSelectFrom
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, ) print(few_shot_prompt.format()) final_prompt = ChatPromptTemplate.from_messages( [ ("system", "You are a wondrous wizard of math."), few_shot_prompt, ("human", "{input}"), ] ) from langchain_community.chat_models import ChatAnthropic chain = final_prompt | ChatAnthropic(temperature=0.0) chain.invoke({"input": "What's the square of a triangle?"}) from langchain.prompts import SemanticSimilarityExampleSelector from langchain_community.vectorstores import Chroma from langchain_openai import OpenAIEmbeddings examples = [ {"input": "2+2", "output": "4"}, {"input": "2+3", "output": "5"}, {"input": "2+4", "output": "6"}, {"input": "What did the cow say to the moon?", "output": "nothing at all"}, { "input": "Write me a poem about the moon", "output": "One for the moon, and one for me, who are we to talk about the moon?", }, ] to_vectorize = [" ".join(example.values()) for example in examples] embeddings = OpenAIEmbeddings() vectorstore = Chroma.from_texts(to_vectorize, embeddings, metadatas=examples) example_selector = SemanticSimilarityExampleSelector( vectorstore=vectorstore, k=2, ) example_selector.select_examples({"input": "horse"}) from langchain.prompts import ( ChatPromptTemplate, FewShotChatMessagePromptTemplate, ) few_shot_prompt = FewShotChatMessagePromptTemplate( input_variables=["input"], example_selector=example_selector, example_prompt=
ChatPromptTemplate.from_messages( [("human", "{input}")
langchain.prompts.ChatPromptTemplate.from_messages
from langchain.output_parsers import ( OutputFixingParser, PydanticOutputParser, ) from langchain.prompts import ( PromptTemplate, ) from langchain_core.pydantic_v1 import BaseModel, Field from langchain_openai import ChatOpenAI, OpenAI template = """Based on the user question, provide an Action and Action Input for what step should be taken. {format_instructions} Question: {query} Response:""" class Action(BaseModel): action: str =
Field(description="action to take")
langchain_core.pydantic_v1.Field
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain tiktoken langchain-openai') get_ipython().run_line_magic('pip', 'install --upgrade --quiet hippo-api==1.1.0.rc3') import os from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores.hippo import Hippo from langchain_openai import ChatOpenAI, OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter os.environ["OPENAI_API_KEY"] = "YOUR OPENAI KEY" loader = TextLoader("../../modules/state_of_the_union.txt") documents = loader.load() text_splitter =
CharacterTextSplitter(chunk_size=500, chunk_overlap=0)
langchain_text_splitters.CharacterTextSplitter
get_ipython().system(' pip install langchain docugami==0.0.8 dgml-utils==0.3.0 pydantic langchainhub chromadb hnswlib --upgrade --quiet') from pprint import pprint from docugami import Docugami from docugami.lib.upload import upload_to_named_docset, wait_for_dgml DOCSET_NAME = "NTSB Aviation Incident Reports" FILE_PATHS = [ "/Users/tjaffri/ntsb/Report_CEN23LA277_192541.pdf", "/Users/tjaffri/ntsb/Report_CEN23LA338_192753.pdf", "/Users/tjaffri/ntsb/Report_CEN23LA363_192876.pdf", "/Users/tjaffri/ntsb/Report_CEN23LA394_192995.pdf", "/Users/tjaffri/ntsb/Report_ERA23LA114_106615.pdf", "/Users/tjaffri/ntsb/Report_WPR23LA254_192532.pdf", ] assert len(FILE_PATHS) > 5, "Please provide at least 6 files" dg_client = Docugami() dg_docs = upload_to_named_docset(dg_client, FILE_PATHS, DOCSET_NAME) dgml_paths = wait_for_dgml(dg_client, dg_docs) pprint(dgml_paths) from pathlib import Path from dgml_utils.segmentation import get_chunks_str dgml_path = dgml_paths[Path(FILE_PATHS[0]).name] with open(dgml_path, "r") as file: contents = file.read().encode("utf-8") chunks = get_chunks_str( contents, include_xml_tags=True, # Ensures Docugami XML semantic tags are included in the chunked output (set to False for text-only chunks and tables as Markdown) max_text_length=1024 * 8, # 8k chars are ~2k tokens for OpenAI. ) print(f"found {len(chunks)} chunks, here are the first few") for chunk in chunks[:10]: print(chunk.text) with open(dgml_path, "r") as file: contents = file.read().encode("utf-8") chunks = get_chunks_str( contents, include_xml_tags=False, # text-only chunks and tables as Markdown max_text_length=1024 * 8, # 8k chars are ~2k tokens for OpenAI. Ref: https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them ) print(f"found {len(chunks)} chunks, here are the first few") for chunk in chunks[:10]: print(chunk.text) import requests dgml = requests.get( "https://raw.githubusercontent.com/docugami/dgml-utils/main/python/tests/test_data/article/Jane%20Doe.xml" ).text chunks = get_chunks_str(dgml, include_xml_tags=True) len(chunks) category_counts = {} for element in chunks: category = element.structure if category in category_counts: category_counts[category] += 1 else: category_counts[category] = 1 category_counts table_elements = [c for c in chunks if "table" in c.structure.split()] print(f"There are {len(table_elements)} tables") text_elements = [c for c in chunks if "table" not in c.structure.split()] print(f"There are {len(text_elements)} text elements") for element in text_elements[:20]: print(element.text) print(table_elements[0].text) chunks_as_text = get_chunks_str(dgml, include_xml_tags=False) table_elements_as_text = [c for c in chunks_as_text if "table" in c.structure.split()] print(table_elements_as_text[0].text) from langchain.prompts import ( ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, ) from langchain_core.output_parsers import StrOutputParser 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")
langchain_openai.ChatOpenAI
from langchain.agents import AgentExecutor, BaseMultiActionAgent, Tool from langchain_community.utilities import SerpAPIWrapper def random_word(query: str) -> str: print("\nNow I'm doing this!") return "foo" search = SerpAPIWrapper() tools = [ Tool( name="Search", func=search.run, description="useful for when you need to answer questions about current events", ), Tool( name="RandomWord", func=random_word, description="call this to get a random word.", ), ] from typing import Any, List, Tuple, Union from langchain_core.agents import AgentAction, AgentFinish class FakeAgent(BaseMultiActionAgent): """Fake Custom Agent.""" @property def input_keys(self): return ["input"] def plan( self, intermediate_steps: List[Tuple[AgentAction, str]], **kwargs: Any ) -> Union[List[AgentAction], AgentFinish]: """Given input, decided what to do. Args: intermediate_steps: Steps the LLM has taken to date, along with observations **kwargs: User inputs. Returns: Action specifying what tool to use. """ if len(intermediate_steps) == 0: return [
AgentAction(tool="Search", tool_input=kwargs["input"], log="")
langchain_core.agents.AgentAction
get_ipython().run_line_magic('pip', 'install --upgrade --quiet doctran') import json from langchain_community.document_transformers import DoctranQATransformer from langchain_core.documents import Document from dotenv import load_dotenv load_dotenv() sample_text = """[Generated with ChatGPT] Confidential Document - For Internal Use Only Date: July 1, 2023 Subject: Updates and Discussions on Various Topics Dear Team, I hope this email finds you well. In this document, I would like to provide you with some important updates and discuss various topics that require our attention. Please treat the information contained herein as highly confidential. Security and Privacy Measures As part of our ongoing commitment to ensure the security and privacy of our customers' data, we have implemented robust measures across all our systems. We would like to commend John Doe (email: john.doe@example.com) from the IT department for his diligent work in enhancing our network security. Moving forward, we kindly remind everyone to strictly adhere to our data protection policies and guidelines. Additionally, if you come across any potential security risks or incidents, please report them immediately to our dedicated team at security@example.com. HR Updates and Employee Benefits Recently, we welcomed several new team members who have made significant contributions to their respective departments. I would like to recognize Jane Smith (SSN: 049-45-5928) for her outstanding performance in customer service. Jane has consistently received positive feedback from our clients. Furthermore, please remember that the open enrollment period for our employee benefits program is fast approaching. Should you have any questions or require assistance, please contact our HR representative, Michael Johnson (phone: 418-492-3850, email: michael.johnson@example.com). Marketing Initiatives and Campaigns Our marketing team has been actively working on developing new strategies to increase brand awareness and drive customer engagement. We would like to thank Sarah Thompson (phone: 415-555-1234) for her exceptional efforts in managing our social media platforms. Sarah has successfully increased our follower base by 20% in the past month alone. Moreover, please mark your calendars for the upcoming product launch event on July 15th. We encourage all team members to attend and support this exciting milestone for our company. Research and Development Projects In our pursuit of innovation, our research and development department has been working tirelessly on various projects. I would like to acknowledge the exceptional work of David Rodriguez (email: david.rodriguez@example.com) in his role as project lead. David's contributions to the development of our cutting-edge technology have been instrumental. Furthermore, we would like to remind everyone to share their ideas and suggestions for potential new projects during our monthly R&D brainstorming session, scheduled for July 10th. Please treat the information in this document with utmost confidentiality and ensure that it is not shared with unauthorized individuals. If you have any questions or concerns regarding the topics discussed, please do not hesitate to reach out to me directly. Thank you for your attention, and let's continue to work together to achieve our goals. Best regards, Jason Fan Cofounder & CEO Psychic jason@psychic.dev """ print(sample_text) documents = [Document(page_content=sample_text)] qa_transformer =
DoctranQATransformer()
langchain_community.document_transformers.DoctranQATransformer
from langchain_community.chat_message_histories import StreamlitChatMessageHistory history = StreamlitChatMessageHistory(key="chat_messages") history.add_user_message("hi!") history.add_ai_message("whats up?") history.messages from langchain_community.chat_message_histories import StreamlitChatMessageHistory msgs = StreamlitChatMessageHistory(key="special_app_key") if len(msgs.messages) == 0: msgs.add_ai_message("How can I help you?") from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain_core.runnables.history import RunnableWithMessageHistory from langchain_openai import ChatOpenAI prompt = ChatPromptTemplate.from_messages( [ ("system", "You are an AI chatbot having a conversation with a human."), MessagesPlaceholder(variable_name="history"), ("human", "{question}"), ] ) chain = prompt |
ChatOpenAI()
langchain_openai.ChatOpenAI
from typing import Callable, List from langchain.schema import ( HumanMessage, SystemMessage, ) from langchain_openai import ChatOpenAI class DialogueAgent: def __init__( self, name: str, system_message: SystemMessage, model: ChatOpenAI, ) -> None: self.name = name self.system_message = system_message self.model = model self.prefix = f"{self.name}: " self.reset() def reset(self): self.message_history = ["Here is the conversation so far."] def send(self) -> str: """ Applies the chatmodel to the message history and returns the message string """ message = self.model( [ self.system_message, HumanMessage(content="\n".join(self.message_history + [self.prefix])), ] ) return message.content def receive(self, name: str, message: str) -> None: """ Concatenates {message} spoken by {name} into message history """ self.message_history.append(f"{name}: {message}") class DialogueSimulator: def __init__( self, agents: List[DialogueAgent], selection_function: Callable[[int, List[DialogueAgent]], int], ) -> None: self.agents = agents self._step = 0 self.select_next_speaker = selection_function def reset(self): for agent in self.agents: agent.reset() def inject(self, name: str, message: str): """ Initiates the conversation with a {message} from {name} """ for agent in self.agents: agent.receive(name, message) self._step += 1 def step(self) -> tuple[str, str]: speaker_idx = self.select_next_speaker(self._step, self.agents) speaker = self.agents[speaker_idx] message = speaker.send() for receiver in self.agents: receiver.receive(speaker.name, message) self._step += 1 return speaker.name, message protagonist_name = "Harry Potter" storyteller_name = "Dungeon Master" quest = "Find all of Lord Voldemort's seven horcruxes." word_limit = 50 # word limit for task brainstorming game_description = f"""Here is the topic for a Dungeons & Dragons game: {quest}. There is one player in this game: the protagonist, {protagonist_name}. The story is narrated by the storyteller, {storyteller_name}.""" player_descriptor_system_message = SystemMessage( content="You can add detail to the description of a Dungeons & Dragons player." ) protagonist_specifier_prompt = [ player_descriptor_system_message, HumanMessage( content=f"""{game_description} Please reply with a creative description of the protagonist, {protagonist_name}, in {word_limit} words or less. Speak directly to {protagonist_name}. Do not add anything else.""" ), ] protagonist_description =
ChatOpenAI(temperature=1.0)
langchain_openai.ChatOpenAI
from langchain.output_parsers.enum import EnumOutputParser from enum import Enum class Colors(Enum): RED = "red" GREEN = "green" BLUE = "blue" parser =
EnumOutputParser(enum=Colors)
langchain.output_parsers.enum.EnumOutputParser
import uuid from pathlib import Path import langchain import torch from bs4 import BeautifulSoup as Soup from langchain.retrievers.multi_vector import MultiVectorRetriever from langchain.storage import InMemoryByteStore, LocalFileStore from langchain_community.document_loaders.recursive_url_loader import ( RecursiveUrlLoader, ) from langchain_community.vectorstores import Chroma from langchain_text_splitters import RecursiveCharacterTextSplitter # noqa DOCSTORE_DIR = "." DOCSTORE_ID_KEY = "doc_id" loader = RecursiveUrlLoader( "https://ar5iv.labs.arxiv.org/html/1706.03762", max_depth=2, extractor=lambda x: Soup(x, "html.parser").text, ) data = loader.load() print(f"Loaded {len(data)} documents") text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0) all_splits = text_splitter.split_documents(data) print(f"Split into {len(all_splits)} documents") from langchain_community.embeddings import QuantizedBiEncoderEmbeddings from langchain_core.embeddings import Embeddings model_name = "Intel/bge-small-en-v1.5-rag-int8-static" encode_kwargs = {"normalize_embeddings": True} # set True to compute cosine similarity model_inc = QuantizedBiEncoderEmbeddings( model_name=model_name, encode_kwargs=encode_kwargs, query_instruction="Represent this sentence for searching relevant passages: ", ) def get_multi_vector_retriever( docstore_id_key: str, collection_name: str, embedding_function: Embeddings ): """Create the composed retriever object.""" vectorstore = Chroma( collection_name=collection_name, embedding_function=embedding_function, ) store = InMemoryByteStore() return MultiVectorRetriever( vectorstore=vectorstore, byte_store=store, id_key=docstore_id_key, ) retriever = get_multi_vector_retriever(DOCSTORE_ID_KEY, "multi_vec_store", model_inc) child_text_splitter = RecursiveCharacterTextSplitter(chunk_size=400) id_key = "doc_id" doc_ids = [str(uuid.uuid4()) for _ in all_splits] sub_docs = [] for i, doc in enumerate(all_splits): _id = doc_ids[i] _sub_docs = child_text_splitter.split_documents([doc]) for _doc in _sub_docs: _doc.metadata[id_key] = _id sub_docs.extend(_sub_docs) retriever.vectorstore.add_documents(sub_docs) retriever.docstore.mset(list(zip(doc_ids, all_splits))) import torch from langchain.llms.huggingface_pipeline import HuggingFacePipeline from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline model_id = "Intel/neural-chat-7b-v3-3" tokenizer = AutoTokenizer.from_pretrained(model_id) model = AutoModelForCausalLM.from_pretrained( model_id, device_map="auto", torch_dtype=torch.bfloat16 ) pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=100) hf =
HuggingFacePipeline(pipeline=pipe)
langchain.llms.huggingface_pipeline.HuggingFacePipeline
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)
langchain.prompts.PromptTemplate.from_template
get_ipython().system('poetry run pip install dgml-utils==0.3.0 --upgrade --quiet') import os from langchain_community.document_loaders import DocugamiLoader DOCUGAMI_API_KEY = os.environ.get("DOCUGAMI_API_KEY") docset_id = "26xpy3aes7xp" document_ids = ["d7jqdzcj50sj", "cgd1eacfkchw"] loader = DocugamiLoader(docset_id=docset_id, document_ids=document_ids) chunks = loader.load() len(chunks) loader.min_text_length = 64 loader.include_xml_tags = True chunks = loader.load() for chunk in chunks[:5]: print(chunk) get_ipython().system('poetry run pip install --upgrade langchain-openai tiktoken chromadb hnswlib') loader = DocugamiLoader(docset_id="zo954yqy53wp") chunks = loader.load() for chunk in chunks: stripped_metadata = chunk.metadata.copy() for key in chunk.metadata: if key not in ["name", "xpath", "id", "structure"]: del stripped_metadata[key] chunk.metadata = stripped_metadata print(len(chunks)) from langchain.chains import RetrievalQA from langchain_community.vectorstores.chroma import Chroma from langchain_openai import OpenAI, OpenAIEmbeddings embedding = OpenAIEmbeddings() vectordb = Chroma.from_documents(documents=chunks, embedding=embedding) retriever = vectordb.as_retriever() qa_chain = RetrievalQA.from_chain_type( llm=OpenAI(), chain_type="stuff", retriever=retriever, return_source_documents=True ) qa_chain("What can tenants do with signage on their properties?") chain_response = qa_chain("What is rentable area for the property owned by DHA Group?") chain_response["result"] # correct answer should be 13,500 sq ft chain_response["source_documents"] loader = DocugamiLoader(docset_id="zo954yqy53wp") loader.include_xml_tags = ( True # for additional semantics from the Docugami knowledge graph ) chunks = loader.load() print(chunks[0].metadata) get_ipython().system('poetry run pip install --upgrade lark --quiet') from langchain.chains.query_constructor.schema import AttributeInfo from langchain.retrievers.self_query.base import SelfQueryRetriever from langchain_community.vectorstores.chroma import Chroma EXCLUDE_KEYS = ["id", "xpath", "structure"] metadata_field_info = [ AttributeInfo( name=key, description=f"The {key} for this chunk", type="string", ) for key in chunks[0].metadata if key.lower() not in EXCLUDE_KEYS ] document_content_description = "Contents of this chunk" llm = OpenAI(temperature=0) vectordb = Chroma.from_documents(documents=chunks, embedding=embedding) retriever = SelfQueryRetriever.from_llm( llm, vectordb, document_content_description, metadata_field_info, verbose=True ) qa_chain = RetrievalQA.from_chain_type( llm=OpenAI(), chain_type="stuff", retriever=retriever, return_source_documents=True, verbose=True, ) qa_chain( "What is rentable area for the property owned by DHA Group?" ) # correct answer should be 13,500 sq ft from typing import Dict, List from langchain_community.document_loaders import DocugamiLoader from langchain_core.documents import Document loader = DocugamiLoader(docset_id="zo954yqy53wp") loader.include_xml_tags = ( True # for additional semantics from the Docugami knowledge graph ) loader.parent_hierarchy_levels = 3 # for expanded context loader.max_text_length = ( 1024 * 8 ) # 8K chars are roughly 2K tokens (ref: https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them) loader.include_project_metadata_in_doc_metadata = ( False # Not filtering on vector metadata, so remove to lighten the vectors ) chunks: List[Document] = loader.load() parents_by_id: Dict[str, Document] = {} children_by_id: Dict[str, Document] = {} for chunk in chunks: chunk_id = chunk.metadata.get("id") parent_chunk_id = chunk.metadata.get(loader.parent_id_key) if not parent_chunk_id: parents_by_id[chunk_id] = chunk else: children_by_id[chunk_id] = chunk for id, chunk in list(children_by_id.items())[:5]: parent_chunk_id = chunk.metadata.get(loader.parent_id_key) if parent_chunk_id: print(f"PARENT CHUNK {parent_chunk_id}: {parents_by_id[parent_chunk_id]}") print(f"CHUNK {id}: {chunk}") from langchain.retrievers.multi_vector import MultiVectorRetriever, SearchType from langchain.storage import InMemoryStore from langchain_community.vectorstores.chroma import Chroma from langchain_openai import OpenAIEmbeddings vectorstore = Chroma(collection_name="big2small", embedding_function=OpenAIEmbeddings()) store =
InMemoryStore()
langchain.storage.InMemoryStore
import os import yaml get_ipython().system('wget https://raw.githubusercontent.com/openai/openai-openapi/master/openapi.yaml -O openai_openapi.yaml') get_ipython().system('wget https://www.klarna.com/us/shopping/public/openai/v0/api-docs -O klarna_openapi.yaml') get_ipython().system('wget https://raw.githubusercontent.com/APIs-guru/openapi-directory/main/APIs/spotify.com/1.0.0/openapi.yaml -O spotify_openapi.yaml') from langchain_community.agent_toolkits.openapi.spec import reduce_openapi_spec with open("openai_openapi.yaml") as f: raw_openai_api_spec = yaml.load(f, Loader=yaml.Loader) openai_api_spec = reduce_openapi_spec(raw_openai_api_spec) with open("klarna_openapi.yaml") as f: raw_klarna_api_spec = yaml.load(f, Loader=yaml.Loader) klarna_api_spec = reduce_openapi_spec(raw_klarna_api_spec) with open("spotify_openapi.yaml") as f: raw_spotify_api_spec = yaml.load(f, Loader=yaml.Loader) spotify_api_spec = reduce_openapi_spec(raw_spotify_api_spec) import spotipy.util as util from langchain.requests import RequestsWrapper def construct_spotify_auth_headers(raw_spec: dict): scopes = list( raw_spec["components"]["securitySchemes"]["oauth_2_0"]["flows"][ "authorizationCode" ]["scopes"].keys() ) access_token = util.prompt_for_user_token(scope=",".join(scopes)) return {"Authorization": f"Bearer {access_token}"} headers = construct_spotify_auth_headers(raw_spotify_api_spec) requests_wrapper = RequestsWrapper(headers=headers) endpoints = [ (route, operation) for route, operations in raw_spotify_api_spec["paths"].items() for operation in operations if operation in ["get", "post"] ] len(endpoints) import tiktoken enc = tiktoken.encoding_for_model("gpt-4") def count_tokens(s): return len(enc.encode(s)) count_tokens(yaml.dump(raw_spotify_api_spec)) from langchain_community.agent_toolkits.openapi import planner from langchain_openai import OpenAI llm =
OpenAI(model_name="gpt-4", temperature=0.0)
langchain_openai.OpenAI
get_ipython().run_line_magic('pip', 'install -qU langchain-text-splitters') from langchain_text_splitters import HTMLHeaderTextSplitter html_string = """ <!DOCTYPE html> <html> <body> <div> <h1>Foo</h1> <p>Some intro text about Foo.</p> <div> <h2>Bar main section</h2> <p>Some intro text about Bar.</p> <h3>Bar subsection 1</h3> <p>Some text about the first subtopic of Bar.</p> <h3>Bar subsection 2</h3> <p>Some text about the second subtopic of Bar.</p> </div> <div> <h2>Baz</h2> <p>Some text about Baz</p> </div> <br> <p>Some concluding text about Foo</p> </div> </body> </html> """ headers_to_split_on = [ ("h1", "Header 1"), ("h2", "Header 2"), ("h3", "Header 3"), ] html_splitter = HTMLHeaderTextSplitter(headers_to_split_on=headers_to_split_on) html_header_splits = html_splitter.split_text(html_string) html_header_splits from langchain_text_splitters import RecursiveCharacterTextSplitter url = "https://plato.stanford.edu/entries/goedel/" headers_to_split_on = [ ("h1", "Header 1"), ("h2", "Header 2"), ("h3", "Header 3"), ("h4", "Header 4"), ] html_splitter = HTMLHeaderTextSplitter(headers_to_split_on=headers_to_split_on) html_header_splits = html_splitter.split_text_from_url(url) chunk_size = 500 chunk_overlap = 30 text_splitter = RecursiveCharacterTextSplitter( chunk_size=chunk_size, chunk_overlap=chunk_overlap ) splits = text_splitter.split_documents(html_header_splits) splits[80:85] url = "https://www.cnn.com/2023/09/25/weather/el-nino-winter-us-climate/index.html" headers_to_split_on = [ ("h1", "Header 1"), ("h2", "Header 2"), ] html_splitter =
HTMLHeaderTextSplitter(headers_to_split_on=headers_to_split_on)
langchain_text_splitters.HTMLHeaderTextSplitter
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() tool = Tool( name="google_search", description="Search Google for recent results.", func=search.run, ) tool.run("Obama's first name?") search = GoogleSearchAPIWrapper(k=1) tool = Tool( name="I'm Feeling Lucky", description="Search Google and return the first result.", func=search.run, ) tool.run("python") search =
GoogleSearchAPIWrapper()
langchain_community.utilities.GoogleSearchAPIWrapper
from langchain.indexes import SQLRecordManager, index from langchain_core.documents import Document from langchain_elasticsearch import ElasticsearchStore from langchain_openai import OpenAIEmbeddings collection_name = "test_index" embedding = OpenAIEmbeddings() vectorstore = ElasticsearchStore( es_url="http://localhost:9200", index_name="test_index", embedding=embedding ) namespace = f"elasticsearch/{collection_name}" record_manager = SQLRecordManager( namespace, db_url="sqlite:///record_manager_cache.sql" ) record_manager.create_schema() doc1 = Document(page_content="kitty", metadata={"source": "kitty.txt"}) doc2 = Document(page_content="doggy", metadata={"source": "doggy.txt"}) def _clear(): """Hacky helper method to clear content. See the `full` mode section to to understand why it works.""" index([], record_manager, vectorstore, cleanup="full", source_id_key="source") _clear() index( [doc1, doc1, doc1, doc1, doc1], record_manager, vectorstore, cleanup=None, source_id_key="source", ) _clear() index([doc1, doc2], record_manager, vectorstore, cleanup=None, source_id_key="source") index([doc1, doc2], record_manager, vectorstore, cleanup=None, source_id_key="source") _clear() index( [doc1, doc2], record_manager, vectorstore, cleanup="incremental", source_id_key="source", ) index( [doc1, doc2], record_manager, vectorstore, cleanup="incremental", source_id_key="source", ) index([], record_manager, vectorstore, cleanup="incremental", source_id_key="source") changed_doc_2 = Document(page_content="puppy", metadata={"source": "doggy.txt"}) index( [changed_doc_2], record_manager, vectorstore, cleanup="incremental", source_id_key="source", ) _clear() all_docs = [doc1, doc2] index(all_docs, record_manager, vectorstore, cleanup="full", source_id_key="source") del all_docs[0] all_docs index(all_docs, record_manager, vectorstore, cleanup="full", source_id_key="source") from langchain_text_splitters import CharacterTextSplitter doc1 = Document( page_content="kitty kitty kitty kitty kitty", metadata={"source": "kitty.txt"} ) doc2 = Document(page_content="doggy doggy the doggy", metadata={"source": "doggy.txt"}) new_docs = CharacterTextSplitter( separator="t", keep_separator=True, chunk_size=12, chunk_overlap=2 ).split_documents([doc1, doc2]) new_docs _clear() index( new_docs, record_manager, vectorstore, cleanup="incremental", source_id_key="source", ) changed_doggy_docs = [ Document(page_content="woof woof", metadata={"source": "doggy.txt"}), Document(page_content="woof woof woof", metadata={"source": "doggy.txt"}), ] index( changed_doggy_docs, record_manager, vectorstore, cleanup="incremental", source_id_key="source", ) vectorstore.similarity_search("dog", k=30) from langchain_community.document_loaders.base import BaseLoader class MyCustomLoader(BaseLoader): def lazy_load(self): text_splitter = CharacterTextSplitter( separator="t", keep_separator=True, chunk_size=12, chunk_overlap=2 ) docs = [ Document(page_content="woof woof", metadata={"source": "doggy.txt"}), Document(page_content="woof woof woof", metadata={"source": "doggy.txt"}), ] yield from text_splitter.split_documents(docs) def load(self): return list(self.lazy_load()) _clear() loader = MyCustomLoader() loader.load()
index(loader, record_manager, vectorstore, cleanup="full", source_id_key="source")
langchain.indexes.index
get_ipython().run_line_magic('pip', 'install --upgrade --quiet azure-search-documents') get_ipython().run_line_magic('pip', 'install --upgrade --quiet azure-identity') import os from langchain_community.vectorstores.azuresearch import AzureSearch from langchain_openai import AzureOpenAIEmbeddings, OpenAIEmbeddings openai_api_key: str = "PLACEHOLDER FOR YOUR API KEY" openai_api_version: str = "2023-05-15" model: str = "text-embedding-ada-002" azure_endpoint: str = "PLACEHOLDER FOR YOUR AZURE OPENAI ENDPOINT" azure_openai_api_key: str = "PLACEHOLDER FOR YOUR AZURE OPENAI KEY" azure_openai_api_version: str = "2023-05-15" azure_deployment: str = "text-embedding-ada-002" vector_store_address: str = "YOUR_AZURE_SEARCH_ENDPOINT" vector_store_password: str = "YOUR_AZURE_SEARCH_ADMIN_KEY" embeddings: OpenAIEmbeddings = OpenAIEmbeddings( openai_api_key=openai_api_key, openai_api_version=openai_api_version, model=model ) embeddings: AzureOpenAIEmbeddings = AzureOpenAIEmbeddings( azure_deployment=azure_deployment, openai_api_version=azure_openai_api_version, azure_endpoint=azure_endpoint, api_key=azure_openai_api_key, ) index_name: str = "langchain-vector-demo" vector_store: AzureSearch = AzureSearch( azure_search_endpoint=vector_store_address, azure_search_key=vector_store_password, index_name=index_name, embedding_function=embeddings.embed_query, ) from langchain_community.document_loaders import TextLoader from langchain_text_splitters import CharacterTextSplitter loader = TextLoader("../../modules/state_of_the_union.txt", encoding="utf-8") documents = loader.load() text_splitter =
CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
langchain_text_splitters.CharacterTextSplitter
from langchain_community.vectorstores import Bagel texts = ["hello bagel", "hello langchain", "I love salad", "my car", "a dog"] cluster = Bagel.from_texts(cluster_name="testing", texts=texts) cluster.similarity_search("bagel", k=3) cluster.similarity_search_with_score("bagel", k=3) cluster.delete_cluster() from langchain_community.document_loaders import TextLoader from langchain_text_splitters import CharacterTextSplitter loader =
TextLoader("../../modules/state_of_the_union.txt")
langchain_community.document_loaders.TextLoader
api_key = "" from langchain_community.document_loaders import ToMarkdownLoader loader =
ToMarkdownLoader( url="https://python.langchain.com/docs/get_started/introduction", api_key=api_key )
langchain_community.document_loaders.ToMarkdownLoader
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) agent = initialize_agent( tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True ) agent.run("What is the weather in Pomfret?") tools = load_tools(["searchapi"], llm=llm) agent = initialize_agent( tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True ) agent.run("What is the weather in Pomfret?") tools =
load_tools(["serpapi"], llm=llm)
langchain.agents.load_tools
from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain_community.llms import GradientLLM import os from getpass import getpass if not os.environ.get("GRADIENT_ACCESS_TOKEN", None): os.environ["GRADIENT_ACCESS_TOKEN"] = getpass("gradient.ai access token:") if not os.environ.get("GRADIENT_WORKSPACE_ID", None): os.environ["GRADIENT_WORKSPACE_ID"] = getpass("gradient.ai workspace id:") get_ipython().run_line_magic('pip', 'install --upgrade --quiet gradientai') import gradientai client = gradientai.Gradient() models = client.list_models(only_base=True) for model in models: print(model.id) new_model = models[-1].create_model_adapter(name="my_model_adapter") new_model.id, new_model.name llm = GradientLLM( model="674119b5-f19e-4856-add2-767ae7f7d7ef_model_adapter", model_kwargs=dict(max_generated_token_count=128), ) template = """Question: {question} Answer: """ prompt =
PromptTemplate.from_template(template)
langchain.prompts.PromptTemplate.from_template
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain langchain-openai wikipedia') from operator import itemgetter from langchain.agents import AgentExecutor, load_tools from langchain.agents.format_scratchpad import format_to_openai_function_messages from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser from langchain.tools import WikipediaQueryRun from langchain_community.utilities import WikipediaAPIWrapper from langchain_core.prompt_values import ChatPromptValue from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain_openai import ChatOpenAI wiki = WikipediaQueryRun( api_wrapper=WikipediaAPIWrapper(top_k_results=5, doc_content_chars_max=10_000) ) tools = [wiki] prompt = ChatPromptTemplate.from_messages( [ ("system", "You are a helpful assistant"), ("user", "{input}"), MessagesPlaceholder(variable_name="agent_scratchpad"), ] ) llm = ChatOpenAI(model="gpt-3.5-turbo") agent = ( { "input": itemgetter("input"), "agent_scratchpad": lambda x: format_to_openai_function_messages( x["intermediate_steps"] ), } | prompt | llm.bind_functions(tools) | OpenAIFunctionsAgentOutputParser() ) agent_executor =
AgentExecutor(agent=agent, tools=tools, verbose=True)
langchain.agents.AgentExecutor
from langchain_community.tools.edenai import ( EdenAiExplicitImageTool, EdenAiObjectDetectionTool, EdenAiParsingIDTool, EdenAiParsingInvoiceTool, EdenAiSpeechToTextTool, EdenAiTextModerationTool, EdenAiTextToSpeechTool, ) from langchain.agents import AgentType, initialize_agent from langchain_community.llms import EdenAI llm = EdenAI( feature="text", provider="openai", params={"temperature": 0.2, "max_tokens": 250} ) tools = [ EdenAiTextModerationTool(providers=["openai"], language="en"), EdenAiObjectDetectionTool(providers=["google", "api4ai"]), EdenAiTextToSpeechTool(providers=["amazon"], language="en", voice="MALE"), EdenAiExplicitImageTool(providers=["amazon", "google"]), EdenAiSpeechToTextTool(providers=["amazon"]), EdenAiParsingIDTool(providers=["amazon", "klippa"], language="en"),
EdenAiParsingInvoiceTool(providers=["amazon", "google"], language="en")
langchain_community.tools.edenai.EdenAiParsingInvoiceTool
from langchain import hub from langchain.agents import AgentExecutor, create_react_agent from langchain_community.tools import WikipediaQueryRun from langchain_community.utilities import WikipediaAPIWrapper from langchain_openai import ChatOpenAI api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=100) tool =
WikipediaQueryRun(api_wrapper=api_wrapper)
langchain_community.tools.WikipediaQueryRun
from langchain_community.llms.azureml_endpoint import AzureMLOnlineEndpoint from langchain_community.llms.azureml_endpoint import ( AzureMLEndpointApiType, LlamaContentFormatter, ) from langchain_core.messages import HumanMessage llm = AzureMLOnlineEndpoint( endpoint_url="https://<your-endpoint>.<your_region>.inference.ml.azure.com/score", endpoint_api_type=AzureMLEndpointApiType.realtime, endpoint_api_key="my-api-key", content_formatter=LlamaContentFormatter(), model_kwargs={"temperature": 0.8, "max_new_tokens": 400}, ) response = llm.invoke("Write me a song about sparkling water:") response response = llm.invoke("Write me a song about sparkling water:", temperature=0.5) response from langchain_community.llms.azureml_endpoint import ( AzureMLEndpointApiType, LlamaContentFormatter, ) from langchain_core.messages import HumanMessage llm = AzureMLOnlineEndpoint( endpoint_url="https://<your-endpoint>.<your_region>.inference.ml.azure.com/v1/completions", endpoint_api_type=AzureMLEndpointApiType.serverless, endpoint_api_key="my-api-key", content_formatter=LlamaContentFormatter(), model_kwargs={"temperature": 0.8, "max_new_tokens": 400}, ) response = llm.invoke("Write me a song about sparkling water:") response import json import os from typing import Dict from langchain_community.llms.azureml_endpoint import ( AzureMLOnlineEndpoint, ContentFormatterBase, ) class CustomFormatter(ContentFormatterBase): content_type = "application/json" accepts = "application/json" def format_request_payload(self, prompt: str, model_kwargs: Dict) -> bytes: input_str = json.dumps( { "inputs": [prompt], "parameters": model_kwargs, "options": {"use_cache": False, "wait_for_model": True}, } ) return str.encode(input_str) def format_response_payload(self, output: bytes) -> str: response_json = json.loads(output) return response_json[0]["summary_text"] content_formatter = CustomFormatter() llm = AzureMLOnlineEndpoint( endpoint_api_type="realtime", endpoint_api_key=os.getenv("BART_ENDPOINT_API_KEY"), endpoint_url=os.getenv("BART_ENDPOINT_URL"), model_kwargs={"temperature": 0.8, "max_new_tokens": 400}, content_formatter=content_formatter, ) large_text = """On January 7, 2020, Blockberry Creative announced that HaSeul would not participate in the promotion for Loona's next album because of mental health concerns. She was said to be diagnosed with "intermittent anxiety symptoms" and would be taking time to focus on her health.[39] On February 5, 2020, Loona released their second EP titled [#] (read as hash), along with the title track "So What".[40] Although HaSeul did not appear in the title track, her vocals are featured on three other songs on the album, including "365". Once peaked at number 1 on the daily Gaon Retail Album Chart,[41] the EP then debuted at number 2 on the weekly Gaon Album Chart. On March 12, 2020, Loona won their first music show trophy with "So What" on Mnet's M Countdown.[42] On October 19, 2020, Loona released their third EP titled [12:00] (read as midnight),[43] accompanied by its first single "Why Not?". HaSeul was again not involved in the album, out of her own decision to focus on the recovery of her health.[44] The EP then became their first album to enter the Billboard 200, debuting at number 112.[45] On November 18, Loona released the music video for "Star", another song on [12:00].[46] Peaking at number 40, "Star" is Loona's first entry on the Billboard Mainstream Top 40, making them the second K-pop girl group to enter the chart.[47] On June 1, 2021, Loona announced that they would be having a comeback on June 28, with their fourth EP, [&] (read as and). [48] The following day, on June 2, a teaser was posted to Loona's official social media accounts showing twelve sets of eyes, confirming the return of member HaSeul who had been on hiatus since early 2020.[49] On June 12, group members YeoJin, Kim Lip, Choerry, and Go Won released the song "Yum-Yum" as a collaboration with Cocomong.[50] On September 8, they released another collaboration song named "Yummy-Yummy".[51] On June 27, 2021, Loona announced at the end of their special clip that they are making their Japanese debut on September 15 under Universal Music Japan sublabel EMI Records.[52] On August 27, it was announced that Loona will release the double A-side single, "Hula Hoop / Star Seed" on September 15, with a physical CD release on October 20.[53] In December, Chuu filed an injunction to suspend her exclusive contract with Blockberry Creative.[54][55] """ summarized_text = llm.invoke(large_text) print(summarized_text) from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain_community.llms.azureml_endpoint import DollyContentFormatter formatter_template = "Write a {word_count} word essay about {topic}." prompt = PromptTemplate( input_variables=["word_count", "topic"], template=formatter_template ) content_formatter =
DollyContentFormatter()
langchain_community.llms.azureml_endpoint.DollyContentFormatter
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()
langchain_community.document_loaders.parsers.OpenAIWhisperParser
get_ipython().run_line_magic('pip', 'install --upgrade --quiet bibtexparser pymupdf') from langchain_community.document_loaders import BibtexLoader import urllib.request urllib.request.urlretrieve( "https://www.fourmilab.ch/etexts/einstein/specrel/specrel.pdf", "einstein1905.pdf" ) bibtex_text = """ @article{einstein1915, title={Die Feldgleichungen der Gravitation}, abstract={Die Grundgleichungen der Gravitation, die ich hier entwickeln werde, wurden von mir in einer Abhandlung: ,,Die formale Grundlage der allgemeinen Relativit{\"a}tstheorie`` in den Sitzungsberichten der Preu{\ss}ischen Akademie der Wissenschaften 1915 ver{\"o}ffentlicht.}, author={Einstein, Albert}, journal={Sitzungsberichte der K{\"o}niglich Preu{\ss}ischen Akademie der Wissenschaften}, volume={1915}, number={1}, pages={844--847}, year={1915}, doi={10.1002/andp.19163540702}, link={https://onlinelibrary.wiley.com/doi/abs/10.1002/andp.19163540702}, file={einstein1905.pdf} } """ with open("./biblio.bib", "w") as file: file.write(bibtex_text) docs =
BibtexLoader("./biblio.bib")
langchain_community.document_loaders.BibtexLoader
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)
langchain_text_splitters.CharacterTextSplitter
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()
langchain_openai.OpenAIEmbeddings
get_ipython().run_cell_magic('writefile', 'wechat_chats.txt', '女朋友 2023/09/16 2:51 PM\n天气有点凉\n\n男朋友 2023/09/16 2:51 PM\n珍簟凉风著,瑶琴寄恨生。嵇君懒书札,底物慰秋情。\n\n女朋友 2023/09/16 3:06 PM\n忙什么呢\n\n男朋友 2023/09/16 3:06 PM\n今天只干成了一件像样的事\n那就是想你\n\n女朋友 2023/09/16 3:06 PM\n[动画表情]\n') import logging import re from typing import Iterator, List from langchain_community.chat_loaders import base as chat_loaders from langchain_core.messages import BaseMessage, HumanMessage logger = logging.getLogger() class WeChatChatLoader(chat_loaders.BaseChatLoader): def __init__(self, path: str): """ Initialize the Discord chat loader. Args: path: Path to the exported Discord chat text file. """ self.path = path self._message_line_regex = re.compile( r"(?P<sender>.+?) (?P<timestamp>\d{4}/\d{2}/\d{2} \d{1,2}:\d{2} (?:AM|PM))", # noqa ) def _append_message_to_results( self, results: List, current_sender: str, current_timestamp: str, current_content: List[str], ): content = "\n".join(current_content).strip() if not re.match(r"\[.*\]", content): results.append( HumanMessage( content=content, additional_kwargs={ "sender": current_sender, "events": [{"message_time": current_timestamp}], }, ) ) return results def _load_single_chat_session_from_txt( self, file_path: str ) -> chat_loaders.ChatSession: """ Load a single chat session from a text file. Args: file_path: Path to the text file containing the chat messages. Returns: A `ChatSession` object containing the loaded chat messages. """ with open(file_path, "r", encoding="utf-8") as file: lines = file.readlines() results: List[BaseMessage] = [] current_sender = None current_timestamp = None current_content = [] for line in lines: if re.match(self._message_line_regex, line): if current_sender and current_content: results = self._append_message_to_results( results, current_sender, current_timestamp, current_content ) current_sender, current_timestamp = re.match( self._message_line_regex, line ).groups() current_content = [] else: current_content.append(line.strip()) if current_sender and current_content: results = self._append_message_to_results( results, current_sender, current_timestamp, current_content ) return chat_loaders.ChatSession(messages=results) def lazy_load(self) -> Iterator[chat_loaders.ChatSession]: """ Lazy load the messages from the chat file and yield them in the required format. Yields: A `ChatSession` object containing the loaded chat messages. """ yield self._load_single_chat_session_from_txt(self.path) loader = WeChatChatLoader( path="./wechat_chats.txt", ) from typing import List from langchain_community.chat_loaders.base import ChatSession from langchain_community.chat_loaders.utils import ( map_ai_messages, merge_chat_runs, ) raw_messages = loader.lazy_load() merged_messages = merge_chat_runs(raw_messages) messages: List[ChatSession] = list(map_ai_messages(merged_messages, sender="男朋友")) messages from langchain_openai import ChatOpenAI llm =
ChatOpenAI()
langchain_openai.ChatOpenAI
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain langchain-openai') from langchain_core.runnables import RunnableParallel, RunnablePassthrough runnable = RunnableParallel( passed=RunnablePassthrough(), extra=RunnablePassthrough.assign(mult=lambda x: x["num"] * 3), modified=lambda x: x["num"] + 1, ) runnable.invoke({"num": 1}) from langchain_community.vectorstores import FAISS 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 vectorstore = FAISS.from_texts( ["harrison worked at kensho"], embedding=
OpenAIEmbeddings()
langchain_openai.OpenAIEmbeddings
get_ipython().run_line_magic('pip', 'install --upgrade --quiet typesense openapi-schema-pydantic langchain-openai tiktoken') import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import Typesense from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter loader = TextLoader("../../modules/state_of_the_union.txt") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.split_documents(documents) embeddings =
OpenAIEmbeddings()
langchain_openai.OpenAIEmbeddings
get_ipython().run_line_magic('pip', 'install --upgrade --quiet annoy') from langchain_community.embeddings import HuggingFaceEmbeddings from langchain_community.vectorstores import Annoy embeddings_func = HuggingFaceEmbeddings() texts = ["pizza is great", "I love salad", "my car", "a dog"] vector_store = Annoy.from_texts(texts, embeddings_func) vector_store_v2 = Annoy.from_texts( texts, embeddings_func, metric="dot", n_trees=100, n_jobs=1 ) vector_store.similarity_search("food", k=3) vector_store.similarity_search_with_score("food", k=3) from langchain_community.document_loaders import TextLoader from langchain_text_splitters import CharacterTextSplitter loader = TextLoader("../../modules/state_of_the_union.txtn.txtn.txt") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.split_documents(documents) docs[:5] vector_store_from_docs =
Annoy.from_documents(docs, embeddings_func)
langchain_community.vectorstores.Annoy.from_documents
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") documents = loader.load() text_splitter =
CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
langchain_text_splitters.CharacterTextSplitter
get_ipython().run_cell_magic('writefile', 'whatsapp_chat.txt', "[8/15/23, 9:12:33 AM] Dr. Feather: \u200eMessages and calls are end-to-end encrypted. No one outside of this chat, not even WhatsApp, can read or listen to them.\n[8/15/23, 9:12:43 AM] Dr. Feather: I spotted a rare Hyacinth Macaw yesterday in the Amazon Rainforest. Such a magnificent creature!\n\u200e[8/15/23, 9:12:48 AM] Dr. Feather: \u200eimage omitted\n[8/15/23, 9:13:15 AM] Jungle Jane: That's stunning! Were you able to observe its behavior?\n\u200e[8/15/23, 9:13:23 AM] Dr. Feather: \u200eimage omitted\n[8/15/23, 9:14:02 AM] Dr. Feather: Yes, it seemed quite social with other macaws. They're known for their playful nature.\n[8/15/23, 9:14:15 AM] Jungle Jane: How's the research going on parrot communication?\n\u200e[8/15/23, 9:14:30 AM] Dr. Feather: \u200eimage omitted\n[8/15/23, 9:14:50 AM] Dr. Feather: It's progressing well. We're learning so much about how they use sound and color to communicate.\n[8/15/23, 9:15:10 AM] Jungle Jane: That's fascinating! Can't wait to read your paper on it.\n[8/15/23, 9:15:20 AM] Dr. Feather: Thank you! I'll send you a draft soon.\n[8/15/23, 9:25:16 PM] Jungle Jane: Looking forward to it! Keep up the great work.\n") from langchain_community.chat_loaders.whatsapp import WhatsAppChatLoader loader = WhatsAppChatLoader( path="./whatsapp_chat.txt", ) from typing import List from langchain_community.chat_loaders.base import ChatSession from langchain_community.chat_loaders.utils import ( map_ai_messages, merge_chat_runs, ) raw_messages = loader.lazy_load() merged_messages =
merge_chat_runs(raw_messages)
langchain_community.chat_loaders.utils.merge_chat_runs
get_ipython().run_line_magic('pip', 'install --upgrade --quiet cohere') get_ipython().run_line_magic('pip', 'install --upgrade --quiet faiss') get_ipython().run_line_magic('pip', 'install --upgrade --quiet faiss-cpu') import getpass import os os.environ["COHERE_API_KEY"] = getpass.getpass("Cohere API Key:") def pretty_print_docs(docs): print( f"\n{'-' * 100}\n".join( [f"Document {i+1}:\n\n" + d.page_content for i, d in enumerate(docs)] ) ) from langchain_community.document_loaders import TextLoader from langchain_community.embeddings import CohereEmbeddings from langchain_community.vectorstores import FAISS from langchain_text_splitters import RecursiveCharacterTextSplitter documents = TextLoader("../../modules/state_of_the_union.txt").load() text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100) texts = text_splitter.split_documents(documents) retriever = FAISS.from_documents(texts,
CohereEmbeddings()
langchain_community.embeddings.CohereEmbeddings
from langchain_community.document_loaders import UnstructuredODTLoader loader =
UnstructuredODTLoader("example_data/fake.odt", mode="elements")
langchain_community.document_loaders.UnstructuredODTLoader
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() pod = "vdb" store = "langchain_rag_store" vector_index = "v" vector_type = "cosine_fraction_float" vector_dimension = 1536 vectorstore = Jaguar( pod, store, vector_index, vector_type, vector_dimension, url, embeddings ) """ Login must be performed to authorize the client. The environment variable JAGUAR_API_KEY or file $HOME/.jagrc should contain the API key for accessing JaguarDB servers. """ vectorstore.login() """ Create vector store on the JaguarDB database server. This should be done only once. """ metadata = "category char(16)" text_size = 4096 vectorstore.create(metadata, text_size) """ Add the texts from the text splitter to our vectorstore """ vectorstore.add_documents(docs) """ Get the retriever object """ retriever = vectorstore.as_retriever() template = """You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise. Question: {question} Context: {context} Answer: """ prompt =
ChatPromptTemplate.from_template(template)
langchain_core.prompts.ChatPromptTemplate.from_template
get_ipython().run_line_magic('pip', 'install --upgrade --quiet doctran') import json from langchain_community.document_transformers import DoctranQATransformer from langchain_core.documents import Document from dotenv import load_dotenv load_dotenv() sample_text = """[Generated with ChatGPT] Confidential Document - For Internal Use Only Date: July 1, 2023 Subject: Updates and Discussions on Various Topics Dear Team, I hope this email finds you well. In this document, I would like to provide you with some important updates and discuss various topics that require our attention. Please treat the information contained herein as highly confidential. Security and Privacy Measures As part of our ongoing commitment to ensure the security and privacy of our customers' data, we have implemented robust measures across all our systems. We would like to commend John Doe (email: john.doe@example.com) from the IT department for his diligent work in enhancing our network security. Moving forward, we kindly remind everyone to strictly adhere to our data protection policies and guidelines. Additionally, if you come across any potential security risks or incidents, please report them immediately to our dedicated team at security@example.com. HR Updates and Employee Benefits Recently, we welcomed several new team members who have made significant contributions to their respective departments. I would like to recognize Jane Smith (SSN: 049-45-5928) for her outstanding performance in customer service. Jane has consistently received positive feedback from our clients. Furthermore, please remember that the open enrollment period for our employee benefits program is fast approaching. Should you have any questions or require assistance, please contact our HR representative, Michael Johnson (phone: 418-492-3850, email: michael.johnson@example.com). Marketing Initiatives and Campaigns Our marketing team has been actively working on developing new strategies to increase brand awareness and drive customer engagement. We would like to thank Sarah Thompson (phone: 415-555-1234) for her exceptional efforts in managing our social media platforms. Sarah has successfully increased our follower base by 20% in the past month alone. Moreover, please mark your calendars for the upcoming product launch event on July 15th. We encourage all team members to attend and support this exciting milestone for our company. Research and Development Projects In our pursuit of innovation, our research and development department has been working tirelessly on various projects. I would like to acknowledge the exceptional work of David Rodriguez (email: david.rodriguez@example.com) in his role as project lead. David's contributions to the development of our cutting-edge technology have been instrumental. Furthermore, we would like to remind everyone to share their ideas and suggestions for potential new projects during our monthly R&D brainstorming session, scheduled for July 10th. Please treat the information in this document with utmost confidentiality and ensure that it is not shared with unauthorized individuals. If you have any questions or concerns regarding the topics discussed, please do not hesitate to reach out to me directly. Thank you for your attention, and let's continue to work together to achieve our goals. Best regards, Jason Fan Cofounder & CEO Psychic jason@psychic.dev """ print(sample_text) documents = [
Document(page_content=sample_text)
langchain_core.documents.Document
import os os.environ["SCENEX_API_KEY"] = "<YOUR_API_KEY>" from langchain.agents import load_tools tools = load_tools(["sceneXplain"]) from langchain.tools import SceneXplainTool tool = SceneXplainTool() from langchain.agents import initialize_agent from langchain.memory import ConversationBufferMemory from langchain_openai import OpenAI llm =
OpenAI(temperature=0)
langchain_openai.OpenAI
import requests def download_drive_file(url: str, output_path: str = "chat.db") -> None: file_id = url.split("/")[-2] download_url = f"https://drive.google.com/uc?export=download&id={file_id}" response = requests.get(download_url) if response.status_code != 200: print("Failed to download the file.") return with open(output_path, "wb") as file: file.write(response.content) print(f"File {output_path} downloaded.") url = ( "https://drive.google.com/file/d/1NebNKqTA2NXApCmeH6mu0unJD2tANZzo/view?usp=sharing" ) download_drive_file(url) from langchain_community.chat_loaders.imessage import IMessageChatLoader loader = IMessageChatLoader( path="./chat.db", ) from typing import List from langchain_community.chat_loaders.base import ChatSession from langchain_community.chat_loaders.utils import ( map_ai_messages, merge_chat_runs, ) raw_messages = loader.lazy_load() merged_messages = merge_chat_runs(raw_messages) chat_sessions: List[ChatSession] = list( map_ai_messages(merged_messages, sender="Tortoise") ) chat_sessions[0]["messages"][:3] from langchain.adapters.openai import convert_messages_for_finetuning training_data = convert_messages_for_finetuning(chat_sessions) print(f"Prepared {len(training_data)} dialogues for training") get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-openai') import json import time from io import BytesIO import openai my_file = BytesIO() for m in training_data: my_file.write((json.dumps({"messages": m}) + "\n").encode("utf-8")) my_file.seek(0) training_file = openai.files.create(file=my_file, purpose="fine-tune") status = openai.files.retrieve(training_file.id).status start_time = time.time() while status != "processed": print(f"Status=[{status}]... {time.time() - start_time:.2f}s", end="\r", flush=True) time.sleep(5) status = openai.files.retrieve(training_file.id).status print(f"File {training_file.id} ready after {time.time() - start_time:.2f} seconds.") job = openai.fine_tuning.jobs.create( training_file=training_file.id, model="gpt-3.5-turbo", ) status = openai.fine_tuning.jobs.retrieve(job.id).status start_time = time.time() while status != "succeeded": print(f"Status=[{status}]... {time.time() - start_time:.2f}s", end="\r", flush=True) time.sleep(5) job = openai.fine_tuning.jobs.retrieve(job.id) status = job.status print(job.fine_tuned_model) from langchain_openai import ChatOpenAI model = ChatOpenAI( model=job.fine_tuned_model, temperature=1, ) from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate prompt =
ChatPromptTemplate.from_messages( [ ("system", "You are speaking to hare.")
langchain_core.prompts.ChatPromptTemplate.from_messages
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), ] def observe(self, obs, rew=0, term=False, trunc=False, info=None): self.ret += rew obs_message = f""" Observation: {obs} Reward: {rew} Termination: {term} Truncation: {trunc} Return: {self.ret} """ self.message_history.append(
HumanMessage(content=obs_message)
langchain.schema.HumanMessage
import re from typing import Union from langchain.agents import ( AgentExecutor, AgentOutputParser, LLMSingleActionAgent, ) from langchain.chains import LLMChain from langchain.prompts import StringPromptTemplate from langchain_community.agent_toolkits import NLAToolkit from langchain_community.tools.plugin import AIPlugin from langchain_core.agents import AgentAction, AgentFinish from langchain_openai import OpenAI llm = OpenAI(temperature=0) urls = [ "https://datasette.io/.well-known/ai-plugin.json", "https://api.speak.com/.well-known/ai-plugin.json", "https://www.wolframalpha.com/.well-known/ai-plugin.json", "https://www.zapier.com/.well-known/ai-plugin.json", "https://www.klarna.com/.well-known/ai-plugin.json", "https://www.joinmilo.com/.well-known/ai-plugin.json", "https://slack.com/.well-known/ai-plugin.json", "https://schooldigger.com/.well-known/ai-plugin.json", ] AI_PLUGINS = [AIPlugin.from_url(url) for url in urls] from langchain_community.vectorstores import FAISS from langchain_core.documents import Document from langchain_openai import OpenAIEmbeddings embeddings = OpenAIEmbeddings() docs = [ Document( page_content=plugin.description_for_model, metadata={"plugin_name": plugin.name_for_model}, ) for plugin in AI_PLUGINS ] vector_store = FAISS.from_documents(docs, embeddings) toolkits_dict = { plugin.name_for_model: NLAToolkit.from_llm_and_ai_plugin(llm, plugin) for plugin in AI_PLUGINS } retriever = vector_store.as_retriever() def get_tools(query): docs = retriever.get_relevant_documents(query) tool_kits = [toolkits_dict[d.metadata["plugin_name"]] for d in docs] tools = [] for tk in tool_kits: tools.extend(tk.nla_tools) return tools tools = get_tools("What could I do today with my kiddo") [t.name for t in tools] tools = get_tools("what shirts can i buy?") [t.name for t in tools] template = """Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools: {tools} 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: the input to the action Observation: the result of the action ... (this Thought/Action/Action Input/Observation can repeat N times) Thought: I now know the final answer Final Answer: the final answer to the original input question Begin! Remember to speak as a pirate when giving your final answer. Use lots of "Arg"s Question: {input} {agent_scratchpad}""" from typing import Callable class CustomPromptTemplate(StringPromptTemplate): template: str tools_getter: Callable def format(self, **kwargs) -> str: intermediate_steps = kwargs.pop("intermediate_steps") thoughts = "" for action, observation in intermediate_steps: thoughts += action.log thoughts += f"\nObservation: {observation}\nThought: " kwargs["agent_scratchpad"] = thoughts tools = self.tools_getter(kwargs["input"]) kwargs["tools"] = "\n".join( [f"{tool.name}: {tool.description}" for tool in tools] ) kwargs["tool_names"] = ", ".join([tool.name for tool in tools]) return self.template.format(**kwargs) prompt = CustomPromptTemplate( template=template, tools_getter=get_tools, input_variables=["input", "intermediate_steps"], ) class CustomOutputParser(AgentOutputParser): def parse(self, llm_output: str) -> Union[AgentAction, AgentFinish]: if "Final Answer:" in llm_output: return AgentFinish( return_values={"output": llm_output.split("Final Answer:")[-1].strip()}, log=llm_output, ) regex = r"Action\s*\d*\s*:(.*?)\nAction\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)" match = re.search(regex, llm_output, re.DOTALL) if not match: raise ValueError(f"Could not parse LLM output: `{llm_output}`") action = match.group(1).strip() action_input = match.group(2) return AgentAction( tool=action, tool_input=action_input.strip(" ").strip('"'), log=llm_output ) output_parser = CustomOutputParser() llm =
OpenAI(temperature=0)
langchain_openai.OpenAI
from langchain_community.document_loaders import AsyncHtmlLoader urls = ["https://www.espn.com", "https://lilianweng.github.io/posts/2023-06-23-agent/"] loader =
AsyncHtmlLoader(urls)
langchain_community.document_loaders.AsyncHtmlLoader
get_ipython().run_line_magic('pip', 'install -U --quiet langchain langchain_community openai chromadb langchain-experimental') get_ipython().run_line_magic('pip', 'install --quiet "unstructured[all-docs]" pypdf pillow pydantic lxml pillow matplotlib chromadb tiktoken') import logging import zipfile import requests logging.basicConfig(level=logging.INFO) data_url = "https://storage.googleapis.com/benchmarks-artifacts/langchain-docs-benchmarking/cj.zip" result = requests.get(data_url) filename = "cj.zip" with open(filename, "wb") as file: file.write(result.content) with zipfile.ZipFile(filename, "r") as zip_ref: zip_ref.extractall() from langchain_community.document_loaders import PyPDFLoader loader = PyPDFLoader("./cj/cj.pdf") docs = loader.load() tables = [] texts = [d.page_content for d in docs] len(texts) from langchain.prompts import PromptTemplate from langchain_community.chat_models import ChatVertexAI from langchain_community.llms import VertexAI from langchain_core.messages import AIMessage from langchain_core.output_parsers import StrOutputParser from langchain_core.runnables import RunnableLambda def generate_text_summaries(texts, tables, summarize_texts=False): """ Summarize text elements texts: List of str tables: List of str summarize_texts: Bool to summarize texts """ prompt_text = """You are an assistant tasked with summarizing tables and text for retrieval. \ These summaries will be embedded and used to retrieve the raw text or table elements. \ Give a concise summary of the table or text that is well optimized for retrieval. Table or text: {element} """ prompt = PromptTemplate.from_template(prompt_text) empty_response = RunnableLambda( lambda x: AIMessage(content="Error processing document") ) model = VertexAI( temperature=0, model_name="gemini-pro", max_output_tokens=1024 ).with_fallbacks([empty_response]) summarize_chain = {"element": lambda x: x} | prompt | model | StrOutputParser() text_summaries = [] table_summaries = [] if texts and summarize_texts: text_summaries = summarize_chain.batch(texts, {"max_concurrency": 1}) elif texts: text_summaries = texts if tables: table_summaries = summarize_chain.batch(tables, {"max_concurrency": 1}) return text_summaries, table_summaries text_summaries, table_summaries = generate_text_summaries( texts, tables, summarize_texts=True ) len(text_summaries) import base64 import os from langchain_core.messages import HumanMessage def encode_image(image_path): """Getting the base64 string""" with open(image_path, "rb") as image_file: return base64.b64encode(image_file.read()).decode("utf-8") def image_summarize(img_base64, prompt): """Make image summary""" model = ChatVertexAI(model_name="gemini-pro-vision", max_output_tokens=1024) msg = model( [ HumanMessage( content=[ {"type": "text", "text": prompt}, { "type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img_base64}"}, }, ] ) ] ) return msg.content def generate_img_summaries(path): """ Generate summaries and base64 encoded strings for images path: Path to list of .jpg files extracted by Unstructured """ img_base64_list = [] image_summaries = [] prompt = """You are an assistant tasked with summarizing images for retrieval. \ These summaries will be embedded and used to retrieve the raw image. \ Give a concise summary of the image that is well optimized for retrieval.""" for img_file in sorted(os.listdir(path)): if img_file.endswith(".jpg"): img_path = os.path.join(path, img_file) base64_image = encode_image(img_path) img_base64_list.append(base64_image) image_summaries.append(image_summarize(base64_image, prompt)) return img_base64_list, image_summaries img_base64_list, image_summaries = generate_img_summaries("./cj") len(image_summaries) import uuid from langchain.retrievers.multi_vector import MultiVectorRetriever from langchain.storage import InMemoryStore from langchain_community.embeddings import VertexAIEmbeddings from langchain_community.vectorstores import Chroma from langchain_core.documents import Document def create_multi_vector_retriever( vectorstore, text_summaries, texts, table_summaries, tables, image_summaries, images ): """ Create retriever that indexes summaries, but returns raw images or texts """ store = InMemoryStore() id_key = "doc_id" retriever = MultiVectorRetriever( vectorstore=vectorstore, docstore=store, id_key=id_key, ) def add_documents(retriever, doc_summaries, doc_contents): doc_ids = [str(uuid.uuid4()) for _ in doc_contents] summary_docs = [ Document(page_content=s, metadata={id_key: doc_ids[i]}) for i, s in enumerate(doc_summaries) ] retriever.vectorstore.add_documents(summary_docs) retriever.docstore.mset(list(zip(doc_ids, doc_contents))) if text_summaries: add_documents(retriever, text_summaries, texts) if table_summaries: add_documents(retriever, table_summaries, tables) if image_summaries: add_documents(retriever, image_summaries, images) return retriever vectorstore = Chroma( collection_name="mm_rag_cj_blog", embedding_function=
VertexAIEmbeddings(model_name="textembedding-gecko@latest")
langchain_community.embeddings.VertexAIEmbeddings
from langchain import hub from langchain.agents import AgentExecutor, create_openai_functions_agent from langchain_community.tools import WikipediaQueryRun from langchain_community.utilities import WikipediaAPIWrapper from langchain_openai import ChatOpenAI api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=100) tool =
WikipediaQueryRun(api_wrapper=api_wrapper)
langchain_community.tools.WikipediaQueryRun
get_ipython().run_line_magic('pip', 'install --upgrade --quiet promptlayer --upgrade') import promptlayer # Don't forget this 🍰 from langchain.callbacks import PromptLayerCallbackHandler from langchain.schema import ( HumanMessage, ) from langchain_openai import ChatOpenAI chat_llm = ChatOpenAI( temperature=0, callbacks=[PromptLayerCallbackHandler(pl_tags=["chatopenai"])], ) llm_results = chat_llm( [ HumanMessage(content="What comes after 1,2,3 ?"),
HumanMessage(content="Tell me another joke?")
langchain.schema.HumanMessage
from langchain_community.document_loaders import TomlLoader loader =
TomlLoader("example_data/fake_rule.toml")
langchain_community.document_loaders.TomlLoader
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()
langchain_community.document_loaders.parsers.OpenAIWhisperParserLocal
get_ipython().run_line_magic('pip', 'install --upgrade --quiet google-search-results') import os from langchain_community.tools.google_trends import GoogleTrendsQueryRun from langchain_community.utilities.google_trends import GoogleTrendsAPIWrapper os.environ["SERPAPI_API_KEY"] = "" tool = GoogleTrendsQueryRun(api_wrapper=
GoogleTrendsAPIWrapper()
langchain_community.utilities.google_trends.GoogleTrendsAPIWrapper
import os import yaml get_ipython().system('wget https://raw.githubusercontent.com/openai/openai-openapi/master/openapi.yaml -O openai_openapi.yaml') get_ipython().system('wget https://www.klarna.com/us/shopping/public/openai/v0/api-docs -O klarna_openapi.yaml') get_ipython().system('wget https://raw.githubusercontent.com/APIs-guru/openapi-directory/main/APIs/spotify.com/1.0.0/openapi.yaml -O spotify_openapi.yaml') from langchain_community.agent_toolkits.openapi.spec import reduce_openapi_spec with open("openai_openapi.yaml") as f: raw_openai_api_spec = yaml.load(f, Loader=yaml.Loader) openai_api_spec = reduce_openapi_spec(raw_openai_api_spec) with open("klarna_openapi.yaml") as f: raw_klarna_api_spec = yaml.load(f, Loader=yaml.Loader) klarna_api_spec = reduce_openapi_spec(raw_klarna_api_spec) with open("spotify_openapi.yaml") as f: raw_spotify_api_spec = yaml.load(f, Loader=yaml.Loader) spotify_api_spec = reduce_openapi_spec(raw_spotify_api_spec) import spotipy.util as util from langchain.requests import RequestsWrapper def construct_spotify_auth_headers(raw_spec: dict): scopes = list( raw_spec["components"]["securitySchemes"]["oauth_2_0"]["flows"][ "authorizationCode" ]["scopes"].keys() ) access_token = util.prompt_for_user_token(scope=",".join(scopes)) return {"Authorization": f"Bearer {access_token}"} headers = construct_spotify_auth_headers(raw_spotify_api_spec) requests_wrapper = RequestsWrapper(headers=headers) endpoints = [ (route, operation) for route, operations in raw_spotify_api_spec["paths"].items() for operation in operations if operation in ["get", "post"] ] len(endpoints) import tiktoken enc = tiktoken.encoding_for_model("gpt-4") def count_tokens(s): return len(enc.encode(s)) count_tokens(yaml.dump(raw_spotify_api_spec)) from langchain_community.agent_toolkits.openapi import planner from langchain_openai import OpenAI llm = OpenAI(model_name="gpt-4", temperature=0.0) spotify_agent = planner.create_openapi_agent(spotify_api_spec, requests_wrapper, llm) user_query = ( "make me a playlist with the first song from kind of blue. call it machine blues." ) spotify_agent.run(user_query) user_query = "give me a song I'd like, make it blues-ey" spotify_agent.run(user_query) headers = {"Authorization": f"Bearer {os.getenv('OPENAI_API_KEY')}"} openai_requests_wrapper = RequestsWrapper(headers=headers) llm = OpenAI(model_name="gpt-4", temperature=0.25) openai_agent = planner.create_openapi_agent( openai_api_spec, openai_requests_wrapper, llm ) user_query = "generate a short piece of advice" openai_agent.run(user_query) from langchain.agents import create_openapi_agent from langchain_community.agent_toolkits import OpenAPIToolkit from langchain_community.tools.json.tool import JsonSpec from langchain_openai import OpenAI with open("openai_openapi.yaml") as f: data = yaml.load(f, Loader=yaml.FullLoader) json_spec = JsonSpec(dict_=data, max_value_length=4000) openapi_toolkit = OpenAPIToolkit.from_llm(
OpenAI(temperature=0)
langchain_openai.OpenAI
get_ipython().run_line_magic('pip', 'install --upgrade --quiet googlemaps') import os os.environ["GPLACES_API_KEY"] = "" from langchain.tools import GooglePlacesTool places =
GooglePlacesTool()
langchain.tools.GooglePlacesTool
get_ipython().run_line_magic('pip', 'install --upgrade --quiet slack_sdk > /dev/null') get_ipython().run_line_magic('pip', 'install --upgrade --quiet beautifulsoup4 > /dev/null # This is optional but is useful for parsing HTML messages') get_ipython().run_line_magic('pip', 'install --upgrade --quiet python-dotenv > /dev/null # This is for loading environmental variables from a .env file') import dotenv dotenv.load_dotenv() from langchain_community.agent_toolkits import SlackToolkit toolkit = SlackToolkit() tools = toolkit.get_tools() tools from langchain import hub from langchain.agents import AgentExecutor, create_react_agent from langchain_openai import ChatOpenAI llm = ChatOpenAI(temperature=0, model="gpt-4") prompt = hub.pull("hwchase17/react") agent = create_react_agent( tools=toolkit.get_tools(), llm=llm, prompt=prompt, ) agent_executor =
AgentExecutor(agent=agent, tools=tools, verbose=True)
langchain.agents.AgentExecutor
get_ipython().run_line_magic('pip', 'install --upgrade --quiet singlestoredb') import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import SingleStoreDB from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter loader = TextLoader("../../modules/state_of_the_union.txt") documents = loader.load() text_splitter =
CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
langchain_text_splitters.CharacterTextSplitter
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain langsmith langchainhub --quiet') get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-openai tiktoken pandas duckduckgo-search --quiet') import os from uuid import uuid4 unique_id = uuid4().hex[0:8] os.environ["LANGCHAIN_TRACING_V2"] = "true" os.environ["LANGCHAIN_PROJECT"] = f"Tracing Walkthrough - {unique_id}" os.environ["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com" os.environ["LANGCHAIN_API_KEY"] = "<YOUR-API-KEY>" # Update to your API key os.environ["OPENAI_API_KEY"] = "<YOUR-OPENAI-API-KEY>" from langsmith import Client client = Client() from langchain import hub from langchain.agents import AgentExecutor from langchain.agents.format_scratchpad.openai_tools import ( format_to_openai_tool_messages, ) from langchain.agents.output_parsers.openai_tools import OpenAIToolsAgentOutputParser from langchain_community.tools import DuckDuckGoSearchResults from langchain_openai import ChatOpenAI prompt =
hub.pull("wfh/langsmith-agent-prompt:5d466cbc")
langchain.hub.pull
get_ipython().system('pip3 install clickhouse-sqlalchemy InstructorEmbedding sentence_transformers openai langchain-experimental') import getpass from os import environ from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain_community.utilities import SQLDatabase from langchain_experimental.sql.vector_sql import VectorSQLDatabaseChain from langchain_openai import OpenAI from sqlalchemy import MetaData, create_engine MYSCALE_HOST = "msc-4a9e710a.us-east-1.aws.staging.myscale.cloud" MYSCALE_PORT = 443 MYSCALE_USER = "chatdata" MYSCALE_PASSWORD = "myscale_rocks" OPENAI_API_KEY = getpass.getpass("OpenAI API Key:") engine = create_engine( f"clickhouse://{MYSCALE_USER}:{MYSCALE_PASSWORD}@{MYSCALE_HOST}:{MYSCALE_PORT}/default?protocol=https" ) metadata = MetaData(bind=engine) environ["OPENAI_API_KEY"] = OPENAI_API_KEY from langchain_community.embeddings import HuggingFaceInstructEmbeddings from langchain_experimental.sql.vector_sql import VectorSQLOutputParser output_parser = VectorSQLOutputParser.from_embeddings( model=HuggingFaceInstructEmbeddings( model_name="hkunlp/instructor-xl", model_kwargs={"device": "cpu"} ) ) from langchain.callbacks import StdOutCallbackHandler from langchain_community.utilities.sql_database import SQLDatabase from langchain_experimental.sql.prompt import MYSCALE_PROMPT from langchain_experimental.sql.vector_sql import VectorSQLDatabaseChain from langchain_openai import OpenAI chain = VectorSQLDatabaseChain( llm_chain=LLMChain( llm=
OpenAI(openai_api_key=OPENAI_API_KEY, temperature=0)
langchain_openai.OpenAI
get_ipython().run_line_magic('pip', 'install --upgrade --quiet pgvector') get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-openai') get_ipython().run_line_magic('pip', 'install --upgrade --quiet psycopg2-binary') get_ipython().run_line_magic('pip', 'install --upgrade --quiet tiktoken') import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") from dotenv import load_dotenv load_dotenv() from langchain.docstore.document import Document from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores.pgvector import PGVector from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter loader = TextLoader("../../modules/state_of_the_union.txt") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings() CONNECTION_STRING = "postgresql+psycopg2://harrisonchase@localhost:5432/test3" COLLECTION_NAME = "state_of_the_union_test" db = PGVector.from_documents( embedding=embeddings, documents=docs, collection_name=COLLECTION_NAME, connection_string=CONNECTION_STRING, ) 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) docs_with_score = db.max_marginal_relevance_search_with_score(query) for doc, score in docs_with_score: print("-" * 80) print("Score: ", score) print(doc.page_content) print("-" * 80) store = PGVector( collection_name=COLLECTION_NAME, connection_string=CONNECTION_STRING, embedding_function=embeddings, ) store.add_documents([
Document(page_content="foo")
langchain.docstore.document.Document
with open("../docs/docs/modules/state_of_the_union.txt") as f: state_of_the_union = f.read() from langchain.chains import AnalyzeDocumentChain from langchain_openai import ChatOpenAI llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0) from langchain.chains.question_answering import load_qa_chain qa_chain = load_qa_chain(llm, chain_type="map_reduce") qa_document_chain =
AnalyzeDocumentChain(combine_docs_chain=qa_chain)
langchain.chains.AnalyzeDocumentChain
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain langchain-openai duckduckgo-search') from langchain.tools import DuckDuckGoSearchRun from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate from langchain_openai import ChatOpenAI search = DuckDuckGoSearchRun() template = """turn the following user input into a search query for a search engine: {input}""" prompt = ChatPromptTemplate.from_template(template) model = ChatOpenAI() chain = prompt | model |
StrOutputParser()
langchain_core.output_parsers.StrOutputParser
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")
langchain_core.messages.HumanMessage
import os os.environ["EXA_API_KEY"] = "..." get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-exa') get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain langchain-openai') from langchain_core.prompts import PromptTemplate from langchain_core.runnables import RunnableParallel, RunnablePassthrough from langchain_exa import ExaSearchRetriever, TextContentsOptions from langchain_openai import ChatOpenAI retriever = ExaSearchRetriever( k=5, text_contents_options=TextContentsOptions(max_length=200) ) prompt = PromptTemplate.from_template( """Answer the following query based on the following context: query: {query} <context> {context} </context""" ) llm = ChatOpenAI() chain = ( RunnableParallel({"context": retriever, "query": RunnablePassthrough()}) | prompt | llm ) chain.invoke("When is the best time to visit japan?") get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain-exa') from exa_py import Exa from langchain.agents import tool exa = Exa(api_key=os.environ["EXA_API_KEY"]) @tool def search(query: str): """Search for a webpage based on the query.""" return exa.search(f"{query}", use_autoprompt=True, num_results=5) @tool def find_similar(url: str): """Search for webpages similar to a given URL. The url passed in should be a URL returned from `search`. """ return exa.find_similar(url, num_results=5) @tool def get_contents(ids: list[str]): """Get the contents of a webpage. The ids passed in should be a list of ids returned from `search`. """ return exa.get_contents(ids) tools = [search, get_contents, find_similar] from langchain.agents import AgentExecutor, OpenAIFunctionsAgent from langchain_core.messages import SystemMessage from langchain_openai import ChatOpenAI llm = ChatOpenAI(temperature=0) system_message = SystemMessage( content="You are a web researcher who answers user questions by looking up information on the internet and retrieving contents of helpful documents. Cite your sources." ) agent_prompt = OpenAIFunctionsAgent.create_prompt(system_message) agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=agent_prompt) agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True) agent_executor.run("Summarize for me a fascinating article about cats.") from exa_py import Exa from langchain.agents import tool exa = Exa(api_key=os.environ["Exa_API_KEY"]) @tool def search(query: str, include_domains=None, start_published_date=None): """Search for a webpage based on the query. Set the optional include_domains (list[str]) parameter to restrict the search to a list of domains. Set the optional start_published_date (str) parameter to restrict the search to documents published after the date (YYYY-MM-DD). """ return exa.search_and_contents( f"{query}", use_autoprompt=True, num_results=5, include_domains=include_domains, start_published_date=start_published_date, ) @tool def find_similar(url: str): """Search for webpages similar to a given URL. The url passed in should be a URL returned from `search`. """ return exa.find_similar_and_contents(url, num_results=5) @tool def get_contents(ids: list[str]): """Get the contents of a webpage. The ids passed in should be a list of ids returned from `search`. """ return exa.get_contents(ids) tools = [search, get_contents, find_similar] from langchain.agents import AgentExecutor, OpenAIFunctionsAgent from langchain_core.messages import SystemMessage from langchain_openai import ChatOpenAI llm = ChatOpenAI(temperature=0, model="gpt-4") system_message = SystemMessage( content="You are a web researcher who answers user questions by looking up information on the internet and retrieving contents of helpful documents. Cite your sources." ) agent_prompt = OpenAIFunctionsAgent.create_prompt(system_message) agent =
OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=agent_prompt)
langchain.agents.OpenAIFunctionsAgent
from langchain.prompts import FewShotPromptTemplate, PromptTemplate from langchain.prompts.example_selector import ( MaxMarginalRelevanceExampleSelector, SemanticSimilarityExampleSelector, ) from langchain_community.vectorstores import FAISS from langchain_openai import OpenAIEmbeddings example_prompt = PromptTemplate( input_variables=["input", "output"], template="Input: {input}\nOutput: {output}", ) examples = [ {"input": "happy", "output": "sad"}, {"input": "tall", "output": "short"}, {"input": "energetic", "output": "lethargic"}, {"input": "sunny", "output": "gloomy"}, {"input": "windy", "output": "calm"}, ] example_selector = MaxMarginalRelevanceExampleSelector.from_examples( examples,
OpenAIEmbeddings()
langchain_openai.OpenAIEmbeddings
get_ipython().run_line_magic('pip', 'install --upgrade --quiet lark weaviate-client') from langchain_community.vectorstores import Weaviate from langchain_core.documents import Document from langchain_openai import OpenAIEmbeddings 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, "director": "Andrei Tarkovsky", "genre": "science fiction", "rating": 9.9, }, ), ] vectorstore = Weaviate.from_documents( docs, embeddings, weaviate_url="http://127.0.0.1:8080" ) 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
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() ) chain.invoke({"context": docs, "question": question}) rag_prompt_llama = hub.pull("rlm/rag-prompt-llama") rag_prompt_llama.messages chain = ( RunnablePassthrough.assign(context=RunnablePick("context") | format_docs) | rag_prompt_llama | llm | StrOutputParser() ) chain.invoke({"context": docs, "question": question}) retriever = vectorstore.as_retriever() qa_chain = ( {"context": retriever | format_docs, "question":
RunnablePassthrough()
langchain_core.runnables.RunnablePassthrough
from langchain_community.chat_models import ChatDatabricks from langchain_core.messages import HumanMessage from mlflow.deployments import get_deploy_client client = get_deploy_client("databricks") secret = "secrets/<scope>/openai-api-key" # replace `<scope>` with your scope name = "my-chat" # rename this if my-chat already exists client.create_endpoint( name=name, config={ "served_entities": [ { "name": "my-chat", "external_model": { "name": "gpt-4", "provider": "openai", "task": "llm/v1/chat", "openai_config": { "openai_api_key": "{{" + secret + "}}", }, }, } ], }, ) chat = ChatDatabricks( target_uri="databricks", endpoint=name, temperature=0.1, ) chat([HumanMessage(content="hello")]) from langchain_community.embeddings import DatabricksEmbeddings embeddings = DatabricksEmbeddings(endpoint="databricks-bge-large-en") embeddings.embed_query("hello")[:3] from langchain_community.llms import Databricks llm = Databricks(endpoint_name="dolly") llm("How are you?") llm("How are you?", stop=["."]) import os import dbutils os.environ["DATABRICKS_TOKEN"] = dbutils.secrets.get("myworkspace", "api_token") llm =
Databricks(host="myworkspace.cloud.databricks.com", endpoint_name="dolly")
langchain_community.llms.Databricks
get_ipython().run_line_magic('pip', 'install --quiet pypdf chromadb tiktoken openai') get_ipython().run_line_magic('pip', 'uninstall -y langchain-fireworks') get_ipython().run_line_magic('pip', 'install --editable /mnt/disks/data/langchain/libs/partners/fireworks') import fireworks print(fireworks) import fireworks.client import requests from langchain_community.document_loaders import PyPDFLoader url = "https://storage.googleapis.com/deepmind-media/gemma/gemma-report.pdf" response = requests.get(url, stream=True) file_name = "temp_file.pdf" with open(file_name, "wb") as pdf: pdf.write(response.content) loader = PyPDFLoader(file_name) data = loader.load() from langchain_text_splitters import RecursiveCharacterTextSplitter text_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=0) all_splits = text_splitter.split_documents(data) from langchain_community.vectorstores import Chroma from langchain_fireworks.embeddings import FireworksEmbeddings vectorstore = Chroma.from_documents( documents=all_splits, collection_name="rag-chroma", embedding=
FireworksEmbeddings()
langchain_fireworks.embeddings.FireworksEmbeddings
get_ipython().run_line_magic('pip', 'install --upgrade --quiet promptlayer') import os import promptlayer from langchain_community.llms import PromptLayerOpenAI from getpass import getpass PROMPTLAYER_API_KEY = getpass() os.environ["PROMPTLAYER_API_KEY"] = PROMPTLAYER_API_KEY from getpass import getpass OPENAI_API_KEY = getpass() os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY llm = PromptLayerOpenAI(pl_tags=["langchain"]) llm("I am a cat and I want") llm =
PromptLayerOpenAI(return_pl_id=True)
langchain_community.llms.PromptLayerOpenAI
get_ipython().run_line_magic('pip', 'install --upgrade --quiet langchain langchain-openai argilla') import os os.environ["ARGILLA_API_URL"] = "..." os.environ["ARGILLA_API_KEY"] = "..." os.environ["OPENAI_API_KEY"] = "..." import argilla as rg from packaging.version import parse as parse_version if parse_version(rg.__version__) < parse_version("1.8.0"): raise RuntimeError( "`FeedbackDataset` is only available in Argilla v1.8.0 or higher, please " "upgrade `argilla` as `pip install argilla --upgrade`." ) dataset = rg.FeedbackDataset( fields=[ rg.TextField(name="prompt"), rg.TextField(name="response"), ], questions=[ rg.RatingQuestion( name="response-rating", description="How would you rate the quality of the response?", values=[1, 2, 3, 4, 5], required=True, ), rg.TextQuestion( name="response-feedback", description="What feedback do you have for the response?", required=False, ), ], guidelines="You're asked to rate the quality of the response and provide feedback.", ) rg.init( api_url=os.environ["ARGILLA_API_URL"], api_key=os.environ["ARGILLA_API_KEY"], ) dataset.push_to_argilla("langchain-dataset") from langchain.callbacks import ArgillaCallbackHandler argilla_callback = ArgillaCallbackHandler( dataset_name="langchain-dataset", api_url=os.environ["ARGILLA_API_URL"], api_key=os.environ["ARGILLA_API_KEY"], ) from langchain.callbacks import ArgillaCallbackHandler, StdOutCallbackHandler from langchain_openai import OpenAI argilla_callback = ArgillaCallbackHandler( dataset_name="langchain-dataset", api_url=os.environ["ARGILLA_API_URL"], api_key=os.environ["ARGILLA_API_KEY"], ) callbacks = [StdOutCallbackHandler(), argilla_callback] llm = OpenAI(temperature=0.9, callbacks=callbacks) llm.generate(["Tell me a joke", "Tell me a poem"] * 3) from langchain.callbacks import ArgillaCallbackHandler, StdOutCallbackHandler from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain_openai import OpenAI argilla_callback = ArgillaCallbackHandler( dataset_name="langchain-dataset", api_url=os.environ["ARGILLA_API_URL"], api_key=os.environ["ARGILLA_API_KEY"], ) callbacks = [StdOutCallbackHandler(), argilla_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"}] synopsis_chain.apply(test_prompts) from langchain.agents import AgentType, initialize_agent, load_tools from langchain.callbacks import ArgillaCallbackHandler, StdOutCallbackHandler from langchain_openai import OpenAI argilla_callback = ArgillaCallbackHandler( dataset_name="langchain-dataset", api_url=os.environ["ARGILLA_API_URL"], api_key=os.environ["ARGILLA_API_KEY"], ) callbacks = [
StdOutCallbackHandler()
langchain.callbacks.StdOutCallbackHandler
get_ipython().system(' pip install langchain unstructured[all-docs] pydantic lxml') from typing import Any from pydantic import BaseModel from unstructured.partition.pdf import partition_pdf path = "/Users/rlm/Desktop/Papers/LLaVA/" 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_community.chat_models import ChatOllama from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate 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)
langchain_core.prompts.ChatPromptTemplate.from_template
get_ipython().run_line_magic('pip', 'install --upgrade --quiet vald-client-python') from langchain_community.document_loaders import TextLoader from langchain_community.embeddings import HuggingFaceEmbeddings from langchain_community.vectorstores import Vald from langchain_text_splitters import CharacterTextSplitter raw_documents = TextLoader("state_of_the_union.txt").load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) documents = text_splitter.split_documents(raw_documents) embeddings = HuggingFaceEmbeddings() db = Vald.from_documents(documents, embeddings, host="localhost", port=8080) query = "What did the president say about Ketanji Brown Jackson" docs = db.similarity_search(query) docs[0].page_content embedding_vector = embeddings.embed_query(query) docs = db.similarity_search_by_vector(embedding_vector) docs[0].page_content docs_and_scores = db.similarity_search_with_score(query) docs_and_scores[0] retriever = db.as_retriever(search_type="mmr") retriever.get_relevant_documents(query) db.max_marginal_relevance_search(query, k=2, fetch_k=10) import grpc with open("test_root_cacert.crt", "rb") as root: credentials = grpc.ssl_channel_credentials(root_certificates=root.read()) with open(".ztoken", "rb") as ztoken: token = ztoken.read().strip() metadata = [(b"athenz-role-auth", token)] from langchain_community.document_loaders import TextLoader from langchain_community.embeddings import HuggingFaceEmbeddings from langchain_community.vectorstores import Vald from langchain_text_splitters import CharacterTextSplitter raw_documents = TextLoader("state_of_the_union.txt").load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) documents = text_splitter.split_documents(raw_documents) embeddings =
HuggingFaceEmbeddings()
langchain_community.embeddings.HuggingFaceEmbeddings
import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass() from langchain_community.document_loaders import TextLoader from langchain_community.vectorstores import FAISS from langchain_openai import OpenAIEmbeddings from langchain_text_splitters import CharacterTextSplitter loader = TextLoader("../../modules/state_of_the_union.txt") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings() db =
FAISS.from_documents(docs, embeddings)
langchain_community.vectorstores.FAISS.from_documents
get_ipython().run_line_magic('pip', 'install --upgrade --quiet redis redisvl langchain-openai tiktoken lark') import getpass import os os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:") from langchain_community.vectorstores import Redis from langchain_core.documents import Document from langchain_openai import OpenAIEmbeddings embeddings =
OpenAIEmbeddings()
langchain_openai.OpenAIEmbeddings
get_ipython().run_line_magic('pip', 'install --upgrade --quiet pymysql') get_ipython().system('pip install sqlalchemy') get_ipython().system('pip install langchain') from langchain.chains import RetrievalQA from langchain_community.document_loaders import ( DirectoryLoader, UnstructuredMarkdownLoader, ) from langchain_community.vectorstores.apache_doris import ( ApacheDoris, ApacheDorisSettings, ) 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 def gen_apache_doris(update_vectordb, embeddings, settings): if update_vectordb: docsearch = ApacheDoris.from_documents(split_docs, embeddings, config=settings) else: docsearch =
ApacheDoris(embeddings, settings)
langchain_community.vectorstores.apache_doris.ApacheDoris
import os from getpass import getpass os.environ["OPENAI_API_KEY"] = getpass() activeloop_token = getpass("Activeloop Token:") os.environ["ACTIVELOOP_TOKEN"] = activeloop_token get_ipython().system('ls "../../../../../../libs"') from langchain_community.document_loaders import TextLoader root_dir = "../../../../../../libs" docs = [] for dirpath, dirnames, filenames in os.walk(root_dir): for file in filenames: if file.endswith(".py") and "*venv/" not in dirpath: try: loader = TextLoader(os.path.join(dirpath, file), encoding="utf-8") docs.extend(loader.load_and_split()) except Exception: pass print(f"{len(docs)}") from langchain_text_splitters import CharacterTextSplitter text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) texts = text_splitter.split_documents(docs) print(f"{len(texts)}") from langchain_openai import OpenAIEmbeddings embeddings =
OpenAIEmbeddings()
langchain_openai.OpenAIEmbeddings
get_ipython().run_line_magic('pip', 'install -U --quiet langchain langchain_community openai chromadb langchain-experimental') get_ipython().run_line_magic('pip', 'install --quiet "unstructured[all-docs]" pypdf pillow pydantic lxml pillow matplotlib chromadb tiktoken') import logging import zipfile import requests logging.basicConfig(level=logging.INFO) data_url = "https://storage.googleapis.com/benchmarks-artifacts/langchain-docs-benchmarking/cj.zip" result = requests.get(data_url) filename = "cj.zip" with open(filename, "wb") as file: file.write(result.content) with zipfile.ZipFile(filename, "r") as zip_ref: zip_ref.extractall() from langchain_community.document_loaders import PyPDFLoader loader = PyPDFLoader("./cj/cj.pdf") docs = loader.load() tables = [] texts = [d.page_content for d in docs] len(texts) from langchain.prompts import PromptTemplate from langchain_community.chat_models import ChatVertexAI from langchain_community.llms import VertexAI from langchain_core.messages import AIMessage from langchain_core.output_parsers import StrOutputParser from langchain_core.runnables import RunnableLambda def generate_text_summaries(texts, tables, summarize_texts=False): """ Summarize text elements texts: List of str tables: List of str summarize_texts: Bool to summarize texts """ prompt_text = """You are an assistant tasked with summarizing tables and text for retrieval. \ These summaries will be embedded and used to retrieve the raw text or table elements. \ Give a concise summary of the table or text that is well optimized for retrieval. Table or text: {element} """ prompt = PromptTemplate.from_template(prompt_text) empty_response = RunnableLambda( lambda x: AIMessage(content="Error processing document") ) model = VertexAI( temperature=0, model_name="gemini-pro", max_output_tokens=1024 ).with_fallbacks([empty_response]) summarize_chain = {"element": lambda x: x} | prompt | model | StrOutputParser() text_summaries = [] table_summaries = [] if texts and summarize_texts: text_summaries = summarize_chain.batch(texts, {"max_concurrency": 1}) elif texts: text_summaries = texts if tables: table_summaries = summarize_chain.batch(tables, {"max_concurrency": 1}) return text_summaries, table_summaries text_summaries, table_summaries = generate_text_summaries( texts, tables, summarize_texts=True ) len(text_summaries) import base64 import os from langchain_core.messages import HumanMessage def encode_image(image_path): """Getting the base64 string""" with open(image_path, "rb") as image_file: return base64.b64encode(image_file.read()).decode("utf-8") def image_summarize(img_base64, prompt): """Make image summary""" model = ChatVertexAI(model_name="gemini-pro-vision", max_output_tokens=1024) msg = model( [ HumanMessage( content=[ {"type": "text", "text": prompt}, { "type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img_base64}"}, }, ] ) ] ) return msg.content def generate_img_summaries(path): """ Generate summaries and base64 encoded strings for images path: Path to list of .jpg files extracted by Unstructured """ img_base64_list = [] image_summaries = [] prompt = """You are an assistant tasked with summarizing images for retrieval. \ These summaries will be embedded and used to retrieve the raw image. \ Give a concise summary of the image that is well optimized for retrieval.""" for img_file in sorted(os.listdir(path)): if img_file.endswith(".jpg"): img_path = os.path.join(path, img_file) base64_image = encode_image(img_path) img_base64_list.append(base64_image) image_summaries.append(image_summarize(base64_image, prompt)) return img_base64_list, image_summaries img_base64_list, image_summaries = generate_img_summaries("./cj") len(image_summaries) import uuid from langchain.retrievers.multi_vector import MultiVectorRetriever from langchain.storage import InMemoryStore from langchain_community.embeddings import VertexAIEmbeddings from langchain_community.vectorstores import Chroma from langchain_core.documents import Document def create_multi_vector_retriever( vectorstore, text_summaries, texts, table_summaries, tables, image_summaries, images ): """ Create retriever that indexes summaries, but returns raw images or texts """ store = InMemoryStore() id_key = "doc_id" retriever = MultiVectorRetriever( vectorstore=vectorstore, docstore=store, id_key=id_key, ) def add_documents(retriever, doc_summaries, doc_contents): doc_ids = [str(uuid.uuid4()) for _ in doc_contents] summary_docs = [
Document(page_content=s, metadata={id_key: doc_ids[i]})
langchain_core.documents.Document