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();

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

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

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

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

import { langchain } from 'genkitx-langchain';

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