mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-07 22:11:51 +00:00
beef up getting started (#8139)
Co-authored-by: Bagatur <baskaryan@gmail.com>
This commit is contained in:
@@ -1,55 +0,0 @@
|
||||
```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'
|
||||
```
|
||||
|
@@ -1,37 +0,0 @@
|
||||
```python
|
||||
from langchain.agents import AgentType, initialize_agent, load_tools
|
||||
from langchain.llms import OpenAI
|
||||
|
||||
# The language model we're going to use to control the agent.
|
||||
llm = OpenAI(temperature=0)
|
||||
|
||||
# The tools we'll give the Agent access to. Note that the 'llm-math' tool uses an LLM, so we need to pass that in.
|
||||
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, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
|
||||
|
||||
# Let's test it out!
|
||||
agent.run("What was the high temperature in SF yesterday in Fahrenheit? What is that number raised to the .023 power?")
|
||||
```
|
||||
```pycon
|
||||
> Entering new AgentExecutor chain...
|
||||
|
||||
Thought: I need to find the temperature first, then use the calculator to raise it to the .023 power.
|
||||
Action: Search
|
||||
Action Input: "High temperature in SF yesterday"
|
||||
Observation: San Francisco Temperature Yesterday. Maximum temperature yesterday: 57 °F (at 1:56 pm) Minimum temperature yesterday: 49 °F (at 1:56 am) Average temperature ...
|
||||
|
||||
Thought: I now have the temperature, so I can use the calculator to raise it to the .023 power.
|
||||
Action: Calculator
|
||||
Action Input: 57^.023
|
||||
Observation: Answer: 1.0974509573251117
|
||||
|
||||
Thought: I now know the final answer
|
||||
Final Answer: The high temperature in SF yesterday in Fahrenheit raised to the .023 power is 1.0974509573251117.
|
||||
|
||||
> Finished chain.
|
||||
```
|
||||
```pycon
|
||||
The high temperature in SF yesterday in Fahrenheit raised to the .023 power is 1.0974509573251117.
|
||||
```
|
@@ -1,23 +0,0 @@
|
||||
```python
|
||||
from langchain import LLMChain
|
||||
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])
|
||||
|
||||
chain = LLMChain(llm=chat, prompt=chat_prompt)
|
||||
chain.run(input_language="English", output_language="French", text="I love programming.")
|
||||
```
|
||||
```pycon
|
||||
J'aime programmer.
|
||||
```
|
@@ -1,17 +0,0 @@
|
||||
Using this we can replace
|
||||
|
||||
```python
|
||||
llm.predict("What would be a good company name for a company that makes colorful socks?")
|
||||
```
|
||||
|
||||
with
|
||||
|
||||
```python
|
||||
from langchain.chains import LLMChain
|
||||
|
||||
chain = LLMChain(llm=llm, prompt=prompt)
|
||||
chain.run("colorful socks")
|
||||
```
|
||||
```pycon
|
||||
Feetful of Fun
|
||||
```
|
@@ -1,21 +0,0 @@
|
||||
```python
|
||||
from langchain.chat_models import ChatOpenAI
|
||||
from langchain.schema import (
|
||||
AIMessage,
|
||||
HumanMessage,
|
||||
SystemMessage
|
||||
)
|
||||
|
||||
chat = ChatOpenAI(temperature=0)
|
||||
chat.predict_messages([HumanMessage(content="Translate this sentence from English to French. I love programming.")])
|
||||
# >> AIMessage(content="J'aime programmer.", additional_kwargs={})
|
||||
```
|
||||
|
||||
It is useful to understand how chat models are different from a normal LLM, but it can often be handy to just be able to treat them the same.
|
||||
LangChain makes that easy by also exposing an interface through which you can interact with a chat model as you would a normal LLM.
|
||||
You can access this through the `predict` interface.
|
||||
|
||||
```python
|
||||
chat.predict("Translate this sentence from English to French. I love programming.")
|
||||
# >> J'aime programmer
|
||||
```
|
13
docs/snippets/get_started/quickstart/import_llms.mdx
Normal file
13
docs/snippets/get_started/quickstart/import_llms.mdx
Normal file
@@ -0,0 +1,13 @@
|
||||
```python
|
||||
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"
|
||||
```
|
12
docs/snippets/get_started/quickstart/input_messages.mdx
Normal file
12
docs/snippets/get_started/quickstart/input_messages.mdx
Normal file
@@ -0,0 +1,12 @@
|
||||
```python
|
||||
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
|
||||
```
|
9
docs/snippets/get_started/quickstart/input_string.mdx
Normal file
9
docs/snippets/get_started/quickstart/input_string.mdx
Normal file
@@ -0,0 +1,9 @@
|
||||
```python
|
||||
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
|
||||
```
|
@@ -1,13 +0,0 @@
|
||||
```python
|
||||
from langchain.llms import OpenAI
|
||||
|
||||
llm = OpenAI(temperature=0.9)
|
||||
```
|
||||
|
||||
And now we can pass in text and get predictions!
|
||||
|
||||
```python
|
||||
llm.predict("What would be a good company name for a company that makes colorful socks?")
|
||||
# >> Feetful of Fun
|
||||
```
|
||||
|
34
docs/snippets/get_started/quickstart/llm_chain.mdx
Normal file
34
docs/snippets/get_started/quickstart/llm_chain.mdx
Normal file
@@ -0,0 +1,34 @@
|
||||
```python
|
||||
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 generated 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']
|
||||
```
|
@@ -1,44 +0,0 @@
|
||||
```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!")
|
||||
```
|
||||
```pycon
|
||||
Hello! How can I assist you today?
|
||||
```
|
||||
|
||||
```python
|
||||
conversation.predict(input="I'm doing well! Just having a conversation with an AI.")
|
||||
```
|
||||
```pycon
|
||||
That sounds like fun! I'm happy to chat with you. Is there anything specific you'd like to talk about?
|
||||
```
|
||||
|
||||
```python
|
||||
conversation.predict(input="Tell me about yourself.")
|
||||
```
|
||||
```pycon
|
||||
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?
|
||||
```
|
@@ -1,51 +0,0 @@
|
||||
```python
|
||||
from langchain import OpenAI, ConversationChain
|
||||
|
||||
llm = OpenAI(temperature=0)
|
||||
conversation = ConversationChain(llm=llm, verbose=True)
|
||||
|
||||
conversation.run("Hi there!")
|
||||
```
|
||||
|
||||
here's what's going on under the hood
|
||||
|
||||
```pycon
|
||||
> Entering new chain...
|
||||
Prompt after formatting:
|
||||
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.
|
||||
|
||||
Current conversation:
|
||||
|
||||
Human: Hi there!
|
||||
AI:
|
||||
|
||||
> Finished chain.
|
||||
|
||||
>> 'Hello! How are you today?'
|
||||
```
|
||||
|
||||
Now if we run the chain again
|
||||
|
||||
```python
|
||||
conversation.run("I'm doing well! Just having a conversation with an AI.")
|
||||
```
|
||||
|
||||
we'll see that the full prompt that's passed to the model contains the input and output of our first interaction, along with our latest input
|
||||
|
||||
```pycon
|
||||
> Entering new chain...
|
||||
Prompt after formatting:
|
||||
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.
|
||||
|
||||
Current conversation:
|
||||
|
||||
Human: Hi there!
|
||||
AI: Hello! How are you today?
|
||||
Human: I'm doing well! Just having a conversation with an AI.
|
||||
AI:
|
||||
|
||||
> Finished chain.
|
||||
|
||||
>> "That's great! What would you like to talk about?"
|
||||
```
|
||||
|
14
docs/snippets/get_started/quickstart/output_parser.mdx
Normal file
14
docs/snippets/get_started/quickstart/output_parser.mdx
Normal file
@@ -0,0 +1,14 @@
|
||||
```python
|
||||
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']
|
||||
```
|
Reference in New Issue
Block a user