Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions backend/src/songs/songs.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { GenresModel } from '../../models/genres/genres.model';
import { SongDetailsModel } from '../../models/song_details/SongDetails.model';
import { SongArtistsModel } from '../../models/song_artists/songArtists.model';
import { SongGenresModel } from '../../models/song_genres/SongGenres.model';
import { PubSub } from 'graphql-subscriptions';
import { GoogleGenAI } from '@google/genai';

@Module({
Expand All @@ -24,10 +23,7 @@ import { GoogleGenAI } from '@google/genai';
SongGenresModel,
]),
],
providers: [SongsResolver, SongsService, {
provide: 'PUB_SUB',
useValue: new PubSub(),
},
providers: [SongsResolver, SongsService,
{
provide: 'AI',
useValue: new GoogleGenAI({
Expand Down
9 changes: 0 additions & 9 deletions backend/src/songs/songs.resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { InternalServerErrorException } from '@nestjs/common';
import { ConnectionResError, TimeoutResError } from 'src/utils/mockErrors';
import { USER_VECTOR } from '../../test/constants/constants';
import { expectSongProps } from 'src/utils/expectSongs';
import { PubSub } from 'graphql-subscriptions';
import { GoogleGenAI } from '@google/genai';

describe('SongsResolver receives the expected songs array from the service', () => {
Expand All @@ -23,10 +22,6 @@ describe('SongsResolver receives the expected songs array from the service', ()
const module: TestingModule = await Test.createTestingModule({
providers: [
SongsResolver,
{
provide: 'PUB_SUB',
useValue: new PubSub(),
},
{
provide: 'AI',
useValue: new GoogleGenAI({
Expand Down Expand Up @@ -121,10 +116,6 @@ describe('SongsResolver is able to communicate the error from the service to hel
const module: TestingModule = await Test.createTestingModule({
providers: [
SongsResolver,
{
provide: 'PUB_SUB',
useValue: new PubSub(),
},
{
provide: 'AI',
useValue: new GoogleGenAI({
Expand Down
33 changes: 4 additions & 29 deletions backend/src/songs/songs.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { Args, Float, Int, Mutation, Query, Resolver, Subscription } from '@nestjs/graphql';
import { Args, Float, Int, Query, Resolver, Subscription } from '@nestjs/graphql';
import { SongsService } from './songs.service';
import { USER_VECTOR } from '../../test/constants/constants';
import { FullSongResponseDto } from './dto/FullSongResponse.dto';
import { SongResponseDto } from './dto/SongResponse.dto';
import { Inject } from '@nestjs/common';
import { PubSub } from 'graphql-subscriptions';

@Resolver()
export class SongsResolver {
constructor(private readonly songsService: SongsService,
@Inject('PUB_SUB') private readonly pubSub: PubSub
) {}
constructor(private readonly songsService: SongsService, ) {}

@Query(() => Int, { name: 'getDBLength' })
async getDBLength(): Promise<number> {
Expand All @@ -31,8 +27,8 @@ export class SongsResolver {
return this.songsService.getSongData(songID);
}

@Subscription(() => String, { resolve: (payload: { aiResponse: string | undefined }) => payload.aiResponse, })
async *responseSub(
@Subscription(() => String)
async *aiResponse(
@Args('genres', { type: () => [String], defaultValue: ["Rock"] } ) genres: string[],
@Args('userVector', { type: () => [Float], defaultValue: USER_VECTOR }) userVector: number[],
) : AsyncGenerator<{ aiResponse: string | undefined }> {
Expand All @@ -42,27 +38,6 @@ export class SongsResolver {
}
}

@Subscription(() => String)
aiResponse() {
return this.pubSub.asyncIterableIterator('AI_RESPONSE')
}

@Mutation(() => Boolean)
async streamAIResponse(
@Args('genres', { type: () => [String], defaultValue: ['Rock'] }) genres: string[],
@Args('userVector', { type: () => [Float], defaultValue: USER_VECTOR }) userVector: number[],
) {
const streamResponse = await this.songsService.getAIStream(genres, userVector);
console.log(`Got the stream response:`)
console.log(streamResponse)
for await (const chunk of streamResponse) {
console.log(`loading chunk: ${chunk.text}`)
await this.pubSub.publish('AI_RESPONSE', { aiResponse: chunk.text });
console.log("response published!")
}
return true;
}

@Query(() => [SongResponseDto], { name: 'getSongRecommendations' })
async getSongRecommendations(
@Args('genres', { type: () => [String], defaultValue: ['Rock'] })
Expand Down
4 changes: 0 additions & 4 deletions backend/src/songs/songs.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ describe('SongsService retrieves, evaluates and parses songs data from the datab
const module: TestingModule = await Test.createTestingModule({
providers: [
SongsService,
{
provide: 'PUB_SUB',
useValue: new PubSub(),
},
{
provide: 'AI',
useValue: new GoogleGenAI({
Expand Down
9 changes: 4 additions & 5 deletions frontend/src/components/Utils/hooks/useSongRec.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useState } from "react";
import { useAppSelector } from "../redux-hooks";
import { responseSub } from "@/queries/LaraRecQuerie";
import { useSubscription } from "@apollo/client";
import generateUserVector from "../generateUserVector";
import { aiSubscription } from "@/queries/LaraRecQuerie";

const useSongRec = () => {
const [visibleCount, setVisibleCount] = useState<number>(20);
Expand All @@ -11,12 +11,11 @@ const useSongRec = () => {
mood, acousticness } = useAppSelector(state => state.songData);
const userVector = generateUserVector(tempo, danceability, energy, mood, speechLevel,
acousticness, voiceType, sentiment);

useSubscription(responseSub, {
useSubscription(aiSubscription, {
variables: { genres, userVector },
onData({ data }) {
console.log(data)
const chunkText = data.data.responseSub;
const chunkText = data.data.aiResponse;
setAIResponse(prev => prev + chunkText);
}
});
Expand Down
14 changes: 1 addition & 13 deletions frontend/src/queries/LaraRecQuerie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,7 @@ query getRecommendedSongs ($genres: [String!], $userVector: [Float!], $limit: In
`;

export const aiSubscription = gql`
subscription {
aiResponse
}
`;

export const responseSub = gql`
subscription GetAIResponse($genres: [String!], $userVector: [Float!]) {
responseSub(genres: $genres, userVector: $userVector)
}
`;

export const streamAIAnswer = gql`
mutation streamResponse($genres: [String!], $userVector: [Float!]) {
streamAIResponse(genres: $genres, userVector: $userVector)
aiResponse(genres: $genres, userVector: $userVector)
}
`;
Loading