1
- import OpenAI from 'openai' ;
2
-
3
1
import { LiteralClient , Maybe } from '..' ;
4
- import { LiteralCallbackHandler } from './langchain' ;
5
- import { instrumentLlamaIndex , withThread } from './llamaindex' ;
6
- import instrumentOpenAI from './openai' ;
7
- import { makeInstrumentVercelSDK } from './vercel-sdk' ;
2
+ import { Thread } from '../observability/thread' ;
3
+ import type { AllVercelFn } from './vercel-sdk' ;
8
4
9
5
export type OpenAIGlobalOptions = {
10
6
tags ?: Maybe < string [ ] > ;
11
7
metadata ?: Maybe < Record < string , any > > ;
12
- client ?: OpenAI ;
8
+ client ?: any ;
13
9
} ;
14
10
15
11
export default ( client : LiteralClient ) => ( {
@@ -23,14 +19,26 @@ export default (client: LiteralClient) => ({
23
19
* @param options.metadata Metadata to attach to all generations.
24
20
* @returns
25
21
*/
26
- openai : ( options ?: OpenAIGlobalOptions ) => instrumentOpenAI ( client , options ) ,
22
+ openai : ( options ?: OpenAIGlobalOptions ) => {
23
+ try {
24
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
25
+ const { default : instrumentOpenAI } = require ( './openai' ) ;
26
+ return instrumentOpenAI ( client , options ) ;
27
+ } catch ( error ) {
28
+ throw new Error (
29
+ 'Failed to load OpenAI. Please ensure openai is installed.'
30
+ ) ;
31
+ }
32
+ } ,
27
33
28
34
langchain : {
29
35
literalCallback : ( params ?: {
30
36
threadId ?: string ;
31
37
chainTypesToIgnore ?: string [ ] ;
32
38
} ) => {
33
39
try {
40
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
41
+ const { LiteralCallbackHandler } = require ( './langchain' ) ;
34
42
return new LiteralCallbackHandler (
35
43
client ,
36
44
params ?. threadId ,
@@ -45,23 +53,41 @@ export default (client: LiteralClient) => ({
45
53
} ,
46
54
47
55
vercel : {
48
- /**
49
- * Instrument a Vercel SDK function to log all invocations to the Literal AI API.
50
- * It will be augmented with a `literalAiParent` option that allows you to specify a parent step or thread.
51
- * @param fn The function to instrument. This can be Vercel SDK's `generateText` or `streamText` functions.
52
- * @returns A new function that logs all invocations to the Literal AI API.
53
- */
54
- instrument : makeInstrumentVercelSDK ( client )
56
+ instrument : < TFunction extends AllVercelFn > ( fn : TFunction ) => {
57
+ try {
58
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
59
+ const { makeInstrumentVercelSDK } = require ( './vercel-sdk' ) ;
60
+ return makeInstrumentVercelSDK ( client ) ( fn ) ;
61
+ } catch ( error ) {
62
+ throw new Error (
63
+ 'Failed to load Vercel SDK. Please ensure @vercel/ai is installed.'
64
+ ) ;
65
+ }
66
+ }
55
67
} ,
56
68
57
69
llamaIndex : {
58
- /**
59
- * Instrument the LlamaIndex client to log all generations to the Literal AI API.
60
- */
61
- instrument : ( ) => instrumentLlamaIndex ( client ) ,
62
- /**
63
- * Run a callback with a thread context.
64
- */
65
- withThread
70
+ instrument : ( ) => {
71
+ try {
72
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
73
+ const { instrumentLlamaIndex } = require ( './llamaindex' ) ;
74
+ return instrumentLlamaIndex ( client ) ;
75
+ } catch ( error ) {
76
+ throw new Error (
77
+ 'Failed to load LlamaIndex. Please ensure llamaindex is installed.'
78
+ ) ;
79
+ }
80
+ } ,
81
+ withThread : < R > ( thread : Thread , callback : ( ) => R ) => {
82
+ try {
83
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
84
+ const { withThread } = require ( './llamaindex' ) ;
85
+ return withThread ( thread , callback ) ;
86
+ } catch ( error ) {
87
+ throw new Error (
88
+ 'Failed to load LlamaIndex. Please ensure llamaindex is installed.'
89
+ ) ;
90
+ }
91
+ }
66
92
}
67
93
} ) ;
0 commit comments