Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Make Google Language Manager optional #10535

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ REDIS_URL='redis://localhost:6379'
# GOOGLE_CLOUD_CLIENT_EMAIL='key_GOOGLE_CLOUD_CLIENT_EMAIL'
# GOOGLE_CLOUD_PRIVATE_KEY='key_GOOGLE_CLOUD_PRIVATE_KEY'
# GOOGLE_CLOUD_PRIVATE_KEY_ID='key_GOOGLE_CLOUD_PRIVATE_KEY_ID'
# Disable Google Cloud Natural Language API for generating retro group titles
# DISABLE_GOOGLE_CLOUD_NATURAL_LANGUAGE='true'

# INTEGRATIONS
# ATLASSIAN_CLIENT_ID='key_ATLASSIAN_CLIENT_ID'
Expand Down
12 changes: 9 additions & 3 deletions packages/server/getGoogleLanguageManager.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import GoogleLanguageManager from './GoogleLanguageManager'
import {Logger} from './utils/Logger'

const clientEmail = process.env.GOOGLE_CLOUD_CLIENT_EMAIL
const privateKey = (process.env.GOOGLE_CLOUD_PRIVATE_KEY || '').replace(/\\n/gm, '\n')
const privateKeyId = process.env.GOOGLE_CLOUD_PRIVATE_KEY_ID
const disabled = !!process.env.DISABLE_GOOGLE_CLOUD_NATURAL_LANGUAGE

if (!disabled && (!clientEmail || !privateKeyId || !privateKey)) {
Logger.warn('Google Cloud Natural Language API is disabled because of missing credentials')
}

let languageManager: GoogleLanguageManager
const getGoogleLanguageManager = () => {
if (disabled || !clientEmail || !privateKeyId || !privateKey) {
return null
}
if (!languageManager) {
if (!clientEmail || !privateKeyId || !privateKey) {
throw new Error('Missing Google Cloud Credentials in ENV')
}
languageManager = new GoogleLanguageManager({clientEmail, privateKey, privateKeyId})
}
return languageManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import manageGoogleNLPErrorResponse from './manageGoogleNLPErrorResponse'
const getReflectionEntities = async (plaintextContent: string) => {
if (!plaintextContent) return []
const manager = getGoogleLanguageManager()
if (!manager) return []
const res = await Promise.all([
manager.analyzeEntities(plaintextContent),
manager.analyzeSyntax(plaintextContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import manageGoogleNLPErrorResponse from './manageGoogleNLPErrorResponse'
const getReflectionSentimentScore = async (question: string, response: string) => {
if (!response) return undefined
const manager = getGoogleLanguageManager()
if (!manager) return undefined
const document = `${question}: ${response}`
const res = await manager.analyzeSentiment(document)
const reflectionSentiment = manageGoogleNLPErrorResponse(res)
Expand Down