homepage overhault, aiService 1. try

This commit is contained in:
2024-07-26 19:18:28 +02:00
parent 38e943c18e
commit a6ae643458
15 changed files with 333 additions and 52 deletions

View File

@@ -38,6 +38,7 @@
"dotenv": "^16.4.5",
"drizzle-orm": "^0.32.0",
"fs-extra": "^11.2.0",
"groq-sdk": "^0.5.0",
"handlebars": "^4.7.8",
"jwks-rsa": "^3.1.0",
"ky": "^1.4.0",

View File

@@ -0,0 +1,12 @@
import { Body, Controller, Post } from '@nestjs/common';
import { AiService } from './ai.service.js';
@Controller('ai')
export class AiController {
constructor(private readonly aiService: AiService) {}
@Post()
async getBusinessCriteria(@Body('query') query: string) {
return this.aiService.getBusinessCriteria(query);
}
}

View File

@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { AiController } from './ai.controller.js';
import { AiService } from './ai.service.js';
@Module({
controllers: [AiController],
providers: [AiService],
})
export class AiModule {}

View File

@@ -0,0 +1,94 @@
import { Injectable } from '@nestjs/common';
import Groq from 'groq-sdk';
import OpenAI from 'openai';
import { BusinessListingCriteria } from '../models/main.model';
const businessListingCriteriaStructure = {
criteriaType: 'business | commercialProperty | broker',
types: "'Automotive'|'Industrial Services'|'Food and Restaurant'|'Real Estate'|'Retail'|'Oilfield SVE and MFG.'|'Service'|'Advertising'|'Agriculture'|'Franchise'|'Professional'|'Manufacturing'",
city: 'string',
state: 'string',
county: 'string',
minPrice: 'number',
maxPrice: 'number',
minRevenue: 'number',
maxRevenue: 'number',
minCashFlow: 'number',
maxCashFlow: 'number',
minNumberEmployees: 'number',
maxNumberEmployees: 'number',
establishedSince: 'number',
establishedUntil: 'number',
realEstateChecked: 'boolean',
leasedLocation: 'boolean',
franchiseResale: 'boolean',
title: 'string',
brokerName: 'string',
searchType: "'exact' | 'radius'",
radius: "'0' | '5' | '20' | '50' | '100' | '200' | '300' | '400' | '500'",
};
@Injectable()
export class AiService {
private readonly openai: OpenAI;
private readonly groq: Groq;
constructor() {
this.openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY, // Verwenden Sie Umgebungsvariablen für den API-Schlüssel
});
this.groq = new Groq({ apiKey: process.env.GROQ_API_KEY });
}
async getBusinessCriteria(query: string): Promise<BusinessListingCriteria> {
// const prompt = `
// Dieses Objekt ist wie folgt definiert: ${JSON.stringify(businessListingCriteriaStructure)}.
// Die Antwort darf nur das von dir befüllte JSON als unformatierten Text enthalten so das es von mir mit JSON.parse() einlesbar ist!!!!
// Falls es Ortsangaben gibt, dann befülle City, County und State wenn möglich Die Suchanfrage des Users lautet: "${query}"`;
const prompt = `The Search Query of the User is: "${query}"`;
let response = null;
try {
// response = await this.openai.chat.completions.create({
// model: 'gpt-4o-mini',
// //model: 'gpt-3.5-turbo',
// max_tokens: 300,
// messages: [
// {
// role: 'system',
// content: `Please create unformatted JSON Object from a user input.
// The type is: ${JSON.stringify(businessListingCriteriaStructure)}.,
// If location details available please fill city, county and state as State Code`,
// },
// ],
// temperature: 0.5,
// response_format: { type: 'json_object' },
// });
response = await this.groq.chat.completions.create({
messages: [
{
role: 'system',
content: `Please create unformatted JSON Object from a user input.
The type must be: ${JSON.stringify(businessListingCriteriaStructure)}.
If location details available please fill city, county and state as State Code`,
},
{
role: 'user',
content: prompt,
},
],
model: 'llama-3.1-70b-versatile',
//model: 'llama-3.1-8b-instant',
temperature: 0.2,
max_tokens: 300,
response_format: { type: 'json_object' },
});
const generatedCriteria = JSON.parse(response.choices[0]?.message?.content);
return generatedCriteria;
// return response.choices[0]?.message?.content;
} catch (error) {
console.error(`Error calling GPT-4 API: ${response.choices[0]}`, error);
throw new Error('Failed to generate business criteria');
}
}
}

View File

@@ -5,6 +5,7 @@ import * as dotenv from 'dotenv';
import fs from 'fs-extra';
import { WinstonModule, utilities as nestWinstonModuleUtilities } from 'nest-winston';
import * as winston from 'winston';
import { AiModule } from './ai/ai.module.js';
import { AppController } from './app.controller.js';
import { AppService } from './app.service.js';
import { AuthModule } from './auth/auth.module.js';
@@ -73,6 +74,7 @@ loadEnvFiles();
SelectOptionsModule,
ImageModule,
PassportModule,
AiModule,
],
controllers: [AppController],
providers: [AppService, FileService],

View File

@@ -62,6 +62,7 @@ export interface ListCriteria {
city: string;
prompt: string;
searchType: 'exact' | 'radius';
// radius: '5' | '20' | '50' | '100' | '200' | '300' | '400' | '500';
radius: number;
criteriaType: 'business' | 'commercialProperty' | 'broker';
}

View File

@@ -14,6 +14,7 @@ export class SelectOptionsController {
locations: this.selectOptionsService.locations,
typesOfCommercialProperty: this.selectOptionsService.typesOfCommercialProperty,
customerSubTypes: this.selectOptionsService.customerSubTypes,
distances: this.selectOptionsService.distances,
};
}
}

View File

@@ -35,6 +35,17 @@ export class SelectOptionsService {
{ name: '$1M', value: '1000000' },
{ name: '$5M', value: '5000000' },
];
public distances: Array<KeyValue> = [
{ name: '5 miles', value: '5' },
{ name: '20 miles', value: '20' },
{ name: '50 miles', value: '50' },
{ name: '100 miles', value: '100' },
{ name: '200 miles', value: '200' },
{ name: '300 miles', value: '300' },
{ name: '400 miles', value: '400' },
{ name: '500 miles', value: '500' },
];
public listingCategories: Array<KeyValue> = [
{ name: 'Business', value: 'business' },
{ name: 'Commercial Property', value: 'commercialProperty' },