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

Palm api fallback #1369

Merged
merged 4 commits into from
Oct 28, 2023
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
9 changes: 9 additions & 0 deletions packages/config/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export const DEFAULT_USER_TOKEN =
export const STANDALONE = getVarForEnvironment('STANDALONE') === 'true' || false
export const PRODUCTION = getVarForEnvironment('PRODUCTION') === 'true'
export const DEFAULT_OPENAI_KEY = getVarForEnvironment('DEFAULT_OPENAI_KEY')
export const DEFAULT_GOOGLEAI_API_KEY = getVarForEnvironment(
'DEFAULT_GOOGLEAI_API_KEY'
)
export const DONT_CRASH_ON_ERROR =
getVarForEnvironment('DONT_CRASH_ON_ERROR') === 'true'
export const SERVER_PORT = getVarForEnvironment('PORT') || '3030'
Expand Down Expand Up @@ -138,3 +141,9 @@ export const MANAGER_WARM_UP_MSEC =
Number(getVarForEnvironment('MANAGER_WARM_UP_MSEC')) || 5000

export const API_ACCESS_KEY = getVarForEnvironment('API_ACCESS_KEY') || 'apiKey'

export const SPELLRUNNER_BUSY_TIMEOUT_MSEC = getVarForEnvironment(
'SPELLRUNNER_BUSY_TIMEOUT_MS'
)
? Number(getVarForEnvironment('SPELLRUNNER_BUSY_TIMEOUT_MS'))
: 120000
2 changes: 1 addition & 1 deletion packages/core/shared/src/spellManager/SpellManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default class SpellManager {
this.logger.debug(`Reloading spell ${spellId}`)
return this.load(spell)
} catch (error) {
this.logger.error(`Error loading spell ${spellId}`, error)
this.logger.error(`Error loading spell ${spellId}: %o`, error)
return
}
}
Expand Down
9 changes: 6 additions & 3 deletions packages/core/shared/src/spellManager/SpellRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { extractModuleInputKeys } from './graphHelpers'
import SpellManager from './SpellManager'
import { getLogger } from '../logger'
import { NodeData } from 'rete/types/core/data'
import { SPELLRUNNER_BUSY_TIMEOUT_MSEC } from '@magickml/config'

export type RunComponentArgs = {
inputs: MagickSpellInput
Expand Down Expand Up @@ -269,7 +270,7 @@ class SpellRunner {
}
error(message: string, error: unknown | null = null) {
this.busy = false
this.logger.error(message, error)
this.logger.error(message, 'error: %o', error)
if (error) throw error
throw new Error(message)
}
Expand Down Expand Up @@ -359,6 +360,8 @@ class SpellRunner {
this.logger.warn(`No suitable fallback found for ${firstInput}.`)
}

this.logger.info('fallback: %o', fallback)

const [node, name] = fallback
triggeredNode = node

Expand All @@ -375,7 +378,7 @@ class SpellRunner {
// A run shouldnt take this long. This is a hacl but we are replacing all this soon.
setTimeout(() => {
this.busy = false
}, 10000)
parzival418 marked this conversation as resolved.
Show resolved Hide resolved
}, SPELLRUNNER_BUSY_TIMEOUT_MSEC)

// this running is where the main "work" happens.
// I do wonder whether we could make this even more elegant by having the node
Expand All @@ -388,7 +391,7 @@ class SpellRunner {
this.busy = false
return this.outputData
} catch (error) {
this.error(`Error loading spell ${this.currentSpell.id}`, error)
this.error(`Error running spell ${this.currentSpell.id} %o`, error)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CompletionHandlerInputData, saveRequest } from '@magickml/core'
import { GOOGLEAI_ENDPOINT } from '../constants'
import { trackGoogleAIUsage } from '@magickml/server-core'
import { wordCount } from './shared'
import { DEFAULT_GOOGLEAI_API_KEY } from '@magickml/config'

type ChatMessage = {
author?: string
Expand Down Expand Up @@ -55,9 +56,22 @@ export async function makeChatCompletion(
top_k: parseFloat((node?.data?.top_k as string) ?? '40'),
} as any

const apiKey =
(context?.module?.secrets &&
context?.module?.secrets['googleai_api_key']) ||
DEFAULT_GOOGLEAI_API_KEY ||
null

if (!apiKey) {
return {
success: false,
error: 'GoogleAI API key is required to make a chat completion',
}
}

try {
const start = Date.now()
const endpoint = `${GOOGLEAI_ENDPOINT}/${node?.data?.model}:generateMessage?key=${context.module?.secrets?.['googleai_api_key']}`
const endpoint = `${GOOGLEAI_ENDPOINT}/${node?.data?.model}:generateMessage?key=${apiKey}`
// Make the API call to GoogleAI
const completion = await fetch(endpoint, {
method: 'POST',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CompletionHandlerInputData, saveRequest } from '@magickml/core'
import { GOOGLEAI_ENDPOINT } from '../constants'
import { trackGoogleAIUsage } from '@magickml/server-core'
import { wordCount } from './shared'
import { DEFAULT_GOOGLEAI_API_KEY } from '@magickml/config'

/**
* Makes an API request to an AI text completion service.
Expand Down Expand Up @@ -50,8 +51,21 @@ export async function makeTextCompletion(
// Make the API request and handle the response.
const start = Date.now()

const apiKey =
(context?.module?.secrets &&
context?.module?.secrets['googleai_api_key']) ||
DEFAULT_GOOGLEAI_API_KEY ||
null

if (!apiKey) {
return {
success: false,
error: 'GoogleAI API key is required to make a text completion',
}
}

try {
const endpoint = `${GOOGLEAI_ENDPOINT}/${node?.data?.model}:generateText?key=${context.module?.secrets?.['googleai_api_key']}`
const endpoint = `${GOOGLEAI_ENDPOINT}/${node?.data?.model}:generateText?key=${apiKey}`
// Make the API call to GoogleAI
const completion = await fetch(endpoint, {
method: 'POST',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import { GOOGLEAI_ENDPOINT } from '../constants'
import { trackGoogleAIUsage } from '@magickml/server-core'
import { wordCount } from './shared'
import { DEFAULT_GOOGLEAI_API_KEY } from '@magickml/config'

/**
* A function that makes a request to create a text embedding using GoogleAI's
Expand Down Expand Up @@ -40,6 +41,7 @@ export async function makeTextEmbedding(
const apiKey =
(context?.module?.secrets &&
context?.module?.secrets['googleai_api_key']) ||
DEFAULT_GOOGLEAI_API_KEY ||
null

if (!apiKey) {
Expand All @@ -57,7 +59,7 @@ export async function makeTextEmbedding(
text: input,
}

const endpoint = `${GOOGLEAI_ENDPOINT}/${node?.data?.model}:embedText?key=${context.module?.secrets?.['googleai_api_key']}`
const endpoint = `${GOOGLEAI_ENDPOINT}/${node?.data?.model}:embedText?key=${apiKey}`
// Make the API call to GoogleAI
const completion = await fetch(endpoint, {
method: 'POST',
Expand Down
Loading