Skip to content

Commit 2b848d2

Browse files
committed
init openai
1 parent c83c288 commit 2b848d2

File tree

9 files changed

+124
-1
lines changed

9 files changed

+124
-1
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@
8181
"mime-types": "^2.1.25",
8282
"mongodb": "^3.7.3",
8383
"morgan": "^1.10.1",
84+
"node-fetch": "^3.3.2",
85+
"openai": "^6.8.1",
8486
"prom-client": "^15.1.3",
8587
"safe-regex": "^2.1.0",
8688
"ts-node-dev": "^2.0.0",
8789
"uuid": "^8.3.2"
8890
}
89-
}
91+
}

src/models/eventsFactory.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const mongo = require('../mongo');
77
const Event = require('../models/event');
88
const { ObjectID } = require('mongodb');
99
const { composeEventPayloadByRepetition } = require('../utils/merge');
10+
const { openAIApi } = require('../openai');
1011

1112
const MAX_DB_READ_BATCH_SIZE = Number(process.env.MAX_DB_READ_BATCH_SIZE);
1213

@@ -178,6 +179,27 @@ class EventsFactory extends Factory {
178179
return new Event(searchResult);
179180
}
180181

182+
/**
183+
* Ask AI about solving repetition
184+
*
185+
* @param {string} repetitionId - repetition ID
186+
* @param {string} eventId - event ID
187+
*
188+
* @return {string} - AI answer
189+
*/
190+
async askAi(repetitionId, eventId) {
191+
const repetition = await this.getEventRepetition(repetitionId, eventId);
192+
193+
if (!repetition) {
194+
throw new Error('Repetition not found');
195+
}
196+
197+
const payload = repetition.event.payload;
198+
const solution = await openAIApi.solveEvent(payload);
199+
200+
return solution;
201+
}
202+
181203
/**
182204
* Returns events that grouped by day
183205
*

src/openai/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { EventAddons, EventData } from "@hawk.so/types";
2+
import OpenAI from "openai";
3+
import { eventSolvingInput } from "./inputs/eventSolving";
4+
import { ctoInstruction } from "./instructions/cto";
5+
6+
class OpenAIApi {
7+
private readonly client: OpenAI;
8+
9+
constructor() {
10+
this.client = new OpenAI({
11+
apiKey: process.env.OPENAI_API_KEY,
12+
// fetch,
13+
});
14+
}
15+
16+
async solveEvent(payload: EventData<EventAddons>) {
17+
const response = await this.client.responses.create({
18+
model: "gpt-4o",
19+
instructions: ctoInstruction,
20+
input: eventSolvingInput(payload),
21+
});
22+
23+
return response.output;
24+
}
25+
}
26+
27+
export const openAIApi = new OpenAIApi();

src/openai/inputs/eventSolving.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { EventData, EventAddons } from "@hawk.so/types"
2+
3+
export const eventSolvingInput = (payload: EventData<EventAddons>) => `
4+
5+
Проанализируй ошибку и предложи решение
6+
7+
Payload: ${JSON.stringify(payload)}
8+
9+
Response:
10+
11+
{
12+
"solution": "...",
13+
"explanation": "..."
14+
}
15+
`

src/openai/instructions/cto.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const ctoInstruction = "Ты технический директор ИТ компании, тебе нужно пояснить ошибку и предложить решение"

src/openai/types.ts

Whitespace-only changes.

src/resolvers/event.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ module.exports = {
8989
return factory.findChartData(days, timezoneOffset, groupHash);
9090
},
9191

92+
async askAi({ projectId, id: eventId, originalEventId }, _args, context) {
93+
const factory = getEventsFactory(context, projectId);
94+
const aiAnswer = await factory.askAi(eventId, originalEventId);
95+
96+
return aiAnswer;
97+
},
98+
9299
/**
93100
* Return release data for the event
94101
*

src/typeDefs/event.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ type Event {
262262
"""
263263
repetitionsPortion(cursor: String = null, limit: Int = 10): RepetitionsPortion!
264264
265+
"""
266+
AI solution for the event
267+
"""
268+
askAi: String!
269+
265270
"""
266271
Array of users who visited event
267272
"""

yarn.lock

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2343,6 +2343,11 @@ cssstyle@^2.3.0:
23432343
dependencies:
23442344
cssom "~0.3.6"
23452345

2346+
data-uri-to-buffer@^4.0.0:
2347+
version "4.0.1"
2348+
resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz#d8feb2b2881e6a4f58c2e08acfd0e2834e26222e"
2349+
integrity sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==
2350+
23462351
data-urls@^2.0.0:
23472352
version "2.0.0"
23482353
resolved "https://registry.yarnpkg.com/data-urls/-/data-urls-2.0.0.tgz#156485a72963a970f5d5821aaf642bef2bf2db9b"
@@ -3095,6 +3100,14 @@ fd-slicer@~1.1.0:
30953100
dependencies:
30963101
pend "~1.2.0"
30973102

3103+
fetch-blob@^3.1.2, fetch-blob@^3.1.4:
3104+
version "3.2.0"
3105+
resolved "https://registry.yarnpkg.com/fetch-blob/-/fetch-blob-3.2.0.tgz#f09b8d4bbd45adc6f0c20b7e787e793e309dcce9"
3106+
integrity sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==
3107+
dependencies:
3108+
node-domexception "^1.0.0"
3109+
web-streams-polyfill "^3.0.3"
3110+
30983111
figures@^3.0.0:
30993112
version "3.2.0"
31003113
resolved "https://registry.yarnpkg.com/figures/-/figures-3.2.0.tgz#625c18bd293c604dc4a8ddb2febf0c88341746af"
@@ -3227,6 +3240,13 @@ form-data@^4.0.0:
32273240
combined-stream "^1.0.8"
32283241
mime-types "^2.1.12"
32293242

3243+
formdata-polyfill@^4.0.10:
3244+
version "4.0.10"
3245+
resolved "https://registry.yarnpkg.com/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz#24807c31c9d402e002ab3d8c720144ceb8848423"
3246+
integrity sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==
3247+
dependencies:
3248+
fetch-blob "^3.1.2"
3249+
32303250
forwarded@0.2.0:
32313251
version "0.2.0"
32323252
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.2.0.tgz#2269936428aad4c15c7ebe9779a84bf0b2a81811"
@@ -5091,6 +5111,11 @@ node-addon-api@^5.0.0:
50915111
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-5.0.0.tgz#7d7e6f9ef89043befdb20c1989c905ebde18c501"
50925112
integrity sha512-CvkDw2OEnme7ybCykJpVcKH+uAOLV2qLqiyla128dN9TkEWfrYmxG6C2boDe5KcNQqZF3orkqzGgOMvZ/JNekA==
50935113

5114+
node-domexception@^1.0.0:
5115+
version "1.0.0"
5116+
resolved "https://registry.yarnpkg.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5"
5117+
integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==
5118+
50945119
node-fetch@^2.6.0:
50955120
version "2.7.0"
50965121
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
@@ -5105,6 +5130,15 @@ node-fetch@^2.6.7:
51055130
dependencies:
51065131
whatwg-url "^5.0.0"
51075132

5133+
node-fetch@^3.3.2:
5134+
version "3.3.2"
5135+
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-3.3.2.tgz#d1e889bacdf733b4ff3b2b243eb7a12866a0b78b"
5136+
integrity sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==
5137+
dependencies:
5138+
data-uri-to-buffer "^4.0.0"
5139+
fetch-blob "^3.1.4"
5140+
formdata-polyfill "^4.0.10"
5141+
51085142
node-int64@^0.4.0:
51095143
version "0.4.0"
51105144
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
@@ -5313,6 +5347,11 @@ onetime@^5.1.0:
53135347
dependencies:
53145348
mimic-fn "^2.1.0"
53155349

5350+
openai@^6.8.1:
5351+
version "6.8.1"
5352+
resolved "https://registry.yarnpkg.com/openai/-/openai-6.8.1.tgz#72610890aa6f67b3473c7be2e2d6ff25ebb9846a"
5353+
integrity sha512-ACifslrVgf+maMz9vqwMP4+v9qvx5Yzssydizks8n+YUJ6YwUoxj51sKRQ8HYMfR6wgKLSIlaI108ZwCk+8yig==
5354+
53165355
optional-require@^1.1.8:
53175356
version "1.1.8"
53185357
resolved "https://registry.yarnpkg.com/optional-require/-/optional-require-1.1.8.tgz#16364d76261b75d964c482b2406cb824d8ec44b7"
@@ -6927,6 +6966,11 @@ walker@^1.0.7, walker@~1.0.5:
69276966
dependencies:
69286967
makeerror "1.0.12"
69296968

6969+
web-streams-polyfill@^3.0.3:
6970+
version "3.3.3"
6971+
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz#2073b91a2fdb1fbfbd401e7de0ac9f8214cecb4b"
6972+
integrity sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==
6973+
69306974
webidl-conversions@^3.0.0:
69316975
version "3.0.1"
69326976
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"

0 commit comments

Comments
 (0)