WIP: add lifestyle block
This commit is contained in:
41
app/home/(user)/_lib/server/load-life-style.ts
Normal file
41
app/home/(user)/_lib/server/load-life-style.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { cache } from 'react';
|
||||
|
||||
import { AccountWithParams } from '@/packages/features/accounts/src/types/accounts';
|
||||
import OpenAI from 'openai';
|
||||
|
||||
import PersonalCode from '~/lib/utils';
|
||||
|
||||
const failedResponse = {
|
||||
lifeStyle: null,
|
||||
summary: null,
|
||||
};
|
||||
|
||||
async function lifeStyleLoader(account: AccountWithParams) {
|
||||
if (!account?.personal_code) {
|
||||
return failedResponse;
|
||||
}
|
||||
|
||||
const openAIClient = new OpenAI();
|
||||
const { gender, age } = PersonalCode.parsePersonalCode(account.personal_code);
|
||||
try {
|
||||
const response = await openAIClient.responses.create({
|
||||
store: false,
|
||||
prompt: {
|
||||
id: analysesRecommendationsPromptId,
|
||||
variables: {
|
||||
analyses: JSON.stringify(formattedAnalyses),
|
||||
results: JSON.stringify(formattedAnalysisResponses),
|
||||
gender: gender.value,
|
||||
age: age.toString(),
|
||||
weight: weight.toString(),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error('Error calling OpenAI: ', error);
|
||||
return failedResponse;
|
||||
}
|
||||
}
|
||||
export const loadLifeStyle = cache(lifeStyleLoader);
|
||||
Reference in New Issue
Block a user