langchain 0.0.196 → 0.0.197-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (225) hide show
  1. package/LICENSE +21 -0
  2. package/dist/agents/executor.cjs +351 -1
  3. package/dist/agents/executor.d.ts +61 -3
  4. package/dist/agents/executor.js +350 -1
  5. package/dist/agents/openai/index.cjs +6 -2
  6. package/dist/agents/openai/index.js +6 -2
  7. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts +1 -1
  8. package/dist/base_language/count_tokens.cjs +4 -4
  9. package/dist/base_language/count_tokens.d.ts +1 -1
  10. package/dist/base_language/count_tokens.js +1 -1
  11. package/dist/base_language/index.cjs +3 -3
  12. package/dist/base_language/index.d.ts +1 -1
  13. package/dist/base_language/index.js +1 -1
  14. package/dist/cache/base.cjs +1 -1
  15. package/dist/cache/base.d.ts +1 -1
  16. package/dist/cache/base.js +1 -1
  17. package/dist/cache/index.cjs +2 -2
  18. package/dist/cache/index.d.ts +1 -1
  19. package/dist/cache/index.js +1 -1
  20. package/dist/callbacks/base.cjs +1 -1
  21. package/dist/callbacks/base.d.ts +1 -1
  22. package/dist/callbacks/base.js +1 -1
  23. package/dist/callbacks/handlers/console.cjs +1 -1
  24. package/dist/callbacks/handlers/console.d.ts +1 -1
  25. package/dist/callbacks/handlers/console.js +1 -1
  26. package/dist/callbacks/handlers/initialize.cjs +1 -1
  27. package/dist/callbacks/handlers/initialize.d.ts +1 -1
  28. package/dist/callbacks/handlers/initialize.js +1 -1
  29. package/dist/callbacks/handlers/log_stream.cjs +1 -1
  30. package/dist/callbacks/handlers/log_stream.d.ts +1 -1
  31. package/dist/callbacks/handlers/log_stream.js +1 -1
  32. package/dist/callbacks/handlers/run_collector.cjs +1 -1
  33. package/dist/callbacks/handlers/run_collector.d.ts +1 -1
  34. package/dist/callbacks/handlers/run_collector.js +1 -1
  35. package/dist/callbacks/handlers/tracer.cjs +1 -1
  36. package/dist/callbacks/handlers/tracer.d.ts +1 -1
  37. package/dist/callbacks/handlers/tracer.js +1 -1
  38. package/dist/callbacks/handlers/tracer_langchain.cjs +1 -1
  39. package/dist/callbacks/handlers/tracer_langchain.d.ts +1 -1
  40. package/dist/callbacks/handlers/tracer_langchain.js +1 -1
  41. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +1 -1
  42. package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +1 -1
  43. package/dist/callbacks/handlers/tracer_langchain_v1.js +1 -1
  44. package/dist/callbacks/manager.cjs +1 -1
  45. package/dist/callbacks/manager.d.ts +1 -1
  46. package/dist/callbacks/manager.js +1 -1
  47. package/dist/callbacks/promises.cjs +1 -1
  48. package/dist/callbacks/promises.d.ts +1 -1
  49. package/dist/callbacks/promises.js +1 -1
  50. package/dist/chains/base.cjs +16 -0
  51. package/dist/chains/base.d.ts +2 -0
  52. package/dist/chains/base.js +16 -0
  53. package/dist/chains/openai_functions/structured_output.cjs +1 -1
  54. package/dist/chains/openai_functions/structured_output.d.ts +1 -1
  55. package/dist/chains/openai_functions/structured_output.js +1 -1
  56. package/dist/chains/openai_moderation.cjs +2 -2
  57. package/dist/chains/openai_moderation.d.ts +1 -1
  58. package/dist/chains/openai_moderation.js +1 -1
  59. package/dist/chat_models/anthropic.cjs +15 -348
  60. package/dist/chat_models/anthropic.d.ts +1 -156
  61. package/dist/chat_models/anthropic.js +1 -346
  62. package/dist/chat_models/base.cjs +1 -1
  63. package/dist/chat_models/base.d.ts +1 -1
  64. package/dist/chat_models/base.js +1 -1
  65. package/dist/chat_models/bedrock/web.cjs +21 -1
  66. package/dist/chat_models/bedrock/web.d.ts +1 -1
  67. package/dist/chat_models/bedrock/web.js +21 -1
  68. package/dist/chat_models/fireworks.d.ts +2 -2
  69. package/dist/chat_models/iflytek_xinghuo/common.d.ts +1 -1
  70. package/dist/chat_models/minimax.d.ts +2 -2
  71. package/dist/chat_models/openai.cjs +4 -698
  72. package/dist/chat_models/openai.d.ts +4 -137
  73. package/dist/chat_models/openai.js +2 -695
  74. package/dist/document.cjs +2 -2
  75. package/dist/document.d.ts +1 -1
  76. package/dist/document.js +1 -1
  77. package/dist/document_loaders/fs/openai_whisper_audio.cjs +2 -2
  78. package/dist/document_loaders/fs/openai_whisper_audio.d.ts +1 -1
  79. package/dist/document_loaders/fs/openai_whisper_audio.js +1 -1
  80. package/dist/document_loaders/web/azure_blob_storage_file.d.ts +1 -1
  81. package/dist/document_loaders/web/github.cjs +105 -0
  82. package/dist/document_loaders/web/github.d.ts +26 -0
  83. package/dist/document_loaders/web/github.js +105 -0
  84. package/dist/document_loaders/web/s3.d.ts +1 -1
  85. package/dist/embeddings/base.cjs +1 -1
  86. package/dist/embeddings/base.d.ts +1 -1
  87. package/dist/embeddings/base.js +1 -1
  88. package/dist/embeddings/cache_backed.cjs +1 -1
  89. package/dist/embeddings/cache_backed.js +1 -1
  90. package/dist/embeddings/openai.cjs +2 -240
  91. package/dist/embeddings/openai.d.ts +1 -82
  92. package/dist/embeddings/openai.js +1 -239
  93. package/dist/experimental/openai_assistant/index.cjs +3 -3
  94. package/dist/experimental/openai_assistant/index.d.ts +1 -1
  95. package/dist/experimental/openai_assistant/index.js +1 -1
  96. package/dist/experimental/openai_assistant/schema.d.ts +1 -1
  97. package/dist/experimental/openai_files/index.cjs +2 -2
  98. package/dist/experimental/openai_files/index.d.ts +1 -1
  99. package/dist/experimental/openai_files/index.js +1 -1
  100. package/dist/experimental/plan_and_execute/prompt.d.ts +1 -1
  101. package/dist/llms/base.cjs +1 -1
  102. package/dist/llms/base.d.ts +1 -1
  103. package/dist/llms/base.js +1 -1
  104. package/dist/llms/bedrock/web.cjs +21 -1
  105. package/dist/llms/bedrock/web.d.ts +1 -1
  106. package/dist/llms/bedrock/web.js +21 -1
  107. package/dist/llms/fireworks.d.ts +2 -2
  108. package/dist/llms/openai-chat.cjs +3 -445
  109. package/dist/llms/openai-chat.d.ts +4 -123
  110. package/dist/llms/openai-chat.js +2 -443
  111. package/dist/llms/openai.cjs +6 -530
  112. package/dist/llms/openai.d.ts +4 -123
  113. package/dist/llms/openai.js +2 -525
  114. package/dist/load/index.cjs +4 -2
  115. package/dist/load/index.d.ts +2 -2
  116. package/dist/load/index.js +4 -2
  117. package/dist/load/serializable.cjs +1 -1
  118. package/dist/load/serializable.d.ts +1 -1
  119. package/dist/load/serializable.js +1 -1
  120. package/dist/memory/base.cjs +2 -2
  121. package/dist/memory/base.d.ts +2 -2
  122. package/dist/memory/base.js +2 -2
  123. package/dist/output_parsers/list.cjs +4 -122
  124. package/dist/output_parsers/list.d.ts +1 -57
  125. package/dist/output_parsers/list.js +1 -119
  126. package/dist/output_parsers/openai_functions.cjs +1 -1
  127. package/dist/output_parsers/openai_functions.d.ts +1 -1
  128. package/dist/output_parsers/openai_functions.js +1 -1
  129. package/dist/output_parsers/structured.d.ts +1 -1
  130. package/dist/prompts/base.cjs +8 -8
  131. package/dist/prompts/base.d.ts +3 -3
  132. package/dist/prompts/base.js +3 -3
  133. package/dist/prompts/chat.cjs +13 -15
  134. package/dist/prompts/chat.d.ts +2 -1
  135. package/dist/prompts/chat.js +2 -1
  136. package/dist/prompts/few_shot.cjs +4 -15
  137. package/dist/prompts/few_shot.d.ts +1 -1
  138. package/dist/prompts/few_shot.js +1 -1
  139. package/dist/prompts/index.cjs +2 -2
  140. package/dist/prompts/index.d.ts +1 -1
  141. package/dist/prompts/index.js +1 -1
  142. package/dist/prompts/pipeline.cjs +3 -15
  143. package/dist/prompts/pipeline.d.ts +1 -1
  144. package/dist/prompts/pipeline.js +1 -1
  145. package/dist/prompts/prompt.cjs +3 -15
  146. package/dist/prompts/prompt.d.ts +1 -1
  147. package/dist/prompts/prompt.js +1 -1
  148. package/dist/prompts/selectors/LengthBasedExampleSelector.cjs +3 -15
  149. package/dist/prompts/selectors/LengthBasedExampleSelector.d.ts +1 -1
  150. package/dist/prompts/selectors/LengthBasedExampleSelector.js +1 -1
  151. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +1 -1
  152. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +1 -1
  153. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +1 -1
  154. package/dist/prompts/selectors/conditional.cjs +6 -15
  155. package/dist/prompts/selectors/conditional.d.ts +1 -1
  156. package/dist/prompts/selectors/conditional.js +1 -1
  157. package/dist/prompts/serde.cjs +0 -15
  158. package/dist/prompts/serde.d.ts +1 -1
  159. package/dist/prompts/serde.js +1 -1
  160. package/dist/prompts/template.cjs +9 -15
  161. package/dist/prompts/template.d.ts +1 -1
  162. package/dist/prompts/template.js +1 -1
  163. package/dist/runnables/index.cjs +1 -1
  164. package/dist/runnables/index.d.ts +1 -1
  165. package/dist/runnables/index.js +1 -1
  166. package/dist/schema/document.cjs +3 -3
  167. package/dist/schema/document.d.ts +1 -1
  168. package/dist/schema/document.js +1 -1
  169. package/dist/schema/index.cjs +12 -12
  170. package/dist/schema/index.d.ts +11 -11
  171. package/dist/schema/index.js +7 -7
  172. package/dist/schema/output_parser.cjs +1 -1
  173. package/dist/schema/output_parser.d.ts +1 -1
  174. package/dist/schema/output_parser.js +1 -1
  175. package/dist/schema/retriever.cjs +1 -1
  176. package/dist/schema/retriever.d.ts +1 -1
  177. package/dist/schema/retriever.js +1 -1
  178. package/dist/schema/runnable/base.cjs +1 -1
  179. package/dist/schema/runnable/base.d.ts +1 -1
  180. package/dist/schema/runnable/base.js +1 -1
  181. package/dist/schema/runnable/branch.cjs +1 -1
  182. package/dist/schema/runnable/branch.d.ts +1 -1
  183. package/dist/schema/runnable/branch.js +1 -1
  184. package/dist/schema/runnable/config.d.ts +1 -1
  185. package/dist/schema/runnable/index.cjs +1 -1
  186. package/dist/schema/runnable/index.d.ts +1 -1
  187. package/dist/schema/runnable/index.js +1 -1
  188. package/dist/schema/runnable/passthrough.cjs +1 -1
  189. package/dist/schema/runnable/passthrough.d.ts +1 -1
  190. package/dist/schema/runnable/passthrough.js +1 -1
  191. package/dist/schema/runnable/router.cjs +1 -1
  192. package/dist/schema/runnable/router.d.ts +1 -1
  193. package/dist/schema/runnable/router.js +1 -1
  194. package/dist/schema/storage.cjs +1 -1
  195. package/dist/schema/storage.d.ts +1 -1
  196. package/dist/schema/storage.js +1 -1
  197. package/dist/tools/base.cjs +4 -108
  198. package/dist/tools/base.d.ts +1 -72
  199. package/dist/tools/base.js +1 -105
  200. package/dist/tools/bingserpapi.d.ts +1 -1
  201. package/dist/tools/convert_to_openai.cjs +4 -38
  202. package/dist/tools/convert_to_openai.d.ts +1 -11
  203. package/dist/tools/convert_to_openai.js +1 -35
  204. package/dist/tools/searchapi.d.ts +1 -1
  205. package/dist/tools/serpapi.d.ts +1 -1
  206. package/dist/tools/serper.d.ts +1 -1
  207. package/dist/types/openai-types.d.ts +1 -133
  208. package/dist/util/async_caller.cjs +1 -1
  209. package/dist/util/async_caller.d.ts +1 -1
  210. package/dist/util/async_caller.js +1 -1
  211. package/dist/util/openai.cjs +2 -18
  212. package/dist/util/openai.d.ts +1 -1
  213. package/dist/util/openai.js +1 -17
  214. package/dist/util/openapi.d.ts +2 -2
  215. package/dist/util/prompt-layer.d.ts +1 -1
  216. package/dist/vectorstores/momento_vector_index.cjs +39 -0
  217. package/dist/vectorstores/momento_vector_index.d.ts +17 -1
  218. package/dist/vectorstores/momento_vector_index.js +40 -1
  219. package/dist/vectorstores/mongodb_atlas.cjs +22 -2
  220. package/dist/vectorstores/mongodb_atlas.d.ts +13 -0
  221. package/dist/vectorstores/mongodb_atlas.js +22 -2
  222. package/package.json +12 -10
  223. package/dist/util/openai-format-fndef.cjs +0 -81
  224. package/dist/util/openai-format-fndef.d.ts +0 -44
  225. package/dist/util/openai-format-fndef.js +0 -77
