mirror of
https://github.com/hwchase17/langchain.git
synced 2026-03-18 11:07:36 +00:00
218 lines
9.1 KiB
Markdown
218 lines
9.1 KiB
Markdown
# Chat Models
|
|
|
|
This guide walks you through how the core LangChain modules work with chat models. We recommend first reading through the [Quickstart Guide](./getting_started.md), which introduces the core concepts in the context of LLMs.\
|
|
\
|
|
Chat models are a variation on language models. While chat models use language models under the hood, the interface they expose is a bit different: rather than expose a "text in, text out" API, they expose an interface where "chat messages" are the inputs and outputs.\
|
|
\
|
|
Chat model APIs are fairly new, so we are still figuring out the correct abstractions.\
|
|
\
|
|
**NOTE**: Before going further please make sure you've followed the [Installation](./getting_started.md#installation) and [Environment Setup](./getting_started.md#environment-setup) steps from the Quickstart Guide.
|
|
|
|
## Message Completions
|
|
|
|
You can get chat completions by passing one or more messages to the chat model. The response will be a message. The types of messages currently supported in LangChain are `AIMessage`, `HumanMessage`, `SystemMessage`, and `ChatMessage` -- `ChatMessage` takes in an arbitrary role parameter. Most of the time, you'll just be dealing with `HumanMessage`, `AIMessage`, and `SystemMessage`.
|
|
|
|
```python
|
|
from langchain.chat_models import ChatOpenAI
|
|
from langchain.schema import (
|
|
AIMessage,
|
|
HumanMessage,
|
|
SystemMessage
|
|
)
|
|
|
|
chat = ChatOpenAI(temperature=0)
|
|
```
|
|
|
|
You can get completions by passing in a single message.
|
|
|
|
```python
|
|
chat([HumanMessage(content="Translate this sentence from English to French. I love programming.")])
|
|
```
|
|
|
|
```pycon
|
|
AIMessage(content="J'aime programmer.", additional_kwargs={})
|
|
```
|
|
|
|
You can also pass in multiple messages for OpenAI's gpt-3.5-turbo and gpt-4 models.
|
|
|
|
```python
|
|
messages = [
|
|
SystemMessage(content="You are a helpful assistant that translates English to French."),
|
|
HumanMessage(content="I love programming.")
|
|
]
|
|
chat(messages)
|
|
# -> AIMessage(content="J'aime programmer.", additional_kwargs={})
|
|
```
|
|
|
|
You can go one step further and generate completions for multiple sets of messages using `generate`. This returns an `LLMResult` with an additional `message` parameter:
|
|
```python
|
|
batch_messages = [
|
|
[
|
|
SystemMessage(content="You are a helpful assistant that translates English to French."),
|
|
HumanMessage(content="I love programming.")
|
|
],
|
|
[
|
|
SystemMessage(content="You are a helpful assistant that translates English to French."),
|
|
HumanMessage(content="I love artificial intelligence.")
|
|
],
|
|
]
|
|
result = chat.generate(batch_messages)
|
|
result
|
|
# -> LLMResult(generations=[[ChatGeneration(text="J'aime programmer.", generation_info=None, message=AIMessage(content="J'aime programmer.", additional_kwargs={}))], [ChatGeneration(text="J'aime l'intelligence artificielle.", generation_info=None, message=AIMessage(content="J'aime l'intelligence artificielle.", additional_kwargs={}))]], llm_output={'token_usage': {'prompt_tokens': 57, 'completion_tokens': 20, 'total_tokens': 77}})
|
|
```
|
|
|
|
You can recover things like token usage from this LLMResult:
|
|
```
|
|
result.llm_output['token_usage']
|
|
# -> {'prompt_tokens': 57, 'completion_tokens': 20, 'total_tokens': 77}
|
|
```
|
|
|
|
|
|
## Prompt Templates
|
|
Similar to LLMs, you can make use of templating by using a `MessagePromptTemplate`. You can build a `ChatPromptTemplate` from one or more `MessagePromptTemplate`s. 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:
|
|
|
|
```python
|
|
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
|
|
The `LLMChain` discussed in the above section can be used with chat models as well:
|
|
|
|
```python
|
|
from langchain.chat_models import ChatOpenAI
|
|
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
|
|
Agents can also be used with chat models, you can initialize one using `AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION` as the agent type.
|
|
|
|
```python
|
|
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.
|
|
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?")
|
|
```
|
|
|
|
```pycon
|
|
|
|
> 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
|
|
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.
|
|
|
|
```python
|
|
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.")
|
|
# -> "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?"
|
|
```
|
|
|