-
Notifications
You must be signed in to change notification settings - Fork 421
refactor: convert bedrock's LlmError to use LlmErrorMessage instead
#3567
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,43 +12,55 @@ | |
| module.exports = class LlmErrorMessage { | ||
| /** | ||
| * @param {object} params Constructor parameters | ||
| * @param {object} [params.response] Instance of an incoming message. | ||
| * @param {object} [params.cause] An instance of the OpenAI error object. | ||
| * @param {object} params.response Instance of an incoming message. | ||
| * @param {object} params.cause An instance of the LLM error object. | ||
| * @param {LlmChatCompletionSummary} [params.summary] Details about the | ||
| * conversation if it was a chat completion conversation. | ||
| * @param {LlmEmbedding} [params.embedding] Details about the conversation | ||
| * if it was an embedding conversation. | ||
| * @param {LlmVectorStoreSearch} [params.vectorsearch] Details about the vector | ||
| * search if it was a vector search event. | ||
| * @param {LlmTool} [params.tool] Details about the tool event if it was a tool event. | ||
| * @param {boolean} [params.useNameAsCode] defaults to false, only Bedrock sets it to true so far | ||
| */ | ||
| constructor({ response, cause, summary, embedding, vectorsearch, tool } = {}) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think I realized that this class is overloaded. We handle specific assignment of attributes depending on params and/or further processing. It seems like most LLMs will either use keys on response or error(cause) to assign attributes but in the case of gemini it parses error and in case of bedrock is uses the error.name for the code instead of error.code
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking of maybe having error message classes that inherit from
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for sure, that's what I was referring to. it can wait until then |
||
| // For @google/genai only, cause does not have the `error` or `status` fields, | ||
| // but it does have `message` with the info we need. So, we need to parse | ||
| // the relevant fields from cause.message to get `status` and `error`. | ||
| let parsedError | ||
| const isGeminiVendor = embedding?.vendor === 'gemini' || summary?.vendor === 'gemini' | ||
| if (isGeminiVendor && cause?.message) { | ||
| try { | ||
| // Extract the JSON portion of the cause.message | ||
| const jsonStartIndex = cause.message.indexOf('{') | ||
| const jsonString = cause.message.substring(jsonStartIndex) | ||
| parsedError = JSON.parse(jsonString)?.error | ||
| } catch { | ||
| parsedError = undefined | ||
| } | ||
| } | ||
| this['http.statusCode'] = response?.status ?? cause?.status ?? parsedError?.code | ||
| constructor({ response, cause, summary = {}, embedding = {}, vectorsearch = {}, tool = {}, useNameAsCode = false } = {}) { | ||
| this['http.statusCode'] = response?.statusCode ?? response?.status ?? cause?.status | ||
| this['error.message'] = cause?.message | ||
| this['error.code'] = response?.code ?? cause?.error?.code ?? parsedError?.code | ||
| this['error.code'] = response?.code ?? cause?.error?.code | ||
| if (useNameAsCode) { | ||
| this['error.code'] = cause?.name | ||
| } | ||
| this['error.param'] = response?.param ?? cause?.error?.param | ||
| this.completion_id = summary?.id | ||
| this.embedding_id = embedding?.id | ||
| this.vector_store_id = vectorsearch?.id | ||
| this.tool_id = tool?.id | ||
|
|
||
| if (embedding?.vendor === 'gemini' || summary?.vendor === 'gemini') { | ||
| this._handleGemini(cause) | ||
| } | ||
| } | ||
|
|
||
| get [Symbol.toStringTag]() { | ||
| return 'LlmErrorMessage' | ||
| } | ||
|
|
||
| /** | ||
| * For `@google/genai` only, `cause` does not have the `error` or `status` fields, | ||
| * but it does have `message` with the info we need. So, we need to parse | ||
| * the relevant fields from cause.message to get `status` and `error`. | ||
| * @param {object} cause error object | ||
| */ | ||
| _handleGemini(cause) { | ||
| if (cause?.message) { | ||
| try { | ||
| const jsonStartIndex = cause.message.indexOf('{') | ||
| const jsonString = cause.message.substring(jsonStartIndex) | ||
| const parsedError = JSON.parse(jsonString)?.error | ||
|
|
||
| this['http.statusCode'] = parsedError?.code | ||
| this['error.code'] = parsedError?.code | ||
| } catch { } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.