langchain 0.0.108 → 0.0.110

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 (50) hide show
  1. package/chat_models/googlepalm.cjs +1 -0
  2. package/chat_models/googlepalm.d.ts +1 -0
  3. package/chat_models/googlepalm.js +1 -0
  4. package/dist/callbacks/handlers/tracer.d.ts +1 -1
  5. package/dist/callbacks/handlers/tracer_langchain.cjs +2 -2
  6. package/dist/callbacks/handlers/tracer_langchain.d.ts +2 -2
  7. package/dist/callbacks/handlers/tracer_langchain.js +1 -1
  8. package/dist/chat_models/anthropic.cjs +34 -8
  9. package/dist/chat_models/anthropic.d.ts +2 -3
  10. package/dist/chat_models/anthropic.js +11 -8
  11. package/dist/chat_models/googlepalm.cjs +167 -0
  12. package/dist/chat_models/googlepalm.d.ts +72 -0
  13. package/dist/chat_models/googlepalm.js +163 -0
  14. package/dist/chat_models/openai.cjs +3 -0
  15. package/dist/chat_models/openai.js +3 -0
  16. package/dist/client/index.cjs +2 -2
  17. package/dist/client/index.d.ts +1 -1
  18. package/dist/client/index.js +1 -1
  19. package/dist/client/{langchainplus.cjs → langsmith.cjs} +3 -3
  20. package/dist/client/{langchainplus.d.ts → langsmith.d.ts} +1 -1
  21. package/dist/client/{langchainplus.js → langsmith.js} +2 -2
  22. package/dist/embeddings/googlepalm.cjs +55 -0
  23. package/dist/embeddings/googlepalm.d.ts +22 -0
  24. package/dist/embeddings/googlepalm.js +51 -0
  25. package/dist/evaluation/run_evaluators/base.d.ts +2 -2
  26. package/dist/evaluation/run_evaluators/implementations.d.ts +2 -2
  27. package/dist/llms/googlepalm.cjs +133 -0
  28. package/dist/llms/googlepalm.d.ts +92 -0
  29. package/dist/llms/googlepalm.js +129 -0
  30. package/dist/load/import_constants.cjs +3 -0
  31. package/dist/load/import_constants.js +3 -0
  32. package/dist/text_splitter.cjs +11 -4
  33. package/dist/text_splitter.d.ts +2 -0
  34. package/dist/text_splitter.js +11 -4
  35. package/dist/tools/dataforseo_api_search.cjs +279 -0
  36. package/dist/tools/dataforseo_api_search.d.ts +160 -0
  37. package/dist/tools/dataforseo_api_search.js +275 -0
  38. package/dist/tools/index.cjs +5 -1
  39. package/dist/tools/index.d.ts +2 -0
  40. package/dist/tools/index.js +2 -0
  41. package/dist/tools/searxng_search.cjs +129 -0
  42. package/dist/tools/searxng_search.d.ts +84 -0
  43. package/dist/tools/searxng_search.js +125 -0
  44. package/embeddings/googlepalm.cjs +1 -0
  45. package/embeddings/googlepalm.d.ts +1 -0
  46. package/embeddings/googlepalm.js +1 -0
  47. package/llms/googlepalm.cjs +1 -0
  48. package/llms/googlepalm.d.ts +1 -0
  49. package/llms/googlepalm.js +1 -0
  50. package/package.json +33 -4
@@ -0,0 +1 @@
1
+ module.exports = require('../dist/chat_models/googlepalm.cjs');
@@ -0,0 +1 @@
1
+ export * from '../dist/chat_models/googlepalm.js'
@@ -0,0 +1 @@
1
+ export * from '../dist/chat_models/googlepalm.js'
@@ -1,4 +1,4 @@
1
- import { KVMap, BaseRun } from "langchainplus-sdk/schemas";
1
+ import { KVMap, BaseRun } from "langsmith/schemas";
2
2
  import { AgentAction, AgentFinish, BaseMessage, ChainValues, LLMResult } from "../../schema/index.js";
3
3
  import { Serialized } from "../../load/serializable.js";
