mirror of
https://github.com/langchain-ai/langchain-teacher.git
synced 2026-07-01 15:19:06 -04:00
added all guides
This commit is contained in:
@@ -0,0 +1,344 @@
|
||||
|
||||
## Agents in LangChain
|
||||
|
||||
### Introduction
|
||||
|
||||
Welcome to the comprehensive lesson on Agents in LangChain. In this guide, we'll delve deep into the world of agents, their role in enhancing the capabilities of Large Language Models (LLMs), and how to effectively utilize them within the LangChain library.
|
||||
|
||||
#### What are Agents?
|
||||
|
||||
Agents are intelligent entities designed to leverage the power of language models to make decisions, execute actions, and perform various tasks based on reasoning. Unlike traditional programming, where actions are hardcoded, agents dynamically choose actions and their order of execution using language models. This lesson will cover a wide range of topics related to agents in LangChain, including their components, types, tools, and execution.
|
||||
|
||||
### Components of an Agent
|
||||
|
||||
Agents in LangChain are composed of several key components that collectively enable them to operate effectively and make informed decisions.
|
||||
|
||||
#### The Agent Core
|
||||
|
||||
The agent core is the central component responsible for making decisions on actions to take. It's powered by a language model and a prompt that includes the agent's personality, background context, and prompting strategies like ReAct. This enables agents to respond in a desired manner and enhances their reasoning abilities.
|
||||
|
||||
**Code Example:**
|
||||
|
||||
```python
|
||||
from langchain.chat_models import ChatOpenAI
|
||||
|
||||
# Initialize the language model
|
||||
llm = ChatOpenAI(temperature=0)
|
||||
|
||||
# Create a prompt for the agent
|
||||
from langchain.schema import SystemMessage
|
||||
from langchain.agents import OpenAIFunctionsAgent
|
||||
from langchain.agents import tool
|
||||
|
||||
from langchain.agents import tool
|
||||
|
||||
@tool
|
||||
def get_word_length(word: str) -> int:
|
||||
"""Returns the length of a word."""
|
||||
return len(word)
|
||||
|
||||
tools = [get_word_length]
|
||||
system_message = SystemMessage(content="You are a helpful assistant with a specific expertise.")
|
||||
prompt = OpenAIFunctionsAgent.create_prompt(system_message=system_message)
|
||||
agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)
|
||||
```
|
||||
|
||||
#### Tools and Toolkits
|
||||
|
||||
Tools are essential for agents to interact with the environment effectively. They can access the right tools and understand their usage based on effective descriptions. Toolkits are groups of related tools that work together to accomplish specific objectives.
|
||||
|
||||
**Code Example:**
|
||||
|
||||
```python
|
||||
from langchain.agents import tool
|
||||
|
||||
@tool
|
||||
def calculate_square(number: float) -> float:
|
||||
"""Calculate the square of a number."""
|
||||
return number * number
|
||||
```
|
||||
|
||||
**Question:**
|
||||
- How do tools and toolkits contribute to an agent's ability to perform tasks effectively?
|
||||
|
||||
#### AgentExecutor
|
||||
|
||||
The AgentExecutor is the runtime environment where agents operate and execute actions. It processes agent decisions and handles complexities such as errors, logging, and ensuring smooth execution.
|
||||
|
||||
**Code Example:**
|
||||
|
||||
```python
|
||||
from langchain.agents import AgentExecutor
|
||||
|
||||
agent_executor = AgentExecutor(agent=agent, tools=[calculate_square], verbose=True)
|
||||
```
|
||||
|
||||
**Question:**
|
||||
- What is the role of AgentExecutor in the execution of agent actions?
|
||||
|
||||
|
||||
---
|
||||
|
||||
### Step-by-Step Guide: Building an Agent in LangChain
|
||||
|
||||
Building an agent in LangChain involves defining its components, choosing the right tools, and configuring its behavior. Here's a comprehensive guide to help you create a functional agent that can perform specific tasks using LangChain's capabilities.
|
||||
|
||||
#### Step 1: Define the Objective
|
||||
|
||||
**Start by identifying the specific tasks or objectives your agent needs to achieve.** This will help you determine the tools and toolkits required to accomplish these tasks effectively.
|
||||
|
||||
**Example Objective:** Build an agent that can perform mathematical calculations and provide the results.
|
||||
|
||||
#### Step 2: Choose the Base Language Model (LLM)
|
||||
|
||||
**Select a base language model (LLM) that suits your agent's requirements.** The LLM forms the foundation of your agent's language understanding and generation capabilities.
|
||||
|
||||
**Example:**
|
||||
```python
|
||||
from langchain import OpenAI
|
||||
|
||||
llm = OpenAI(
|
||||
openai_api_key="YOUR_API_KEY",
|
||||
temperature=0.7,
|
||||
model_name="text-davinci-003"
|
||||
)
|
||||
```
|
||||
|
||||
#### Step 3: Define Tools
|
||||
|
||||
**Create tools that encapsulate specific functionalities required for your agent.** Define functions that correspond to these tools and decorate them with `@tool` to make them accessible to the agent.
|
||||
|
||||
**Example Tools:**
|
||||
|
||||
```python
|
||||
from langchain.agents import tool
|
||||
|
||||
@tool
|
||||
def calculator(operation: str, num1: float, num2: float) -> float:
|
||||
"""Perform arithmetic operations."""
|
||||
if operation == 'add':
|
||||
return num1 + num2
|
||||
elif operation == 'subtract':
|
||||
return num1 - num2
|
||||
elif operation == 'multiply':
|
||||
return num1 * num2
|
||||
elif operation == 'divide':
|
||||
return num1 / num2
|
||||
```
|
||||
|
||||
#### Step 4: Create a Prompt
|
||||
|
||||
**Design a prompt that sets the context and behavior for your agent's responses.** The prompt can include the agent's personality, background information, and any specific instructions.
|
||||
|
||||
**Example:**
|
||||
```python
|
||||
from langchain.schema import SystemMessage
|
||||
from langchain.agents import OpenAIFunctionsAgent
|
||||
|
||||
system_message = SystemMessage(content="You are a mathematical assistant. Help users with calculations.")
|
||||
prompt = OpenAIFunctionsAgent.create_prompt(system_message=system_message)
|
||||
```
|
||||
|
||||
#### Step 5: Initialize the Agent
|
||||
|
||||
**Initialize your agent using the base LLM, tools, and the created prompt.** This step creates an instance of the agent that can execute tasks using the specified tools.
|
||||
|
||||
**Example:**
|
||||
```python
|
||||
from langchain.agents import AgentExecutor
|
||||
|
||||
agent = OpenAIFunctionsAgent(llm=llm, tools=[calculator], prompt=prompt)
|
||||
agent_executor = AgentExecutor(agent=agent, tools=[calculator], verbose=True)
|
||||
```
|
||||
|
||||
#### Step 6: Run and test the Agent
|
||||
|
||||
**Run and test your agent by providing input and observing its responses.** This step helps you verify that the agent's behavior aligns with your intended objectives.
|
||||
|
||||
**Example:**
|
||||
```python
|
||||
input_query = "Calculate the result of (5 * 3) + 7"
|
||||
result = agent_executor.run(input_query)
|
||||
print(result) # Output: "The result is 22."
|
||||
```
|
||||
|
||||
#### Step 7: Customize and Iterate
|
||||
|
||||
**Refine and customize your agent's behavior based on test results and user interactions.** Tweak the prompt, tools, and other components as needed to enhance the agent's performance and accuracy.
|
||||
|
||||
**Question:**
|
||||
- What is the purpose of defining tools when building an agent in LangChain?
|
||||
|
||||
---
|
||||
|
||||
### Agent Types
|
||||
|
||||
LangChain supports multiple agent runtimes, each catering to specific use cases and offering different ways to implement agents effectively.
|
||||
|
||||
#### Plan-and-Execute Agent
|
||||
|
||||
Plan-and-execute agents first plan a sequence of actions and then execute subtasks to achieve an objective. This approach is inspired by the BabyAGI and Plan-and-Solve concepts.
|
||||
|
||||
```python
|
||||
from langchain.chat_models import ChatOpenAI
|
||||
from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner
|
||||
from langchain.llms import OpenAI
|
||||
from langchain.agents import load_tools
|
||||
|
||||
llm = OpenAI(temperature=0)
|
||||
tools = load_tools(["serpapi", "llm-math"], llm=llm)
|
||||
model = ChatOpenAI(temperature=0)
|
||||
planner = load_chat_planner(model)
|
||||
executor = load_agent_executor(model, tools, verbose=True)
|
||||
agent = PlanAndExecute(planner=planner, executor=executor, verbose=True)
|
||||
agent.run("Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?")
|
||||
```
|
||||
|
||||
|
||||
#### Zero-Shot ReAct
|
||||
|
||||
Zero-shot agents utilize the ReAct framework to choose tools based solely on tool descriptions. This type of agent is versatile and doesn't require memory for multi-step tasks.
|
||||
|
||||
**Code Example:**
|
||||
|
||||
```python
|
||||
from langchain.agents import load_tools
|
||||
from langchain.agents import initialize_agent
|
||||
from langchain.agents import AgentType
|
||||
from langchain.llms import OpenAI
|
||||
llm = OpenAI(temperature=0)
|
||||
tools = load_tools(["serpapi", "llm-math"], llm=llm)
|
||||
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
|
||||
agent.run("Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?")
|
||||
```
|
||||
|
||||
#### Conversational ReAct
|
||||
|
||||
Conversational agents use the ReAct framework for multi-turn conversations and retain memory of previous interactions. This is particularly useful for chatbot-like interactions.
|
||||
|
||||
**Code Example:**
|
||||
|
||||
```python
|
||||
from langchain.memory import ConversationBufferMemory
|
||||
|
||||
memory = ConversationBufferMemory(memory_key="chat_history")
|
||||
conversational_agent = initialize_agent(
|
||||
agent='conversational-react-description',
|
||||
tools=[calculate_square],
|
||||
llm=llm,
|
||||
verbose=True,
|
||||
max_iterations=3,
|
||||
memory=memory
|
||||
)
|
||||
```
|
||||
|
||||
**Question:**
|
||||
- How does the Zero-Shot ReAct agent choose tools based on their descriptions?
|
||||
|
||||
**Question:**
|
||||
- How does the provided code snippet create an agent that calculates the factorial of a number?
|
||||
|
||||
#### ReAct Docstore Agent
|
||||
|
||||
The ReAct document store agent specializes in searching and retrieving information from documents stored within LangChain's docstore. It's particularly useful for performing information searches.
|
||||
|
||||
**Code Example:**
|
||||
|
||||
```python
|
||||
from langchain.docstore import DocStoreMemory
|
||||
|
||||
docstore_memory = DocStoreMemory(
|
||||
docstore_key="stock_data",
|
||||
docstore_path="./docstore.json"
|
||||
)
|
||||
|
||||
docstore_agent = initialize_agent(
|
||||
agent='react-docstore',
|
||||
tools=tools,
|
||||
llm=llm,
|
||||
verbose=True,
|
||||
max_iterations=3,
|
||||
docstore_memory=docstore_memory
|
||||
)
|
||||
```
|
||||
|
||||
**Question:**
|
||||
- How does the ReAct Docstore Agent interact with LangChain's document store to retrieve information?
|
||||
|
||||
|
||||
|
||||
### Tools and Toolkits
|
||||
|
||||
In the realm of LangChain, tools play a pivotal role in enabling agents to interact with the environment, perform tasks, and achieve their objectives. Tools define the capabilities of an agent and provide a structured way for the agent to access and utilize various functionalities. Let's delve deeper into the concept of tools and how they contribute to the effectiveness of LangChain agents.
|
||||
|
||||
#### Understanding Tools
|
||||
|
||||
**Tools are functions or methods that agents can utilize to perform specific tasks.** These tasks can range from simple calculations to complex interactions with external systems. Each tool encapsulates a particular piece of functionality and is associated with a name, a description, and the underlying implementation.
|
||||
|
||||
**Example:**
|
||||
Consider a scenario where an agent needs to perform basic math calculations. We can create a tool named "Calculator" that performs arithmetic operations.
|
||||
|
||||
```python
|
||||
from langchain.agents import tool
|
||||
|
||||
@tool
|
||||
def calculator(operation: str, num1: float, num2: float) -> float:
|
||||
"""Perform arithmetic operations."""
|
||||
if operation == 'add':
|
||||
return num1 + num2
|
||||
elif operation == 'subtract':
|
||||
return num1 - num2
|
||||
elif operation == 'multiply':
|
||||
return num1 * num2
|
||||
elif operation == 'divide':
|
||||
return num1 / num2
|
||||
```
|
||||
|
||||
#### Toolkits: Organizing Tools
|
||||
|
||||
**Toolkits are collections of related tools grouped together to achieve specific objectives.** They provide a higher-level abstraction, making it easier to manage and organize tools with similar functionalities. Toolkits enhance the modularity and reusability of an agent's components.
|
||||
|
||||
**Example:**
|
||||
Imagine an agent designed for financial calculations. We can create a toolkit named "Financial Toolkit" containing tools for performing calculations related to investments, currency conversions, and interest rates.
|
||||
|
||||
```python
|
||||
from langchain.agents import Toolkit
|
||||
|
||||
financial_toolkit = Toolkit(name="Financial Toolkit", tools=[investment_calculator, currency_converter, interest_calculator])
|
||||
```
|
||||
|
||||
**Question:**
|
||||
- How do tools contribute to the functionality of LangChain agents?
|
||||
|
||||
#### Extending Agent Capabilities with Tools and Toolkits
|
||||
|
||||
When building an agent, developers can choose from a variety of built-in tools or create custom tools to suit their specific needs. By selecting and organizing tools into toolkits, developers can effectively extend the agent's capabilities and enhance its ability to perform diverse tasks.
|
||||
|
||||
**Code Example:**
|
||||
|
||||
```python
|
||||
# Creating custom tools
|
||||
@tool
|
||||
def search_engine(query: str) -> str:
|
||||
"""Perform a web search."""
|
||||
# Use an external library or API to execute the search
|
||||
search_result = external_search_engine(query)
|
||||
return search_result
|
||||
|
||||
@tool
|
||||
def email_sender(subject: str, content: str, recipient: str) -> str:
|
||||
"""Send an email."""
|
||||
# Use an email API to send the email
|
||||
email_api.send_email(subject, content, recipient)
|
||||
return f"Email sent to {recipient}"
|
||||
|
||||
# Creating a toolkit
|
||||
communication_toolkit = Toolkit(name="Communication Toolkit", tools=[search_engine, email_sender])
|
||||
```
|
||||
|
||||
By combining tools and organizing them into toolkits, developers can enhance the agent's functionality and enable it to interact with the environment more effectively.
|
||||
|
||||
### Conclusion
|
||||
|
||||
Agents in LangChain open up a world of possibilities by leveraging language models and tools to make intelligent decisions and perform complex tasks. Their adaptability and customization options make them invaluable for a wide range of applications. By combining agents, tools, and runtime environments effectively, developers can create sophisticated systems capable of handling diverse tasks with ease.
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
You're right, my previous lesson on Chains in Langchain was too brief. Here is an expanded version with more details, examples, and code snippets:
|
||||
|
||||
# Chains in Langchain
|
||||
|
||||
## Introduction
|
||||
|
||||
Chains are a core concept in Langchain that enable combining multiple components like language models, retrievers, routers, etc. into a cohesive AI application. Chains provide a modular and extensible way to build complex assistants by composing simpler building blocks.
|
||||
|
||||
**Q: What are chains and why are they useful in Langchain?**
|
||||
|
||||
A: Chains are reusable structures that allow combining multiple components like language models, prompts, retrievers, etc. into a single AI application. They provide a clean, modular architecture that makes it easy to build complex assistants by piecing together simpler building block chains. The main benefits are:
|
||||
|
||||
- **Modularity:** It's easy to swap out components as needed.
|
||||
- **Extensibility:** We can chain simple blocks into complex apps.
|
||||
- **Maintainability:** Each component encapsulates specific logic.
|
||||
- **Reusability:** Chains are reusable across applications.
|
||||
|
||||
|
||||
## Types of Chains
|
||||
|
||||
Langchain provides several common chain implementations out-of-the-box:
|
||||
|
||||
### LLMChain
|
||||
|
||||
The simplest chain wraps a language model. It takes a prompt template, formats a prompt using the input, queries the LLM, and returns the response.
|
||||
|
||||
```python
|
||||
from langchain.chains import LLMChain
|
||||
|
||||
prompt = PromptTemplate(template="Hello {name}!")
|
||||
llm = OpenAI()
|
||||
|
||||
llm_chain = LLMChain(llm=llm, prompt=prompt)
|
||||
print(llm_chain.run(input={"name": "Jane"}))
|
||||
|
||||
# Output: Hello Jane!
|
||||
```
|
||||
|
||||
**Q: How does the LLMChain work step-by-step?**
|
||||
|
||||
A: Here is what the LLMChain does under the hood:
|
||||
|
||||
1. Take input data
|
||||
2. Format prompt template using input
|
||||
3. Pass formatted prompt to LLM
|
||||
4. Get LLM response
|
||||
5. Return response
|
||||
|
||||
So it handles prompt formatting and querying the LLM.
|
||||
|
||||
### SequentialChain
|
||||
|
||||
Chains multiple chains sequentially. The output of one chain is passed as input to the next chain automatically.
|
||||
|
||||
```python
|
||||
from langchain.chains import SequentialChain
|
||||
|
||||
chain1 = LLMChain(llm1, prompt1)
|
||||
chain2 = LLMChain(llm2, prompt2)
|
||||
|
||||
seq_chain = SequentialChain(chains=[chain1, chain2])
|
||||
```
|
||||
|
||||
**Q: What does SequentialChain allow you to do?**
|
||||
|
||||
A: SequentialChain enables chaining multiple chains together into a pipeline. The key benefit is it automatically handles passing data between the chains. For example, we can chain a preprocessing, LLM, and postprocessing step together easily.
|
||||
|
||||
### RouterChain
|
||||
|
||||
Contains logic to route input dynamically to different destination chains based on the input. Useful for routing between domains.
|
||||
|
||||
```python
|
||||
from langchain.chains import RouterChain
|
||||
|
||||
router = EmbeddingRouter(...)
|
||||
destination_chains = {...}
|
||||
|
||||
chain = RouterChain(router=router,
|
||||
destination_chains=destination_chains)
|
||||
```
|
||||
|
||||
**Q: How does the RouterChain route between chains?**
|
||||
|
||||
A: The RouterChain encapsulates logic to select the appropriate destination chain for a given input. For example, an embedding router could select chains based on semantic similarity. This enables dynamically switching behavior based on the input.
|
||||
|
||||
## Building Applications
|
||||
|
||||
|
||||
|
||||
This shows how we can build complex applications from smaller modular chains - while keeping each part simple, reusable, and focused.
|
||||
|
||||
**Q: What are some examples of AI apps you could build using chains?**
|
||||
|
||||
A: Here are some examples of AI applications you could build using chains:
|
||||
|
||||
- Question Answering over Documents
|
||||
- Multi-Domain Chatbot
|
||||
- Sentiment Analysis Pipeline
|
||||
- Automated Email Response Agent
|
||||
- Resume Screening System
|
||||
- Article Summarization Workflow
|
||||
|
||||
The possibilities are endless! Chains make it much easier to build robust, maintainable AI systems.
|
||||
|
||||
|
||||
|
||||
Chains also maintain state like conversation history across runs. This simplifies building stateful applications like chatbots.
|
||||
|
||||
**Q: How do chains work behind the scenes?**
|
||||
|
||||
A: Chains handle passing data between components under the hood. They often preprocess input, call the next component, and postprocess outputs. Chains also maintain state across multiple runs, storing things like conversation history. This simplifies building stateful apps like chatbots that need to track context.
|
||||
|
||||
## Conclusion
|
||||
|
||||
In this lesson, we looked at:
|
||||
|
||||
- Core chain implementations like LLMChain, SequentialChain, RouterChain
|
||||
- Combining chains to build complex AI applications
|
||||
- How chains work behind the scenes
|
||||
|
||||
Chains provide a clean architecture for production AI systems by breaking down complex apps into simple, reusable components. This results in modular, robust and extensible architectures.
|
||||
@@ -0,0 +1,338 @@
|
||||
**Getting Started with LangChain: Building LLM-Powered Applications**
|
||||
|
||||
In this comprehensive lesson, we'll dive into the world of LangChain, an open-source framework that empowers developers to create applications using Large Language Models (LLMs) like GPT-4. We'll cover various aspects of LangChain, from its core concepts to practical examples of building applications. Throughout the lesson, we'll ask questions, provide answers, and offer detailed code examples to help you grasp the concepts effectively.
|
||||
|
||||
**Table of Contents:**
|
||||
|
||||
1. **Introduction to LangChain**
|
||||
- What is LangChain?
|
||||
- Value Propositions of LangChain
|
||||
- Applications of LangChain
|
||||
|
||||
2. **Key Concepts of LangChain**
|
||||
- Components: Modular Building Blocks
|
||||
- Chains: Combining Components for Tasks
|
||||
- Agents: LLM Interaction with Environment
|
||||
|
||||
3. **Getting Started: Building a Simple Application**
|
||||
- Importing LangChain and Initializing LLM
|
||||
- Predicting with the LLM
|
||||
- Interpreting the Response
|
||||
|
||||
4. **Understanding Components**
|
||||
- LLM Wrappers: Working with LLMs
|
||||
- Prompt Templates: Structuring Input for LLMs
|
||||
- Indexes: Retrieving Relevant Information
|
||||
|
||||
5. **Chains: Combining Components**
|
||||
- Using Chains for Complex Applications
|
||||
- Modularity, Debugging, and Maintenance
|
||||
|
||||
6. **Agents: LLM Interaction with Environment**
|
||||
- LLMs Performing Actions via External APIs
|
||||
- Integrating LLMs into the Workflow
|
||||
|
||||
7. **Building Advanced Applications with LangChain**
|
||||
- Document Analysis and Summarization
|
||||
- Chatbots and Customer Assistance
|
||||
- Code Analysis for Bugs and Security
|
||||
- Answering Questions with Diverse Sources
|
||||
- Data Augmentation for Machine Learning
|
||||
|
||||
**1. Introduction to LangChain:**
|
||||
|
||||
**What is LangChain?**
|
||||
LangChain is an open-source framework designed to simplify the creation of applications using Large Language Models (LLMs). It provides standardized interfaces, modular components, and pre-built chains for various tasks.
|
||||
|
||||
**Value Propositions of LangChain:**
|
||||
- **Components:** Modular, ready-to-use building blocks for language model applications.
|
||||
- **Chains:** Structured combinations of components to solve specific tasks.
|
||||
- **Customizability:** Chains are customizable to cater to unique use cases.
|
||||
|
||||
**Applications of LangChain:**
|
||||
- Document analysis and summarization.
|
||||
- Natural language understanding and generation.
|
||||
- Chatbots for customer support and information retrieval.
|
||||
- Code analysis for bugs and security.
|
||||
- Answering questions using diverse sources (text, code, data).
|
||||
- Data augmentation for machine learning.
|
||||
- Language translation and text classification.
|
||||
|
||||
**2. Key Concepts of LangChain:**
|
||||
|
||||
**Components:**
|
||||
Components in LangChain are modular building blocks used to create language model applications. They include LLM wrappers, prompt templates, and indexes for relevant information retrieval.
|
||||
|
||||
**Chains:**
|
||||
Chains combine multiple components to achieve specific tasks. Chains enhance modularity, simplification, debugging, and maintenance in application development.
|
||||
|
||||
**Agents:**
|
||||
Agents enable LLMs to interact with their environment. They allow LLMs to perform actions using external APIs and integrate with external tools and data sources.
|
||||
|
||||
**3. Getting Started: Building a Simple Application**
|
||||
|
||||
Installation
|
||||
To install LangChain run:
|
||||
|
||||
Pip
|
||||
pip install langchain
|
||||
|
||||
Conda
|
||||
conda install langchain -c conda-forge
|
||||
|
||||
For more details, see our Installation guide.
|
||||
|
||||
Environment setup
|
||||
Using 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.
|
||||
|
||||
First we'll need to install their Python package:
|
||||
|
||||
pip install openai
|
||||
|
||||
Accessing 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:
|
||||
|
||||
export OPENAI_API_KEY="..."
|
||||
|
||||
If 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:
|
||||
|
||||
from langchain.llms import OpenAI
|
||||
|
||||
llm = OpenAI(openai_api_key="...")
|
||||
|
||||
**Code Example:**
|
||||
```python
|
||||
# Import LangChain and initialize LLM
|
||||
from langchain.llms import OpenAI
|
||||
|
||||
llm = OpenAI(temperature=0.9, openai_api_key=api_key)
|
||||
|
||||
# Predict with the LLM
|
||||
response = llm.predict("Suggest me a skill that is in demand?")
|
||||
print(response)
|
||||
```
|
||||
|
||||
**Output:**
|
||||
```
|
||||
One skill in demand right now is software/web development, which includes everything from coding to content management systems to web design...
|
||||
```
|
||||
|
||||
**4. Understanding Components**
|
||||
|
||||
**LLM Wrappers:**
|
||||
LLM wrappers provide standardized interfaces for working with different language models. They offer methods to predict text and messages using the model.
|
||||
|
||||
**Prompt Templates:**
|
||||
Prompt templates structure input for LLMs. They allow easy formatting of user inputs and instructions for generating responses.
|
||||
|
||||
**Indexes:**
|
||||
Indexes facilitate information retrieval from external sources. They help LLMs fetch relevant data to enhance response quality.
|
||||
|
||||
**5. Building an application
|
||||
Now we can start building our language model application. LangChain provides many modules that can be used to build language model applications. Modules can be used as stand-alones in simple applications and they can be combined for more complex use cases.
|
||||
|
||||
The core building block of LangChain applications is the LLMChain. This combines three things:
|
||||
|
||||
LLM: 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 Templates: 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 Parsers: These translate the raw response from the LLM to a more workable format, making it easy to use the output downstream.
|
||||
In this getting started guide we will cover those three components by themselves, and then cover the LLMChain which combines all of 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 LLM and/or the prompt used, so knowing how to take advantage of this will be a big enabler.
|
||||
|
||||
LLMs
|
||||
There are two types of language models, which in LangChain are called:
|
||||
|
||||
LLMs: this is a language model which takes a string as input and returns a string
|
||||
ChatModels: this is a language model which takes a list of messages as input and returns a message
|
||||
The input/output for LLMs is simple and easy to understand - a string. But what about ChatModels? The input there is a list of ChatMessages, and the output is a single ChatMessage. A ChatMessage has two required components:
|
||||
|
||||
content: This is the content of the message.
|
||||
role: This is the role of the entity from which the ChatMessage is coming from.
|
||||
LangChain provides several objects to easily distinguish between different roles:
|
||||
|
||||
HumanMessage: A ChatMessage coming from a human/user.
|
||||
AIMessage: A ChatMessage coming from an AI/assistant.
|
||||
SystemMessage: A ChatMessage coming from the system.
|
||||
FunctionMessage: A ChatMessage coming from a function call.
|
||||
If none of those roles sound right, there is also a ChatMessage class where you can specify the role manually. For more information on how to use these different messages most effectively, see our prompting guide.
|
||||
|
||||
LangChain exposes a standard interface for both, but it's useful to understand this difference in order to construct prompts for a given language model. The standard interface that LangChain exposes has two methods:
|
||||
|
||||
predict: Takes in a string, returns a string
|
||||
predict_messages: Takes in a list of messages, returns a message.
|
||||
Let's see how to work with these different types of models and these different types of inputs. First, let's import an LLM and a ChatModel.
|
||||
|
||||
from langchain.llms import OpenAI
|
||||
from langchain.chat_models import ChatOpenAI
|
||||
|
||||
llm = OpenAI()
|
||||
chat_model = ChatOpenAI()
|
||||
|
||||
llm.predict("hi!")
|
||||
>>> "Hi"
|
||||
|
||||
chat_model.predict("hi!")
|
||||
>>> "Hi"
|
||||
|
||||
The OpenAI and ChatOpenAI objects are basically just configuration objects. You can initialize them with parameters like temperature and others, and pass them around.
|
||||
|
||||
Next, let's use the predict method to run over a string input.
|
||||
|
||||
text = "What would be a good company name for a company that makes colorful socks?"
|
||||
|
||||
llm.predict(text)
|
||||
# >> Feetful of Fun
|
||||
|
||||
chat_model.predict(text)
|
||||
# >> Socks O'Color
|
||||
|
||||
Finally, let's use the predict_messages method to run over a list of messages.
|
||||
|
||||
from langchain.schema import HumanMessage
|
||||
|
||||
text = "What would be a good company name for a company that makes colorful socks?"
|
||||
messages = [HumanMessage(content=text)]
|
||||
|
||||
llm.predict_messages(messages)
|
||||
# >> Feetful of Fun
|
||||
|
||||
chat_model.predict_messages(messages)
|
||||
# >> Socks O'Color
|
||||
|
||||
For both these methods, you can also pass in parameters as key word arguments. For example, you could pass in temperature=0 to adjust the temperature that is used from what the object was configured with. Whatever values are passed in during run time will always override what the object was configured with.
|
||||
|
||||
Prompt templates
|
||||
Most LLM applications do not pass user input directly into an LLM. Usually they will add the user input to a larger piece of text, called a prompt template, that provides additional context on the specific task at hand.
|
||||
|
||||
In the previous example, the text we passed to the model contained instructions to generate a company name. For our application, it'd be great if the user only had to provide the description of a company/product, without having to worry about giving the model instructions.
|
||||
|
||||
PromptTemplates help with exactly this! They bundle up all the logic for going from user input into a fully formatted prompt. This can start off very simple - for example, a prompt to produce the above string would just be:
|
||||
|
||||
from langchain.prompts import PromptTemplate
|
||||
|
||||
prompt = PromptTemplate.from_template("What is a good name for a company that makes {product}?")
|
||||
prompt.format(product="colorful socks")
|
||||
|
||||
What is a good name for a company that makes colorful socks?
|
||||
|
||||
However, the advantages of using these over raw string formatting are several. You can "partial" out variables - eg you can format only some of the variables at a time. You can compose them together, easily combining different templates into a single prompt. For explanations of these functionalities, see the section on prompts for more detail.
|
||||
|
||||
PromptTemplates can also be used to produce a list of messages. In this case, the prompt not only contains information about the content, but also each message (its role, its position in the list, etc) Here, what happens most often is a ChatPromptTemplate is a list of ChatMessageTemplates. Each ChatMessageTemplate contains instructions for how to format that ChatMessage - its role, and then also its content. Let's take a look at this below:
|
||||
|
||||
from langchain.prompts.chat import (
|
||||
ChatPromptTemplate,
|
||||
SystemMessagePromptTemplate,
|
||||
HumanMessagePromptTemplate,
|
||||
)
|
||||
|
||||
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])
|
||||
|
||||
chat_prompt.format_messages(input_language="English", output_language="French", text="I love programming.")
|
||||
|
||||
|
||||
[
|
||||
SystemMessage(content="You are a helpful assistant that translates English to French.", additional_kwargs={}),
|
||||
HumanMessage(content="I love programming.")
|
||||
]
|
||||
|
||||
|
||||
ChatPromptTemplates can also include other things besides ChatMessageTemplates - see the section on prompts for more detail.
|
||||
|
||||
Output Parsers
|
||||
OutputParsers convert the raw output of an LLM into a format that can be used downstream. There are few main type of OutputParsers, including:
|
||||
|
||||
Convert text from LLM -> structured information (eg JSON)
|
||||
Convert a ChatMessage into just a string
|
||||
Convert the extra information returned from a call besides the message (like OpenAI function invocation) into a string.
|
||||
For full information on this, see the section on output parsers
|
||||
|
||||
In this getting started guide, we will write our own output parser - one that converts a comma separated list into a list.
|
||||
|
||||
from langchain.schema import BaseOutputParser
|
||||
|
||||
class CommaSeparatedListOutputParser(BaseOutputParser):
|
||||
"""Parse the output of an LLM call to a comma-separated list."""
|
||||
|
||||
|
||||
def parse(self, text: str):
|
||||
"""Parse the output of an LLM call."""
|
||||
return text.strip().split(", ")
|
||||
|
||||
CommaSeparatedListOutputParser().parse("hi, bye")
|
||||
# >> ['hi', 'bye']
|
||||
|
||||
LLMChain
|
||||
We can now combine all these into one chain. This chain will take input variables, pass those to a prompt template to create a prompt, pass the prompt to an LLM, and then pass the output through an (optional) output parser. This is a convenient way to bundle up a modular piece of logic. Let's see it in action!
|
||||
|
||||
from langchain.chat_models import ChatOpenAI
|
||||
from langchain.prompts.chat import (
|
||||
ChatPromptTemplate,
|
||||
SystemMessagePromptTemplate,
|
||||
HumanMessagePromptTemplate,
|
||||
)
|
||||
from langchain.chains import LLMChain
|
||||
from langchain.schema import BaseOutputParser
|
||||
|
||||
class CommaSeparatedListOutputParser(BaseOutputParser):
|
||||
"""Parse the output of an LLM call to a comma-separated list."""
|
||||
|
||||
|
||||
def parse(self, text: str):
|
||||
"""Parse the output of an LLM call."""
|
||||
return text.strip().split(", ")
|
||||
|
||||
template = """You are a helpful assistant who generates comma separated lists.
|
||||
A user will pass in a category, and you should generate 5 objects in that category in a comma separated list.
|
||||
ONLY return a comma separated list, and nothing more."""
|
||||
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=ChatOpenAI(),
|
||||
prompt=chat_prompt,
|
||||
output_parser=CommaSeparatedListOutputParser()
|
||||
)
|
||||
chain.run("colors")
|
||||
# >> ['red', 'blue', 'green', 'yellow', 'orange']
|
||||
|
||||
**6. Agents: LLM Interaction with Environment**
|
||||
|
||||
**Code Example:**
|
||||
```python
|
||||
from langchain.agents import ExternalAPICall
|
||||
|
||||
class TranslatorAgent(ExternalAPICall):
|
||||
def __init__(self):
|
||||
super().__init__(api_url="https://translation.api.com", method="POST")
|
||||
|
||||
def perform_action(self, input_text):
|
||||
data = {"text": input_text, "target_language": "fr"}
|
||||
response = self.call_api(data)
|
||||
return response["translated_text"]
|
||||
|
||||
# Initialize the agent
|
||||
translator_agent = TranslatorAgent()
|
||||
|
||||
# Perform translation
|
||||
translated_text = translator_agent.perform_action("Hello, how are you?")
|
||||
print(translated_text)
|
||||
```
|
||||
|
||||
**Output:**
|
||||
```
|
||||
"Bonjour, comment ça va ?"
|
||||
```
|
||||
|
||||
**7. Building Advanced Applications with LangChain**
|
||||
|
||||
**Conclusion:**
|
||||
LangChain is a versatile framework that simplifies the development of applications powered by Large Language Models. It offers standardized components, chains, and agents to create a wide range of applications, from chatbots to document summarization. With LangChain, developers can harness the power of language models while maintaining modularity and customization.
|
||||
|
||||
Throughout this lesson, we've explored LangChain's core concepts, demonstrated practical examples, and discussed its potential applications. By mastering LangChain, you can embark on a journey to build innovative and powerful AI-driven applications.
|
||||
|
||||
Remember, this lesson provides a solid foundation, but there's much more to explore within LangChain. Dive deeper into prompt templates, explore more use cases, and expand your knowledge to create advanced and impactful language model applications. Happy coding!
|
||||
@@ -0,0 +1,169 @@
|
||||
Here is a consolidated lesson on Retrieval Augmented Generation in Langchain with sections, questions, answers, and code examples:
|
||||
|
||||
# Retrieval Augmented Generation in Langchain
|
||||
|
||||
## Introduction
|
||||
|
||||
Retrieval augmented generation (RAG) refers to combining large language models with external knowledge sources to improve their capabilities. In RAG, the LLM retrieves relevant information from a knowledge base to inform its response to a given prompt or question. This helps reduce hallucination and improve accuracy.
|
||||
|
||||
Langchain provides tools for implementing retrieval augmented generation by allowing easy integration of external knowledge sources. Let's learn how it works.
|
||||
|
||||
**Q: What is retrieval augmented generation?**
|
||||
|
||||
A: Retrieval augmented generation refers to combining a large language model with an external knowledge source to improve the accuracy and reduce hallucination in the LLM's responses. The LLM retrieves relevant information from the knowledge source to inform its response to prompts and questions.
|
||||
|
||||
**Q: Why is retrieval augmentation useful for large language models?**
|
||||
|
||||
A: Retrieval augmentation is useful because LLMs have limited knowledge based on what they were trained on. By combining them with external knowledge sources, we can provide them with up-to-date, relevant information to ground their responses in facts rather than hallucinations. This improves accuracy.
|
||||
|
||||
## Knowledge Sources
|
||||
|
||||
The first step in implementing RAG is selecting and integrating external knowledge sources. Langchain supports integrating diverse knowledge sources:
|
||||
|
||||
- **Documents:** Text, PDFs, HTML pages, etc.
|
||||
- **Structured data:** SQL tables, Pandas dataframes, etc.
|
||||
- **APIs:** Web APIs can be wrapped as knowledge sources.
|
||||
|
||||
Langchain provides utilities for loading data from each of these sources and converting them into a standard `Document` format that can be used downstream.
|
||||
|
||||
**Q: What kinds of knowledge sources can be integrated with Langchain?**
|
||||
|
||||
A: Langchain supports integrating diverse knowledge sources including unstructured text documents, structured data like SQL tables and Pandas dataframes, and web APIs. These sources can be loaded and converted into Langchain's standard Document format.
|
||||
|
||||
**Q: How does Langchain convert diverse sources into a standard format?**
|
||||
|
||||
A: Langchain provides DocumentLoaders to load each type of source and convert it into the standard Document format which contains the text content and metadata. This abstraction allows seamlessly integrating diverse sources.
|
||||
|
||||
## Working with Documents
|
||||
|
||||
Unstructured text documents are a common knowledge source. Let's see how to work with documents in Langchain.
|
||||
|
||||
### Loading Documents
|
||||
|
||||
Langchain provides `DocumentLoaders` to load documents from files, web pages, Google Drive, etc. For example:
|
||||
|
||||
```python
|
||||
from langchain.document_loaders import TextLoader
|
||||
|
||||
loader = TextLoader("data.txt")
|
||||
docs = loader.load()
|
||||
```
|
||||
|
||||
**Q: How do you load unstructured text documents in Langchain?**
|
||||
|
||||
A: Langchain provides DocumentLoaders like TextLoader to load text documents from files, webpages, Google Drive, etc. The DocumentLoaders expose a load() method to load the data and return a list of Document objects.
|
||||
|
||||
### Splitting Documents
|
||||
|
||||
Large documents should be split into smaller chunks before feeding to the LLM. Langchain provides `TextSplitters` for this:
|
||||
|
||||
```python
|
||||
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
||||
|
||||
splitter = RecursiveCharacterTextSplitter()
|
||||
docs = splitter.split_documents(docs)
|
||||
```
|
||||
|
||||
**Q: Why is it important to split text documents before using them for retrieval?**
|
||||
|
||||
A: Large documents need to be split into smaller chunks so that they can fit into the LLM's limited context window. Smaller chunks also allow more focused, relevant retrieval for a given query. Langchain provides TextSplitters to split documents into chunks.
|
||||
|
||||
### Embedding Documents
|
||||
|
||||
To retrieve documents for a query, we need to represent them in a vector space using embeddings. Langchain provides `Embeddings` for this:
|
||||
|
||||
```python
|
||||
from langchain.embeddings import OpenAIEmbeddings
|
||||
|
||||
embedder = OpenAIEmbeddings()
|
||||
embeds = embedder.embed_documents(docs)
|
||||
```
|
||||
|
||||
Now the documents are encoded as vectors ready for retrieval.
|
||||
|
||||
**Q: How does Langchain generate vector embeddings for text documents?**
|
||||
|
||||
A: Langchain provides Embeddings models like OpenAIEmbeddings to generate vector representations of text documents. These embedding vectors allow computing semantic similarity between pieces of text for retrieval.
|
||||
|
||||
## Vector Databases
|
||||
|
||||
To perform efficient retrieval over a large corpus, the document vectors need to be indexed in a vector database. Langchain provides utilities for working with vector databases.
|
||||
|
||||
### Creating the Index
|
||||
|
||||
A vector database index can be created from embedded documents like:
|
||||
|
||||
```python
|
||||
from langchain.vectorstores import FAISS
|
||||
|
||||
vectorstore = FAISS()
|
||||
vectorstore.add_documents(embeds)
|
||||
```
|
||||
|
||||
**Q: How do you create a vector database index in Langchain?**
|
||||
|
||||
A: A vector database index can be created by first embedding documents using an Embeddings model and then passing the document vectors to a VectorStore like FAISS. The VectorStore will create the index from the vectors.
|
||||
|
||||
### Querying the Index
|
||||
|
||||
Once indexed, we can perform efficient semantic search over the vectors:
|
||||
|
||||
```python
|
||||
docs = vectorstore.similarity_search("fix my computer")
|
||||
```
|
||||
|
||||
This retrieves the most similar document vectors to the query.
|
||||
|
||||
**Q: How can you retrieve documents from a vector database index?**
|
||||
|
||||
A: The VectorStore provides a similarity_search method to query the index and return the most similar document vectors to a query string or embedding vector. This enables fast semantic search over the indexed documents.
|
||||
|
||||
## Building Chains
|
||||
|
||||
Langchain provides chain abstractions that combine the knowledge source with LLMs to enable retrieval augmented generation.
|
||||
|
||||
### RetrievalQA
|
||||
|
||||
The `RetrievalQA` chain performs question answering using retrieved documents:
|
||||
|
||||
```python
|
||||
from langchain.chains import RetrievalQA
|
||||
from langchain.llms import OpenAI
|
||||
|
||||
llm = OpenAI()
|
||||
retriever = vectorstore.as_retriever()
|
||||
qa_chain = RetrievalQA(llm, retriever)
|
||||
|
||||
qa_chain.run("when was langchain created?")
|
||||
```
|
||||
|
||||
**Q: How can you build a question answering system using retrieved documents in Langchain?**
|
||||
|
||||
A: The RetrievalQA chain combines a retriever over documents with a QA-capable LLM like OpenAI to perform question answering using retrieved information. It automatically retrieves relevant documents for a question and has the LLM answer based on those.
|
||||
|
||||
### ConversationalQA
|
||||
|
||||
For dialog, `ConversationalQA` incorporates conversation history:
|
||||
|
||||
```python
|
||||
from langchain.chains import ConversationalQA
|
||||
|
||||
conv_qa = ConversationalQA(llm, retriever)
|
||||
|
||||
conv_qa("what is langchain?", "Langchain is an AI assistant created by Anthropic.")
|
||||
```
|
||||
|
||||
**Q: How does Langchain support multi-turn conversational question answering?**
|
||||
|
||||
A: The ConversationalQA chain maintains conversation state and incorporates it into queries to the retriever and LLM at each turn. This enables multi-turn QA where context from earlier in the conversation is used.
|
||||
|
||||
## Conclusion
|
||||
|
||||
In this lesson, we looked at how Langchain implements retrieval augmented generation by:
|
||||
|
||||
- Supporting diverse knowledge sources
|
||||
- Providing utilities for working with documents
|
||||
- Building vector databases for retrieval
|
||||
- Creating chains that connect retrievers and LLMs
|
||||
|
||||
With these building blocks, you can augment any LLM with external knowledge to reduce hallucination and improve accuracy!
|
||||
+42
-31
@@ -28,42 +28,48 @@ class StreamHandler(BaseCallbackHandler):
|
||||
self.text += token
|
||||
self.container.markdown(self.text)
|
||||
|
||||
# Load guides
|
||||
with open("guide.txt", "r") as f:
|
||||
guide = f.read()
|
||||
with open("lc_guides/prompt_guide.txt", "r") as f:
|
||||
prompt_guide = f.read()
|
||||
with open("lc_guides/models_guide.txt", "r") as f:
|
||||
models_guide = f.read()
|
||||
with open("lc_guides/memory_guide.txt", "r") as f:
|
||||
memory_guide = f.read()
|
||||
# Lesson selection dictionary
|
||||
lesson_guides = {
|
||||
"Lesson 1: Getting Started with LangChain": {
|
||||
"file": "lc_guides/getting_started_guide.txt",
|
||||
"description": "This lesson covers the basics of getting started with LangChain."
|
||||
},
|
||||
"Lesson 2: Prompts": {
|
||||
"file": "lc_guides/prompt_guide.txt",
|
||||
"description": "This lesson focuses on prompts and their usage."
|
||||
},
|
||||
"Lesson 3: Language Models": {
|
||||
"file": "lc_guides/models_guide.txt",
|
||||
"description": "This lesson provides an overview of language models."
|
||||
},
|
||||
"Lesson 4: Memory": {
|
||||
"file": "lc_guides/memory_guide.txt",
|
||||
"description": "This lesson is about Memory."
|
||||
},
|
||||
"Lesson 5: Chains": {
|
||||
"file": "lc_guides/chains_guide.txt",
|
||||
"description": "This lesson provides information on Chains in LangChain, their types, and usage."
|
||||
},
|
||||
"Lesson 6: Retrieval": {
|
||||
"file": "lc_guides/retrieval_guide.txt",
|
||||
"description": "This lesson provides information on indexing and retrieving information using LangChain."
|
||||
},
|
||||
"Lesson 7: Agents": {
|
||||
"file": "lc_guides/agents_guide.txt",
|
||||
"description": "This lesson provides information on agents, tools, and toolkits."
|
||||
}
|
||||
}
|
||||
|
||||
# Initialize LangSmith client
|
||||
client = Client()
|
||||
|
||||
# Lesson selection sidebar
|
||||
lesson_selection = st.sidebar.selectbox("Select Lesson", [
|
||||
"Lesson 1: Getting Started with LangChain",
|
||||
"Lesson 2: Prompts",
|
||||
"Lesson 3: Language Models",
|
||||
"Lesson 4: Memory"
|
||||
])
|
||||
lesson_selection = st.sidebar.selectbox("Select Lesson", list(lesson_guides.keys()))
|
||||
|
||||
# Display lesson content and description based on selection
|
||||
if lesson_selection == "Lesson 1: Getting Started with LangChain":
|
||||
lesson_content = guide
|
||||
lesson_description = "This lesson covers the basics of getting started with LangChain."
|
||||
elif lesson_selection == "Lesson 2: Prompts":
|
||||
lesson_content = prompt_guide
|
||||
lesson_description = "This lesson focuses on prompts and their usage."
|
||||
elif lesson_selection == "Lesson 3: Language Models":
|
||||
lesson_content = models_guide
|
||||
lesson_description = "This lesson provides an overview of language models."
|
||||
else:
|
||||
lesson_content = memory_guide
|
||||
lesson_description = "This lesson is about Memory."
|
||||
|
||||
prompt_template = load_prompt(content=lesson_content)
|
||||
lesson_info = lesson_guides[lesson_selection]
|
||||
lesson_content = open(lesson_info["file"], "r").read()
|
||||
lesson_description = lesson_info["description"]
|
||||
|
||||
# Radio buttons for lesson type selection
|
||||
lesson_type = st.sidebar.radio("Select Lesson Type", ["Instructions based lesson", "Interactive lesson with questions"])
|
||||
@@ -93,14 +99,19 @@ if prompt := st.chat_input():
|
||||
|
||||
with st.chat_message("assistant"):
|
||||
stream_handler = StreamHandler(st.empty())
|
||||
model = ChatOpenAI(streaming=True, callbacks=[stream_handler], model="gpt-3.5-turbo")
|
||||
model = ChatOpenAI(streaming=True, callbacks=[stream_handler], model="gpt-3.5-turbo-16k")
|
||||
|
||||
if lesson_type == "Instructions based lesson":
|
||||
prompt_template = load_prompt(content=lesson_content)
|
||||
else:
|
||||
prompt_template = load_prompt_with_questions(content=lesson_content)
|
||||
|
||||
chain = LLMChain(prompt=prompt_template, llm=model)
|
||||
|
||||
response = chain(
|
||||
{"input": prompt, "chat_history": st.session_state.messages[-20:]},
|
||||
include_run_info=True,
|
||||
tags=[lesson_selection]
|
||||
tags=[lesson_selection, lesson_type]
|
||||
)
|
||||
st.session_state.messages.append(HumanMessage(content=prompt))
|
||||
st.session_state.messages.append(AIMessage(content=response[chain.output_key]))
|
||||
|
||||
Reference in New Issue
Block a user