@@ -5,6 +5,8 @@ import { ReadStream } from 'fs';
5
5
import { v4 as uuidv4 } from 'uuid' ;
6
6
7
7
import { LiteralClient } from '.' ;
8
+ import { sharedCache } from './cache/sharedcache' ;
9
+ import { getPromptCacheKey } from './cache/utils' ;
8
10
import {
9
11
Dataset ,
10
12
DatasetExperiment ,
@@ -340,6 +342,8 @@ type CreateAttachmentParams = {
340
342
* Then you can use the `api` object to make calls to the Literal service.
341
343
*/
342
344
export class API {
345
+ /** @ignore */
346
+ private cache : typeof sharedCache ;
343
347
/** @ignore */
344
348
public client : LiteralClient ;
345
349
/** @ignore */
@@ -372,6 +376,8 @@ export class API {
372
376
throw new Error ( 'LITERAL_API_URL not set' ) ;
373
377
}
374
378
379
+ this . cache = sharedCache ;
380
+
375
381
this . apiKey = apiKey ;
376
382
this . url = url ;
377
383
this . environment = environment ;
@@ -399,7 +405,7 @@ export class API {
399
405
* @returns The data part of the response from the GraphQL endpoint.
400
406
* @throws Will throw an error if the GraphQL call returns errors or if the request fails.
401
407
*/
402
- private async makeGqlCall ( query : string , variables : any ) {
408
+ private async makeGqlCall ( query : string , variables : any , timeout ?: number ) {
403
409
try {
404
410
const response = await axios ( {
405
411
url : this . graphqlEndpoint ,
@@ -408,7 +414,8 @@ export class API {
408
414
data : {
409
415
query : query ,
410
416
variables : variables
411
- }
417
+ } ,
418
+ timeout
412
419
} ) ;
413
420
if ( response . data . errors ) {
414
421
throw new Error ( JSON . stringify ( response . data . errors ) ) ;
@@ -2110,41 +2117,75 @@ export class API {
2110
2117
}
2111
2118
2112
2119
/**
2113
- * Retrieves a prompt by its id.
2114
- *
2115
- * @param id ID of the prompt to retrieve.
2116
- * @returns The prompt with given ID.
2120
+ * Retrieves a prompt by its id. If the request fails, it will try to get the prompt from the cache.
2117
2121
*/
2118
2122
public async getPromptById ( id : string ) {
2119
2123
const query = `
2120
- query GetPrompt($id: String!) {
2121
- promptVersion(id: $id) {
2122
- createdAt
2123
- id
2124
- label
2125
- settings
2126
- status
2127
- tags
2128
- templateMessages
2129
- tools
2130
- type
2131
- updatedAt
2132
- url
2133
- variables
2134
- variablesDefaultValues
2135
- version
2136
- lineage {
2137
- name
2124
+ query GetPrompt($id: String!) {
2125
+ promptVersion(id: $id) {
2126
+ createdAt
2127
+ id
2128
+ label
2129
+ settings
2130
+ status
2131
+ tags
2132
+ templateMessages
2133
+ tools
2134
+ type
2135
+ updatedAt
2136
+ url
2137
+ variables
2138
+ variablesDefaultValues
2139
+ version
2140
+ lineage {
2141
+ name
2142
+ }
2138
2143
}
2139
2144
}
2140
- }
2141
2145
` ;
2142
2146
2143
2147
return await this . getPromptWithQuery ( query , { id } ) ;
2144
2148
}
2145
2149
2146
2150
/**
2147
- * Retrieves a prompt by its name and optionally by its version.
2151
+ * Private helper method to execute prompt queries with error handling and caching
2152
+ */
2153
+ private async getPromptWithQuery (
2154
+ query : string ,
2155
+ variables : { id ?: string ; name ?: string ; version ?: number }
2156
+ ) {
2157
+ const cachedPrompt = sharedCache . get ( getPromptCacheKey ( variables ) ) ;
2158
+ const timeout = cachedPrompt ? 1000 : undefined ;
2159
+
2160
+ try {
2161
+ const result = await this . makeGqlCall ( query , variables , timeout ) ;
2162
+
2163
+ if ( ! result . data || ! result . data . promptVersion ) {
2164
+ return cachedPrompt ;
2165
+ }
2166
+
2167
+ const promptData = result . data . promptVersion ;
2168
+ promptData . provider = promptData . settings ?. provider ;
2169
+ promptData . name = promptData . lineage ?. name ;
2170
+ delete promptData . lineage ;
2171
+ if ( promptData . settings ) {
2172
+ delete promptData . settings . provider ;
2173
+ }
2174
+
2175
+ const prompt = new Prompt ( this , promptData ) ;
2176
+
2177
+ sharedCache . put ( prompt . id , prompt ) ;
2178
+ sharedCache . put ( prompt . name , prompt ) ;
2179
+ sharedCache . put ( `${ prompt . name } :${ prompt . version } ` , prompt ) ;
2180
+
2181
+ return prompt ;
2182
+ } catch ( error ) {
2183
+ return cachedPrompt ;
2184
+ }
2185
+ }
2186
+
2187
+ /**
2188
+ * Retrieves a prompt by its name and optionally by its version. If the request fails, it will try to get the prompt from the cache.
2148
2189
*
2149
2190
* @param name - The name of the prompt to retrieve.
2150
2191
* @param version - The version number of the prompt (optional).
@@ -2171,31 +2212,9 @@ export class API {
2171
2212
}
2172
2213
}
2173
2214
` ;
2174
-
2175
2215
return await this . getPromptWithQuery ( query , { name, version } ) ;
2176
2216
}
2177
2217
2178
- private async getPromptWithQuery (
2179
- query : string ,
2180
- variables : Record < string , any >
2181
- ) {
2182
- const result = await this . makeGqlCall ( query , variables ) ;
2183
-
2184
- if ( ! result . data || ! result . data . promptVersion ) {
2185
- return null ;
2186
- }
2187
-
2188
- const promptData = result . data . promptVersion ;
2189
- promptData . provider = promptData . settings ?. provider ;
2190
- promptData . name = promptData . lineage ?. name ;
2191
- delete promptData . lineage ;
2192
- if ( promptData . settings ) {
2193
- delete promptData . settings . provider ;
2194
- }
2195
-
2196
- return new Prompt ( this , promptData ) ;
2197
- }
2198
-
2199
2218
/**
2200
2219
* Retrieves a prompt A/B testing rollout by its name.
2201
2220
*
0 commit comments