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

इंस्टॉल करना

npm i --save genkitx-langchain

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

Genkit के फ़्लो में, ज़्यादातर LangChain चेन या यूटिलिटी टूल का इस्तेमाल किया जा सकता है. नीचे दिए गए उदाहरण में, आसान RAG सैंपल बनाने के लिए 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 का इस्तेमाल, Genkit की जांच करने की सुविधाओं की मदद से LangChain चेन बनाने के लिए किया गया है. अब Dev यूज़र इंटरफ़ेस (यूआई) से या प्रोडक्शन में फ़्लो चलाने पर, आपको LangChain चेन के बारे में पूरी जानकारी मिलेगी.

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

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

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

import { langchain } from 'genkitx-langchain';

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