استراتيجيات التكوين: شرح مُنشئ السلسلة

تعلم كل ما تحتاج إلى معرفته عن السلاسل مع LangChain

سنغطي ما يلي...

LangChain chains

السلاسل مكونات أساسية في بنية أنظمة فهم اللغة، حيث تعمل كتسلسلات من الاستدعاءات التي تتفاعل مع نماذج اللغة الكبيرة (LLMs) أو الأدوات أو خطوات المعالجة المسبقة للبيانات. تتميز LangChain في بناء هذه السلاسل بتوفير بيئة متينة تُعرف باسم LCEL (لغة تنفيذ سلسلة LangChain) تُسهّل إنشاء سلاسل مخصصة مُصممة خصيصًا لسير عمل مُحدد أو مُنشئات سلاسل جاهزة للاستخدام.

Press + to interact
RAG workflow: Chain constructor
RAG workflow: Chain constructor

تم تصميم هذه السلاسل المعدة مسبقًا لتبسيط عملية تكامل ونشر التسلسلات المعقدة إخراج أو استجابة برامج الدردشة الآلية، مما يجعل من الأسهل على المطورين الاستفادة من ميزات معالجة اللغة المتقدمة دون الحاجة إلى تخصيص مكثف.

هناك نوعان من السلاسل الجاهزة التي يدعمها LangChain.

Legacy chains

تُمثل السلاسل القديمة في LangChain نهجًا أساسيًا لهيكلة تسلسلات العمليات في مهام معالجة اللغة. تُنشأ هذه السلاسل عن طريق إنشاء فئات فرعية من فئة سلسلة قديمة مخصصة، مما يجعلها فئات مميزة ومستقلة داخل الإطار. تُغلّف هذه الطريقة وظائف وسير عمل محددة في مكونات قابلة للإدارة وإعادة الاستخدام. تُوفر السلاسل القديمة طريقة منظمة للتعامل مع تسلسلات معقدة من استدعاءات نماذج اللغة الكبيرة (LLMs)، أو الأدوات، أو خطوات المعالجة المسبقة للبيانات، كل منها مُصمم خصيصًا لمهام أو متطلبات محددة. باستخدام هذه الفئات المُحددة مسبقًا، يُمكن للمطورين الاستفادة من الأنماط والممارسات الراسخة داخل LangChain، مما يُسهّل تنفيذ وصيانة التطبيقات القائمة على اللغة. يتيح توافر سلاسل قديمة مُتنوعة مجموعة واسعة من التطبيقات، بدءًا من معالجة البيانات البسيطة ووصولًا إلى تفاعلات النماذج المُعقدة، مما يدعم نشرًا مُتنوعًا لتقنيات اللغة في بيئات مُتنوعة.

Chain types

فيما يلي جدول يحتوي على بعض السلاسل القديمة:

Chain

Usage

ConversationalRetrievalChain

This chain can be used to have conversations with a document. It takes in a question and (optional) previous conversation history. If there is a previous conversation history, it uses an LLM to rewrite the conversation into a query to send to a retriever (otherwise, it just uses the newest user input). It then fetches those documents and passes them (along with the conversation) to an LLM to respond.

StuffDocumentsChain

This chain takes a list of documents and formats them all into a prompt, then passes that prompt to an LLM. It passes ALL documents, so we should make sure it fits within the context window of the LLM you are using.

ReduceDocumentsChain

This chain combines documents by iteratively reducing them. It groups documents into chunks (less than some context length) and passes them into an LLM. It then takes the responses and continues to do this until it can fit everything into one final LLM call. This is useful when we have a lot of documents and want to have the LLM run over all of them, and we can do this in parallel.

MapRerankDocumentsChain

This calls on LLM on each document, asking it to not only answer but also produce a score of how confident it is. The answer with the highest confidence is then returned. This is useful when we have a lot of documents, but only want to answer based on a single document, rather than trying to combine answers.

LLMMath

This chain converts a user question to a math problem and then executes it.

QAGenerationChain

This chain creates both questions and answers from documents. It can be used to generate question/answer pairs for the evaluation of retrieval projects.

RetrievalQA

This chain first does a retrieval step to fetch relevant documents, then passes those documents into an LLM to generate a response.

LLMRouterChain

This chain uses an LLM to route between potential options.

تجدر الإشارة إلى أن LangChain تُحوّل تدريجيًا جميع أطر عمل السلاسل نحو لغة تعبير LangChain (LCEL)، والتي سنناقشها لاحقًا. في مرحلة ما، سيتم التخلي عن السلاسل القديمة.

LCEL Chains

تُمثل لغة تنفيذ سلسلة LangChain (LCEL) من LangChain أسلوبًا متطورًا لبناء وإدارة سلاسل العمليات في مهام معالجة اللغة. توفر LCEL أسلوب إنشاء عالي المستوى ونهجًا تعريفيًا لإنشاء سلاسل متنوعة بسلاسة، مما يُبسط الانتقال من النموذج الأولي إلى الإنتاج دون الحاجة إلى أي تغييرات في الكود. تُعد هذه المرونة أساسية لنشر كل شيء، بدءًا من سلاسل "prompt + LLM" البسيطة وصولًا إلى التسلسلات الأكثر تعقيدًا المطلوبة للتطبيقات المتقدمة.

Chain types

فيما يلي جدول يحتوي على بعض سلاسل LCEL:

Chain

Usage

create_stuff_documents_chain

This chain takes a list of documents and formats them all into a prompt, then passes that prompt to an LLM. It passes ALL documents, so we should make sure it fits within the context window of the LLM we are using.

create_openai_fn_runnable

If we want to use OpenAI function calling to optionally structure an output response, we may pass in multiple functions for it to call, but it does not have to.

create_structured_output_runnable

If we want to use OpenAI function calling to force the LLM to respond with a certain function. We may only pass in one function; the chain will always return this response.

load_query_constructor_runnable

This chain can be used to generate queries. We must specify a list of allowed operations, and then will return a runnable that converts a natural language query into those allowed operations.

create_sql_query_chain

If we want to construct a query for a SQL database from natural language.

create_history_aware_retriever

This chain takes in conversation history and then uses that to generate a search query which is passed to the underlying retriever.

create_retrieval_chain

This chain takes in a user inquiry, which is then passed to the retriever to fetch relevant documents. Those documents (and original inputs) are then passed to an LLM to generate a response

Key benefits

LCEL هي الطريقة المفضلة لاستخدام السلاسل لأنها توفر العديد من المزايا مقارنة بالسلاسل القديمة.

  • دعم البث عالي المستوى: يُحسّن استخدام LCEL بناء السلاسل من الكفاءة، خاصةً مع مقياس الوقت المستغرق لإنتاج أول رمز، والذي يقيس الوقت المستغرق حتى إنتاج أول جزء من إخراج . هذا يعني إمكانية بث الرموز مباشرةً من وحدة إدارة سلسلة التوريد (LLM) إلى مُحلِّل إخراج البث، مما يسمح باستلام أجزاء إخراج المُحلَّلة بسرعة بنفس معدل توفيرها من وحدة إدارة سلسلة التوريد.

  • دعم غير متزامن : السلاسل المُصممة باستخدام LCEL متوافقة مع واجهات برمجة التطبيقات المتزامنة غير متزامن . يُسهّل هذا التوافق المزدوج استخدام نفس قاعدة التعليمات البرمجية، بدءًا من النماذج الأولية وصولًا إلى بيئات الإنتاج الكاملة، مما يضمن أداءً ممتازًا والقدرة على التعامل مع العديد من الطلبات المتزامنة على خادم نفسه.

  • ...