langchain 0.0.138 → 0.0.140

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 (55) hide show
  1. package/dist/agents/initialize.cjs +11 -0
  2. package/dist/agents/initialize.d.ts +4 -0
  3. package/dist/agents/initialize.js +11 -0
  4. package/dist/agents/xml/index.cjs +119 -0
  5. package/dist/agents/xml/index.d.ts +51 -0
  6. package/dist/agents/xml/index.js +114 -0
  7. package/dist/agents/xml/prompt.cjs +23 -0
  8. package/dist/agents/xml/prompt.d.ts +1 -0
  9. package/dist/agents/xml/prompt.js +20 -0
  10. package/dist/callbacks/base.d.ts +12 -4
  11. package/dist/callbacks/handlers/run_collector.cjs +50 -0
  12. package/dist/callbacks/handlers/run_collector.d.ts +26 -0
  13. package/dist/callbacks/handlers/run_collector.js +46 -0
  14. package/dist/callbacks/handlers/tracer.cjs +16 -3
  15. package/dist/callbacks/handlers/tracer.d.ts +6 -2
  16. package/dist/callbacks/handlers/tracer.js +16 -3
  17. package/dist/callbacks/handlers/tracer_langchain.cjs +1 -0
  18. package/dist/callbacks/handlers/tracer_langchain.d.ts +2 -1
  19. package/dist/callbacks/handlers/tracer_langchain.js +1 -0
  20. package/dist/callbacks/index.cjs +3 -1
  21. package/dist/callbacks/index.d.ts +1 -0
  22. package/dist/callbacks/index.js +1 -0
  23. package/dist/callbacks/manager.cjs +4 -4
  24. package/dist/callbacks/manager.d.ts +6 -2
  25. package/dist/callbacks/manager.js +4 -4
  26. package/dist/chains/openai_functions/extraction.cjs +2 -2
  27. package/dist/chains/openai_functions/extraction.d.ts +5 -4
  28. package/dist/chains/openai_functions/extraction.js +2 -2
  29. package/dist/chains/openai_functions/openapi.d.ts +2 -1
  30. package/dist/chains/openai_functions/structured_output.d.ts +4 -3
  31. package/dist/chains/openai_functions/tagging.cjs +2 -2
  32. package/dist/chains/openai_functions/tagging.d.ts +5 -4
  33. package/dist/chains/openai_functions/tagging.js +2 -2
  34. package/dist/chat_models/anthropic.cjs +7 -5
  35. package/dist/chat_models/anthropic.d.ts +17 -12
  36. package/dist/chat_models/anthropic.js +4 -2
  37. package/dist/experimental/chat_models/anthropic_functions.cjs +129 -0
  38. package/dist/experimental/chat_models/anthropic_functions.d.ts +20 -0
  39. package/dist/experimental/chat_models/anthropic_functions.js +125 -0
  40. package/dist/hub.d.ts +1 -1
  41. package/dist/llms/sagemaker_endpoint.cjs +1 -1
  42. package/dist/llms/sagemaker_endpoint.js +1 -1
  43. package/dist/load/import_constants.cjs +1 -0
  44. package/dist/load/import_constants.js +1 -0
  45. package/dist/schema/output_parser.cjs +1 -1
  46. package/dist/schema/output_parser.js +1 -1
  47. package/dist/schema/runnable.cjs +54 -15
  48. package/dist/schema/runnable.d.ts +9 -3
  49. package/dist/schema/runnable.js +55 -16
  50. package/dist/sql_db.cjs +3 -1
  51. package/dist/sql_db.js +3 -1
  52. package/experimental/chat_models/anthropic_functions.cjs +1 -0
  53. package/experimental/chat_models/anthropic_functions.d.ts +1 -0
  54. package/experimental/chat_models/anthropic_functions.js +1 -0
  55. package/package.json +17 -8
@@ -1,4 +1,10 @@
1
1
  import { BaseCallbackHandler, } from "../base.js";
