File size: 7,423 Bytes
1244914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env tsx

/**
 * Manual Query Tester for Semantic Search
 * 
 * This script helps developers test and improve their semantic search queries
 * before committing them to the codebase using Gemini 3 Pro via Vertex AI.
 */

import { vertex } from "@ai-sdk/google-vertex";
import { generateObject } from "ai";
import { z } from "zod";
import * as readline from "readline";

const QueryEvaluationSchema = z.object({
  embedding_query: z.object({
    score: z.number().min(0).max(10),
    strengths: z.array(z.string()),
    weaknesses: z.array(z.string()),
    suggestions: z.array(z.string()),
  }),
  reranking_query: z.object({
    score: z.number().min(0).max(10),
    strengths: z.array(z.string()),
    weaknesses: z.array(z.string()),
    suggestions: z.array(z.string()),
  }),
  differentiation: z.object({
    are_different: z.boolean(),
    explanation: z.string(),
    suggestion: z.string().optional(),
  }),
  overall_recommendation: z.string(),
});

async function evaluateQueries(
  embeddingQuery: string,
  rerankingQuery: string,
): Promise<z.infer<typeof QueryEvaluationSchema>> {
  const prompt = `You are an expert in semantic code search. Evaluate these two queries:

**Embedding Query (for vector search):**
${embeddingQuery}

**Reranking Query (for result filtering):**
${rerankingQuery}

Provide detailed, constructive feedback on:
1. The quality of each query
2. Whether they are sufficiently different
3. How to improve them

Scoring (0-10):
- 9-10: Excellent
- 7-8: Good
- 5-6: Adequate
- 3-4: Poor
- 0-2: Failing

Focus on practical, actionable suggestions.`;

  // @ts-ignore - Type instantiation depth issue with complex Zod schemas
  const result = await generateObject({
    model: vertex("gemini-3-pro-preview"),
    schema: QueryEvaluationSchema,
    prompt,
    temperature: 0.3,
  });
  return result.object as z.infer<typeof QueryEvaluationSchema>;
}

function formatEvaluation(evaluation: z.infer<typeof QueryEvaluationSchema>): string {
  const lines: string[] = [];

  lines.push("\n╔═══════════════════════════════════════════════════════════╗");
  lines.push("β•‘          SEMANTIC SEARCH QUERY EVALUATION                 β•‘");
  lines.push("β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•\n");

  // Embedding Query
  lines.push("πŸ“Š EMBEDDING QUERY ANALYSIS");
  lines.push(`   Score: ${evaluation.embedding_query.score}/10 ${getScoreEmoji(evaluation.embedding_query.score)}\n`);
  
  if (evaluation.embedding_query.strengths.length > 0) {
    lines.push("   βœ… Strengths:");
    for (const strength of evaluation.embedding_query.strengths) {
      lines.push(`      β€’ ${strength}`);
    }
    lines.push("");
  }

  if (evaluation.embedding_query.weaknesses.length > 0) {
    lines.push("   ⚠️  Weaknesses:");
    for (const weakness of evaluation.embedding_query.weaknesses) {
      lines.push(`      β€’ ${weakness}`);
    }
    lines.push("");
  }

  if (evaluation.embedding_query.suggestions.length > 0) {
    lines.push("   πŸ’‘ Suggestions:");
    for (const suggestion of evaluation.embedding_query.suggestions) {
      lines.push(`      β€’ ${suggestion}`);
    }
    lines.push("");
  }

  // Reranking Query
  lines.push("πŸ“Š RERANKING QUERY ANALYSIS");
  lines.push(`   Score: ${evaluation.reranking_query.score}/10 ${getScoreEmoji(evaluation.reranking_query.score)}\n`);

  if (evaluation.reranking_query.strengths.length > 0) {
    lines.push("   βœ… Strengths:");
    for (const strength of evaluation.reranking_query.strengths) {
      lines.push(`      β€’ ${strength}`);
    }
    lines.push("");
  }

  if (evaluation.reranking_query.weaknesses.length > 0) {
    lines.push("   ⚠️  Weaknesses:");
    for (const weakness of evaluation.reranking_query.weaknesses) {
      lines.push(`      β€’ ${weakness}`);
    }
    lines.push("");
  }

  if (evaluation.reranking_query.suggestions.length > 0) {
    lines.push("   πŸ’‘ Suggestions:");
    for (const suggestion of evaluation.reranking_query.suggestions) {
      lines.push(`      β€’ ${suggestion}`);
    }
    lines.push("");
  }

  // Differentiation
  lines.push("πŸ”„ QUERY DIFFERENTIATION");
  lines.push(`   ${evaluation.differentiation.are_different ? "βœ…" : "❌"} Queries are ${evaluation.differentiation.are_different ? "sufficiently different" : "too similar"}`);
  lines.push(`   ${evaluation.differentiation.explanation}\n`);
  
  if (evaluation.differentiation.suggestion) {
    lines.push(`   πŸ’‘ ${evaluation.differentiation.suggestion}\n`);
  }

  // Overall
  lines.push("🎯 OVERALL RECOMMENDATION");
  lines.push(`   ${evaluation.overall_recommendation}\n`);

  return lines.join("\n");
}

function getScoreEmoji(score: number): string {
  if (score >= 9) return "🌟";
  if (score >= 7) return "πŸ‘";
  if (score >= 5) return "πŸ‘Œ";
  if (score >= 3) return "⚠️";
  return "❌";
}

async function promptUser(question: string): Promise<string> {
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
  });

  return new Promise((resolve) => {
    rl.question(question, (answer) => {
      rl.close();
      resolve(answer.trim());
    });
  });
}

async function main() {
  console.log("\n╔═══════════════════════════════════════════════════════════╗");
  console.log("β•‘     SEMANTIC SEARCH QUERY TESTER                          β•‘");
  console.log("β•‘     Test and improve your queries with AI feedback        β•‘");
  console.log("β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•\n");

  // Get queries from user
  const embeddingQuery = await promptUser(
    "Enter your embedding query (for vector search):\n> ",
  );
  
  if (!embeddingQuery) {
    console.error("Embedding query is required");
    process.exit(1);
  }

  console.log("");
  const rerankingQuery = await promptUser(
    "Enter your reranking query (for result filtering):\n> ",
  );

  if (!rerankingQuery) {
    console.error("Reranking query is required");
    process.exit(1);
  }

  console.log("\nπŸ”„ Evaluating your queries...\n");

  try {
    const evaluation = await evaluateQueries(embeddingQuery, rerankingQuery);
    console.log(formatEvaluation(evaluation));
  } catch (error) {
    console.error("Error evaluating queries:", error);
    process.exit(1);
  }
}

// Check if running in interactive mode or with args
const args = process.argv.slice(2);
if (args.length >= 2) {
  // Non-interactive mode
  const embeddingQuery = args[0];
  const rerankingQuery = args[1];
  
  console.log("\nπŸ”„ Evaluating your queries...\n");
  evaluateQueries(embeddingQuery!, rerankingQuery!)
    .then((evaluation) => {
      console.log(formatEvaluation(evaluation));
    })
    .catch((error) => {
      console.error("Error evaluating queries:", error);
      process.exit(1);
    });
} else {
  // Interactive mode
  main().catch((error) => {
    console.error("Unexpected error:", error);
    process.exit(1);
  });
}