We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

82
3.931.608,45

Cart

Quantity 18,00
Quantity 19.800,00
Quantity 1.000,00
Quantity 400,00
Quantity 1.500,00
Quantity 116,00
Quantity 450,00
Quantity 99,00
Quantity 10,00
Quantity 62,50
Quantity 19,00
Quantity 3.200,00

Experience the Power of Langchain: The Next Generation of Language Learning

LangChain is an innovative framework for creating language-powered applications. LangChain, with its configurable approach and extensive integration features, gives developers a new level of control and flexibility when it comes to exploiting language models.

LangChain

LangChain is a framework for creating language-powered apps. The most powerful and distinct apps will not only use an API to access a language model, but will also:

  • Be data-aware: Connect a language model to additional data sources.
  • Be agentic: Permit a language model to interact with its surroundings.

The LangChain framework is designed with the above principles in mind. This is the Python specific portion of the documentation.

Prompts:

At a high level, prompts are organized by use case inside the prompts directory. To load a prompt in LangChain, you should use the following code snippet:

from langchain.prompts import load_prompt

prompt = load_prompt('lc://prompts/path/to/file.json')

Chains

Chains extend beyond a single LLM call to include sequences of calls (to an LLM or another utility). LangChain offers a standard chain interface, numerous connections with other tools, and end-to-end chains for typical applications.

Chains are organized by use case inside the chains directory at a high level. Use the following code snippet to load a chain in LangChain:

from langchain.chains import load_chain

chain = load_chain('lc://chains/path/to/file.json')

Agents

Gents involve an LLM making judgements on which Actions to do, performing that Action, observing an Observation, and repeating this process until completed. LangChain provides a standard agent interface, a collection of agents, and examples of end-to-end agents.

Agents are organized by use case inside the agents directory at a high level. Use the following code snippet to load an agent in LangChain:

from langchain.agents import initialize_agent

llm = ...
tools = ...

agent = initialize_agent(tools, llm, agent="lc://agents/self-ask-with-search/agent.json")

Installation

To get started, install LangChain with the following command:

pip install langchain
# or
conda install langchain -c conda-forge

Environment Setup

Integrations with one or more model providers, data storage, APIs, and so on are frequently required when using LangChain.

Because we will be using OpenAI’s APIs in this example, we must first install their SDK:

pip install openai

We will then need to set the environment variable in the terminal.

export OPENAI_API_KEY="..."

Alternatively, you could do this from inside the Jupyter notebook (or Python script):

import os
os.environ["OPENAI_API_KEY"] = "..."

Building a Language Model Application: LLMs

We can begin developing our language model application now that we have installed LangChain and configured our environment.

LangChain has a number of modules that may be used to create language model applications. Modules can be integrated to make more complicated applications or used alone to construct basic apps.

LLMs: Get predictions from a language model

The most fundamental LangChain building component is calling an LLM on some input. Let’s go over an easy example of how to achieve this. Assume we’re creating a service that generates a company name based on what the company produces.

To achieve this, we must first import the LLM wrapper.

from langchain.llms import OpenAI

We can then initialize the wrapper with any arguments. In this example, we probably want the outputs to be MORE random, so we’ll initialize it with a HIGH temperature.

llm = OpenAI(temperature=0.9)

We can now call it on some input!

text = "What would be a good company name for a company that makes colorful socks?"
print(llm(text))
Feetful of Fun

Prompt Templates: Manage prompts for LLMs.

Calling an LLM is a good start, but it’s only the beginning. When you utilise an LLM in an application, you usually do not pass user input directly to the LLM. Instead, you’re presumably gathering user input and creating a prompt, which you then send to the LLM.

In the last example, for example, the text we provided in was hardcoded to request a name for a firm that sold colourful socks. In this hypothetical service, we’d like to take only the user input describing what the company does and format the prompt with that information.

This is simple with LangChain!

Let’s start with the prompt template:

from langchain.prompts import PromptTemplate

prompt = PromptTemplate(
    input_variables=["product"],
    template="What is a good name for a company that makes {product}?",
)

Let’s now see how this works! We can call the .format method to format it.

print(prompt.format(product="colorful socks"))
What is a good name for a company that makes colorful socks?

Chains: Combine LLMs and prompts in multi-step workflows

Until now, we’ve only used the PromptTemplate and LLM primitives on their own. Of course, a real application is a combination of primitives rather than a single one.

In LangChain, a chain is built up of links that can be primitives like LLMs or other chains.

An LLMChain is the most basic sort of chain, consisting of a Prompt Template and an LLM.

Extending on the previous example, we can build an LLMChain that accepts user input, prepares it with a Prompt Template, and then sends the processed result to an LLM.

from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI

llm = OpenAI(temperature=0.9)
prompt = PromptTemplate(
    input_variables=["product"],
    template="What is a good name for a company that makes {product}?",
)

We can now create a very simple chain that will take user input, format the prompt with it, and then send it to the LLM:

