noahsolomon
noahsolomon
Explore posts from servers
DTDrizzle Team
Created by noahsolomon on 10/1/2024 in #help
pgvector returning less than it should
export const querySimilarTechnologies = async (
inputSkill: string,
topK: number = 10000,
) => {
try {
console.log(
`[1] Starting search for similar technologies to: "${inputSkill}"`,
);

// Step 1: Generate embedding for the input skill
const embedding = await getEmbedding(inputSkill);
console.log(`[2] Embedding generated for: "${inputSkill}"`);

// Step 2: Perform similarity search directly in PostgreSQL
const similarity = sql<number>`1 - (${cosineDistance(skills.vector, embedding)})`;

const similarSkills = await db
.select({
technology: skills.skill,
similarity,
})
.from(skills)
.orderBy(cosineDistance(skills.vector, embedding))
.limit(topK);

console.log(
`[3] Retrieved ${similarSkills.length} similar technologies after similarity search.`,
);

// Optional: Filter based on a threshold if necessary
/*
const threshold = 0.7;
const filteredSimilarities = similarSkills.filter(s => s.similarity >= threshold);
console.log(`[4] Found ${filteredSimilarities.length} similar technologies after filtering.`);
*/

// Return the similar technologies with similarity scores
const result = similarSkills.map((s) => ({
technology: s.technology,
score: parseFloat(s.similarity.toFixed(6)),
}));

console.log(`[5] Returning ${result.length} similar technologies.`);
return result;
} catch (error) {
console.error("Error querying similar technologies:", error);
return [];
}
};
export const querySimilarTechnologies = async (
inputSkill: string,
topK: number = 10000,
) => {
try {
console.log(
`[1] Starting search for similar technologies to: "${inputSkill}"`,
);

// Step 1: Generate embedding for the input skill
const embedding = await getEmbedding(inputSkill);
console.log(`[2] Embedding generated for: "${inputSkill}"`);

// Step 2: Perform similarity search directly in PostgreSQL
const similarity = sql<number>`1 - (${cosineDistance(skills.vector, embedding)})`;

const similarSkills = await db
.select({
technology: skills.skill,
similarity,
})
.from(skills)
.orderBy(cosineDistance(skills.vector, embedding))
.limit(topK);

console.log(
`[3] Retrieved ${similarSkills.length} similar technologies after similarity search.`,
);

// Optional: Filter based on a threshold if necessary
/*
const threshold = 0.7;
const filteredSimilarities = similarSkills.filter(s => s.similarity >= threshold);
console.log(`[4] Found ${filteredSimilarities.length} similar technologies after filtering.`);
*/

// Return the similar technologies with similarity scores
const result = similarSkills.map((s) => ({
technology: s.technology,
score: parseFloat(s.similarity.toFixed(6)),
}));

console.log(`[5] Returning ${result.length} similar technologies.`);
return result;
} catch (error) {
console.error("Error querying similar technologies:", error);
return [];
}
};
3 replies
DTDrizzle Team
Created by noahsolomon on 6/18/2024 in #help
Where clause dependent on with relation
const news = await ctx.db.query.newsTable.findMany({
limit: 20,
offset: 20 * input.page,
orderBy: (newsTable, { desc }) => [desc(newsTable.createdAt)],
with: {
tweets: {
with: { pundit: true },
},
},
});
const news = await ctx.db.query.newsTable.findMany({
limit: 20,
offset: 20 * input.page,
orderBy: (newsTable, { desc }) => [desc(newsTable.createdAt)],
with: {
tweets: {
with: { pundit: true },
},
},
});
So I want to add to this query where tweets.length > 0. But I don't know how to anchor on to this tweets relation
4 replies
TtRPC
Created by noahsolomon on 11/4/2023 in #❓-help
How to refetch a call made on the server
const section = await api.course.getCourseSection.query({
sectionId: parseInt(params.lesson),
});
const section = await api.course.getCourseSection.query({
sectionId: parseInt(params.lesson),
});
I need to refetch this when some button is clicked (or if easier, when some query string param is changed), but this component NEEDS to be a server component unfortunately. How can I revalidate this tRPC call?
3 replies
TtRPC
Created by noahsolomon on 10/17/2023 in #❓-help
tRPC requests on client vs server for NextJs
Hello, I'm new to tRPC, and am confused on one thing. I have watched videos of some using a server client to make their tRPC requests on server side with async/await before sending the data with html back to the user, and the more common approach I've seen is requesting with 'use client'. When is it practical to use these approaches?
Also, I don't quite understand how to properly set up a server client in tRPC with next in regards to providing context.
13 replies