@payload-enchants/translator 1.1.22 → 1.1.23
Sign up to get free protection for your applications and to get access to all the features.
@@ -6,6 +6,7 @@ export type OpenAIPrompt = (args: {
|
|
6
6
|
}) => string;
|
7
7
|
export type OpenAIResolverConfig = {
|
8
8
|
apiKey: string;
|
9
|
+
baseUrl?: string;
|
9
10
|
/**
|
10
11
|
* How many texts to include into 1 request
|
11
12
|
* @default 100
|
@@ -17,5 +18,5 @@ export type OpenAIResolverConfig = {
|
|
17
18
|
model?: string;
|
18
19
|
promt?: OpenAIPrompt;
|
19
20
|
};
|
20
|
-
export declare const openAIResolver: ({ apiKey, chunkLength, model, promt, }: OpenAIResolverConfig) => TranslateResolver;
|
21
|
+
export declare const openAIResolver: ({ apiKey, baseUrl, chunkLength, model, promt, }: OpenAIResolverConfig) => TranslateResolver;
|
21
22
|
//# sourceMappingURL=openAI.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"openAI.d.ts","sourceRoot":"","sources":["../../src/resolvers/openAI.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAEjD,MAAM,MAAM,YAAY,GAAG,CAAC,IAAI,EAAE;IAChC,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB,KAAK,MAAM,CAAC;AAEb,MAAM,MAAM,oBAAoB,GAAG;IACjC,MAAM,EAAE,MAAM,CAAC;IACf;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,YAAY,CAAC;CACtB,CAAC;AAcF,eAAO,MAAM,cAAc,
|
1
|
+
{"version":3,"file":"openAI.d.ts","sourceRoot":"","sources":["../../src/resolvers/openAI.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAEjD,MAAM,MAAM,YAAY,GAAG,CAAC,IAAI,EAAE;IAChC,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB,KAAK,MAAM,CAAC;AAEb,MAAM,MAAM,oBAAoB,GAAG;IACjC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,YAAY,CAAC;CACtB,CAAC;AAcF,eAAO,MAAM,cAAc,oDAMxB,oBAAoB,KAAG,iBA6GzB,CAAC"}
|
package/dist/resolvers/openAI.js
CHANGED
@@ -2,11 +2,11 @@ import { chunkArray } from '../utils/chunkArray';
|
|
2
2
|
const defaultPromt = ({ localeFrom, localeTo, texts })=>{
|
3
3
|
return `Translate me the following array: ${JSON.stringify(texts)} in locale=${localeFrom} to locale ${localeTo}, respond me with the same array structure`;
|
4
4
|
};
|
5
|
-
export const openAIResolver = ({ apiKey, chunkLength = 100, model = 'gpt-3.5-turbo', promt = defaultPromt })=>{
|
5
|
+
export const openAIResolver = ({ apiKey, baseUrl, chunkLength = 100, model = 'gpt-3.5-turbo', promt = defaultPromt })=>{
|
6
6
|
return {
|
7
7
|
key: 'openai',
|
8
8
|
resolve: async ({ localeFrom, localeTo, req, texts })=>{
|
9
|
-
const apiUrl = 'https://api.openai.com/v1/chat/completions
|
9
|
+
const apiUrl = `${baseUrl || 'https://api.openai.com'}/v1/chat/completions`;
|
10
10
|
try {
|
11
11
|
const respones = await Promise.all(chunkArray(texts, chunkLength).map((texts)=>{
|
12
12
|
return fetch(apiUrl, {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/resolvers/openAI.ts"],"sourcesContent":["import { chunkArray } from '../utils/chunkArray';\nimport type { TranslateResolver } from './types';\n\nexport type OpenAIPrompt = (args: {\n localeFrom: string;\n localeTo: string;\n texts: string[];\n}) => string;\n\nexport type OpenAIResolverConfig = {\n apiKey: string;\n /**\n * How many texts to include into 1 request\n * @default 100\n */\n chunkLength?: number;\n /**\n * @default \"gpt-3.5-turbo\"\n */\n model?: string;\n promt?: OpenAIPrompt;\n};\n\ntype OpenAIResponse = {\n choices: {\n message: {\n content: string;\n };\n }[];\n};\n\nconst defaultPromt: OpenAIPrompt = ({ localeFrom, localeTo, texts }) => {\n return `Translate me the following array: ${JSON.stringify(texts)} in locale=${localeFrom} to locale ${localeTo}, respond me with the same array structure`;\n};\n\nexport const openAIResolver = ({\n apiKey,\n chunkLength = 100,\n model = 'gpt-3.5-turbo',\n promt = defaultPromt,\n}: OpenAIResolverConfig): TranslateResolver => {\n return {\n key: 'openai',\n resolve: async ({ localeFrom, localeTo, req, texts }) => {\n const apiUrl = 'https://api.openai.com/v1/chat/completions
|
1
|
+
{"version":3,"sources":["../../src/resolvers/openAI.ts"],"sourcesContent":["import { chunkArray } from '../utils/chunkArray';\nimport type { TranslateResolver } from './types';\n\nexport type OpenAIPrompt = (args: {\n localeFrom: string;\n localeTo: string;\n texts: string[];\n}) => string;\n\nexport type OpenAIResolverConfig = {\n apiKey: string;\n baseUrl?: string;\n /**\n * How many texts to include into 1 request\n * @default 100\n */\n chunkLength?: number;\n /**\n * @default \"gpt-3.5-turbo\"\n */\n model?: string;\n promt?: OpenAIPrompt;\n};\n\ntype OpenAIResponse = {\n choices: {\n message: {\n content: string;\n };\n }[];\n};\n\nconst defaultPromt: OpenAIPrompt = ({ localeFrom, localeTo, texts }) => {\n return `Translate me the following array: ${JSON.stringify(texts)} in locale=${localeFrom} to locale ${localeTo}, respond me with the same array structure`;\n};\n\nexport const openAIResolver = ({\n apiKey,\n baseUrl,\n chunkLength = 100,\n model = 'gpt-3.5-turbo',\n promt = defaultPromt,\n}: OpenAIResolverConfig): TranslateResolver => {\n return {\n key: 'openai',\n resolve: async ({ localeFrom, localeTo, req, texts }) => {\n const apiUrl = `${baseUrl || 'https://api.openai.com'}/v1/chat/completions`;\n\n try {\n const respones: {\n data: OpenAIResponse;\n success: boolean;\n }[] = await Promise.all(\n chunkArray(texts, chunkLength).map((texts) => {\n return fetch(apiUrl, {\n body: JSON.stringify({\n messages: [\n {\n content: promt({ localeFrom, localeTo, texts }),\n role: 'user',\n },\n ],\n model,\n }),\n headers: {\n Authorization: `Bearer ${apiKey}`,\n 'Content-Type': 'application/json',\n },\n method: 'post',\n }).then(async (res) => {\n const data = await res.json();\n\n if (!res.ok)\n req.payload.logger.info({\n message: `An error occurred when trying to translate the data using OpenAI API`,\n openAIresponse: data,\n });\n\n return {\n data,\n success: res.ok,\n };\n });\n }),\n );\n\n const translated: string[] = [];\n\n for (const { data, success } of respones) {\n if (!success)\n return {\n success: false as const,\n };\n\n const content = data?.choices?.[0]?.message?.content;\n\n if (!content) {\n req.payload.logger.info(\n `An error occurred when trying to translate the data using OpenAI API - missing content in the response`,\n );\n\n return {\n success: false as const,\n };\n }\n\n const translatedChunk: string[] = JSON.parse(content);\n\n if (!Array.isArray(translatedChunk)) {\n req.payload.logger.info({\n data: translatedChunk,\n message: `An error occurred when trying to translate the data using OpenAI API - parsed content is not an array`,\n });\n\n return {\n success: false as const,\n };\n }\n\n for (const text of translatedChunk) {\n if (typeof text !== 'string') {\n req.payload.logger.info({\n data: text,\n message: `An error occurred when trying to translate the data using OpenAI API - parsed content is not an array`,\n });\n\n return {\n success: false as const,\n };\n }\n\n translated.push(text);\n }\n }\n\n return {\n success: true as const,\n translatedTexts: translated,\n };\n } catch (e) {\n if (e instanceof Error) {\n req.payload.logger.info({\n message: `An error occurred when trying to translate the data using OpenAI API`,\n originalErr: e.message,\n });\n }\n\n return { success: false as const };\n }\n },\n };\n};\n"],"names":["chunkArray","defaultPromt","localeFrom","localeTo","texts","JSON","stringify","openAIResolver","apiKey","baseUrl","chunkLength","model","promt","key","resolve","req","apiUrl","respones","Promise","all","map","fetch","body","messages","content","role","headers","Authorization","method","then","res","data","json","ok","payload","logger","info","message","openAIresponse","success","translated","choices","translatedChunk","parse","Array","isArray","text","push","translatedTexts","e","Error","originalErr"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,SAASA,UAAU,QAAQ,sBAAsB;AAgCjD,MAAMC,eAA6B,CAAC,EAAEC,UAAU,EAAEC,QAAQ,EAAEC,KAAK,EAAE;IACjE,OAAO,CAAC,kCAAkC,EAAEC,KAAKC,SAAS,CAACF,OAAO,WAAW,EAAEF,WAAW,WAAW,EAAEC,SAAS,0CAA0C,CAAC;AAC7J;AAEA,OAAO,MAAMI,iBAAiB,CAAC,EAC7BC,MAAM,EACNC,OAAO,EACPC,cAAc,GAAG,EACjBC,QAAQ,eAAe,EACvBC,QAAQX,YAAY,EACC;IACrB,OAAO;QACLY,KAAK;QACLC,SAAS,OAAO,EAAEZ,UAAU,EAAEC,QAAQ,EAAEY,GAAG,EAAEX,KAAK,EAAE;YAClD,MAAMY,SAAS,CAAC,EAAEP,WAAW,yBAAyB,oBAAoB,CAAC;YAE3E,IAAI;gBACF,MAAMQ,WAGA,MAAMC,QAAQC,GAAG,CACrBnB,WAAWI,OAAOM,aAAaU,GAAG,CAAC,CAAChB;oBAClC,OAAOiB,MAAML,QAAQ;wBACnBM,MAAMjB,KAAKC,SAAS,CAAC;4BACnBiB,UAAU;gCACR;oCACEC,SAASZ,MAAM;wCAAEV;wCAAYC;wCAAUC;oCAAM;oCAC7CqB,MAAM;gCACR;6BACD;4BACDd;wBACF;wBACAe,SAAS;4BACPC,eAAe,CAAC,OAAO,EAAEnB,OAAO,CAAC;4BACjC,gBAAgB;wBAClB;wBACAoB,QAAQ;oBACV,GAAGC,IAAI,CAAC,OAAOC;wBACb,MAAMC,OAAO,MAAMD,IAAIE,IAAI;wBAE3B,IAAI,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