from langchain.chains import LLMChain
chain = LLMChain(llm=llm, prompt=prompt)

Now we can run that chain only specifying the product!

chain.run("colorful socks")
# -> 'nnSocktastic!'

The first chain is an LLM Chain. Although this is one of the simpler types of chains, understanding how it works will prepare you for working with more complex chains.

Agents: Dynamically Call Chains Based on User Input

So far, the chains we’ve looked at run in a predetermined order.

An LLM is no longer used by agents to choose which actions to do and in what order. An action might be examining the output of a tool or returning to the user.

Agents may be immensely strong when utilized appropriately. In this tutorial, we will demonstrate how to utilize agents using the simplest, highest-level API.

In order to load agents, you should understand the following concepts:

  • Tool: A function that serves a certain purpose. This can include Google Search, database lookups, Python REPLs, and other chains. A tool’s interface is presently a function that is meant to take a string as input and return a string as output.
  • LLM: The language model that drives the agent.
  • Agent: The agent to use. This should be a string containing the name of a support agent class. This notebook only covers utilizing the standard supported agents because it focuses on the simplest, highest-level API. See the documentation on custom agents if you wish to implement one.

Agents: For a list of supported agents and their specifications, see here.

Tools: For a list of predefined tools and their specifications, see here.

For this example, you will also need to install the SerpAPI Python package.

pip install google-search-results

And set the appropriate environment variables.

import os
os.environ["SERPAPI_API_KEY"] = "..."

Now we can get started!

from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms import OpenAI

# First, let's load the language model we're going to use to control the agent.
llm = OpenAI(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.
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)

# Now 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?")
> Entering new AgentExecutor chain...
 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.

Memory: Add State to Chains and Agents

All of the chains and agents we’ve encountered so far have been stateless. However, you may want a chain or agent to have some concept of “memory” in order for it to remember information from previous interactions. When designing a chatbot, for example, you want it to remember previous messages so that it can use context from that to have a better conversation. This is a sort of “short-term memory.” On the more complex side, you could imagine a chain/agent remembering key pieces of information over time – this would be a form of “long-term memory”.

LangChain provides several specially created chains just for this purpose. This notebook walks through using one of those chains (the ConversationChain) with two different types of memory.

By default, the ConversationChain has a simple type of memory that remembers all previous inputs/outputs and adds them to the context that is passed. Let’s take a look at using this chain (setting verbose=True so we can see the prompt).

from langchain import OpenAI, ConversationChain

llm = OpenAI(temperature=0)
conversation = ConversationChain(llm=llm, verbose=True)

output = conversation.predict(input="Hi there!")
print(output)
> 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?'
output = conversation.predict(input="I'm doing well! Just having a conversation with an AI.")
print(output)
> 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?"

Building a Language Model Application: Chat Models

Similarly, conversation models can be used instead of LLMs. Language models are a subset of chat models. While chat models use language models behind the scenes, the interface they expose is slightly different: instead of exposing a “text in, text out” API, they expose an interface where “chat messages” are the inputs and outputs.

Because chat model APIs are still in their early stages, they are still determining the appropriate abstractions.

Get Message Completions from a Chat Model

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 AIMessageHumanMessageSystemMessage, and ChatMessage – ChatMessage takes in an arbitrary role parameter. Most of the time, you’ll just be dealing with HumanMessageAIMessage, and SystemMessage.

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.

chat([HumanMessage(content="Translate this sentence from English to French. I love programming.")])
# -> 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.

messages = [
    SystemMessage(content="You are a helpful assistant that translates English to French."),
    HumanMessage(content="Translate this sentence from English to French. 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:

batch_messages = [
    [
        SystemMessage(content="You are a helpful assistant that translates English to French."),
        HumanMessage(content="Translate this sentence from English to French. I love programming.")
    ],
    [
        SystemMessage(content="You are a helpful assistant that translates English to French."),
        HumanMessage(content="Translate this sentence from English to French. 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': 71, 'completion_tokens': 18, 'total_tokens': 89}})

You can recover things like token usage from this LLMResult:

result.llm_output['token_usage']
# -> {'prompt_tokens': 71, 'completion_tokens': 18, 'total_tokens': 89}

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 (
    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.
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

Memory may be used with chains and agents that have been initialized using conversation models. The primary difference between this and Memory for LLMs is that instead of condensing all previous messages into a string, we may store them as their own distinct 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.")
# -> "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?"

This article helps you learn about LangChain. We trust that it has been helpful to you. Please feel free to share your thoughts and feedback in the comment section below.

82
3.931.608,45

Cart

Quantity 18,00
Quantity 19.800,00
Quantity 1.000,00
Quantity 400,00
Quantity 1.500,00
Quantity 116,00
Quantity 450,00
Quantity 99,00
Quantity 10,00
Quantity 62,50
Quantity 19,00
Quantity 3.200,00