1. ایل سی ای ایل کی معرفت
LCEL (لینگ چین ایکسپریشن لینگویج) ایک سادہ اور آسان فریم ورک ہے جو پیچیدہ چینوں کو بنانے کے لیے ہے۔ یہ ایک متحدہ انٹرفیس فراہم کرتا ہے اور کمپوزٹ پرائمٹوز کو فراہم کرتا ہے تاکہ چین بنانا آسان ہو۔ ہر ایل سی ای ایل آبجیکٹ Runnable
انٹرفیس کا اطلاق کرتا ہے، جو عام استعمال ہونے والے بلانے کی میثاقیں (جیسے invoke
, batch
, stream
, ainvoke
وغیرہ) کا مجموعہ تعین کرتا ہے۔ لہذا، ایل سی ای ایل آبجیکٹ کی ہر سلسلے کو خود ایک ایل سی ای ایل آبجیکٹ بنانے کی ایک اجازت ہوتی ہے۔
2. بلانے
2.1. ایل سی ای ایل کے بغیر
ایل سی ای ایل کے بغیر، آپ مندرجہ ذیل کوڈ فراہم کرتا ہے تاکہ آپ ایک موضوع کی سٹرنگ پاس کریں اور ایک لطیفہ سٹرنگ حاصل کریں۔
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. ایل سی ای ایل کا استعمال
برعکس میں، ایل سی ای ایل کا استعمال ہمیں اسی کام کو مختصر طریقے سے حاصل کر سکتا ہے۔ ذیل میں دی گئی کوڈ کی مدد سے آپ دیکھ سکتے ہیں کہ کیسے ایسے چین بنانا ممکن ہوتا ہے۔
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. ایل سی ای ایل کے بغیر
مندرجہ ذیل کوڈ کی مدد سے آپ دیکھ سکتے ہیں کہ کیسے سٹریم میں نتائج کا عمل کریں۔
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. ایل سی ایل کا استعمال
ایل سی ایل کے ساتھ سٹریمنگ کرنا زیادہ آسان ہے۔ نیچے دی گئی کوڈ کی مدد سے آپ دیکھ سکتے ہیں کہ ایل سی ای ل کے ساتھ سٹریمنگ کیسے کریں۔
for chunk in chain.stream("ice cream"):
print(chunk, end="", flush=True)
4. بیچ پروسیسنگ
4.1. ایل سی ای ایل کے بغیر
مندرجہ ذیل کوڈ کی مدد سے آپ دیکھ سکتے ہیں کہ کیسے ایک بیچ میں داخلات کا متوازی عمل کریں۔
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. ایل سی ای ایل کا استعمال
ایل سی ایل کا استعمال بیچ پروسیسنگ کے لئے بہت سیدھا ہے۔ مواد ذیل میں دی گئی کوڈ کی مدد سے آپ دیکھ سکتے ہیں کہ کیسے ایل سی ای ایل کا استعمال بیچ پروسیسنگ کے لیے ہوتا ہے۔
chain.batch(["ice cream", "spaghetti", "dumplings"])