@@ -101,5 +101,18 @@ export declare class MongoDBAtlasVectorSearch extends VectorStore {
101
101
  * @returns Promise that resolves to a new instance of MongoDBAtlasVectorSearch.
102
102
  */
103
103
  static fromDocuments(docs: Document[], embeddings: Embeddings, dbConfig: MongoDBAtlasVectorSearchLibArgs): Promise<MongoDBAtlasVectorSearch>;
104
+ /**
105
+ * Static method to fix the precision of the array that ensures that
106
+ * every number in this array is always float when casted to other types.
107
+ * This is needed since MongoDB Atlas Vector Search does not cast integer
108
+ * inside vector search to float automatically.
109
+ * This method shall introduce a hint of error but should be safe to use
110
+ * since introduced error is very small, only applies to integer numbers
111
+ * returned by embeddings, and most embeddings shall not have precision
112
+ * as high as 15 decimal places.
113
+ * @param array Array of number to be fixed.
114
+ * @returns
115
+ */
116
+ static fixArrayPrecision(array: number[]): number[];
104
117
  }
105
118
  export {};
@@ -96,7 +96,7 @@ export class MongoDBAtlasVectorSearch extends VectorStore {
96
96
  const pipeline = [
97
97
  {
98
98
  $vectorSearch: {
99
- queryVector: query,
99
+ queryVector: MongoDBAtlasVectorSearch.fixArrayPrecision(query),
100
100
  index: this.indexName,
101
101
  path: this.embeddingKey,
102
102
  limit: k,
@@ -145,7 +145,7 @@ export class MongoDBAtlasVectorSearch extends VectorStore {
145
145
  ...filter,
146
146
  includeEmbeddings: true,
147
147
  };
148
- const resultDocs = await this.similaritySearchVectorWithScore(queryEmbedding, fetchK, includeEmbeddingsFilter);
148
+ const resultDocs = await this.similaritySearchVectorWithScore(MongoDBAtlasVectorSearch.fixArrayPrecision(queryEmbedding), fetchK, includeEmbeddingsFilter);
149
149
  const embeddingList = resultDocs.map((doc) => doc[0].metadata[this.embeddingKey]);
150
150
  const mmrIndexes = maximalMarginalRelevance(queryEmbedding, embeddingList, lambda, k);
151
151
  return mmrIndexes.map((idx) => {
@@ -193,4 +193,24 @@ export class MongoDBAtlasVectorSearch extends VectorStore {
193
193
  await instance.addDocuments(docs);
194
194
  return instance;
195
195
  }
196
+ /**
197
+ * Static method to fix the precision of the array that ensures that
198
+ * every number in this array is always float when casted to other types.
199
+ * This is needed since MongoDB Atlas Vector Search does not cast integer
200
+ * inside vector search to float automatically.
201
+ * This method shall introduce a hint of error but should be safe to use
202
+ * since introduced error is very small, only applies to integer numbers
203
+ * returned by embeddings, and most embeddings shall not have precision
204
+ * as high as 15 decimal places.
205
+ * @param array Array of number to be fixed.
206
+ * @returns
207
+ */
208
+ static fixArrayPrecision(array) {
209
+ return array.map((value) => {
210
+ if (Number.isInteger(value)) {
211
+ return value + 0.000000000000001;
212
+ }
213
+ return value;
214
+ });
215
+ }
196
216
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langchain",
3
- "version": "0.0.196",
3
+ "version": "0.0.197-rc.1",
4
4
  "description": "Typescript bindings for langchain",
5
5
  "type": "module",
6
6
  "engines": {
@@ -859,12 +859,15 @@
859
859
  "@faker-js/faker": "^7.6.0",
860
860
  "@getmetal/metal-sdk": "^4.0.0",
861
861
  "@getzep/zep-js": "^0.9.0",
862
- "@gomomento/sdk": "^1.47.1",
863
- "@gomomento/sdk-core": "^1.47.1",
862
+ "@gomomento/sdk": "^1.51.1",
863
+ "@gomomento/sdk-core": "^1.51.1",
864
864
  "@google-ai/generativelanguage": "^0.2.1",
865
865
  "@google-cloud/storage": "^6.10.1",
866
866
  "@huggingface/inference": "^2.6.4",
867
867
  "@jest/globals": "^29.5.0",
868
+ "@langchain/anthropic": "workspace:*",
869
+ "@langchain/core": "workspace:*",
870
+ "@langchain/openai": "workspace:*",
868
871
  "@mozilla/readability": "^0.4.4",
869
872
  "@notionhq/client": "^2.2.10",
870
873
  "@opensearch-project/opensearch": "^2.2.0",
@@ -942,7 +945,6 @@
942
945
  "jest": "^29.5.0",
943
946
  "jest-environment-node": "^29.6.4",
944
947
  "jsdom": "^22.1.0",
945
- "langchain-core": "workspace:*",
946
948
  "llmonitor": "^0.5.9",
947
949
  "lodash": "^4.17.21",
948
950
  "mammoth": "^1.5.1",
@@ -997,9 +999,9 @@
997
999
  "@elastic/elasticsearch": "^8.4.0",
998
1000
  "@getmetal/metal-sdk": "*",
999
1001
  "@getzep/zep-js": "^0.9.0",
1000
- "@gomomento/sdk": "^1.47.1",
1001
- "@gomomento/sdk-core": "^1.47.1",
1002
- "@gomomento/sdk-web": "^1.47.1",
1002
+ "@gomomento/sdk": "^1.51.1",
1003
+ "@gomomento/sdk-core": "^1.51.1",
1004
+ "@gomomento/sdk-web": "^1.51.1",
1003
1005
  "@google-ai/generativelanguage": "^0.2.1",
1004
1006
  "@google-cloud/storage": "^6.10.1",
1005
1007
  "@huggingface/inference": "^2.6.4",
@@ -1378,18 +1380,18 @@
1378
1380
  }
1379
1381
  },
1380
1382
  "dependencies": {
1381
- "@anthropic-ai/sdk": "^0.9.1",
1383
+ "@langchain/anthropic": "^0.0.3",
1384
+ "@langchain/core": "^0.0.1",
1385
+ "@langchain/openai": "^0.0.1",
1382
1386
  "binary-extensions": "^2.2.0",
1383
1387
  "expr-eval": "^2.0.2",
1384
1388
  "flat": "^5.0.2",
1385
1389
  "js-tiktoken": "^1.0.7",
1386
1390
  "js-yaml": "^4.1.0",
1387
1391
  "jsonpointer": "^5.0.1",
1388
- "langchain-core": "^0.0.1",
1389
1392
  "langchainhub": "~0.0.6",
1390
1393
  "langsmith": "~0.0.48",
1391
1394
  "ml-distance": "^4.0.0",
1392
- "openai": "^4.19.0",
1393
1395
  "openapi-types": "^12.1.3",
1394
1396
  "p-retry": "4",
1395
1397
  "uuid": "^9.0.0",
@@ -1,81 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.formatFunctionDefinitions = void 0;
4
- function isAnyOfProp(prop) {
5
- return (prop.anyOf !== undefined &&
6
- Array.isArray(prop.anyOf));
7
- }
8
- // When OpenAI use functions in the prompt, they format them as TypeScript definitions rather than OpenAPI JSON schemas.
9
- // This function converts the JSON schemas into TypeScript definitions.
10
- function formatFunctionDefinitions(functions) {
11
- const lines = ["namespace functions {", ""];
12
- for (const f of functions) {
13
- if (f.description) {
14
- lines.push(`// ${f.description}`);
15
- }
16
- if (Object.keys(f.parameters.properties ?? {}).length > 0) {
17
- lines.push(`type ${f.name} = (_: {`);
18
- lines.push(formatObjectProperties(f.parameters, 0));
19
- lines.push("}) => any;");
20
- }
21
- else {
22
- lines.push(`type ${f.name} = () => any;`);
23
- }
24
- lines.push("");
25
- }
26
- lines.push("} // namespace functions");
27
- return lines.join("\n");
28
- }
29
- exports.formatFunctionDefinitions = formatFunctionDefinitions;
30
- // Format just the properties of an object (not including the surrounding braces)
31
- function formatObjectProperties(obj, indent) {
32
- const lines = [];
33
- for (const [name, param] of Object.entries(obj.properties ?? {})) {
34
- if (param.description && indent < 2) {
35
- lines.push(`// ${param.description}`);
36
- }
37
- if (obj.required?.includes(name)) {
38
- lines.push(`${name}: ${formatType(param, indent)},`);
39
- }
40
- else {
41
- lines.push(`${name}?: ${formatType(param, indent)},`);
42
- }
43
- }
44
- return lines.map((line) => " ".repeat(indent) + line).join("\n");
45
- }
46
- // Format a single property type
47
- function formatType(param, indent) {
48
- if (isAnyOfProp(param)) {
49
- return param.anyOf.map((v) => formatType(v, indent)).join(" | ");
50
- }
51
- switch (param.type) {
52
- case "string":
53
- if (param.enum) {
54
- return param.enum.map((v) => `"${v}"`).join(" | ");
55
- }
56
- return "string";
57
- case "number":
58
- if (param.enum) {
59
- return param.enum.map((v) => `${v}`).join(" | ");
60
- }
61
- return "number";
62
- case "integer":
63
- if (param.enum) {
64
- return param.enum.map((v) => `${v}`).join(" | ");
65
- }
66
- return "number";
67
- case "boolean":
68
- return "boolean";
69
- case "null":
70
- return "null";
71
- case "object":
72
- return ["{", formatObjectProperties(param, indent + 2), "}"].join("\n");
73
- case "array":
74
- if (param.items) {
75
- return `${formatType(param.items, indent)}[]`;
76
- }
77
- return "any[]";
78
- default:
79
- return "";
80
- }
81
- }
@@ -1,44 +0,0 @@
1
- /**
2
- * Formatting function definitions for calculating openai function defination token usage.
3
- *
4
- * https://github.com/hmarr/openai-chat-tokens/blob/main/src/functions.ts
5
- * (c) 2023 Harry Marr
6
- * MIT license
7
- */
8
- import OpenAI from "openai";
9
- type OpenAIFunction = OpenAI.Chat.ChatCompletionCreateParams.Function;
10
- export interface FunctionDef extends Omit<OpenAIFunction, "parameters"> {
11
- name: string;
12
- description?: string;
13
- parameters: ObjectProp;
14
- }
15
- interface ObjectProp {
16
- type: "object";
17
- properties?: {
18
- [key: string]: Prop;
19
- };
20
- required?: string[];
21
- }
22
- interface AnyOfProp {
23
- anyOf: Prop[];
24
- }
25
- type Prop = {
26
- description?: string;
27
- } & (AnyOfProp | ObjectProp | {
28
- type: "string";
29
- enum?: string[];
30
- } | {
31
- type: "number" | "integer";
32
- minimum?: number;
33
- maximum?: number;
34
- enum?: number[];
35
- } | {
36
- type: "boolean";
37
- } | {
38
- type: "null";
39
- } | {
40
- type: "array";
41
- items?: Prop;
42
- });
43
- export declare function formatFunctionDefinitions(functions: FunctionDef[]): string;
44
- export {};
@@ -1,77 +0,0 @@
1
- function isAnyOfProp(prop) {
2
- return (prop.anyOf !== undefined &&
3
- Array.isArray(prop.anyOf));
4
- }
5
- // When OpenAI use functions in the prompt, they format them as TypeScript definitions rather than OpenAPI JSON schemas.
6
- // This function converts the JSON schemas into TypeScript definitions.
7
- export function formatFunctionDefinitions(functions) {
8
- const lines = ["namespace functions {", ""];
9
- for (const f of functions) {
10
- if (f.description) {
11
- lines.push(`// ${f.description}`);
12
- }
13
- if (Object.keys(f.parameters.properties ?? {}).length > 0) {
14
- lines.push(`type ${f.name} = (_: {`);
15
- lines.push(formatObjectProperties(f.parameters, 0));
16
- lines.push("}) => any;");
17
- }
18
- else {
19
- lines.push(`type ${f.name} = () => any;`);
20
- }
21
- lines.push("");
22
- }
23
- lines.push("} // namespace functions");
24
- return lines.join("\n");
25
- }
26
- // Format just the properties of an object (not including the surrounding braces)
27
- function formatObjectProperties(obj, indent) {
28
- const lines = [];
29
- for (const [name, param] of Object.entries(obj.properties ?? {})) {
30
- if (param.description && indent < 2) {
31
- lines.push(`// ${param.description}`);
32
- }
33
- if (obj.required?.includes(name)) {
34
- lines.push(`${name}: ${formatType(param, indent)},`);
35
- }
36
- else {
37
- lines.push(`${name}?: ${formatType(param, indent)},`);
38
- }
39
- }
40
- return lines.map((line) => " ".repeat(indent) + line).join("\n");
41
- }
42
- // Format a single property type
43
- function formatType(param, indent) {
44
- if (isAnyOfProp(param)) {
45
- return param.anyOf.map((v) => formatType(v, indent)).join(" | ");
46
- }
47
- switch (param.type) {
48
- case "string":
49
- if (param.enum) {
50
- return param.enum.map((v) => `"${v}"`).join(" | ");
51
- }
52
- return "string";
53
- case "number":
54
- if (param.enum) {
55
- return param.enum.map((v) => `${v}`).join(" | ");
56
- }
57
- return "number";
58
- case "integer":
59
- if (param.enum) {
60
- return param.enum.map((v) => `${v}`).join(" | ");
61
- }
62
- return "number";
63
- case "boolean":
64
- return "boolean";
65
- case "null":
66
- return "null";
67
- case "object":
68
- return ["{", formatObjectProperties(param, indent + 2), "}"].join("\n");
69
- case "array":
70
- if (param.items) {
71
- return `${formatType(param.items, indent)}[]`;
72
- }
73
- return "any[]";
74
- default:
75
- return "";
76
- }
77
- }