2
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3
+ function _coerceToDict(value, defaultKey) {
4
+ return value && !Array.isArray(value) && typeof value === "object"
5
+ ? value
6
+ : { [defaultKey]: value };
7
+ }
2
8
  export class BaseTracer extends BaseCallbackHandler {
3
9
  constructor(_fields) {
4
10
  super(...arguments);
@@ -20,6 +26,7 @@ export class BaseTracer extends BaseCallbackHandler {
20
26
  const parentRun = this.runMap.get(run.parent_run_id);
21
27
  if (parentRun) {
22
28
  this._addChildRun(parentRun, run);
29
+ parentRun.child_execution_order = Math.max(parentRun.child_execution_order, run.child_execution_order);
23
30
  }
24
31
  }
25
32
  this.runMap.set(run.id, run);
@@ -154,21 +161,24 @@ export class BaseTracer extends BaseCallbackHandler {
154
161
  this._startTrace(run);
155
162
  await this.onChainStart?.(run);
156
163
  }
157
- async handleChainEnd(outputs, runId) {
164
+ async handleChainEnd(outputs, runId, _parentRunId, _tags, kwargs) {
158
165
  const run = this.runMap.get(runId);
159
166
  if (!run) {
160
167
  throw new Error("No chain run to end.");
161
168
  }
162
169
  run.end_time = Date.now();
163
- run.outputs = outputs;
170
+ run.outputs = _coerceToDict(outputs, "output");
164
171
  run.events.push({
165
172
  name: "end",
166
173
  time: new Date(run.end_time).toISOString(),
167
174
  });
175
+ if (kwargs?.inputs !== undefined) {
176
+ run.inputs = _coerceToDict(kwargs.inputs, "input");
177
+ }
168
178
  await this.onChainEnd?.(run);
169
179
  await this._endTrace(run);
170
180
  }
171
- async handleChainError(error, runId) {
181
+ async handleChainError(error, runId, _parentRunId, _tags, kwargs) {
172
182
  const run = this.runMap.get(runId);
173
183
  if (!run) {
174
184
  throw new Error("No chain run to end.");
@@ -179,6 +189,9 @@ export class BaseTracer extends BaseCallbackHandler {
179
189
  name: "error",
180
190
  time: new Date(run.end_time).toISOString(),
181
191
  });
192
+ if (kwargs?.inputs !== undefined) {
193
+ run.inputs = _coerceToDict(kwargs.inputs, "input");
194
+ }
182
195
  await this.onChainError?.(run);
183
196
  await this._endTrace(run);
184
197
  }
@@ -62,6 +62,7 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
62
62
  error: run.error,
63
63
  outputs: run.outputs,
64
64
  events: run.events,
65
+ inputs: run.inputs,
65
66
  };
66
67
  await this.client.updateRun(run.id, runUpdate);
67
68
  }
@@ -1,5 +1,5 @@
1
1
  import { Client } from "langsmith";
2
- import { BaseRun, RunUpdate as BaseRunUpdate } from "langsmith/schemas";
2
+ import { BaseRun, RunUpdate as BaseRunUpdate, KVMap } from "langsmith/schemas";
3
3
  import { BaseTracer } from "./tracer.js";
4
4
  import { BaseCallbackHandlerInput } from "../base.js";
5
5
  export interface Run extends BaseRun {
@@ -9,6 +9,7 @@ export interface Run extends BaseRun {
9
9
  }
10
10
  export interface RunUpdate extends BaseRunUpdate {
11
11
  events: BaseRun["events"];
12
+ inputs: KVMap;
12
13
  }
13
14
  export interface LangChainTracerFields extends BaseCallbackHandlerInput {
14
15
  exampleId?: string;
@@ -59,6 +59,7 @@ export class LangChainTracer extends BaseTracer {
59
59
  error: run.error,
60
60
  outputs: run.outputs,
61
61
  events: run.events,
62
+ inputs: run.inputs,
62
63
  };
63
64
  await this.client.updateRun(run.id, runUpdate);
64
65
  }
@@ -1,12 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.consumeCallback = exports.awaitAllCallbacks = exports.traceAsGroup = exports.TraceGroup = exports.CallbackManagerForToolRun = exports.CallbackManagerForLLMRun = exports.CallbackManagerForChainRun = exports.CallbackManagerForRetrieverRun = exports.CallbackManager = exports.getTracingV2CallbackHandler = exports.getTracingCallbackHandler = exports.LangChainTracerV1 = exports.LangChainTracer = exports.ConsoleCallbackHandler = exports.BaseTracer = exports.BaseCallbackHandler = void 0;
3
+ exports.consumeCallback = exports.awaitAllCallbacks = exports.traceAsGroup = exports.TraceGroup = exports.CallbackManagerForToolRun = exports.CallbackManagerForLLMRun = exports.CallbackManagerForChainRun = exports.CallbackManagerForRetrieverRun = exports.CallbackManager = exports.getTracingV2CallbackHandler = exports.getTracingCallbackHandler = exports.LangChainTracerV1 = exports.LangChainTracer = exports.RunCollectorCallbackHandler = exports.ConsoleCallbackHandler = exports.BaseTracer = exports.BaseCallbackHandler = void 0;
4
4
  var base_js_1 = require("./base.cjs");
5
5
  Object.defineProperty(exports, "BaseCallbackHandler", { enumerable: true, get: function () { return base_js_1.BaseCallbackHandler; } });
6
6
  var tracer_js_1 = require("./handlers/tracer.cjs");
7
7
  Object.defineProperty(exports, "BaseTracer", { enumerable: true, get: function () { return tracer_js_1.BaseTracer; } });
8
8
  var console_js_1 = require("./handlers/console.cjs");
9
9
  Object.defineProperty(exports, "ConsoleCallbackHandler", { enumerable: true, get: function () { return console_js_1.ConsoleCallbackHandler; } });
10
+ var run_collector_js_1 = require("./handlers/run_collector.cjs");
11
+ Object.defineProperty(exports, "RunCollectorCallbackHandler", { enumerable: true, get: function () { return run_collector_js_1.RunCollectorCallbackHandler; } });
10
12
  var tracer_langchain_js_1 = require("./handlers/tracer_langchain.cjs");
11
13
  Object.defineProperty(exports, "LangChainTracer", { enumerable: true, get: function () { return tracer_langchain_js_1.LangChainTracer; } });
12
14
  var tracer_langchain_v1_js_1 = require("./handlers/tracer_langchain_v1.cjs");
@@ -1,6 +1,7 @@
1
1
  export { BaseCallbackHandler, CallbackHandlerMethods, BaseCallbackHandlerInput, NewTokenIndices, } from "./base.js";
2
2
  export { Run, RunType, BaseTracer } from "./handlers/tracer.js";
3
3
  export { ConsoleCallbackHandler } from "./handlers/console.js";
4
+ export { RunCollectorCallbackHandler } from "./handlers/run_collector.js";
4
5
  export { LangChainTracer } from "./handlers/tracer_langchain.js";
5
6
  export { LangChainTracerV1 } from "./handlers/tracer_langchain_v1.js";
6
7
  export { getTracingCallbackHandler, getTracingV2CallbackHandler, } from "./handlers/initialize.js";
@@ -1,6 +1,7 @@
1
1
  export { BaseCallbackHandler, } from "./base.js";
2
2
  export { BaseTracer } from "./handlers/tracer.js";
3
3
  export { ConsoleCallbackHandler } from "./handlers/console.js";
4
+ export { RunCollectorCallbackHandler } from "./handlers/run_collector.js";
4
5
  export { LangChainTracer } from "./handlers/tracer_langchain.js";
5
6
  export { LangChainTracerV1 } from "./handlers/tracer_langchain_v1.js";
6
7
  export { getTracingCallbackHandler, getTracingV2CallbackHandler, } from "./handlers/initialize.js";
@@ -187,11 +187,11 @@ class CallbackManagerForChainRun extends BaseRunManager {
187
187
  }
188
188
  return manager;
189
189
  }
190
- async handleChainError(err) {
190
+ async handleChainError(err, _runId, _parentRunId, _tags, kwargs) {
191
191
  await Promise.all(this.handlers.map((handler) => (0, promises_js_1.consumeCallback)(async () => {
192
192
  if (!handler.ignoreChain) {
193
193
  try {
194
- await handler.handleChainError?.(err, this.runId, this._parentRunId, this.tags);
194
+ await handler.handleChainError?.(err, this.runId, this._parentRunId, this.tags, kwargs);
195
195
  }
196
196
  catch (err) {
197
197
  console.error(`Error in handler ${handler.constructor.name}, handleChainError: ${err}`);
@@ -199,11 +199,11 @@ class CallbackManagerForChainRun extends BaseRunManager {
199
199
  }
200
200
  }, handler.awaitHandlers)));
201
201
  }
202
- async handleChainEnd(output) {
202
+ async handleChainEnd(output, _runId, _parentRunId, _tags, kwargs) {
203
203
  await Promise.all(this.handlers.map((handler) => (0, promises_js_1.consumeCallback)(async () => {
204
204
  if (!handler.ignoreChain) {
205
205
  try {
206
- await handler.handleChainEnd?.(output, this.runId, this._parentRunId, this.tags);
206
+ await handler.handleChainEnd?.(output, this.runId, this._parentRunId, this.tags, kwargs);
207
207
  }
208
208
  catch (err) {
209
209
  console.error(`Error in handler ${handler.constructor.name}, handleChainEnd: ${err}`);
@@ -68,8 +68,12 @@ export declare class CallbackManagerForLLMRun extends BaseRunManager implements
68
68
  }
69
69
  export declare class CallbackManagerForChainRun extends BaseRunManager implements BaseCallbackManagerMethods {
70
70
  getChild(tag?: string): CallbackManager;
71
- handleChainError(err: Error | unknown): Promise<void>;
72
- handleChainEnd(output: ChainValues): Promise<void>;
71
+ handleChainError(err: Error | unknown, _runId?: string, _parentRunId?: string, _tags?: string[], kwargs?: {
72
+ inputs?: Record<string, unknown>;
73
+ }): Promise<void>;
74
+ handleChainEnd(output: ChainValues, _runId?: string, _parentRunId?: string, _tags?: string[], kwargs?: {
75
+ inputs?: Record<string, unknown>;
76
+ }): Promise<void>;
73
77
  handleAgentAction(action: AgentAction): Promise<void>;
74
78
  handleAgentEnd(action: AgentFinish): Promise<void>;
75
79
  }
@@ -180,11 +180,11 @@ export class CallbackManagerForChainRun extends BaseRunManager {
180
180
  }
181
181
  return manager;
182
182
  }
183
- async handleChainError(err) {
183
+ async handleChainError(err, _runId, _parentRunId, _tags, kwargs) {
184
184
  await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
185
185
  if (!handler.ignoreChain) {
186
186
  try {
187
- await handler.handleChainError?.(err, this.runId, this._parentRunId, this.tags);
187
+ await handler.handleChainError?.(err, this.runId, this._parentRunId, this.tags, kwargs);
188
188
  }
189
189
  catch (err) {
190
190
  console.error(`Error in handler ${handler.constructor.name}, handleChainError: ${err}`);
@@ -192,11 +192,11 @@ export class CallbackManagerForChainRun extends BaseRunManager {
192
192
  }
193
193
  }, handler.awaitHandlers)));
194
194
  }
195
- async handleChainEnd(output) {
195
+ async handleChainEnd(output, _runId, _parentRunId, _tags, kwargs) {
196
196
  await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
197
197
  if (!handler.ignoreChain) {
198
198
  try {
199
- await handler.handleChainEnd?.(output, this.runId, this._parentRunId, this.tags);
199
+ await handler.handleChainEnd?.(output, this.runId, this._parentRunId, this.tags, kwargs);
200
200
  }
201
201
  catch (err) {
202
202
  console.error(`Error in handler ${handler.constructor.name}, handleChainEnd: ${err}`);
@@ -42,7 +42,7 @@ Passage:
42
42
  * Function that creates an extraction chain using the provided JSON schema.
43
43
  * It sets up the necessary components, such as the prompt, output parser, and tags.
44
44
  * @param schema JSON schema of the function parameters.
45
- * @param llm Must be a ChatOpenAI model that supports function calling.
45
+ * @param llm Must be a ChatOpenAI or AnthropicFunctions model that supports function calling.
46
46
  * @returns A LLMChain instance configured to return data matching the schema.
47
47
  */
48
48
  function createExtractionChain(schema, llm) {
@@ -63,7 +63,7 @@ exports.createExtractionChain = createExtractionChain;
63
63
  * converts the Zod schema to a JSON schema using zod-to-json-schema
64
64
  * before creating the extraction chain.
65
65
  * @param schema The Zod schema which extracted data should match
66
- * @param llm Must be a ChatOpenAI model that supports function calling.
66
+ * @param llm Must be a ChatOpenAI or AnthropicFunctions model that supports function calling.
67
67
  * @returns A LLMChain instance configured to return data matching the schema.
68
68
  */
69
69
  function createExtractionChainFromZod(
@@ -2,20 +2,21 @@ import { z } from "zod";
2
2
  import { ChatOpenAI } from "../../chat_models/openai.js";
3
3
  import { FunctionParameters } from "../../output_parsers/openai_functions.js";
4
4
  import { LLMChain } from "../llm_chain.js";
5
+ import { AnthropicFunctions } from "../../experimental/chat_models/anthropic_functions.js";
5
6
  /**
6
7
  * Function that creates an extraction chain using the provided JSON schema.
7
8
  * It sets up the necessary components, such as the prompt, output parser, and tags.
8
9
  * @param schema JSON schema of the function parameters.
9
- * @param llm Must be a ChatOpenAI model that supports function calling.
10
+ * @param llm Must be a ChatOpenAI or AnthropicFunctions model that supports function calling.
10
11
  * @returns A LLMChain instance configured to return data matching the schema.
11
12
  */
12
- export declare function createExtractionChain(schema: FunctionParameters, llm: ChatOpenAI): LLMChain<object, ChatOpenAI>;
13
+ export declare function createExtractionChain(schema: FunctionParameters, llm: ChatOpenAI | AnthropicFunctions): LLMChain<object, ChatOpenAI | AnthropicFunctions>;
13
14
  /**
14
15
  * Function that creates an extraction chain from a Zod schema. It
15
16
  * converts the Zod schema to a JSON schema using zod-to-json-schema
16
17
  * before creating the extraction chain.
17
18
  * @param schema The Zod schema which extracted data should match
18
- * @param llm Must be a ChatOpenAI model that supports function calling.
19
+ * @param llm Must be a ChatOpenAI or AnthropicFunctions model that supports function calling.
19
20
  * @returns A LLMChain instance configured to return data matching the schema.
20
21
  */
21
- export declare function createExtractionChainFromZod(schema: z.ZodObject<any, any, any, any>, llm: ChatOpenAI): LLMChain<object, ChatOpenAI>;
22
+ export declare function createExtractionChainFromZod(schema: z.ZodObject<any, any, any, any>, llm: ChatOpenAI | AnthropicFunctions): LLMChain<object, ChatOpenAI | AnthropicFunctions>;
@@ -39,7 +39,7 @@ Passage:
39
39
  * Function that creates an extraction chain using the provided JSON schema.
40
40
  * It sets up the necessary components, such as the prompt, output parser, and tags.
41
41
  * @param schema JSON schema of the function parameters.
42
- * @param llm Must be a ChatOpenAI model that supports function calling.
42
+ * @param llm Must be a ChatOpenAI or AnthropicFunctions model that supports function calling.
43
43
  * @returns A LLMChain instance configured to return data matching the schema.
44
44
  */
45
45
  export function createExtractionChain(schema, llm) {
@@ -59,7 +59,7 @@ export function createExtractionChain(schema, llm) {
59
59
  * converts the Zod schema to a JSON schema using zod-to-json-schema
60
60
  * before creating the extraction chain.
61
61
  * @param schema The Zod schema which extracted data should match
62
- * @param llm Must be a ChatOpenAI model that supports function calling.
62
+ * @param llm Must be a ChatOpenAI or AnthropicFunctions model that supports function calling.
63
63
  * @returns A LLMChain instance configured to return data matching the schema.
64
64
  */
65
65
  export function createExtractionChainFromZod(
@@ -4,11 +4,12 @@ import { LLMChainInput } from "../llm_chain.js";
4
4
  import { ChatOpenAI } from "../../chat_models/openai.js";
5
5
  import { BasePromptTemplate } from "../../prompts/base.js";
6
6
  import { SequentialChain } from "../sequential_chain.js";
7
+ import { AnthropicFunctions } from "../../experimental/chat_models/anthropic_functions.js";
7
8
  /**
8
9
  * Type representing the options for creating an OpenAPI chain.
9
10
  */
10
11
  export type OpenAPIChainOptions = {
11
- llm?: ChatOpenAI;
12
+ llm?: ChatOpenAI | AnthropicFunctions;
12
13
  prompt?: BasePromptTemplate;
13
14
  requestChain?: BaseChain;
14
15
  llmChainInputs?: LLMChainInput;
@@ -7,6 +7,7 @@ import { BasePromptTemplate } from "../../prompts/index.js";
7
7
  import { BaseLLMOutputParser } from "../../schema/output_parser.js";
8
8
  import { OutputFunctionsParser } from "../../output_parsers/openai_functions.js";
9
9
  import { ChatGeneration } from "../../schema/index.js";
10
+ import { AnthropicFunctions } from "../../experimental/chat_models/anthropic_functions.js";
10
11
  /**
11
12
  * Type representing the input for creating a structured output chain. It
12
13
  * extends the LLMChainInput type and includes an additional
@@ -16,7 +17,7 @@ import { ChatGeneration } from "../../schema/index.js";
16
17
  export type StructuredOutputChainInput = Omit<LLMChainInput, "outputParser" | "llm"> & {
17
18
  outputSchema: JsonSchema7Type;
18
19
  prompt: BasePromptTemplate;
19
- llm?: ChatOpenAI;
20
+ llm?: ChatOpenAI | AnthropicFunctions;
20
21
  };
21
22
  /**
22
23
  * Class that extends the BaseLLMOutputParser class. It provides
@@ -44,5 +45,5 @@ export declare class FunctionCallStructuredOutputParser<T extends z.AnyZodObject
44
45
  * as well as an additional required "outputSchema" JSON Schema object.
45
46
  * @returns OpenAPIChain
46
47
  */
47
- export declare function createStructuredOutputChain<T extends z.AnyZodObject = z.AnyZodObject>(input: StructuredOutputChainInput): LLMChain<any, ChatOpenAI>;
48
- export declare function createStructuredOutputChainFromZod<T extends z.AnyZodObject>(zodSchema: T, input: Omit<StructuredOutputChainInput, "outputSchema">): LLMChain<any, ChatOpenAI>;
48
+ export declare function createStructuredOutputChain<T extends z.AnyZodObject = z.AnyZodObject>(input: StructuredOutputChainInput): LLMChain<any, ChatOpenAI | AnthropicFunctions>;
49
+ export declare function createStructuredOutputChainFromZod<T extends z.AnyZodObject>(zodSchema: T, input: Omit<StructuredOutputChainInput, "outputSchema">): LLMChain<any, ChatOpenAI | AnthropicFunctions>;
@@ -30,7 +30,7 @@ Passage:
30
30
  * LLM, and options. It constructs the LLM with the necessary
31
31
  * functions, prompt, output parser, and tags.
32
32
  * @param schema The schema defining the structure of function parameters.
33
- * @param llm LLM to use in the chain. Must support OpenAI function calling.
33
+ * @param llm LLM to use in the chain. Must support function calling.
34
34
  * @param options Options for creating the tagging chain.
35
35
  * @returns A new instance of LLMChain configured for tagging.
36
36
  */
@@ -53,7 +53,7 @@ exports.createTaggingChain = createTaggingChain;
53
53
  * the Zod schema to a JSON schema using the zodToJsonSchema function and
54
54
  * then calls createTaggingChain with the converted schema.
55
55
  * @param schema The Zod schema which extracted data should match.
56
- * @param llm LLM to use in the chain. Must support OpenAI function calling.
56
+ * @param llm LLM to use in the chain. Must support function calling.
57
57
  * @param options Options for creating the tagging chain.
58
58
  * @returns A new instance of LLMChain configured for tagging.
59
59
  */
@@ -3,6 +3,7 @@ import { ChatOpenAI } from "../../chat_models/openai.js";
3
3
  import { PromptTemplate } from "../../prompts/prompt.js";
4
4
  import { FunctionParameters } from "../../output_parsers/openai_functions.js";
5
5
  import { LLMChain, LLMChainInput } from "../llm_chain.js";
6
+ import { AnthropicFunctions } from "../../experimental/chat_models/anthropic_functions.js";
6
7
  /**
7
8
  * Type representing the options for creating a tagging chain.
8
9
  */
@@ -14,18 +15,18 @@ export type TaggingChainOptions = {
14
15
  * LLM, and options. It constructs the LLM with the necessary
15
16
  * functions, prompt, output parser, and tags.
16
17
  * @param schema The schema defining the structure of function parameters.
17
- * @param llm LLM to use in the chain. Must support OpenAI function calling.
18
+ * @param llm LLM to use in the chain. Must support function calling.
18
19
  * @param options Options for creating the tagging chain.
19
20
  * @returns A new instance of LLMChain configured for tagging.
20
21
  */
21
- export declare function createTaggingChain(schema: FunctionParameters, llm: ChatOpenAI, options?: TaggingChainOptions): LLMChain<object, ChatOpenAI>;
22
+ export declare function createTaggingChain(schema: FunctionParameters, llm: ChatOpenAI | AnthropicFunctions, options?: TaggingChainOptions): LLMChain<object, ChatOpenAI | AnthropicFunctions>;
22
23
  /**
23
24
  * Function that creates a tagging chain from a Zod schema. It converts
24
25
  * the Zod schema to a JSON schema using the zodToJsonSchema function and
25
26
  * then calls createTaggingChain with the converted schema.
26
27
  * @param schema The Zod schema which extracted data should match.
27
- * @param llm LLM to use in the chain. Must support OpenAI function calling.
28
+ * @param llm LLM to use in the chain. Must support function calling.
28
29
  * @param options Options for creating the tagging chain.
29
30
  * @returns A new instance of LLMChain configured for tagging.
30
31
  */
31
- export declare function createTaggingChainFromZod(schema: z.ZodObject<any, any, any, any>, llm: ChatOpenAI, options?: TaggingChainOptions): LLMChain<object, ChatOpenAI>;
32
+ export declare function createTaggingChainFromZod(schema: z.ZodObject<any, any, any, any>, llm: ChatOpenAI | AnthropicFunctions, options?: TaggingChainOptions): LLMChain<object, ChatOpenAI | AnthropicFunctions>;
@@ -27,7 +27,7 @@ Passage:
27
27
  * LLM, and options. It constructs the LLM with the necessary
28
28
  * functions, prompt, output parser, and tags.
29
29
  * @param schema The schema defining the structure of function parameters.
30
- * @param llm LLM to use in the chain. Must support OpenAI function calling.
30
+ * @param llm LLM to use in the chain. Must support function calling.
31
31
  * @param options Options for creating the tagging chain.
32
32
  * @returns A new instance of LLMChain configured for tagging.
33
33
  */
@@ -49,7 +49,7 @@ export function createTaggingChain(schema, llm, options = {}) {
49
49
  * the Zod schema to a JSON schema using the zodToJsonSchema function and
50
50
  * then calls createTaggingChain with the converted schema.
51
51
  * @param schema The Zod schema which extracted data should match.
52
- * @param llm LLM to use in the chain. Must support OpenAI function calling.
52
+ * @param llm LLM to use in the chain. Must support function calling.
53
53
  * @param options Options for creating the tagging chain.
54
54
  * @returns A new instance of LLMChain configured for tagging.
55
55
  */
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ChatAnthropic = void 0;
3
+ exports.ChatAnthropic = exports.DEFAULT_STOP_SEQUENCES = void 0;
4
4
  const sdk_1 = require("@anthropic-ai/sdk");
5
5
  const index_js_1 = require("../schema/index.cjs");
6
6
  const env_js_1 = require("../util/env.cjs");
@@ -41,7 +41,7 @@ function getAnthropicPromptFromMessage(message) {
41
41
  throw new Error(`Unknown message type: ${type}`);
42
42
  }
43
43
  }
44
- const DEFAULT_STOP_SEQUENCES = [sdk_1.HUMAN_PROMPT];
44
+ exports.DEFAULT_STOP_SEQUENCES = [sdk_1.HUMAN_PROMPT];
45
45
  /**
46
46
  * Wrapper around Anthropic large language models.
47
47
  *
@@ -117,7 +117,7 @@ class ChatAnthropic extends base_js_1.BaseChatModel {
117
117
  enumerable: true,
118
118
  configurable: true,
119
119
  writable: true,
120
- value: "claude-v1"
120
+ value: "claude-2"
121
121
  });
122
122
  Object.defineProperty(this, "invocationKwargs", {
123
123
  enumerable: true,
@@ -184,9 +184,9 @@ class ChatAnthropic extends base_js_1.BaseChatModel {
184
184
  temperature: this.temperature,
185
185
  top_k: this.topK,
186
186
  top_p: this.topP,
187
- stop_sequences: options?.stop?.concat(DEFAULT_STOP_SEQUENCES) ??
187
+ stop_sequences: options?.stop?.concat(exports.DEFAULT_STOP_SEQUENCES) ??
188
188
  this.stopSequences ??
189
- DEFAULT_STOP_SEQUENCES,
189
+ exports.DEFAULT_STOP_SEQUENCES,
190
190
  max_tokens_to_sample: this.maxTokensToSample,
191
191
  stream: this.streaming,
192
192
  ...this.invocationKwargs,
@@ -308,6 +308,7 @@ class ChatAnthropic extends base_js_1.BaseChatModel {
308
308
  ...this.clientOptions,
309
309
  ...options,
310
310
  apiKey: this.anthropicApiKey,
311
+ maxRetries: 0,
311
312
  });
312
313
  }
313
314
  const makeCompletionRequest = async () => this.streamingClient.completions.create({ ...request, stream: true }, { headers: request.headers });
@@ -324,6 +325,7 @@ class ChatAnthropic extends base_js_1.BaseChatModel {
324
325
  ...this.clientOptions,
325
326
  ...options,
326
327
  apiKey: this.anthropicApiKey,
328
+ maxRetries: 0,
327
329
  });
328
330
  }
329
331
  const makeCompletionRequest = async () => this.batchClient.completions.create({ ...request, stream: false }, { headers: request.headers });
@@ -1,8 +1,11 @@
1
1
  import { Anthropic, ClientOptions } from "@anthropic-ai/sdk";
2
2
  import type { CompletionCreateParams } from "@anthropic-ai/sdk/resources/completions";
3
+ import type { Stream } from "@anthropic-ai/sdk/streaming";
3
4
  import { CallbackManagerForLLMRun } from "../callbacks/manager.js";
4
5
  import { BaseMessage, ChatGenerationChunk, ChatResult } from "../schema/index.js";
5
6
  import { BaseChatModel, BaseChatModelParams } from "./base.js";
7
+ import { BaseLanguageModelCallOptions } from "../base_language/index.js";
8
+ export declare const DEFAULT_STOP_SEQUENCES: string[];
6
9
  /**
7
10
  * Input to AnthropicChat class.
8
11
  */
@@ -68,7 +71,7 @@ type Kwargs = Record<string, any>;
68
71
  * even if not explicitly available on this class.
69
72
  *
70
73
  */
71
- export declare class ChatAnthropic extends BaseChatModel implements AnthropicInput {
74
+ export declare class ChatAnthropic<CallOptions extends BaseLanguageModelCallOptions = BaseLanguageModelCallOptions> extends BaseChatModel<CallOptions> implements AnthropicInput {
72
75
  static lc_name(): string;
73
76
  get lc_secrets(): {
74
77
  [key: string]: string;
@@ -86,8 +89,8 @@ export declare class ChatAnthropic extends BaseChatModel implements AnthropicInp
86
89
  stopSequences?: string[];
87
90
  streaming: boolean;
88
91
  clientOptions: ClientOptions;
89
- private batchClient;
90
- private streamingClient;
92
+ protected batchClient: Anthropic;
93
+ protected streamingClient: Anthropic;
91
94
  constructor(fields?: Partial<AnthropicInput> & BaseChatModelParams);
92
95
  /**
93
96
  * Get the parameters used to invoke the model
@@ -95,28 +98,28 @@ export declare class ChatAnthropic extends BaseChatModel implements AnthropicInp
95
98
  invocationParams(options?: this["ParsedCallOptions"]): Omit<CompletionCreateParams, "prompt"> & Kwargs;
96
99
  /** @ignore */
97
100
  _identifyingParams(): {
98
- metadata?: Anthropic.Completions.CompletionCreateParams.CompletionRequestNonStreaming.Metadata | Anthropic.Completions.CompletionCreateParams.CompletionRequestStreaming.Metadata | undefined;
101
+ metadata?: Anthropic.Completions.CompletionCreateParams.Metadata | undefined;
99
102
  stream?: boolean | undefined;
100
- model: (string & {}) | "claude-2" | "claude-instant-1";
103
+ model: "claude-2" | (string & {}) | "claude-instant-1";
101
104
  temperature?: number | undefined;
102
105
  top_p?: number | undefined;
103
- top_k?: number | undefined;
104
106
  max_tokens_to_sample: number;
105
107
  stop_sequences?: string[] | undefined;
108
+ top_k?: number | undefined;
106
109
  model_name: string;
107
110
  };
108
111
  /**
109
112
  * Get the identifying parameters for the model
110
113
  */
111
114
  identifyingParams(): {
112
- metadata?: Anthropic.Completions.CompletionCreateParams.CompletionRequestNonStreaming.Metadata | Anthropic.Completions.CompletionCreateParams.CompletionRequestStreaming.Metadata | undefined;
115
+ metadata?: Anthropic.Completions.CompletionCreateParams.Metadata | undefined;
113
116
  stream?: boolean | undefined;
114
- model: (string & {}) | "claude-2" | "claude-instant-1";
117
+ model: "claude-2" | (string & {}) | "claude-instant-1";
115
118
  temperature?: number | undefined;
116
119
  top_p?: number | undefined;
117
- top_k?: number | undefined;
118
120
  max_tokens_to_sample: number;
119
121
  stop_sequences?: string[] | undefined;
122
+ top_k?: number | undefined;
120
123
  model_name: string;
121
124
  };
122
125
  _streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
@@ -125,7 +128,7 @@ export declare class ChatAnthropic extends BaseChatModel implements AnthropicInp
125
128
  * @param messages The base messages to format as a prompt.
126
129
  * @returns The formatted prompt.
127
130
  */
128
- private formatMessagesAsPrompt;
131
+ protected formatMessagesAsPrompt(messages: BaseMessage[]): string;
129
132
  /** @ignore */
130
133
  _generate(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
131
134
  /**
@@ -133,9 +136,11 @@ export declare class ChatAnthropic extends BaseChatModel implements AnthropicInp
133
136
  * @param request The parameters for creating a completion.
134
137
  * @returns A streaming request.
135
138
  */
136
- private createStreamWithRetry;
139
+ protected createStreamWithRetry(request: CompletionCreateParams & Kwargs): Promise<Stream<Anthropic.Completions.Completion>>;
137
140
  /** @ignore */
138
- private completionWithRetry;
141
+ protected completionWithRetry(request: CompletionCreateParams & Kwargs, options: {
142
+ signal?: AbortSignal;
143
+ }): Promise<Anthropic.Completions.Completion>;
139
144
  _llmType(): string;
140
145
  /** @ignore */
141
146
  _combineLLMOutput(): never[];
@@ -38,7 +38,7 @@ function getAnthropicPromptFromMessage(message) {
38
38
  throw new Error(`Unknown message type: ${type}`);
39
39
  }
40
40
  }
41
- const DEFAULT_STOP_SEQUENCES = [HUMAN_PROMPT];
41
+ export const DEFAULT_STOP_SEQUENCES = [HUMAN_PROMPT];
42
42
  /**
43
43
  * Wrapper around Anthropic large language models.
44
44
  *
@@ -114,7 +114,7 @@ export class ChatAnthropic extends BaseChatModel {
114
114
  enumerable: true,
115
115
  configurable: true,
116
116
  writable: true,
117
- value: "claude-v1"
117
+ value: "claude-2"
118
118
  });
119
119
  Object.defineProperty(this, "invocationKwargs", {
120
120
  enumerable: true,
@@ -305,6 +305,7 @@ export class ChatAnthropic extends BaseChatModel {
305
305
  ...this.clientOptions,
306
306
  ...options,
307
307
  apiKey: this.anthropicApiKey,
308
+ maxRetries: 0,
308
309
  });
309
310
  }
310
311
  const makeCompletionRequest = async () => this.streamingClient.completions.create({ ...request, stream: true }, { headers: request.headers });
@@ -321,6 +322,7 @@ export class ChatAnthropic extends BaseChatModel {
321
322
  ...this.clientOptions,
322
323
  ...options,
323
324
  apiKey: this.anthropicApiKey,
325
+ maxRetries: 0,
324
326
  });
325
327
  }
326
328
  const makeCompletionRequest = async () => this.batchClient.completions.create({ ...request, stream: false }, { headers: request.headers });