-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathutils.ts
More file actions
93 lines (79 loc) · 3.2 KB
/
utils.ts
File metadata and controls
93 lines (79 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { captureException } from '../../exports';
import { SPAN_STATUS_ERROR } from '../../tracing';
import type { Span } from '../../types-hoist/span';
import type { SpanStatusType } from '../../types-hoist/spanStatus';
import {
GEN_AI_INPUT_MESSAGES_ATTRIBUTE,
GEN_AI_INPUT_MESSAGES_ORIGINAL_LENGTH_ATTRIBUTE,
GEN_AI_SYSTEM_INSTRUCTIONS_ATTRIBUTE,
} from '../ai/gen-ai-attributes';
import { extractSystemInstructions, getJsonString, getTruncatedJsonString } from '../ai/utils';
import type { AnthropicAiResponse } from './types';
/**
* Set the messages and messages original length attributes.
* Extracts system instructions before truncation.
*/
export function setMessagesAttribute(span: Span, messages: unknown, enableTruncation: boolean): void {
if (Array.isArray(messages) && messages.length === 0) {
return;
}
const { systemInstructions, filteredMessages } = extractSystemInstructions(messages);
if (systemInstructions) {
span.setAttributes({
[GEN_AI_SYSTEM_INSTRUCTIONS_ATTRIBUTE]: systemInstructions,
});
}
const filteredLength = Array.isArray(filteredMessages) ? filteredMessages.length : 1;
span.setAttributes({
[GEN_AI_INPUT_MESSAGES_ATTRIBUTE]: enableTruncation
? getTruncatedJsonString(filteredMessages)
: getJsonString(filteredMessages),
[GEN_AI_INPUT_MESSAGES_ORIGINAL_LENGTH_ATTRIBUTE]: filteredLength,
});
}
const ANTHROPIC_ERROR_TYPE_TO_SPAN_STATUS: Record<string, SpanStatusType> = {
invalid_request_error: 'invalid_argument',
authentication_error: 'unauthenticated',
permission_error: 'permission_denied',
not_found_error: 'not_found',
request_too_large: 'failed_precondition',
rate_limit_error: 'resource_exhausted',
api_error: 'internal_error',
overloaded_error: 'unavailable',
};
/**
* Map an Anthropic API error type to a SpanStatusType value.
* @see https://docs.anthropic.com/en/api/errors#error-shapes
*/
export function mapAnthropicErrorToStatusMessage(errorType: string | undefined): SpanStatusType {
if (!errorType) {
return 'internal_error';
}
return ANTHROPIC_ERROR_TYPE_TO_SPAN_STATUS[errorType] || 'internal_error';
}
/**
* Capture error information from the response
* @see https://docs.anthropic.com/en/api/errors#error-shapes
*/
export function handleResponseError(span: Span, response: AnthropicAiResponse): void {
if (response.error) {
span.setStatus({ code: SPAN_STATUS_ERROR, message: mapAnthropicErrorToStatusMessage(response.error.type) });
captureException(response.error, {
mechanism: {
handled: false,
type: 'auto.ai.anthropic.anthropic_error',
},
});
}
}
/**
* Include the system prompt in the messages list, if available
*/
export function messagesFromParams(params: Record<string, unknown>): unknown[] {
const { system, messages, input } = params;
const systemMessages = typeof system === 'string' ? [{ role: 'system', content: params.system }] : [];
const inputParamMessages = Array.isArray(input) ? input : input != null ? [input] : undefined;
const messagesParamMessages = Array.isArray(messages) ? messages : messages != null ? [messages] : [];
const userMessages = inputParamMessages ?? messagesParamMessages;
return [...systemMessages, ...userMessages];
}