-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb4_integration.js
More file actions
96 lines (90 loc) · 2.74 KB
/
web4_integration.js
File metadata and controls
96 lines (90 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const mockOpenAIAPI = require('./test/mocks/mock_openai_api');
class Web4Integration {
constructor(apiKey) {
this.apiKey = apiKey;
this.baseUrl = 'https://api.openai.com/v1';
this.cache = new Map();
}
async analyzeMarketTrends(prompt) {
if (this.cache.has(prompt)) {
return this.cache.get(prompt);
}
try {
let response;
if (this.apiKey === 'mock') {
response = await mockOpenAIAPI.chatCompletions({
model: 'gpt-4-turbo',
messages: [{
role: 'user',
content: `As a real estate market analyst: ${prompt}`
}],
max_tokens: 500
});
} else {
const axios = require('axios');
response = await axios.post(
`${this.baseUrl}/chat/completions`,
{
model: 'gpt-4-turbo',
messages: [{
role: 'user',
content: `As a real estate market analyst: ${prompt}`
}],
max_tokens: 500
},
{
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
}
}
);
}
const result = response.data.choices[0].message.content;
this.cache.set(prompt, result);
return result;
} catch (error) {
throw new Error(`AI analysis failed: ${error.response?.data?.error?.message || error.message}`);
}
}
async generatePropertyDescription(propertyData) {
const prompt = `Generate compelling real estate listing description for:\n${JSON.stringify(propertyData, null, 2)}`;
return this.analyzeMarketTrends(prompt);
}
async predictMarketValue(propertyData) {
const prompt = `Estimate market value for property with features:\n${JSON.stringify(propertyData, null, 2)}`;
return this.analyzeMarketTrends(prompt);
}
async generateImageFromDescription(description) {
try {
let response;
if (this.apiKey === 'mock') {
response = await mockOpenAIAPI.imageGenerations({
prompt: description,
n: 1,
size: '1024x1024'
});
} else {
const axios = require('axios');
response = await axios.post(
`${this.baseUrl}/images/generations`,
{
prompt: description,
n: 1,
size: '1024x1024'
},
{
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
}
}
);
}
return response.data.data[0].url;
} catch (error) {
throw new Error(`Image generation failed: ${error.response?.data?.error?.message || error.message}`);
}
}
}
module.exports = Web4Integration;