-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor PowerPagesChatParticipantConstants and add NL2PAGE and NL2SI…
…TE constants
- Loading branch information
amitjoshi
committed
Oct 25, 2024
1 parent
7246c08
commit de5b198
Showing
3 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/common/chat-participants/powerpages/commands/create-site/Nl2PageService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*/ | ||
|
||
import { ITelemetry } from "../../../../OneDSLoggerTelemetry/telemetry/ITelemetry"; | ||
import { NL2PAGE_REQUEST_FAILED, VSCODE_EXTENSION_NL2PAGE_REQUEST_FAILED, VSCODE_EXTENSION_NL2PAGE_REQUEST_SUCCESS } from "../../PowerPagesChatParticipantConstants"; | ||
|
||
export async function getNL2PageData(aibEndpoint: string, aibToken: string, userPrompt: string, siteName: string, sitePagesList: string[], sessionId: string, telemetry: ITelemetry) { | ||
const constructRequestBody = (pageType: string) => ({ | ||
"crossGeoOptions": { | ||
"enableCrossGeoCall": true | ||
}, | ||
"question": `${userPrompt} - ${pageType} page`, | ||
"context": { | ||
"shouldCheckBlockList": false, | ||
"sessionId": sessionId, | ||
"scenario": "NL2Page", | ||
"subScenario": "GenerateNewPage", | ||
"version": "V1", | ||
"information": { | ||
"scope": "Page", | ||
"includeImages": true, | ||
"pageType": pageType === 'FAQ' ? 'FAQ' : 'Home', | ||
"title": siteName, | ||
"pageName": pageType, | ||
"colorNumber": 7, // Add a function to get a random number | ||
"shuffleImages": false, | ||
"exampleNumber": 2 // Add a function to get a random number | ||
} | ||
} | ||
}); | ||
|
||
const requests = sitePagesList.map(async pageType => { | ||
const requestBody = constructRequestBody(pageType); | ||
|
||
const requestInit: RequestInit = { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"Authorization": `Bearer ${aibToken}` | ||
}, | ||
body: JSON.stringify(requestBody) | ||
}; | ||
|
||
try { | ||
const response = await fetch(aibEndpoint, requestInit); | ||
if (!response.ok) { | ||
throw new Error(`${NL2PAGE_REQUEST_FAILED} ${pageType}`); | ||
} | ||
|
||
const responseData = await response.json(); | ||
|
||
if (responseData && responseData.additionalData[0]) { | ||
return responseData.additionalData[0].snippets[0]; | ||
} | ||
return null; | ||
} catch (error) { | ||
telemetry.sendTelemetryErrorEvent(VSCODE_EXTENSION_NL2PAGE_REQUEST_FAILED, { error: (error as Error)?.message, pageType }); | ||
return null; | ||
} | ||
}); | ||
|
||
const responses = await Promise.all(requests); | ||
|
||
telemetry.sendTelemetryEvent(VSCODE_EXTENSION_NL2PAGE_REQUEST_SUCCESS, { sessionId }); | ||
|
||
// TODO: Home page is mandatory, so if it is not generated, return null | ||
return responses.filter(response => response !== null); | ||
} |
55 changes: 55 additions & 0 deletions
55
src/common/chat-participants/powerpages/commands/create-site/Nl2SiteService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*/ | ||
|
||
import { ITelemetry } from "../../../../OneDSLoggerTelemetry/telemetry/ITelemetry"; | ||
import { NL2SITE_INVALID_RESPONSE, VSCODE_EXTENSION_NL2SITE_REQUEST_FAILED, VSCODE_EXTENSION_NL2SITE_REQUEST_SUCCESS } from "../../PowerPagesChatParticipantConstants"; | ||
|
||
export async function getNL2SiteData(aibEndpoint: string, aibToken: string, userPrompt: string, sessionId: string, telemetry: ITelemetry) { | ||
const requestBody = { | ||
"crossGeoOptions": { | ||
"enableCrossGeoCall": true | ||
}, | ||
"question": userPrompt, | ||
"context": { | ||
"sessionId": sessionId, | ||
"scenario": "NL2Site", | ||
"subScenario": "GenerateNewSite", | ||
// "shouldCheckBlockList": false, //TODO: Check if this is needed | ||
"version": "V1", | ||
"information": { | ||
"minPages": 7, | ||
"maxPages": 7 | ||
} | ||
} | ||
}; | ||
|
||
const requestInit: RequestInit = { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"Authorization": `Bearer ${aibToken}` | ||
}, | ||
body: JSON.stringify(requestBody) | ||
}; | ||
|
||
try { | ||
const response = await fetch(aibEndpoint, requestInit); | ||
if (!response.ok) { | ||
throw new Error(`${response.statusText} - ${response.status}`); | ||
} | ||
|
||
const responseBody = await response.json(); | ||
|
||
if (responseBody && responseBody.additionalData[0]?.website) { | ||
telemetry.sendTelemetryEvent(VSCODE_EXTENSION_NL2SITE_REQUEST_SUCCESS, {sessionId: sessionId}); | ||
return responseBody.additionalData[0].website; // Contains the pages, siteName & site description | ||
} else { | ||
throw new Error(NL2SITE_INVALID_RESPONSE); | ||
} | ||
} catch (error) { | ||
telemetry.sendTelemetryErrorEvent(VSCODE_EXTENSION_NL2SITE_REQUEST_FAILED, { error: (error as Error)?.message }); | ||
return null; | ||
} | ||
} |