4
4
  import { BaseCallbackHandler, BaseCallbackHandlerInput, NewTokenIndices } from "../base.js";
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LangChainTracer = void 0;
4
- const langchainplus_sdk_1 = require("langchainplus-sdk");
4
+ const langsmith_1 = require("langsmith");
5
5
  const env_js_1 = require("../../util/env.cjs");
6
6
  const tracer_js_1 = require("./tracer.cjs");
7
7
  class LangChainTracer extends tracer_js_1.BaseTracer {
@@ -37,7 +37,7 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
37
37
  (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_PROJECT") ??
38
38
  (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_SESSION");
39
39
  this.exampleId = exampleId;
40
- this.client = client ?? new langchainplus_sdk_1.Client({});
40
+ this.client = client ?? new langsmith_1.Client({});
41
41
  }
42
42
  async _convertToCreate(run, example_id = undefined) {
43
43
  return {
@@ -1,5 +1,5 @@
1
- import { Client } from "langchainplus-sdk";
2
- import { BaseRun, RunUpdate as BaseRunUpdate } from "langchainplus-sdk/schemas";
1
+ import { Client } from "langsmith";
2
+ import { BaseRun, RunUpdate as BaseRunUpdate } from "langsmith/schemas";
3
3
  import { BaseTracer } from "./tracer.js";
4
4
  import { BaseCallbackHandlerInput } from "../base.js";
5
5
  export interface Run extends BaseRun {
@@ -1,4 +1,4 @@
1
- import { Client } from "langchainplus-sdk";
1
+ import { Client } from "langsmith";
2
2
  import { getEnvironmentVariable, getRuntimeEnvironment, } from "../../util/env.js";
3
3
  import { BaseTracer } from "./tracer.js";
4
4
  export class LangChainTracer extends BaseTracer {
@@ -1,23 +1,49 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
26
  exports.ChatAnthropic = void 0;
4
- const sdk_1 = require("@anthropic-ai/sdk");
27
+ const AnthropicApi = __importStar(require("@anthropic-ai/sdk"));
5
28
  const index_js_1 = require("../schema/index.cjs");
6
29
  const env_js_1 = require("../util/env.cjs");
7
30
  const base_js_1 = require("./base.cjs");
31
+ // Anthropic's 0.5.3 SDK currently has a collision with the default exported class
32
+ // and an exported namespace that causes issues when transpiling to CommonJS
33
+ const AnthropicClientConstructor = AnthropicApi.Anthropic ?? AnthropicApi.default;
8
34
  function getAnthropicPromptFromMessage(type) {
9
35
  switch (type) {
10
36
  case "ai":
11
- return sdk_1.AI_PROMPT;
37
+ return AnthropicApi.AI_PROMPT;
12
38
  case "human":
13
- return sdk_1.HUMAN_PROMPT;
39
+ return AnthropicApi.HUMAN_PROMPT;
14
40
  case "system":
15
41
  return "";
16
42
  default:
17
43
  throw new Error(`Unknown message type: ${type}`);
18
44
  }
19
45
  }
20
- const DEFAULT_STOP_SEQUENCES = [sdk_1.HUMAN_PROMPT];
46
+ const DEFAULT_STOP_SEQUENCES = [AnthropicApi.HUMAN_PROMPT];
21
47
  /**
22
48
  * Wrapper around Anthropic large language models.
23
49
  *
@@ -180,7 +206,7 @@ class ChatAnthropic extends base_js_1.BaseChatModel {
180
206
  const messagePrompt = getAnthropicPromptFromMessage(message._getType());
181
207
  return `${messagePrompt} ${message.content}`;
182
208
  })
183
- .join("") + sdk_1.AI_PROMPT);
209
+ .join("") + AnthropicApi.AI_PROMPT);
184
210
  }
185
211
  /** @ignore */
186
212
  async _generate(messages, options, runManager) {
@@ -193,7 +219,7 @@ class ChatAnthropic extends base_js_1.BaseChatModel {
193
219
  prompt: this.formatMessagesAsPrompt(messages),
194
220
  }, { signal: options.signal }, runManager);
195
221
  const generations = response.completion
196
- .split(sdk_1.AI_PROMPT)
222
+ .split(AnthropicApi.AI_PROMPT)
197
223
  .map((message) => ({
198
224
  text: message,
199
225
  message: new index_js_1.AIMessage(message),
@@ -212,7 +238,7 @@ class ChatAnthropic extends base_js_1.BaseChatModel {
212
238
  if (request.stream) {
213
239
  if (!this.streamingClient) {
214
240
  const options = this.apiUrl ? { apiUrl: this.apiUrl } : undefined;
215
- this.streamingClient = new sdk_1.Anthropic({
241
+ this.streamingClient = new AnthropicClientConstructor({
216
242
  ...options,
217
243
  apiKey: this.anthropicApiKey,
218
244
  });
@@ -249,7 +275,7 @@ class ChatAnthropic extends base_js_1.BaseChatModel {
249
275
  else {
250
276
  if (!this.batchClient) {
251
277
  const options = this.apiUrl ? { apiUrl: this.apiUrl } : undefined;
252
- this.batchClient = new sdk_1.Anthropic({
278
+ this.batchClient = new AnthropicClientConstructor({
253
279
  ...options,
254
280
  apiKey: this.anthropicApiKey,
255
281
  });
@@ -1,4 +1,3 @@
1
- import { Anthropic as AnthropicApi } from "@anthropic-ai/sdk";
2
1
  import type { CompletionCreateParams } from "@anthropic-ai/sdk/resources/completions";
3
2
  import { BaseLanguageModelCallOptions } from "../base_language/index.js";
4
3
  import { CallbackManagerForLLMRun } from "../callbacks/manager.js";
@@ -89,7 +88,7 @@ export declare class ChatAnthropic extends BaseChatModel implements AnthropicInp
89
88
  invocationParams(options?: this["ParsedCallOptions"]): Omit<CompletionCreateParams, "prompt"> & Kwargs;
90
89
  /** @ignore */
91
90
  _identifyingParams(): {
92
- metadata?: AnthropicApi.Completions.CompletionCreateParams.CompletionRequestNonStreaming.Metadata | AnthropicApi.Completions.CompletionCreateParams.CompletionRequestStreaming.Metadata | undefined;
91
+ metadata?: CompletionCreateParams.CompletionRequestNonStreaming.Metadata | CompletionCreateParams.CompletionRequestStreaming.Metadata | undefined;
93
92
  stream?: boolean | undefined;
94
93
  model: string;
95
94
  temperature?: number | undefined;
@@ -103,7 +102,7 @@ export declare class ChatAnthropic extends BaseChatModel implements AnthropicInp
103
102
  * Get the identifying parameters for the model
104
103
  */
105
104
  identifyingParams(): {
106
- metadata?: AnthropicApi.Completions.CompletionCreateParams.CompletionRequestNonStreaming.Metadata | AnthropicApi.Completions.CompletionCreateParams.CompletionRequestStreaming.Metadata | undefined;
105
+ metadata?: CompletionCreateParams.CompletionRequestNonStreaming.Metadata | CompletionCreateParams.CompletionRequestStreaming.Metadata | undefined;
107
106
  stream?: boolean | undefined;
108
107
  model: string;
109
108
  temperature?: number | undefined;
@@ -1,20 +1,23 @@
1
- import { AI_PROMPT, Anthropic as AnthropicApi, HUMAN_PROMPT, } from "@anthropic-ai/sdk";
1
+ import * as AnthropicApi from "@anthropic-ai/sdk";
2
2
  import { AIMessage, } from "../schema/index.js";
3
3
  import { getEnvironmentVariable } from "../util/env.js";
4
4
  import { BaseChatModel } from "./base.js";
5
+ // Anthropic's 0.5.3 SDK currently has a collision with the default exported class
6
+ // and an exported namespace that causes issues when transpiling to CommonJS
7
+ const AnthropicClientConstructor = AnthropicApi.Anthropic ?? AnthropicApi.default;
5
8
  function getAnthropicPromptFromMessage(type) {
6
9
  switch (type) {
7
10
  case "ai":
8
- return AI_PROMPT;
11
+ return AnthropicApi.AI_PROMPT;
9
12
  case "human":
10
- return HUMAN_PROMPT;
13
+ return AnthropicApi.HUMAN_PROMPT;
11
14
  case "system":
12
15
  return "";
13
16
  default:
14
17
  throw new Error(`Unknown message type: ${type}`);
15
18
  }
16
19
  }
17
- const DEFAULT_STOP_SEQUENCES = [HUMAN_PROMPT];
20
+ const DEFAULT_STOP_SEQUENCES = [AnthropicApi.HUMAN_PROMPT];
18
21
  /**
19
22
  * Wrapper around Anthropic large language models.
20
23
  *
@@ -177,7 +180,7 @@ export class ChatAnthropic extends BaseChatModel {
177
180
  const messagePrompt = getAnthropicPromptFromMessage(message._getType());
178
181
  return `${messagePrompt} ${message.content}`;
179
182
  })
180
- .join("") + AI_PROMPT);
183
+ .join("") + AnthropicApi.AI_PROMPT);
181
184
  }
182
185
  /** @ignore */
183
186
  async _generate(messages, options, runManager) {
@@ -190,7 +193,7 @@ export class ChatAnthropic extends BaseChatModel {
190
193
  prompt: this.formatMessagesAsPrompt(messages),
191
194
  }, { signal: options.signal }, runManager);
192
195
  const generations = response.completion
193
- .split(AI_PROMPT)
196
+ .split(AnthropicApi.AI_PROMPT)
194
197
  .map((message) => ({
195
198
  text: message,
196
199
  message: new AIMessage(message),
@@ -209,7 +212,7 @@ export class ChatAnthropic extends BaseChatModel {
209
212
  if (request.stream) {
210
213
  if (!this.streamingClient) {
211
214
  const options = this.apiUrl ? { apiUrl: this.apiUrl } : undefined;
212
- this.streamingClient = new AnthropicApi({
215
+ this.streamingClient = new AnthropicClientConstructor({
213
216
  ...options,
214
217
  apiKey: this.anthropicApiKey,
215
218
  });
@@ -246,7 +249,7 @@ export class ChatAnthropic extends BaseChatModel {
246
249
  else {
247
250
  if (!this.batchClient) {
248
251
  const options = this.apiUrl ? { apiUrl: this.apiUrl } : undefined;
249
- this.batchClient = new AnthropicApi({
252
+ this.batchClient = new AnthropicClientConstructor({
250
253
  ...options,
251
254
  apiKey: this.anthropicApiKey,
252
255
  });
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ChatGooglePaLM = void 0;
4
+ const generativelanguage_1 = require("@google-ai/generativelanguage");
5
+ const google_auth_library_1 = require("google-auth-library");
6
+ const index_js_1 = require("../schema/index.cjs");
7
+ const env_js_1 = require("../util/env.cjs");
8
+ const base_js_1 = require("./base.cjs");
9
+ class ChatGooglePaLM extends base_js_1.BaseChatModel {
10
+ get lc_secrets() {
11
+ return {
12
+ apiKey: "GOOGLE_PALM_API_KEY",
13
+ };
14
+ }
15
+ constructor(fields) {
16
+ super(fields ?? {});
17
+ Object.defineProperty(this, "modelName", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: "models/chat-bison-001"
22
+ });
23
+ Object.defineProperty(this, "temperature", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: void 0
28
+ }); // default value chosen based on model
29
+ Object.defineProperty(this, "topP", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: void 0
34
+ }); // default value chosen based on model
35
+ Object.defineProperty(this, "topK", {
36
+ enumerable: true,
37
+ configurable: true,
38
+ writable: true,
39
+ value: void 0
40
+ }); // default value chosen based on model
41
+ Object.defineProperty(this, "examples", {
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true,
45
+ value: []
46
+ });
47
+ Object.defineProperty(this, "apiKey", {
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true,
51
+ value: void 0
52
+ });
53
+ Object.defineProperty(this, "client", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: void 0
58
+ });
59
+ this.modelName = fields?.modelName ?? this.modelName;
60
+ this.temperature = fields?.temperature ?? this.temperature;
61
+ if (this.temperature && (this.temperature < 0 || this.temperature > 1)) {
62
+ throw new Error("`temperature` must be in the range of [0.0,1.0]");
63
+ }
64
+ this.topP = fields?.topP ?? this.topP;
65
+ if (this.topP && this.topP < 0) {
66
+ throw new Error("`topP` must be a positive integer");
67
+ }
68
+ this.topK = fields?.topK ?? this.topK;
69
+ if (this.topK && this.topK < 0) {
70
+ throw new Error("`topK` must be a positive integer");
71
+ }
72
+ this.examples = fields?.examples ?? this.examples;
73
+ this.apiKey =
74
+ fields?.apiKey ?? (0, env_js_1.getEnvironmentVariable)("GOOGLE_PALM_API_KEY");
75
+ if (!this.apiKey) {
76
+ throw new Error("Please set an API key for Google Palm 2 in the environment variable GOOGLE_PALM_API_KEY or in the `apiKey` field of the GooglePalm constructor");
77
+ }
78
+ this.client = new generativelanguage_1.DiscussServiceClient({
79
+ authClient: new google_auth_library_1.GoogleAuth().fromAPIKey(this.apiKey),
80
+ });
81
+ }
82
+ _combineLLMOutput() {
83
+ return [];
84
+ }
85
+ _llmType() {
86
+ return "googlepalm";
87
+ }
88
+ async _generate(messages, options, runManager) {
89
+ const palmMessages = await this.caller.callWithOptions({ signal: options.signal }, this._generateMessage.bind(this), this._mapBaseMessagesToPalmMessages(messages), this._getPalmContextInstruction(messages), this.examples);
90
+ const chatResult = this._mapPalmMessagesToChatResult(palmMessages);
91
+ // Google Palm doesn't provide streaming as of now. But to support streaming handlers
92
+ // we call the handler with entire response text
93
+ void runManager?.handleLLMNewToken(chatResult.generations.length > 0 ? chatResult.generations[0].text : "");
94
+ return chatResult;
95
+ }
96
+ async _generateMessage(messages, context, examples) {
97
+ const [palmMessages] = await this.client.generateMessage({
98
+ candidateCount: 1,
99
+ model: this.modelName,
100
+ temperature: this.temperature,
101
+ topK: this.topK,
102
+ topP: this.topP,
103
+ prompt: {
104
+ context,
105
+ examples,
106
+ messages,
107
+ },
108
+ });
109
+ return palmMessages;
110
+ }
111
+ _getPalmContextInstruction(messages) {
112
+ // get the first message and checks if it's a system 'system' messages
113
+ const systemMessage = messages.length > 0 && messages[0]._getType() === "system"
114
+ ? messages[0]
115
+ : undefined;
116
+ return systemMessage?.content;
117
+ }
118
+ _mapBaseMessagesToPalmMessages(messages) {
119
+ // remove all 'system' messages
120
+ const nonSystemMessages = messages.filter((m) => m._getType() !== "system");
121
+ // requires alternate human & ai messages. Throw error if two messages are consecutive
122
+ nonSystemMessages.forEach((msg, index) => {
123
+ if (index < 1)
124
+ return;
125
+ if (msg._getType() === nonSystemMessages[index - 1]._getType()) {
126
+ throw new Error(`Google PaLM requires alternate messages between authors`);
127
+ }
128
+ });
129
+ return nonSystemMessages.map((m) => ({
130
+ author: m.name ?? m._getType(),
131
+ content: m.content,
132
+ citationMetadata: {
133
+ citationSources: m.additional_kwargs.citationSources,
134
+ },
135
+ }));
136
+ }
137
+ _mapPalmMessagesToChatResult(msgRes) {
138
+ if (msgRes.candidates &&
139
+ msgRes.candidates.length > 0 &&
140
+ msgRes.candidates[0]) {
141
+ const message = msgRes.candidates[0];
142
+ return {
143
+ generations: [
144
+ {
145
+ text: message.content ?? "",
146
+ message: new index_js_1.AIMessage({
147
+ content: message.content ?? "",
148
+ name: message.author === null ? undefined : message.author,
149
+ additional_kwargs: {
150
+ citationSources: message.citationMetadata?.citationSources,
151
+ filters: msgRes.filters, // content filters applied
152
+ },
153
+ }),
154
+ },
155
+ ],
156
+ };
157
+ }
158
+ // if rejected or error, return empty generations with reason in filters
159
+ return {
160
+ generations: [],
161
+ llmOutput: {
162
+ filters: msgRes.filters,
163
+ },
164
+ };
165
+ }
166
+ }
167
+ exports.ChatGooglePaLM = ChatGooglePaLM;
@@ -0,0 +1,72 @@
1
+ import type { protos } from "@google-ai/generativelanguage";
2
+ import { CallbackManagerForLLMRun } from "../callbacks/manager.js";
3
+ import { BaseMessage, ChatResult } from "../schema/index.js";
4
+ import { BaseChatModel, BaseChatModelParams } from "./base.js";
5
+ export interface GooglePaLMChatInput extends BaseChatModelParams {
6
+ /**
7
+ * Model Name to use
8
+ *
9
+ * Note: The format must follow the pattern - `models/{model}`
10
+ */
11
+ modelName?: string;
12
+ /**
13
+ * Controls the randomness of the output.
14
+ *
15
+ * Values can range from [0.0,1.0], inclusive. A value closer to 1.0
16
+ * will produce responses that are more varied and creative, while
17
+ * a value closer to 0.0 will typically result in less surprising
18
+ * responses from the model.
19
+ *
20
+ * Note: The default value varies by model
21
+ */
22
+ temperature?: number;
23
+ /**
24
+ * Top-p changes how the model selects tokens for output.
25
+ *
26
+ * Tokens are selected from most probable to least until the sum
27
+ * of their probabilities equals the top-p value.
28
+ *
29
+ * For example, if tokens A, B, and C have a probability of
30
+ * .3, .2, and .1 and the top-p value is .5, then the model will
31
+ * select either A or B as the next token (using temperature).
32
+ *
33
+ * Note: The default value varies by model
34
+ */
35
+ topP?: number;
36
+ /**
37
+ * Top-k changes how the model selects tokens for output.
38
+ *
39
+ * A top-k of 1 means the selected token is the most probable among
40
+ * all tokens in the model’s vocabulary (also called greedy decoding),
41
+ * while a top-k of 3 means that the next token is selected from
42
+ * among the 3 most probable tokens (using temperature).
43
+ *
44
+ * Note: The default value varies by model
45
+ */
46
+ topK?: number;
47
+ examples?: protos.google.ai.generativelanguage.v1beta2.IExample[];
48
+ /**
49
+ * Google Palm API key to use
50
+ */
51
+ apiKey?: string;
52
+ }
53
+ export declare class ChatGooglePaLM extends BaseChatModel implements GooglePaLMChatInput {
54
+ get lc_secrets(): {
55
+ [key: string]: string;
56
+ } | undefined;
57
+ modelName: string;
58
+ temperature?: number;
59
+ topP?: number;
60
+ topK?: number;
61
+ examples: protos.google.ai.generativelanguage.v1beta2.IExample[];
62
+ apiKey?: string;
63
+ private client;
64
+ constructor(fields?: GooglePaLMChatInput);
65
+ _combineLLMOutput(): never[];
66
+ _llmType(): string;
67
+ _generate(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
68
+ protected _generateMessage(messages: protos.google.ai.generativelanguage.v1beta2.IMessage[], context?: string, examples?: protos.google.ai.generativelanguage.v1beta2.IExample[]): Promise<protos.google.ai.generativelanguage.v1beta2.IGenerateMessageResponse>;
69
+ protected _getPalmContextInstruction(messages: BaseMessage[]): string | undefined;
70
+ protected _mapBaseMessagesToPalmMessages(messages: BaseMessage[]): protos.google.ai.generativelanguage.v1beta2.IMessage[];
71
+ protected _mapPalmMessagesToChatResult(msgRes: protos.google.ai.generativelanguage.v1beta2.IGenerateMessageResponse): ChatResult;
72
+ }
@@ -0,0 +1,163 @@
1
+ import { DiscussServiceClient } from "@google-ai/generativelanguage";
2
+ import { GoogleAuth } from "google-auth-library";
3
+ import { AIMessage } from "../schema/index.js";
4
+ import { getEnvironmentVariable } from "../util/env.js";
5
+ import { BaseChatModel } from "./base.js";
6
+ export class ChatGooglePaLM extends BaseChatModel {
7
+ get lc_secrets() {
8
+ return {
9
+ apiKey: "GOOGLE_PALM_API_KEY",
10
+ };
11
+ }
12
+ constructor(fields) {
13
+ super(fields ?? {});
14
+ Object.defineProperty(this, "modelName", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: "models/chat-bison-001"
19
+ });
20
+ Object.defineProperty(this, "temperature", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: void 0
25
+ }); // default value chosen based on model
26
+ Object.defineProperty(this, "topP", {
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true,
30
+ value: void 0
31
+ }); // default value chosen based on model
32
+ Object.defineProperty(this, "topK", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: void 0
37
+ }); // default value chosen based on model
38
+ Object.defineProperty(this, "examples", {
39
+ enumerable: true,
40
+ configurable: true,
41
+ writable: true,
42
+ value: []
43
+ });
44
+ Object.defineProperty(this, "apiKey", {
45
+ enumerable: true,
46
+ configurable: true,
47
+ writable: true,
48
+ value: void 0
49
+ });
50
+ Object.defineProperty(this, "client", {
51
+ enumerable: true,
52
+ configurable: true,
53
+ writable: true,
54
+ value: void 0
55
+ });
56
+ this.modelName = fields?.modelName ?? this.modelName;
57
+ this.temperature = fields?.temperature ?? this.temperature;
58
+ if (this.temperature && (this.temperature < 0 || this.temperature > 1)) {
59
+ throw new Error("`temperature` must be in the range of [0.0,1.0]");
60
+ }
61
+ this.topP = fields?.topP ?? this.topP;
62
+ if (this.topP && this.topP < 0) {
63
+ throw new Error("`topP` must be a positive integer");
64
+ }
65
+ this.topK = fields?.topK ?? this.topK;
66
+ if (this.topK && this.topK < 0) {
67
+ throw new Error("`topK` must be a positive integer");
68
+ }
69
+ this.examples = fields?.examples ?? this.examples;
70
+ this.apiKey =
71
+ fields?.apiKey ?? getEnvironmentVariable("GOOGLE_PALM_API_KEY");
72
+ if (!this.apiKey) {
73
+ throw new Error("Please set an API key for Google Palm 2 in the environment variable GOOGLE_PALM_API_KEY or in the `apiKey` field of the GooglePalm constructor");
74
+ }
75
+ this.client = new DiscussServiceClient({
76
+ authClient: new GoogleAuth().fromAPIKey(this.apiKey),
77
+ });
78
+ }
79
+ _combineLLMOutput() {
80
+ return [];
81
+ }
82
+ _llmType() {
83
+ return "googlepalm";
84
+ }
85
+ async _generate(messages, options, runManager) {
86
+ const palmMessages = await this.caller.callWithOptions({ signal: options.signal }, this._generateMessage.bind(this), this._mapBaseMessagesToPalmMessages(messages), this._getPalmContextInstruction(messages), this.examples);
87
+ const chatResult = this._mapPalmMessagesToChatResult(palmMessages);
88
+ // Google Palm doesn't provide streaming as of now. But to support streaming handlers
89
+ // we call the handler with entire response text
90
+ void runManager?.handleLLMNewToken(chatResult.generations.length > 0 ? chatResult.generations[0].text : "");
91
+ return chatResult;
92
+ }
93
+ async _generateMessage(messages, context, examples) {
94
+ const [palmMessages] = await this.client.generateMessage({
95
+ candidateCount: 1,
96
+ model: this.modelName,
97
+ temperature: this.temperature,
98
+ topK: this.topK,
99
+ topP: this.topP,
100
+ prompt: {
101
+ context,
102
+ examples,
103
+ messages,
104
+ },
105
+ });
106
+ return palmMessages;
107
+ }
108
+ _getPalmContextInstruction(messages) {
109
+ // get the first message and checks if it's a system 'system' messages
110
+ const systemMessage = messages.length > 0 && messages[0]._getType() === "system"
111
+ ? messages[0]
112
+ : undefined;
113
+ return systemMessage?.content;
114
+ }
115
+ _mapBaseMessagesToPalmMessages(messages) {
116
+ // remove all 'system' messages
117
+ const nonSystemMessages = messages.filter((m) => m._getType() !== "system");
118
+ // requires alternate human & ai messages. Throw error if two messages are consecutive
119
+ nonSystemMessages.forEach((msg, index) => {
120
+ if (index < 1)
121
+ return;
122
+ if (msg._getType() === nonSystemMessages[index - 1]._getType()) {
123
+ throw new Error(`Google PaLM requires alternate messages between authors`);
124
+ }
125
+ });
126
+ return nonSystemMessages.map((m) => ({
127
+ author: m.name ?? m._getType(),
128
+ content: m.content,
129
+ citationMetadata: {
130
+ citationSources: m.additional_kwargs.citationSources,
131
+ },
132
+ }));
133
+ }
134
+ _mapPalmMessagesToChatResult(msgRes) {
135
+ if (msgRes.candidates &&
136
+ msgRes.candidates.length > 0 &&
137
+ msgRes.candidates[0]) {
138
+ const message = msgRes.candidates[0];
139
+ return {
140
+ generations: [
141
+ {
142
+ text: message.content ?? "",
143
+ message: new AIMessage({
144
+ content: message.content ?? "",
145
+ name: message.author === null ? undefined : message.author,
146
+ additional_kwargs: {
147
+ citationSources: message.citationMetadata?.citationSources,
148
+ filters: msgRes.filters, // content filters applied
149
+ },
150
+ }),
151
+ },
152
+ ],
153
+ };
154
+ }
155
+ // if rejected or error, return empty generations with reason in filters
156
+ return {
157
+ generations: [],
158
+ llmOutput: {
159
+ filters: msgRes.filters,
160
+ },
161
+ };
162
+ }
163
+ }
@@ -589,6 +589,9 @@ class PromptLayerChatOpenAI extends ChatOpenAI {
589
589
  else if (message._getType() === "ai") {
590
590
  messageDict = { role: "assistant", content: message.content };
591
591
  }
592
+ else if (message._getType() === "function") {
593
+ messageDict = { role: "assistant", content: message.content };
594
+ }
592
595
  else if (message._getType() === "system") {
593
596
  messageDict = { role: "system", content: message.content };
594
597
  }
@@ -582,6 +582,9 @@ export class PromptLayerChatOpenAI extends ChatOpenAI {
582
582
  else if (message._getType() === "ai") {
583
583
  messageDict = { role: "assistant", content: message.content };
584
584
  }
585
+ else if (message._getType() === "function") {
586
+ messageDict = { role: "assistant", content: message.content };
587
+ }
585
588
  else if (message._getType() === "system") {
586
589
  messageDict = { role: "system", content: message.content };
587
590
  }
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.runOnDataset = void 0;
4
- var langchainplus_js_1 = require("./langchainplus.cjs");
5
- Object.defineProperty(exports, "runOnDataset", { enumerable: true, get: function () { return langchainplus_js_1.runOnDataset; } });
4
+ var langsmith_js_1 = require("./langsmith.cjs");
5
+ Object.defineProperty(exports, "runOnDataset", { enumerable: true, get: function () { return langsmith_js_1.runOnDataset; } });
@@ -1 +1 @@
1
- export { DatasetRunResults, runOnDataset } from "./langchainplus.js";
1
+ export { DatasetRunResults, runOnDataset } from "./langsmith.js";
@@ -1 +1 @@
1
- export { runOnDataset } from "./langchainplus.js";
1
+ export { runOnDataset } from "./langsmith.js";