Final commit
This commit is contained in:
parent
91fffb3294
commit
c35e0716af
372 changed files with 16591 additions and 1 deletions
65
backend/ai-suggestion.ts
Normal file
65
backend/ai-suggestion.ts
Normal file
|
@ -0,0 +1,65 @@
|
|||
import { GoogleGenAI } from "npm:@google/genai@1.12.0";
|
||||
import { assert } from "https://deno.land/std@0.224.0/assert/mod.ts";
|
||||
import schema from "./schemas/AiSnewuggestionOutput.json" with { type: "json" };
|
||||
import { GetNewSuggestInput, GetNewSuggestOutput } from "./types/api.ts";
|
||||
import { AiSnewuggestionOutput } from "./types/ai.ts";
|
||||
|
||||
const ai = new GoogleGenAI({});
|
||||
|
||||
// Generate songs using AI
|
||||
export async function generateVibes(
|
||||
input: GetNewSuggestInput,
|
||||
): Promise<AiSnewuggestionOutput> {
|
||||
const { rules } = input;
|
||||
let aiPrompt = "";
|
||||
let contextText = "";
|
||||
|
||||
if (input.type === "scratch-suggestion") {
|
||||
contextText =
|
||||
`The music request is for a place: "${input.room_name}". Your job is to find 15 songs that match the vibe of this place.`;
|
||||
} else if (input.type === "from-songs-suggestion") {
|
||||
const songList = input.songs.map((s, i) => `${i + 1}. ${s.song}`).join(
|
||||
"\n",
|
||||
);
|
||||
contextText =
|
||||
`The music request is based on the last 10 songs:\n${songList}\nYour job is to 15 find songs that match the vibe of these songs.`;
|
||||
}
|
||||
|
||||
const rulesText = rules && rules.trim().length > 0
|
||||
? `Make sure that they follow the following set of rules: "${rules}".`
|
||||
: "";
|
||||
|
||||
aiPrompt = `
|
||||
${contextText}
|
||||
Make sure they are real, popular songs that exist on Spotify.
|
||||
${rulesText}
|
||||
`;
|
||||
|
||||
const response = await ai.models.generateContent({
|
||||
model: "gemini-2.5-flash",
|
||||
contents: aiPrompt,
|
||||
config: { responseMimeType: "application/json", responseSchema: schema },
|
||||
});
|
||||
|
||||
const text = response.candidates?.at(0)?.content?.parts?.at(0)?.text;
|
||||
if (!text) {
|
||||
throw Error("Text not found");
|
||||
}
|
||||
|
||||
return JSON.parse(text) as AiSnewuggestionOutput;
|
||||
}
|
||||
|
||||
Deno.test("generateVibes with array of songs", async () => {
|
||||
const input: GetNewSuggestInput = {
|
||||
type: "from-songs-suggestion" as const,
|
||||
rules: "The songs must be great for food",
|
||||
songs: [
|
||||
{ song: "Something - The Beatles" },
|
||||
{ song: "Come Together - The Beatles" },
|
||||
{ song: "Help! - The Beatles" },
|
||||
],
|
||||
};
|
||||
const result = await generateVibes(input);
|
||||
|
||||
console.log(result);
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue