Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Feat ChatGPT LLMApi [Text Moderation] [Azure] #121

Merged
merged 3 commits into from
Nov 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions app/client/platforms/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ export class ChatGPTApi implements LLMApi {
async chat(options: ChatOptions) {
const textmoderation = useAppConfig.getState().textmoderation;
const latest = OpenaiPath.TextModerationModels.latest;
const accessStore = useAccessStore.getState();
const checkprovider = this.getServiceProvider();
if (textmoderation
/**
* This A Text Moderation OpenAI, default is enabled
* you can disabled it in settings
**/
&& DEFAULT_MODELS
&& options.whitelist !== true
&& accessStore.provider !== ServiceProvider.Azure) { // Skip text moderation for Azure provider since azure already have text-moderation, and its enabled by default on their service
&& checkprovider !== ServiceProvider.Azure) { // Skip text moderation for Azure provider since azure already have text-moderation, and its enabled by default on their service
const messages = options.messages.map((v) => ({
role: v.role,
content: v.content,
Expand Down Expand Up @@ -472,11 +472,20 @@ export class ChatGPTApi implements LLMApi {
const text = msg.data;
try {
const json = JSON.parse(text);
const delta = json.choices[0].delta.content;
const delta = json.choices?.[0]?.delta?.content;
const textmoderation = json.prompt_filter_results;

if (delta) {
responseText += delta;
options.onUpdate?.(responseText, delta);
}

if (textmoderation
&& textmoderation.length > 0
&& provider === ServiceProvider.Azure) {
const contentFilterResults = textmoderation[0].content_filter_results;
console.log(`[${provider}] [Text Moderation] flagged categories result:`, contentFilterResults);
}
} catch (e) {
console.error("[Request] parse error", text, msg);
}
Expand Down