|
| 1 | +/** |
| 2 | + * Integration tests for reranking models with real API. |
| 3 | + * |
| 4 | + * Run manually with: pnpm test:integration |
| 5 | + * Requires DASHSCOPE_API_KEY environment variable. |
| 6 | + */ |
| 7 | +import { rerank } from "ai" |
| 8 | +import { describe, expect, it } from "vitest" |
| 9 | +import { createQwen } from "../../qwen-provider" |
| 10 | +import "dotenv/config" |
| 11 | + |
| 12 | +// Skip if no API key |
| 13 | +const SKIP_INTEGRATION = !process.env.DASHSCOPE_API_KEY |
| 14 | + |
| 15 | +const qwen = createQwen({ |
| 16 | + baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1", |
| 17 | +}) |
| 18 | + |
| 19 | +const testDocuments = [ |
| 20 | + "The quick brown fox jumps over the lazy dog.", |
| 21 | + "A lazy cat naps in the sun.", |
| 22 | + "The dog barks loudly at the mailman.", |
| 23 | + "The fox is a cunning animal.", |
| 24 | +] |
| 25 | + |
| 26 | +describe.skipIf(SKIP_INTEGRATION)("reranking Integration Tests", () => { |
| 27 | + describe("gte-rerank-v2 (DashScope native API)", () => { |
| 28 | + it("should rerank documents successfully", async () => { |
| 29 | + const result = await rerank({ |
| 30 | + model: qwen.rerankingModel("gte-rerank-v2"), |
| 31 | + documents: testDocuments, |
| 32 | + query: "animals that are not lazy", |
| 33 | + topN: 2, |
| 34 | + }) |
| 35 | + |
| 36 | + expect(result.ranking).toHaveLength(2) |
| 37 | + expect(result.ranking[0]).toHaveProperty("originalIndex") |
| 38 | + expect(result.ranking[0]).toHaveProperty("score") |
| 39 | + expect(result.rerankedDocuments).toHaveLength(2) |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + describe("qwen3-rerank (OpenAI-compatible API)", () => { |
| 44 | + it("should rerank documents successfully", async () => { |
| 45 | + const result = await rerank({ |
| 46 | + model: qwen.rerankingModel("qwen3-rerank"), |
| 47 | + documents: testDocuments, |
| 48 | + query: "animals that are not lazy", |
| 49 | + topN: 2, |
| 50 | + }) |
| 51 | + |
| 52 | + expect(result.ranking).toHaveLength(2) |
| 53 | + expect(result.ranking[0]).toHaveProperty("originalIndex") |
| 54 | + expect(result.ranking[0]).toHaveProperty("score") |
| 55 | + expect(result.rerankedDocuments).toHaveLength(2) |
| 56 | + }) |
| 57 | + |
| 58 | + it("should support custom instruction", async () => { |
| 59 | + const result = await rerank({ |
| 60 | + model: qwen.rerankingModel("qwen3-rerank", { |
| 61 | + instruct: "Retrieve semantically similar text.", |
| 62 | + }), |
| 63 | + documents: testDocuments, |
| 64 | + query: "animals that are not lazy", |
| 65 | + topN: 2, |
| 66 | + }) |
| 67 | + |
| 68 | + expect(result.ranking).toHaveLength(2) |
| 69 | + expect(result.rerankedDocuments).toHaveLength(2) |
| 70 | + }) |
| 71 | + }) |
| 72 | +}) |
0 commit comments