forked from microsoft/azurechat
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Application Insights Provider * Change from CRLF to LF * Change biceps line ending * Refactor ApplicationInsightsProvider and initializeTelemetry * Add session parameter to initializeTelemetry function * Add logger and track metrics for prompt and completion tokens * Add OpenTelemetry instrumentation for metrics tracking * Update model encoding to "gpt-4" * Add OpenTelemetry metrics instrumentation * Remove cleanup step * Add chat metrics tracking and token service
- Loading branch information
Showing
13 changed files
with
2,637 additions
and
28 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
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,20 @@ | ||
'use client' | ||
|
||
import { AppInsightsContext } from '@microsoft/applicationinsights-react-js' | ||
import { createContext } from 'react' | ||
import { initializeTelemetry } from './application-insights-service' | ||
import { useSession } from 'next-auth/react' | ||
|
||
export const ApplicationInsightsContext = createContext({}) | ||
|
||
export default function ApplicationInsightsProvider({ | ||
instrumentationKey, | ||
children, | ||
}: { | ||
instrumentationKey: string, | ||
children: React.ReactNode | ||
}) { | ||
const session = useSession() | ||
const { reactPlugin } = initializeTelemetry(instrumentationKey, session) | ||
return <AppInsightsContext.Provider value={reactPlugin}>{children}</AppInsightsContext.Provider> | ||
} |
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 @@ | ||
"use client" | ||
import {ApplicationInsights, ITelemetryItem} from '@microsoft/applicationinsights-web'; | ||
import {ReactPlugin} from '@microsoft/applicationinsights-react-js'; | ||
import { SessionContextValue } from 'next-auth/react'; | ||
|
||
let logger: ApplicationInsights; | ||
|
||
function initializeTelemetry(instrumentationKey: string, session: SessionContextValue): { reactPlugin: ReactPlugin, appInsights: ApplicationInsights } { | ||
|
||
const defaultBrowserHistory = { | ||
url: "/", | ||
location: { pathname: ""}, | ||
state: { url: "" }, | ||
listen: () => {}, | ||
}; | ||
|
||
let browserHistory = defaultBrowserHistory; | ||
|
||
if (typeof window !== "undefined") { | ||
browserHistory = { ...browserHistory, ...window.history }; | ||
browserHistory.location.pathname = browserHistory?.state?.url; | ||
} | ||
|
||
const reactPlugin = new ReactPlugin(); | ||
const appInsights = new ApplicationInsights({ | ||
config: { | ||
instrumentationKey: instrumentationKey, | ||
extensions: [reactPlugin], | ||
extensionConfig: { | ||
[reactPlugin.identifier]: { history: browserHistory }, | ||
}, | ||
enableAutoRouteTracking: true, | ||
disableAjaxTracking: false, | ||
autoTrackPageVisitTime: true, | ||
enableCorsCorrelation: true, | ||
enableRequestHeaderTracking: true, | ||
enableResponseHeaderTracking: true, | ||
} | ||
}); | ||
|
||
appInsights.loadAppInsights(); | ||
|
||
appInsights.addTelemetryInitializer((env:ITelemetryItem) => { | ||
env.tags = env.tags || []; | ||
env.tags["ai.cloud.role"] = "Bühler ChatGPT"; | ||
env.data = env.data || []; | ||
env.data["email"] = session?.data?.user?.email; | ||
}); | ||
|
||
logger = appInsights; | ||
|
||
return { reactPlugin, appInsights }; | ||
} | ||
|
||
export { initializeTelemetry, logger }; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { metrics } from "@opentelemetry/api"; | ||
import { userHashedId, userSession } from "@/features/auth/helpers"; | ||
|
||
function getChatMeter(){ | ||
const meter = metrics.getMeter("chat"); | ||
return meter; | ||
} | ||
|
||
async function getAttributes(chatModel: string){ | ||
const user = await userSession(); | ||
const userId = await userHashedId(); | ||
const attributes = { "email": user?.email, "name": user?.name, "userHashedId": userId, "chatModel": chatModel || "unknown", "userId": userId }; | ||
return attributes; | ||
} | ||
|
||
export async function reportPromptTokens(tokenCount: number, model: string) { | ||
|
||
const meter = getChatMeter(); | ||
|
||
const promptTokensUsed = meter.createHistogram("promptTokensUsed", { | ||
description: "Number of tokens used in the input prompt", | ||
unit: "tokens", | ||
}); | ||
|
||
promptTokensUsed.record(tokenCount, await getAttributes(model)); | ||
} | ||
|
||
export async function reportCompletionTokens(tokenCount: number, model: string) { | ||
|
||
const meter = getChatMeter(); | ||
|
||
const completionsTokensUsed = meter.createHistogram("completionsTokensUsed", { | ||
description: "Number of tokens used in the completions", | ||
unit: "tokens", | ||
}); | ||
|
||
completionsTokensUsed.record(tokenCount, await getAttributes(model)); | ||
} | ||
|
||
export async function reportUserChatMessage(model: string) { | ||
|
||
const meter = getChatMeter(); | ||
|
||
const userChatMessage = meter.createCounter("userChatMessage", { | ||
description: "Number of messages", | ||
unit: "messages", | ||
}); | ||
|
||
userChatMessage.add(1, await getAttributes(model)); | ||
} |
Oops, something went wrong.