Genkit के साथ LangChain का इस्तेमाल करना

इंस्टॉल करना

npm i --save genkitx-langchain

इस्तेमाल किए जाने से जुड़ी जानकारी

जेनकिट फ़्लो में, LangChain की ज़्यादातर चेन या यूटिलिटी का इस्तेमाल उसी तरह किया जा सकता है. नीचे दिए गए उदाहरण में, बेहतर आरएजी सैंपल बनाने के लिए, LangChain रिट्रीवर, दस्तावेज़ लोड करने वाले टूल, और चेन कंस्ट्रक्शन का इस्तेमाल किया गया है.

import { initializeGenkit } from '@genkit-ai/core';
import { defineFlow, run, startFlowsServer } from '@genkit-ai/flow';
import { GoogleVertexAIEmbeddings } from '@langchain/community/embeddings/googlevertexai';
import { GoogleVertexAI } from '@langchain/community/llms/googlevertexai';
import { StringOutputParser } from '@langchain/core/output_parsers';
import { PromptTemplate } from '@langchain/core/prompts';
import {
  RunnablePassthrough,
  RunnableSequence,
} from '@langchain/core/runnables';
import { GenkitTracer } from 'genkitx-langchain';
import { PDFLoader } from 'langchain/document_loaders/fs/pdf';
import { formatDocumentsAsString } from 'langchain/util/document';
import { MemoryVectorStore } from 'langchain/vectorstores/memory';
import * as z from 'zod';

import config from './genkit.config';

initializeGenkit(config);

const vectorStore = new MemoryVectorStore(new GoogleVertexAIEmbeddings());
const model = new GoogleVertexAI();

export const indexPdf = defineFlow(
  { name: 'indexPdf', inputSchema: z.string(), outputSchema: z.void() },
  async (filePath) => {
    const docs = await run('load-pdf', async () => {
      return await new PDFLoader(filePath).load();
    });
    await run('index', async () => {
      vectorStore.addDocuments(docs);
    });
  }
);

const prompt =
  PromptTemplate.fromTemplate(`Answer the question based only on the following context:
{context}

Question: {question}`);
const retriever = vectorStore.asRetriever();

export const pdfQA = defineFlow(
  { name: 'pdfQA', inputSchema: z.string(), outputSchema: z.string() },
  async (question) => {
    const chain = RunnableSequence.from([
      {
        context: retriever.pipe(formatDocumentsAsString),
        question: new RunnablePassthrough(),
      },
      prompt,
      model,
      new StringOutputParser(),
    ]);

    return await chain.invoke(question, { callbacks: [new GenkitTracer()] });
  }
);

startFlowsServer();

ध्यान दें कि इस उदाहरण में, genkitx-langchain प्लगिन से मिले GenkitTracer का इस्तेमाल, जेनकिट मॉनिटर करने की सुविधाओं वाले LangChain चेन को इंस्ट्रुमेंट करने के लिए किया गया है. अब Dev यूज़र इंटरफ़ेस (यूआई) से या प्रोडक्शन में, आपको LangChain की चेन के बारे में पूरी जानकारी मिलेगी.

यह भी ध्यान रखें कि LangChain कॉम्पोनेंट, जेनकिट प्रिमिटिव (मॉडल, दस्तावेज़, रिट्रीवर वगैरह) के साथ काम नहीं करते.

मूल्यांकन करने वाले (झलक)

Genkit के साथ LangChain के समीक्षकों का इस्तेमाल किया जा सकता है. कॉन्फ़िगर करें कि आपको langchain प्लगिन से कौनसे समीक्षक चाहिए. इसके बाद, आकलन करने की स्टैंडर्ड प्रोसेस को फ़ॉलो करें:

import { langchain } from 'genkitx-langchain';

configureGenkit({
  plugins: [
    langchain({
      evaluators: {
        judge: geminiPro,
        criteria: ['harmfulness', 'maliciousness'],
      },
    }),
  ],
});