Skip to content

Commit aec9707

Browse files
authored
Merge pull request #13 from AmbireTech/feat/add-error-to-llm-output
Add error to LLM output
2 parents ab60d3f + 956eaa2 commit aec9707

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export * from './utils/llm/grok'
66
export * from './utils/llm/structures/zod'
77
export * from './utils/prompts'
88
export * from './utils/strategies'
9+
export * from './utils/errors'

lib/types/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export type LlmProcessOutput = {
4949
model: string
5050
}
5151
response: Strategy[] | null
52+
error?: string | null
5253
}
5354

5455
export type ProcessAddressProps = {

lib/utils/errors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function stringifyError(error: unknown): string {
2+
return error instanceof Error ? error.message : JSON.stringify(error)
3+
}

lib/utils/llm/gemini.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { GoogleGenerativeAI, Schema, SchemaType } from '@google/generative-ai'
22
import { LlmProcessOutput, LlmProcessProps, Strategy } from '../../types'
3+
import { stringifyError } from '../errors'
34

45
export const GEMINI_MODELS = {
56
// TODO: more pre-config models
@@ -57,6 +58,7 @@ const schema = {
5758

5859
export async function callGemini(llmInput: LlmProcessProps): Promise<LlmProcessOutput> {
5960
let output = null
61+
let error = null
6062
const model = llmInput.model || GEMINI_MODELS.gemini20flashExp
6163

6264
try {
@@ -74,10 +76,12 @@ export async function callGemini(llmInput: LlmProcessProps): Promise<LlmProcessO
7476

7577
try {
7678
output = JSON.parse(content || '[]') as Strategy[]
77-
} catch (error) {
78-
console.error('Invalid JSON in Gemini AI output: ', error)
79+
} catch (err) {
80+
error = stringifyError(err)
81+
console.error(`Invalid JSON in Gemini AI output: ${error}`)
7982
}
80-
} catch (error) {
83+
} catch (err) {
84+
error = stringifyError(err)
8185
console.error(`Error querying Gemini AI: ${error}`)
8286
}
8387

@@ -86,6 +90,7 @@ export async function callGemini(llmInput: LlmProcessProps): Promise<LlmProcessO
8690
provider: 'Google',
8791
model
8892
},
89-
response: output
93+
response: output,
94+
error
9095
}
9196
}

lib/utils/llm/grok.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import OpenAI from 'openai'
22
import { zodResponseFormat } from 'openai/helpers/zod'
33
import { LlmProcessOutput, LlmProcessProps, Strategy } from '../../types'
44
import { StrategiesZodSchema } from './structures/zod'
5+
import { stringifyError } from '../errors'
56

67
export const XAI_MODELS = {
78
grok2latest: 'grok-2-latest'
@@ -14,6 +15,7 @@ const apiClient = new OpenAI({
1415

1516
export async function callGrok(llmInput: LlmProcessProps): Promise<LlmProcessOutput> {
1617
let output = null
18+
let error = null
1719
const model = llmInput.model || XAI_MODELS.grok2latest
1820

1921
try {
@@ -36,10 +38,12 @@ export async function callGrok(llmInput: LlmProcessProps): Promise<LlmProcessOut
3638
try {
3739
const parsed = JSON.parse(outputContent) as { strategies: Strategy[] }
3840
output = parsed.strategies || []
39-
} catch (error) {
40-
console.error('Invalid JSON in Grok output: ', error)
41+
} catch (err) {
42+
error = stringifyError(err)
43+
console.error(`Invalid JSON in Grok output: ${error}`)
4144
}
42-
} catch (error) {
45+
} catch (err) {
46+
error = stringifyError(err)
4347
console.error(`Error querying Grok: ${error}`)
4448
}
4549

@@ -48,6 +52,7 @@ export async function callGrok(llmInput: LlmProcessProps): Promise<LlmProcessOut
4852
provider: 'xAI',
4953
model
5054
},
51-
response: output
55+
response: output,
56+
error
5257
}
5358
}

0 commit comments

Comments
 (0)