# 06. LCEL interface

### LCEL interface <a href="#lcel" id="lcel"></a>

To make custom chains as easy as possible, [`Runnable`](https://api.python.langchain.com/en/stable/runnables/langchain_core.runnables.base.Runnable.html#langchain_core.runnables.base.Runnable) Protocol implemented.

`Runnable` The protocol is implemented in most components.

This is a standard interface, which easily defines custom chains and makes calls in a standard way. Standard interfaces include:

* [`stream`](https://wikidocs.net/233345#stream) : Stream chunks of responses.
* [`invoke`](https://wikidocs.net/233345#invoke) : Call the chain for input.
* [`batch`](https://wikidocs.net/233345#batch) : Call the chain for the input list.

There are also asynchronous methods.

* [`astream`](https://wikidocs.net/233345#async-stream) : Asynchronously stream the chunk of the response.
* [`ainvoke`](https://wikidocs.net/233345#async-invoke) : Asynchronously calls the chain for input.
* [`abatch`](https://wikidocs.net/233345#async-batch) : Asynchronously calls the chain for the input list.
* [`astream_log`](https://wikidocs.net/233345#async-stream-intermediate-steps) : Stream the intermediate steps that occur as well as the final response.

```python
# Configuration file for managing API KEY as environment variable
from dotenv import load_dotenv

# Load API KEY information
load_dotenv()
```

```
 True 
```

```python
# Set up LangSmith tracking. https://smith.langchain.com
from langchain_teddynote import logging

# Enter a project name.
logging.langsmith("CH01-Basic")
```

Generate chain using LCEL grammar.

```python
from langchain_openai import ChatOpenAI
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser

# Instantiate the ChatOpenAI model.
model = ChatOpenAI()
# Create a prompt template that asks for a joke about a given topic.
prompt = PromptTemplate.from_template("{topic} Explain in 3 sentences.")
# Create conversation chains by connecting prompts and models.
chain = prompt | model | StrOutputParser()
```

### stream: real-time output <a href="#stream" id="stream"></a>

This function `chain.stream` Use methods to create a data stream for a given topic, and repeat this stream to the content of each data ( `content` ) Output immediately. `end=""` The factor is set not to give a lapel after output, `flush=True` The factor immediately empties the output buffer.

```python
# Create and iterate over a stream for a 'multimodal' topic using the chain.stream method..
for token in chain.stream({"topic": "multimodal"}):
    # Prints the contents of data received from the stream. Prints continuously without line breaks, and immediately empties the buffer.
    print(token, end="", flush=True)
```

```
 Multimodal refers to the technique of communicating and interacting with information through several different forms of communication. For example, you can use a variety of media, such as voice, text, images, and videos, to interact with users. Multimodal technology can help improve the user experience and increase the efficiency of information delivery. 
```

### invoke: call <a href="#invoke" id="invoke"></a>

`chain` Object `invoke` The method takes the subject as a factor and performs processing on that subject.

```python
# Call the invoke method of the chain object and pass a dictionary with the topic 'ChatGPT'.
chain.invoke({"topic": "ChatGPT"})
```

```
 'ChatGPT is an interactive, intelligent model developed by OpenAI, which can naturally lead conversations on various topics. Users can answer questions or continue conversations and acquire new information through ChatGPT. ChatGPT also learns the user's input, gradually providing a more fluent and natural conversation.' 
```

### batch: batch (unit execution) <a href="#batch" id="batch"></a>

function `chain.batch` Is a list containing multiple dictionary, which is in each dictionary `topic` Perform batch processing using the value of the key.

```python
# Call a function to batch process a given list of topics
chain.batch([{"topic": "ChatGPT"}, {"topic": "Instagram"}])
```

```
 ['ChatGPT is an artificial chatbot that uses natural language processing techniques to conduct conversations. It's naturally interactive with users and lets you talk about a variety of topics. ChatGPT can be used for a variety of purposes, including information provision, question answering, counseling and entertainment.','Instagram is a social media platform that shares photos and videos and communicates with others. The hashtag allows you to search for photos by interest or topic and communicate with followers. It is popular worldwide as a platform for popular influencers or brands to be active.'] 
```

`max_concurrency` You can set the number of concurrent requests using parameters

`config` Dictary `max_concurrency` Sets the maximum number of jobs that can be handled simultaneously through the key. It is set here to handle up to 3 tasks simultaneously.

```python
chain.batch(
    [
        {"topic": "ChatGPT"},
        {"topic": "Instagram"},
        {"topic": "multimodal"},
        {"topic": "programming"},
        {"topic": "machine learning"},
    ],
    config={"max_concurrency": 3},
)
```

```
 ['ChatGPT is an artificial chatbot, using natural language processing techniques to interact with chat partners. You can answer user questions, continue conversations, and talk about various topics. ChatGPT not only helps you communicate naturally with users, but also provides information and helps you solve problems.','Instagram is a social media platform that allows you to share photos and videos and communicate with others. It offers a variety of filters and editing features, making it easy for users to upload stunning photos, and hashtags to find content that suits their interests. It is also a platform with a lot of popular influencers' activities and advertising.','Multimidal is a technology that provides or processes several different forms of information together. It integrates multiple forms of data, such as text, images, voice, and video, to effectively deliver and interoperate. Multimodal improves the user experience and helps to understand and utilize information more easily.','Programming is a kind of communication method that directs the computer to work. For this, the language used by programmers must be in a form that the computer can understand, and grammar and logic can achieve the desired results. Programming can be used in a variety of fields, such as developing software and solving problems.','Machining is a field of artificial intelligence where computer systems learn from data and discover patterns to predict or make decisions. This allows computers to learn and solve problems on their own without human intervention. Machine learning is image and speech recognition, autonomous vehicles,  It is used in various fields such as health care.'] 
```

### async stream: asynchronous stream <a href="#async-stream" id="async-stream"></a>

function `chain.astream` It generates an asynchronous stream, and asynchronously processes messages for a given topic.

Asynchronous for loop ( `async for` Using ) to receive messages sequentially from the stream, `print` The content of the message through the function ( `s.content` ) Output immediately. `end=""` Is set to not give a tape measure after output, `flush=True` Force the output buffer to be emptied and output immediately.

```python
# Process messages on the 'YouTube' topic using asynchronous streams.
async for token in chain.astream({"topic": "YouTube"}):
    # Prints the message content. Prints it directly without line breaks and clears the buffer.
    print(token, end="", flush=True)
```

```
 YouTube is an online video platform for sharing and watching videos. Anyone can upload their own videos to share with others, watch video content and find a variety of information or things to enjoy. You can also get entertainment and information through videos from famous creators. 
```

### async invoke: asynchronous call <a href="#async-invoke" id="async-invoke"></a>

`chain` Object `ainvoke` Methods perform tasks using factors given asynchronously. here `topic` Come on `NVDA` (Evidia's Tiker) Ra is passing a dictary with a value as a factor. This method can be used to asynchronously request processing for a specific topic.

```python
# We handle the 'NVDA' topic by calling the 'ainvoke' method of the asynchronous chain object.
my_process = chain.ainvoke({"topic": "NVDA"})
```

```python
# 비동기로 처리되는 프로세스가 완료될 때까지 기다립니다.
await my_process
```

```
 'NVDA is NVIDIA's stock code, which refers to the shares of NVIDIA, an American semiconductor company. NVIDIA is a company specializing in graphics processing units (GPUs), providing technology in a variety of fields, including artificial intelligence, virtual reality, and autonomous vehicles. NVDA stocks are generating high returns with the growth of the technology industry.' 
```

### async batch: asynchronous placement <a href="#async-batch" id="async-batch"></a>

function `abatch` Asynchronously, a series of tasks are batch processed.

In this example `chain` Object `abatch` Using methods `topic` I am dealing with the work on asynchronously.

`await` Keywords are used to wait for the asynchronous operation to complete.

```python
# Asynchronously performs batch processing on a given topic.
my_abatch_process = chain.abatch(
    [{"topic": "YouTube"}, {"topic": "Instagram"}, {"topic": "Facebook"}]
)
```

```python
# Waits for a batch process that is processed asynchronously to complete.
await my_abatch_process
```

```
 ['YouTube is a video sharing platform that allows users to upload and watch videos. It offers a variety of content and users can watch videos for free. It is also known as a platform where famous YouTubes can act and make money.','Instagram is a social media platform that shares photos and videos, and users can use a variety of filters and effects to make their content look great. You can also communicate with followers, like other users' posts, or leave comments and communicate. Instagram is also used for business or personal branding, and many people share a small moment to a special moment in their daily lives.','Facebook is an American social network service, a platform for users to communicate and share information. Currently, there are more than 30 billion users worldwide, and it is widely used in advertising and business activities. It has also been controversial, including privacy concerns and fake news, but it is still a representative SNS service that many people use.'] 
```

### Parallel: Parallel <a href="#parallel" id="parallel"></a>

Let's take a look at how LangChain Expression Language supports parallel requests. For example, `RunnableParallel` When used (written in the form of a often dictionary), each element is executed in parallel.

`langchain_core.runnables` Of module `RunnableParallel` Shows examples of running two tasks in parallel using classes.

`ChatPromptTemplate.from_template` Given using methods `country` for **Capital** Wow **area** Two chains to save `chain1` , `chain2` ).

Each of these chains `model` And pipe ( `|` ) Connected through the operator. Finally, `RunnableParallel` Use class to chain these two `capital` Wow `area` Ira can be combined with a key and run simultaneously `combined` Create an object.

```python
from langchain_core.runnables import RunnableParallel

# {country} Create a chain that asks for the capital of.
chain1 = (
    PromptTemplate.from_template("{country} Where is the capital of?")
    | model
    | StrOutputParser()
)

# {country} Create a chain that asks for the area of.
chain2 = (
    PromptTemplate.from_template("{country} What is the area of?")
    | model
    | StrOutputParser()
)

# Create a parallel execution chain that creates the two chains above simultaneously.
combined = RunnableParallel(capital=chain1, area=chain2)
```

`chain1.invoke()` function `chain1` Object `invoke` Call the method.

At this time, `country` Ira is tall `대한민국` Rag passes a dictation with a value.

```python
# Run chain 1.
chain1.invoke({"country": "korea"})
```

```
 'The capital of Korea is Seoul.' 
```

this time `chain2.invoke()` Call. `country` Other countries on the key `미국` Pass.

```python
# Run chain 2.
chain2.invoke({"country": "USA"})
```

```
 'The area of the United States is about 9,826,675 square kilometers.' 
```

`combined` Object `invoke` Methods given `country` Perform processing on.

In this example `대한민국` LA theme `invoke` Pass to the method to run it.

```python
# Executes a parallel execution chain.
combined.invoke({"country": "korea"})
```

```
 {'capital':'The capital of the Republic of Korea is Seoul.','area':'The area of the Republic of Korea is about 100,363.4 square kilometers.'} 
```

#### Parallel processing in batches <a href="#id-1" id="id-1"></a>

Parallel processing can be combined with other executable codes. Let's use batch and parallel processing.

`chain1.batch` The function takes a list containing multiple dicks as an factor, processing the value corresponding to the "topic" key for each dictionary. In this example, we are deploying two topics, "Korea" and "USA".

```python
# Perform batch processing.
chain1.batch([{"country": "korea"}, {"country": "USA"}])
```

```
 ['The capital of Korea is Seoul.','The capital of the United States is Washington, D.C.is.'] 
```

`chain2.batch` The function receives multiple dicks in list form and performs batch processing.

In this example `korea` Wow `USA` Ra requests processing for two countries.

```python
# Perform batch processing.
chain2.batch([{"country": "korea"}, {"country": "USA"}])
```

```
 ['The total area of Korea is about 100,363 square kilometers.','The area of the United States is about 9,834,000 km².'] 
```

`combined.batch` Functions are used to process a given data into batches. In this example, we take a list containing two dictionary objects as a factor, each `대한민국` Wow `미국` Placing data for both countries.

```python
# Processes given data in batches.
combined.batch([{"country": "korea"}, {"country": "USA"}])
```

```
 [{'capital':'The capital of the Republic of Korea is Seoul.','area':'The area of the Republic of Korea is about 100,363 km².'}, {'capital':'The capital of the United States Washington D.C.is.','area':'The area of the United States is about 9,833,520 km².'}] 
```

<br>
