使用rerank模型

2025-03-20 13:59:03 阅读:8 编辑

D:\edison\langchainjs\node_modules\cohere-ai\environments.js

exports.CohereEnvironment = {
    Production: "http://123.249.34.10:8123",
};

D:\edison\langchainjs\node_modules\cohere-ai\api\resources\v2\client\Client.js(390行)

//v2/rerank  -->改成v1/rerank

也可以修改源代码peanut-shell(/main.py)

app.include_router(router=router, prefix="/v1")

https://www.youtube.com/watch?v=OjezGs6vKPQ

https://github.com/sugarforever/peanut-shell

https://js.langchain.com/docs/integrations/document_compressors/cohere_rerank

https://docs.cohere.com/docs/cohere-works-everywhere

docker-compose.yaml

version: '3.1'
services:
  peanutshell:
    image: ghcr.io/sugarforever/peanut-shell:latest
    pull_policy: always
    environment:
      - HF_ENDPOINT=${HF_ENDPOINT:-https://hf-mirror.com}
    ports:
      - "8123:8000"
    volumes:
      - hf_data:/root/.cache

volumes:
  hf_data:

demo

import { CohereRerank } from "@langchain/cohere";
import { Document } from "@langchain/core/documents";

const query = "What is the capital of the United States?";
const docs = [
    new Document({
        pageContent:
            "Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a population of 55,274.",
    }),
    new Document({
        pageContent:
            "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan.",
    }),
    new Document({
        pageContent:
            "Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.",
    }),
    new Document({
        pageContent:
            "Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district. The President of the USA and many major national government offices are in the territory. This makes it the political center of the United States of America.",
    }),
    new Document({
        pageContent:
            "Capital punishment (the death penalty) has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states. The federal government (including the United States military) also uses capital punishment.",
    }),
];

const cohereRerank = new CohereRerank({
    apiKey: "123456", // Default
   // model: "rerank-english-v3.0",
    model: "ms-marco-MiniLM-L-2-v2",
});

const rerankedDocuments = await cohereRerank.rerank(docs, query, {
    topN: 5,
});

console.log(rerankedDocuments);
/**
 [
 { index: 3, relevanceScore: 0.9871293 },
 { index: 1, relevanceScore: 0.29961726 },
 { index: 4, relevanceScore: 0.27542195 },
 { index: 0, relevanceScore: 0.08977329 },
 { index: 2, relevanceScore: 0.041462272 }
 ]
 */
  1. 可以不使用GPU,使用CPU
  2. 可以使用DOCKER方式部署
  3. 要测试使用CPU的情况,是否占CPU
  4. 使用该rerank平替cohere(langchainjs)