-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
107 additions
and
90 deletions.
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
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
113 changes: 55 additions & 58 deletions
113
examples-standalone/coffee-warehouse/src/app/openai.service.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 |
---|---|---|
@@ -1,78 +1,75 @@ | ||
import { Injectable } from '@angular/core'; | ||
|
||
import { AzureOpenAI } from "openai"; | ||
import type { | ||
ChatCompletionCreateParamsNonStreaming, | ||
ChatCompletionCreateParamsNonStreaming, | ||
} from "openai/resources/index"; | ||
|
||
// You will need to set these environment variables or edit the following values | ||
const apiKey = 'asd'; | ||
const endpoint = 'https://dx-hackathon-2024q3.openai.azure.com/'; | ||
|
||
|
||
// Required Azure OpenAI deployment name and API version | ||
const apiVersion = "2024-08-01-preview"; | ||
const GPT_4o_MINI = 'gpt-4o-mini-dx-hackathon'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
providedIn: 'root', | ||
}) | ||
export class OpenAIService { | ||
private client: AzureOpenAI; | ||
|
||
constructor() { | ||
this.client = new AzureOpenAI({ | ||
endpoint, | ||
apiKey, | ||
apiVersion, | ||
deployment: GPT_4o_MINI, | ||
dangerouslyAllowBrowser: true | ||
}); | ||
} | ||
|
||
public async getAIResponse(disabilities: string[]): Promise<string> { | ||
const messages = this.createMessages(disabilities); | ||
|
||
const completion = await this.client.chat.completions.create(messages); | ||
const result = completion.choices[0].message.content; | ||
console.log(result); | ||
return result; | ||
} | ||
|
||
private createMessages(selectedDisabilities: string[]): ChatCompletionCreateParamsNonStreaming { | ||
const prompt = ` | ||
Based on the following disabilities: ${selectedDisabilities.join(', ')}, | ||
suggest the appropriate accessibility settings as a single object. | ||
Only include the settings that are applicable to the disabilities. | ||
The possible settings are: | ||
- textSize: Numeric value for text size (e.g. 16) | ||
- colorTheme: Either 'contrast' or 'dark' | ||
- font: Either 'legible' or 'dyslexia' | ||
- underlineLinks: Boolean (true or false) | ||
- pauseAnimations: Boolean (true or false) | ||
- lgSizeWidgets: Boolean (true or false) | ||
- lineHeight: Numeric value for line height (e.g. 1.2) | ||
- letterSpacing: Numeric value for letter spacing (e.g. 1) | ||
private client: AzureOpenAI; | ||
|
||
Respond with only an object that contains the applicable settings. | ||
Do not wrap the object in any syntax, such as \`\`\`json\`. | ||
For example: { textSize: 18, font: 'dyslexia' } | ||
`; | ||
constructor() { | ||
this.client = new AzureOpenAI({ | ||
endpoint, | ||
apiKey, | ||
apiVersion, | ||
deployment: GPT_4o_MINI, | ||
dangerouslyAllowBrowser: true | ||
}); | ||
} | ||
|
||
return { | ||
messages: [ | ||
{ | ||
role: "system", | ||
content: "You are a helpful assistant who recommends accessibility settings based on disabilities provided." | ||
}, | ||
{ | ||
role: "user", | ||
content: prompt, | ||
}, | ||
], | ||
model: "gpt-4", | ||
}; | ||
} | ||
public async getAIRecommendedSettings(disability: string): Promise<string> { | ||
const messages = this.createMessages(disability); | ||
console.log(`Disability: ${disability}`); | ||
|
||
const completion = await this.client.chat.completions.create(messages); | ||
const result = completion.choices[0].message.content; | ||
console.log(`Result: ${result}`); | ||
return result; | ||
} | ||
|
||
private createMessages(selectedDisabilities: string): ChatCompletionCreateParamsNonStreaming { | ||
const prompt = ` | ||
Based on the following disabilities: ${selectedDisabilities}, | ||
suggest the appropriate accessibility settings as a single object. | ||
Only include the settings that are applicable to the disabilities. | ||
The possible settings are: | ||
- fontSize: Numeric value for text size | ||
- colorTheme: Either "contrast" or "dark" | ||
- fontFamily: Either "legible" or "dyslexia" | ||
- underlineLinks: Boolean (true or false) | ||
- pauseAnimations: Boolean (true or false) | ||
- lineHeight: Numerical value for line height - e.g. 1.5 | ||
- letterSpacing: Integer (whole) value for letter spacing - e.g. 1 instead of 0.8 | ||
Respond with a valid JSON string that contains the applicable settings which can then be parsed into an object using JSON.parse. | ||
Do not include any extra syntax like \`\`\`json or any text before or after the object. | ||
Example: {"fontSize": 18, "fontFamily": "dyslexia"} | ||
`; | ||
|
||
return { | ||
messages: [ | ||
{ | ||
role: "system", | ||
content: "You are a helpful assistant who recommends accessibility settings based on disabilities provided." | ||
}, | ||
{ | ||
role: "user", | ||
content: prompt, | ||
}, | ||
], | ||
model: "gpt-4", | ||
}; | ||
} | ||
} |