id
stringlengths
14
16
text
stringlengths
36
2.73k
source
stringlengths
49
117
588714d360f7-8
You can recover things like token usage from this LLMResult: result.llm_output['token_usage'] # -> {'prompt_tokens': 57, 'completion_tokens': 20, 'total_tokens': 77} Chat Prompt Templates# Similar to LLMs, you can make use of templating by using a MessagePromptTemplate. You can build a ChatPromptTemplate from one or more MessagePromptTemplates. You can use ChatPromptTemplate’s format_prompt – this returns a PromptValue, which you can convert to a string or Message object, depending on whether you want to use the formatted value as input to an llm or chat model. For convenience, there is a from_template method exposed on the template. If you were to use this template, this is what it would look like: from langchain.chat_models import ChatOpenAI from langchain.prompts.chat import ( ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate, ) chat = ChatOpenAI(temperature=0) template = "You are a helpful assistant that translates {input_language} to {output_language}." system_message_prompt = SystemMessagePromptTemplate.from_template(template) human_template = "{text}" human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt]) # get a chat completion from the formatted messages chat(chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.").to_messages()) # -> AIMessage(content="J'aime programmer.", additional_kwargs={}) Chains with Chat Models# The LLMChain discussed in the above section can be used with chat models as well: from langchain.chat_models import ChatOpenAI from langchain import LLMChain from langchain.prompts.chat import (
https://python.langchain.com/en/latest/getting_started/getting_started.html
588714d360f7-9
from langchain import LLMChain from langchain.prompts.chat import ( ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate, ) chat = ChatOpenAI(temperature=0) template = "You are a helpful assistant that translates {input_language} to {output_language}." system_message_prompt = SystemMessagePromptTemplate.from_template(template) human_template = "{text}" human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt]) chain = LLMChain(llm=chat, prompt=chat_prompt) chain.run(input_language="English", output_language="French", text="I love programming.") # -> "J'aime programmer." Agents with Chat Models# Agents can also be used with chat models, you can initialize one using AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION as the agent type. from langchain.agents import load_tools from langchain.agents import initialize_agent from langchain.agents import AgentType from langchain.chat_models import ChatOpenAI from langchain.llms import OpenAI # First, let's load the language model we're going to use to control the agent. chat = ChatOpenAI(temperature=0) # Next, let's load some tools to use. Note that the `llm-math` tool uses an LLM, so we need to pass that in. llm = OpenAI(temperature=0) tools = load_tools(["serpapi", "llm-math"], llm=llm) # Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use.
https://python.langchain.com/en/latest/getting_started/getting_started.html
588714d360f7-10
agent = initialize_agent(tools, chat, agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True) # Now let's test it out! agent.run("Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?") > Entering new AgentExecutor chain... Thought: I need to use a search engine to find Olivia Wilde's boyfriend and a calculator to raise his age to the 0.23 power. Action: { "action": "Search", "action_input": "Olivia Wilde boyfriend" } Observation: Sudeikis and Wilde's relationship ended in November 2020. Wilde was publicly served with court documents regarding child custody while she was presenting Don't Worry Darling at CinemaCon 2022. In January 2021, Wilde began dating singer Harry Styles after meeting during the filming of Don't Worry Darling. Thought:I need to use a search engine to find Harry Styles' current age. Action: { "action": "Search", "action_input": "Harry Styles age" } Observation: 29 years Thought:Now I need to calculate 29 raised to the 0.23 power. Action: { "action": "Calculator", "action_input": "29^0.23" } Observation: Answer: 2.169459462491557 Thought:I now know the final answer. Final Answer: 2.169459462491557 > Finished chain. '2.169459462491557' Memory: Add State to Chains and Agents#
https://python.langchain.com/en/latest/getting_started/getting_started.html
588714d360f7-11
'2.169459462491557' Memory: Add State to Chains and Agents# You can use Memory with chains and agents initialized with chat models. The main difference between this and Memory for LLMs is that rather than trying to condense all previous messages into a string, we can keep them as their own unique memory object. from langchain.prompts import ( ChatPromptTemplate, MessagesPlaceholder, SystemMessagePromptTemplate, HumanMessagePromptTemplate ) from langchain.chains import ConversationChain from langchain.chat_models import ChatOpenAI from langchain.memory import ConversationBufferMemory prompt = ChatPromptTemplate.from_messages([ SystemMessagePromptTemplate.from_template("The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know."), MessagesPlaceholder(variable_name="history"), HumanMessagePromptTemplate.from_template("{input}") ]) llm = ChatOpenAI(temperature=0) memory = ConversationBufferMemory(return_messages=True) conversation = ConversationChain(memory=memory, prompt=prompt, llm=llm) conversation.predict(input="Hi there!") # -> 'Hello! How can I assist you today?' conversation.predict(input="I'm doing well! Just having a conversation with an AI.") # -> "That sounds like fun! I'm happy to chat with you. Is there anything specific you'd like to talk about?" conversation.predict(input="Tell me about yourself.")
https://python.langchain.com/en/latest/getting_started/getting_started.html
588714d360f7-12
conversation.predict(input="Tell me about yourself.") # -> "Sure! I am an AI language model created by OpenAI. I was trained on a large dataset of text from the internet, which allows me to understand and generate human-like language. I can answer questions, provide information, and even have conversations like this one. Is there anything else you'd like to know about me?" previous Welcome to LangChain next Concepts Contents Installation Environment Setup Building a Language Model Application: LLMs LLMs: Get predictions from a language model Prompt Templates: Manage prompts for LLMs Chains: Combine LLMs and prompts in multi-step workflows Agents: Dynamically Call Chains Based on User Input Memory: Add State to Chains and Agents Building a Language Model Application: Chat Models Get Message Completions from a Chat Model Chat Prompt Templates Chains with Chat Models Agents with Chat Models Memory: Add State to Chains and Agents By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 11, 2023.
https://python.langchain.com/en/latest/getting_started/getting_started.html
abf278748d67-0
.md .pdf Tutorials Contents DeepLearning.AI course Handbook Tutorials Tutorials# ⛓ icon marks a new addition [last update 2023-05-15] DeepLearning.AI course# ⛓LangChain for LLM Application Development by Harrison Chase presented by Andrew Ng Handbook# LangChain AI Handbook By James Briggs and Francisco Ingham Tutorials# LangChain Tutorials by Edrick: ⛓ LangChain, Chroma DB, OpenAI Beginner Guide | ChatGPT with your PDF ⛓ LangChain 101: The Complete Beginner’s Guide LangChain Crash Course: Build an AutoGPT app in 25 minutes by Nicholas Renotte LangChain Crash Course - Build apps with language models by Patrick Loeber LangChain Explained in 13 Minutes | QuickStart Tutorial for Beginners by Rabbitmetrics # LangChain for Gen AI and LLMs by James Briggs: #1 Getting Started with GPT-3 vs. Open Source LLMs #2 Prompt Templates for GPT 3.5 and other LLMs #3 LLM Chains using GPT 3.5 and other LLMs #4 Chatbot Memory for Chat-GPT, Davinci + other LLMs #5 Chat with OpenAI in LangChain ⛓ #6 Fixing LLM Hallucinations with Retrieval Augmentation in LangChain ⛓ #7 LangChain Agents Deep Dive with GPT 3.5 ⛓ #8 Create Custom Tools for Chatbots in LangChain ⛓ #9 Build Conversational Agents with Vector DBs # LangChain 101 by Data Independent: What Is LangChain? - LangChain + ChatGPT Overview Quickstart Guide Beginner Guide To 7 Essential Concepts OpenAI + Wolfram Alpha
https://python.langchain.com/en/latest/getting_started/tutorials.html
abf278748d67-1
Quickstart Guide Beginner Guide To 7 Essential Concepts OpenAI + Wolfram Alpha Ask Questions On Your Custom (or Private) Files Connect Google Drive Files To OpenAI YouTube Transcripts + OpenAI Question A 300 Page Book (w/ OpenAI + Pinecone) Workaround OpenAI's Token Limit With Chain Types Build Your Own OpenAI + LangChain Web App in 23 Minutes Working With The New ChatGPT API OpenAI + LangChain Wrote Me 100 Custom Sales Emails Structured Output From OpenAI (Clean Dirty Data) Connect OpenAI To +5,000 Tools (LangChain + Zapier) Use LLMs To Extract Data From Text (Expert Mode) ⛓ Extract Insights From Interview Transcripts Using LLMs ⛓ 5 Levels Of LLM Summarizing: Novice to Expert # LangChain How to and guides by Sam Witteveen: LangChain Basics - LLMs & PromptTemplates with Colab LangChain Basics - Tools and Chains ChatGPT API Announcement & Code Walkthrough with LangChain Conversations with Memory (explanation & code walkthrough) Chat with Flan20B Using Hugging Face Models locally (code walkthrough) PAL : Program-aided Language Models with LangChain code Building a Summarization System with LangChain and GPT-3 - Part 1 Building a Summarization System with LangChain and GPT-3 - Part 2 Microsoft’s Visual ChatGPT using LangChain LangChain Agents - Joining Tools and Chains with Decisions Comparing LLMs with LangChain Using Constitutional AI in LangChain Talking to Alpaca with LangChain - Creating an Alpaca Chatbot Talk to your CSV & Excel with LangChain BabyAGI: Discover the Power of Task-Driven Autonomous Agents!
https://python.langchain.com/en/latest/getting_started/tutorials.html
abf278748d67-2
BabyAGI: Discover the Power of Task-Driven Autonomous Agents! Improve your BabyAGI with LangChain ⛓ Master PDF Chat with LangChain - Your essential guide to queries on documents ⛓ Using LangChain with DuckDuckGO Wikipedia & PythonREPL Tools ⛓ Building Custom Tools and Agents with LangChain (gpt-3.5-turbo) ⛓ LangChain Retrieval QA Over Multiple Files with ChromaDB ⛓ LangChain Retrieval QA with Instructor Embeddings & ChromaDB for PDFs ⛓ LangChain + Retrieval Local LLMs for Retrieval QA - No OpenAI!!! # LangChain by Prompt Engineering: LangChain Crash Course — All You Need to Know to Build Powerful Apps with LLMs Working with MULTIPLE PDF Files in LangChain: ChatGPT for your Data ChatGPT for YOUR OWN PDF files with LangChain Talk to YOUR DATA without OpenAI APIs: LangChain ⛓️ CHATGPT For WEBSITES: Custom ChatBOT # LangChain by Chat with data LangChain Beginner’s Tutorial for Typescript/Javascript GPT-4 Tutorial: How to Chat With Multiple PDF Files (~1000 pages of Tesla’s 10-K Annual Reports) GPT-4 & LangChain Tutorial: How to Chat With A 56-Page PDF Document (w/Pinecone) ⛓ LangChain & Supabase Tutorial: How to Build a ChatGPT Chatbot For Your Website # Get SH*T Done with Prompt Engineering and LangChain by Venelin Valkov Getting Started with LangChain: Load Custom Data, Run OpenAI Models, Embeddings and ChatGPT Loaders, Indexes & Vectorstores in LangChain: Question Answering on PDF files with ChatGPT
https://python.langchain.com/en/latest/getting_started/tutorials.html
abf278748d67-3
LangChain Models: ChatGPT, Flan Alpaca, OpenAI Embeddings, Prompt Templates & Streaming LangChain Chains: Use ChatGPT to Build Conversational Agents, Summaries and Q&A on Text With LLMs Analyze Custom CSV Data with GPT-4 using Langchain ⛓ Build ChatGPT Chatbots with LangChain Memory: Understanding and Implementing Memory in Conversations ⛓ icon marks a new addition [last update 2023-05-15] previous Concepts next Models Contents DeepLearning.AI course Handbook Tutorials By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 11, 2023.
https://python.langchain.com/en/latest/getting_started/tutorials.html
bea0c8a34b38-0
.md .pdf Concepts Contents Chain of Thought Action Plan Generation ReAct Self-ask Prompt Chaining Memetic Proxy Self Consistency Inception MemPrompt Concepts# These are concepts and terminology commonly used when developing LLM applications. It contains reference to external papers or sources where the concept was first introduced, as well as to places in LangChain where the concept is used. Chain of Thought# Chain of Thought (CoT) is a prompting technique used to encourage the model to generate a series of intermediate reasoning steps. A less formal way to induce this behavior is to include “Let’s think step-by-step” in the prompt. Chain-of-Thought Paper Step-by-Step Paper Action Plan Generation# Action Plan Generation is a prompting technique that uses a language model to generate actions to take. The results of these actions can then be fed back into the language model to generate a subsequent action. WebGPT Paper SayCan Paper ReAct# ReAct is a prompting technique that combines Chain-of-Thought prompting with action plan generation. This induces the model to think about what action to take, then take it. Paper LangChain Example Self-ask# Self-ask is a prompting method that builds on top of chain-of-thought prompting. In this method, the model explicitly asks itself follow-up questions, which are then answered by an external search engine. Paper LangChain Example Prompt Chaining# Prompt Chaining is combining multiple LLM calls, with the output of one-step being the input to the next. PromptChainer Paper Language Model Cascades ICE Primer Book Socratic Models Memetic Proxy# Memetic Proxy is encouraging the LLM to respond in a certain way framing the discussion in a context that the model knows of and that
https://python.langchain.com/en/latest/getting_started/concepts.html
bea0c8a34b38-1
to respond in a certain way framing the discussion in a context that the model knows of and that will result in that type of response. For example, as a conversation between a student and a teacher. Paper Self Consistency# Self Consistency is a decoding strategy that samples a diverse set of reasoning paths and then selects the most consistent answer. Is most effective when combined with Chain-of-thought prompting. Paper Inception# Inception is also called First Person Instruction. It is encouraging the model to think a certain way by including the start of the model’s response in the prompt. Example MemPrompt# MemPrompt maintains a memory of errors and user feedback, and uses them to prevent repetition of mistakes. Paper previous Quickstart Guide next Tutorials Contents Chain of Thought Action Plan Generation ReAct Self-ask Prompt Chaining Memetic Proxy Self Consistency Inception MemPrompt By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 11, 2023.
https://python.langchain.com/en/latest/getting_started/concepts.html