Sử dụng LangChain với Genkit

Cài đặt

npm i --save genkitx-langchain

Hoạt động sử dụng

Bạn có thể sử dụng hầu hết các chuỗi LangChain hoặc tiện ích trong các flow của Genkit. Ví dụ dưới đây sử dụng trình truy xuất LangChain, trình tải tài liệu và cấu trúc chuỗi để tạo một mẫu RAG đơn thuần.

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

Xin lưu ý rằng ví dụ này sử dụng GenkitTracer do trình bổ trợ genkitx-langchain cung cấp để đo lường chuỗi LangChain bằng các tính năng quan sát của Genkit. Giờ đây, khi chạy flow từ giao diện người dùng dành cho nhà phát triển hoặc trong phiên bản chính thức, bạn sẽ có thể xem toàn bộ các chuỗi LangChain.

Ngoài ra, xin lưu ý rằng các thành phần LangChain không có khả năng tương tác với các dữ liệu gốc Genkit (mô hình, tài liệu, trình truy xuất, v.v.).

Người đánh giá (Xem trước)

Bạn có thể sử dụng trình đánh giá LangChain với Genkit. Hãy định cấu hình những trình đánh giá mà bạn muốn trong trình bổ trợ langchain, sau đó làm theo quy trình đánh giá tiêu chuẩn:

import { langchain } from 'genkitx-langchain';

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