1. LCEL সম্পর্কে পরিচিতি

LCEL (LangChain Expression Language) হল একটি সহজ এবং ব্যবহারযোগ্য ফ্রেমওয়ার্ক যা জটিল চেইন তৈরি করতে ব্যবহার করা হয়। এটি একটি একক ইন্টারফেস এবং সংযোজিত প্রাকৃতিক প্রাইমিটিভ প্রদান করে যাতে চেইন তৈরি করা সহজ হয়। প্রতিটি LCEL অবজেক্ট মৌলিকভাবে Runnable ইন্টারফেস ইমপ্লিমেন্ট করে, যা একটি সাধারণভাবে ব্যবহৃত ডাকনামা (যেমন invoke, batch, stream, ainvoke ইত্যাদি) সংজ্ঞা করে। তাই, LCEL অবজেক্টগুলির চেইনগুলি এই ডাকনামা সমর্থন করতে পারে, যেহেতু প্রতিটি LCEL অবজেক্টের চেইন নিজেই একটি LCEL অবজেক্ট।

2. আহ্বান

2.1. LCEL ব্যবহার ছাড়া

LCEL ব্যবহার ছাড়া, আপনি নিম্নলিখিত কোড টুকরা ব্যবহার করে একটি টপিক স্ট্রিং পাঠান এবং একটি উক্তি স্ট্রিং প্রাপ্ত করতে পারেন।

from typing import List
import openai

prompt_template = "Tell me a short joke about {topic}"
client = openai.OpenAI()

def call_chat_model(messages: List[dict]) -> str:
    response = client.chat.completions.create(
        model="gpt-3.5-turbo", 
        messages=messages,
    )
    return response.choices[0].message.content

def invoke_chain(topic: str) -> str:
    prompt_value = prompt_template.format(topic=topic)
    messages = [{"role": "user", "content": prompt_value}]
    return call_chat_model(messages)

invoke_chain("ice cream")

2.2. LCEL ব্যবহার করে

তিনিরিক্ষা করলে, LCEL ব্যবহার করে একই কার্যকারিতা অধিক সংক্ষিপ্তভাবে অর্জন করা যায়। নীচের কোড টুকরার মাধ্যমে সহজেই LCEL অবজেক্টগুলি ব্যবহার করে চেইন তৈরি করার উপায় দেখানো হয়।

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough

prompt = ChatPromptTemplate.from_template(
    "Tell me a short joke about {topic}"
)
output_parser = StrOutputParser()
model = ChatOpenAI(model="gpt-3.5-turbo")
chain = (
    {"topic": RunnablePassthrough()} 
    | prompt
    | model
    | output_parser
)

chain.invoke("ice cream")

3. স্ট্রিমিং

3.1. LCEL ছাড়া ব্যবহার করে

নীচের কোড টুকরা দেখায় যে ভাবে LCEL ব্যবহার না করে ফলাফল স্ট্রিম প্রসেস করা যায়।

from typing import Iterator

def stream_chat_model(messages: List[dict]) -> Iterator[str]:
    stream = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=messages,
        stream=True,
    )
    for response in stream:
        content = response.choices[0].delta.content
        if content is not None:
            yield content

def stream_chain(topic: str) -> Iterator[str]:
    prompt_value = prompt.format(topic=topic)
    stream = stream_chat_model([{"role": "user", "content": prompt_value}])
    for chunk in stream:
        print(chunk, end="", flush=True)

stream_chain("ice cream")

3.2. LCEL ব্যবহার করে

LCEL ব্যবহার করে ফলাফল স্ট্রিমিং অধিক সুবিধাজনক। নীচের কোড টুকরাটি দেখায় যে ভাবে LCEL ব্যবহার করে স্ট্রিম করা হয়।

for chunk in chain.stream("ice cream"):
    print(chunk, end="", flush=True)

4. ব্যাচ প্রসেসিং

4.1. LCEL ছাড়া ব্যবহার করে

নীচের কোড টুকরা দেখায় যে ভাবে ব্যবহার করা যায় LCEL ব্যবহার না করে একটি ব্যাচ ইনপুটের প্যারালেল প্রসেসিং করার জন্য।

from concurrent.futures import ThreadPoolExecutor

def batch_chain(topics: list) -> list:
    with ThreadPoolExecutor(max_workers=5) as executor:
        return list(executor.map(invoke_chain, topics))

batch_chain(["ice cream", "spaghetti", "dumplings"])

4.2. LCEL ব্যবহার করে

LCEL ব্যবহার করে ব্যাচ প্রসেসিং খুব সরল। নীচের কোড টুকরা দেখায় LCEL ব্যবহার করে ব্যাচ প্রসেসিং করার উপায়।

chain.batch(["ice cream", "spaghetti", "dumplings"])