Outline
Outline is an open-source collaborative knowledge base platform designed for team information sharing.
This notebook shows how to retrieve documents from your Outline instance into the Document format that is used downstream.
Setup
%pip install --upgrade --quiet langchain langchain-openai
You first need to create an api key for your Outline instance. Then you need to set the following environment variables:
import os
os.environ["OUTLINE_API_KEY"] = "xxx"
os.environ["OUTLINE_INSTANCE_URL"] = "https://app.getoutline.com"
OutlineRetriever
has these arguments:
- optional
top_k_results
: default=3. Use it to limit number of documents retrieved. - optional
load_all_available_meta
: default=False. By default only the most important fields retrieved:title
,source
(the url of the document). If True, other fields also retrieved. - optional
doc_content_chars_max
default=4000. Use it to limit the number of characters for each document retrieved.
get_relevant_documents()
has one argument, query
: free text which used to find documents in your Outline instance.
Examples
Running retriever
from langchain_community.retrievers import OutlineRetriever
API Reference:OutlineRetriever
retriever = OutlineRetriever()
retriever.invoke("LangChain", doc_content_chars_max=100)
[Document(page_content='This walkthrough demonstrates how to use an agent optimized for conversation. Other agents are often optimized for using tools to figure out the best response, which is not ideal in a conversational setting where you may want the agent to be able to chat with the user as well.\n\nIf we compare it to the standard ReAct agent, the main difference is the prompt. We want it to be much more conversational.\n\nfrom langchain.agents import AgentType, Tool, initialize_agent\n\nfrom langchain_openai import OpenAI\n\nfrom langchain.memory import ConversationBufferMemory\n\nfrom langchain_community.utilities import SerpAPIWrapper\n\nsearch = SerpAPIWrapper() tools = \\[ Tool( name="Current Search", func=search.run, description="useful for when you need to answer questions about current events or the current state of the world", ), \\]\n\n\\\nllm = OpenAI(temperature=0)\n\nUsing LCEL\n\nWe will first show how to create this agent using LCEL\n\nfrom langchain import hub\n\nfrom langchain.agents.format_scratchpad import format_log_to_str\n\nfrom langchain.agents.output_parsers import ReActSingleInputOutputParser\n\nfrom langchain.tools.render import render_text_description\n\nprompt = hub.pull("hwchase17/react-chat")\n\nprompt = prompt.partial( tools=render_text_description(tools), tool_names=", ".join(\\[[t.name](http://t.name) for t in tools\\]), )\n\nllm_with_stop = llm.bind(stop=\\["\\nObservation"\\])\n\nagent = ( { "input": lambda x: x\\["input"\\], "agent_scratchpad": lambda x: format_log_to_str(x\\["intermediate_steps"\\]), "chat_history": lambda x: x\\["chat_history"\\], } | prompt | llm_with_stop | ReActSingleInputOutputParser() )\n\nfrom langchain.agents import AgentExecutor\n\nmemory = ConversationBufferMemory(memory_key="chat_history") agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, memory=memory)\n\nagent_executor.invoke({"input": "hi, i am bob"})\\["output"\\]\n\n```\n> Entering new AgentExecutor chain...\n\nThought: Do I need to use a tool? No\nFinal Answer: Hi Bob, nice to meet you! How can I help you today?\n\n> Finished chain.\n```\n\n\\\n\'Hi Bob, nice to meet you! How can I help you today?\'\n\nagent_executor.invoke({"input": "whats my name?"})\\["output"\\]\n\n```\n> Entering new AgentExecutor chain...\n\nThought: Do I need to use a tool? No\nFinal Answer: Your name is Bob.\n\n> Finished chain.\n```\n\n\\\n\'Your name is Bob.\'\n\nagent_executor.invoke({"input": "what are some movies showing 9/21/2023?"})\\["output"\\]\n\n```\n> Entering new AgentExecutor chain...\n\nThought: Do I need to use a tool? Yes\nAction: Current Search\nAction Input: Movies showing 9/21/2023[\'September 2023 Movies: The Creator • Dumb Money • Expend4bles • The Kill Room • The Inventor • The Equalizer 3 • PAW Patrol: The Mighty Movie, ...\'] Do I need to use a tool? No\nFinal Answer: According to current search, some movies showing on 9/21/2023 are The Creator, Dumb Money, Expend4bles, The Kill Room, The Inventor, The Equalizer 3, and PAW Patrol: The Mighty Movie.\n\n> Finished chain.\n```\n\n\\\n\'According to current search, some movies showing on 9/21/2023 are The Creator, Dumb Money, Expend4bles, The Kill Room, The Inventor, The Equalizer 3, and PAW Patrol: The Mighty Movie.\'\n\n\\\nUse the off-the-shelf agent\n\nWe can also create this agent using the off-the-shelf agent class\n\nagent_executor = initialize_agent( tools, llm, agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory=memory, )\n\nUse a chat model\n\nWe can also use a chat model here. The main difference here is in the prompts used.\n\nfrom langchain import hub\n\nfrom langchain_openai import ChatOpenAI\n\nprompt = hub.pull("hwchase17/react-chat-json") chat_model = ChatOpenAI(temperature=0, model="gpt-4")\n\nprompt = prompt.partial( tools=render_text_description(tools), tool_names=", ".join(\\[[t.name](http://t.name) for t in tools\\]), )\n\nchat_model_with_stop = chat_model.bind(stop=\\["\\nObservation"\\])\n\nfrom langchain.agents.format_scratchpad import format_log_to_messages\n\nfrom langchain.agents.output_parsers import JSONAgentOutputParser\n\n# We need some extra steering, or the c', metadata={'title': 'Conversational', 'source': 'https://d01.getoutline.com/doc/conversational-B5dBkUgQ4b'}),
Document(page_content='Quickstart\n\nIn this quickstart we\'ll show you how to:\n\nGet setup with LangChain, LangSmith and LangServe\n\nUse the most basic and common components of LangChain: prompt templates, models, and output parsers\n\nUse LangChain Expression Language, the protocol that LangChain is built on and which facilitates component chaining\n\nBuild a simple application with LangChain\n\nTrace your application with LangSmith\n\nServe your application with LangServe\n\nThat\'s a fair amount to cover! Let\'s dive in.\n\nSetup\n\nInstallation\n\nTo install LangChain run:\n\nPip\n\nConda\n\npip install langchain\n\nFor more details, see our Installation guide.\n\nEnvironment\n\nUsing LangChain will usually require integrations with one or more model providers, data stores, APIs, etc. For this example, we\'ll use OpenAI\'s model APIs.\n\nFirst we\'ll need to install their Python package:\n\npip install openai\n\nAccessing the API requires an API key, which you can get by creating an account and heading here. Once we have a key we\'ll want to set it as an environment variable by running:\n\nexport OPENAI_API_KEY="..."\n\nIf you\'d prefer not to set an environment variable you can pass the key in directly via the openai_api_key named parameter when initiating the OpenAI LLM class:\n\nfrom langchain_openai import ChatOpenAI\n\nllm = ChatOpenAI(openai_api_key="...")\n\nLangSmith\n\nMany of the applications you build with LangChain will contain multiple steps with multiple invocations of LLM calls. As these applications get more and more complex, it becomes crucial to be able to inspect what exactly is going on inside your chain or agent. The best way to do this is with LangSmith.\n\nNote that LangSmith is not needed, but it is helpful. If you do want to use LangSmith, after you sign up at the link above, make sure to set your environment variables to start logging traces:\n\nexport LANGCHAIN_TRACING_V2="true" export LANGCHAIN_API_KEY=...\n\nLangServe\n\nLangServe helps developers deploy LangChain chains as a REST API. You do not need to use LangServe to use LangChain, but in this guide we\'ll show how you can deploy your app with LangServe.\n\nInstall with:\n\npip install "langserve\\[all\\]"\n\nBuilding with LangChain\n\nLangChain provides many modules that can be used to build language model applications. Modules can be used as standalones in simple applications and they can be composed for more complex use cases. Composition is powered by LangChain Expression Language (LCEL), which defines a unified Runnable interface that many modules implement, making it possible to seamlessly chain components.\n\nThe simplest and most common chain contains three things:\n\nLLM/Chat Model: The language model is the core reasoning engine here. In order to work with LangChain, you need to understand the different types of language models and how to work with them. Prompt Template: This provides instructions to the language model. This controls what the language model outputs, so understanding how to construct prompts and different prompting strategies is crucial. Output Parser: These translate the raw response from the language model to a more workable format, making it easy to use the output downstream. In this guide we\'ll cover those three components individually, and then go over how to combine them. Understanding these concepts will set you up well for being able to use and customize LangChain applications. Most LangChain applications allow you to configure the model and/or the prompt, so knowing how to take advantage of this will be a big enabler.\n\nLLM / Chat Model\n\nThere are two types of language models:\n\nLLM: underlying model takes a string as input and returns a string\n\nChatModel: underlying model takes a list of messages as input and returns a message\n\nStrings are simple, but what exactly are messages? The base message interface is defined by BaseMessage, which has two required attributes:\n\ncontent: The content of the message. Usually a string. role: The entity from which the BaseMessage is coming. LangChain provides several ob', metadata={'title': 'Quick Start', 'source': 'https://d01.getoutline.com/doc/quick-start-jGuGGGOTuL'}),
Document(page_content='This walkthrough showcases using an agent to implement the [ReAct](https://react-lm.github.io/) logic.\n\n```javascript\nfrom langchain.agents import AgentType, initialize_agent, load_tools\nfrom langchain_openai import OpenAI\n```\n\nFirst, let\'s load the language model we\'re going to use to control the agent.\n\n```javascript\nllm = OpenAI(temperature=0)\n```\n\nNext, let\'s load some tools to use. Note that the llm-math tool uses an LLM, so we need to pass that in.\n\n```javascript\ntools = load_tools(["serpapi", "llm-math"], llm=llm)\n```\n\n## Using LCEL[\u200b](/docs/modules/agents/agent_types/react#using-lcel "Direct link to Using LCEL")\n\nWe will first show how to create the agent using LCEL\n\n```javascript\nfrom langchain import hub\nfrom langchain.agents.format_scratchpad import format_log_to_str\nfrom langchain.agents.output_parsers import ReActSingleInputOutputParser\nfrom langchain.tools.render import render_text_description\n```\n\n```javascript\nprompt = hub.pull("hwchase17/react")\nprompt = prompt.partial(\n tools=render_text_description(tools),\n tool_names=", ".join([t.name for t in tools]),\n)\n```\n\n```javascript\nllm_with_stop = llm.bind(stop=["\\nObservation"])\n```\n\n```javascript\nagent = (\n {\n "input": lambda x: x["input"],\n "agent_scratchpad": lambda x: format_log_to_str(x["intermediate_steps"]),\n }\n | prompt\n | llm_with_stop\n | ReActSingleInputOutputParser()\n)\n```\n\n```javascript\nfrom langchain.agents import AgentExecutor\n```\n\n```javascript\nagent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)\n```\n\n```javascript\nagent_executor.invoke(\n {\n "input": "Who is Leo DiCaprio\'s girlfriend? What is her current age raised to the 0.43 power?"\n }\n)\n```\n\n```javascript\n \n \n > Entering new AgentExecutor chain...\n I need to find out who Leo DiCaprio\'s girlfriend is and then calculate her age raised to the 0.43 power.\n Action: Search\n Action Input: "Leo DiCaprio girlfriend"model Vittoria Ceretti I need to find out Vittoria Ceretti\'s age\n Action: Search\n Action Input: "Vittoria Ceretti age"25 years I need to calculate 25 raised to the 0.43 power\n Action: Calculator\n Action Input: 25^0.43Answer: 3.991298452658078 I now know the final answer\n Final Answer: Leo DiCaprio\'s girlfriend is Vittoria Ceretti and her current age raised to the 0.43 power is 3.991298452658078.\n \n > Finished chain.\n\n\n\n\n\n {\'input\': "Who is Leo DiCaprio\'s girlfriend? What is her current age raised to the 0.43 power?",\n \'output\': "Leo DiCaprio\'s girlfriend is Vittoria Ceretti and her current age raised to the 0.43 power is 3.991298452658078."}\n```\n\n## Using ZeroShotReactAgent[\u200b](/docs/modules/agents/agent_types/react#using-zeroshotreactagent "Direct link to Using ZeroShotReactAgent")\n\nWe will now show how to use the agent with an off-the-shelf agent implementation\n\n```javascript\nagent_executor = initialize_agent(\n tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True\n)\n```\n\n```javascript\nagent_executor.invoke(\n {\n "input": "Who is Leo DiCaprio\'s girlfriend? What is her current age raised to the 0.43 power?"\n }\n)\n```\n\n```javascript\n \n \n > Entering new AgentExecutor chain...\n I need to find out who Leo DiCaprio\'s girlfriend is and then calculate her age raised to the 0.43 power.\n Action: Search\n Action Input: "Leo DiCaprio girlfriend"\n Observation: model Vittoria Ceretti\n Thought: I need to find out Vittoria Ceretti\'s age\n Action: Search\n Action Input: "Vittoria Ceretti age"\n Observation: 25 years\n Thought: I need to calculate 25 raised to the 0.43 power\n Action: Calculator\n Action Input: 25^0.43\n Observation: Answer: 3.991298452658078\n Thought: I now know the final answer\n Final Answer: Leo DiCaprio\'s girlfriend is Vittoria Ceretti and her current age raised to the 0.43 power is 3.991298452658078.\n \n > Finished chain.\n\n\n\n\n\n {\'input\': "Who is L', metadata={'title': 'ReAct', 'source': 'https://d01.getoutline.com/doc/react-d6rxRS1MHk'})]
Answering Questions on Outline Documents
import os
from getpass import getpass
os.environ["OPENAI_API_KEY"] = getpass("OpenAI API Key:")
from langchain.chains import ConversationalRetrievalChain
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model="gpt-3.5-turbo")
qa = ConversationalRetrievalChain.from_llm(model, retriever=retriever)
API Reference:ConversationalRetrievalChain | ChatOpenAI
qa({"question": "what is langchain?", "chat_history": {}})
{'question': 'what is langchain?',
'chat_history': {},
'answer': "LangChain is a framework for developing applications powered by language models. It provides a set of libraries and tools that enable developers to build context-aware and reasoning-based applications. LangChain allows you to connect language models to various sources of context, such as prompt instructions, few-shot examples, and content, to enhance the model's responses. It also supports the composition of multiple language model components using LangChain Expression Language (LCEL). Additionally, LangChain offers off-the-shelf chains, templates, and integrations for easy application development. LangChain can be used in conjunction with LangSmith for debugging and monitoring chains, and with LangServe for deploying applications as a REST API."}
Related
- Retriever conceptual guide
- Retriever how-to guides