@langchain/google-common 0.1.5 → 0.1.7
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.
- package/dist/chat_models.cjs +55 -2
- package/dist/chat_models.d.ts +8 -2
- package/dist/chat_models.js +55 -2
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/output_parsers.cjs +175 -0
- package/dist/output_parsers.d.ts +49 -0
- package/dist/output_parsers.js +169 -0
- package/dist/types.cjs +10 -1
- package/dist/types.d.ts +133 -1
- package/dist/types.js +9 -0
- package/dist/utils/common.cjs +24 -1
- package/dist/utils/common.d.ts +1 -1
- package/dist/utils/common.js +24 -1
- package/dist/utils/gemini.cjs +129 -5
- package/dist/utils/gemini.js +129 -5
- package/package.json +1 -1
package/dist/chat_models.cjs
CHANGED
|
@@ -53,10 +53,33 @@ class ChatConnection extends connection_js_1.AbstractGoogleLLMConnection {
|
|
|
53
53
|
}
|
|
54
54
|
return true;
|
|
55
55
|
}
|
|
56
|
+
computeGoogleSearchToolAdjustmentFromModel() {
|
|
57
|
+
if (this.modelName.startsWith("gemini-1.0")) {
|
|
58
|
+
return "googleSearchRetrieval";
|
|
59
|
+
}
|
|
60
|
+
else if (this.modelName.startsWith("gemini-1.5")) {
|
|
61
|
+
return "googleSearchRetrieval";
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
return "googleSearch";
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
computeGoogleSearchToolAdjustment(apiConfig) {
|
|
68
|
+
const adj = apiConfig.googleSearchToolAdjustment;
|
|
69
|
+
if (adj === undefined || adj === true) {
|
|
70
|
+
return this.computeGoogleSearchToolAdjustmentFromModel();
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
return adj;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
56
76
|
buildGeminiAPI() {
|
|
77
|
+
const apiConfig = this.apiConfig ?? {};
|
|
78
|
+
const googleSearchToolAdjustment = this.computeGoogleSearchToolAdjustment(apiConfig);
|
|
57
79
|
const geminiConfig = {
|
|
58
80
|
useSystemInstruction: this.useSystemInstruction,
|
|
59
|
-
|
|
81
|
+
googleSearchToolAdjustment,
|
|
82
|
+
...apiConfig,
|
|
60
83
|
};
|
|
61
84
|
return (0, gemini_js_1.getGeminiAPI)(geminiConfig);
|
|
62
85
|
}
|
|
@@ -128,12 +151,36 @@ class ChatGoogleBase extends chat_models_1.BaseChatModel {
|
|
|
128
151
|
writable: true,
|
|
129
152
|
value: 40
|
|
130
153
|
});
|
|
154
|
+
Object.defineProperty(this, "presencePenalty", {
|
|
155
|
+
enumerable: true,
|
|
156
|
+
configurable: true,
|
|
157
|
+
writable: true,
|
|
158
|
+
value: void 0
|
|
159
|
+
});
|
|
160
|
+
Object.defineProperty(this, "frequencyPenalty", {
|
|
161
|
+
enumerable: true,
|
|
162
|
+
configurable: true,
|
|
163
|
+
writable: true,
|
|
164
|
+
value: void 0
|
|
165
|
+
});
|
|
131
166
|
Object.defineProperty(this, "stopSequences", {
|
|
132
167
|
enumerable: true,
|
|
133
168
|
configurable: true,
|
|
134
169
|
writable: true,
|
|
135
170
|
value: []
|
|
136
171
|
});
|
|
172
|
+
Object.defineProperty(this, "logprobs", {
|
|
173
|
+
enumerable: true,
|
|
174
|
+
configurable: true,
|
|
175
|
+
writable: true,
|
|
176
|
+
value: false
|
|
177
|
+
});
|
|
178
|
+
Object.defineProperty(this, "topLogprobs", {
|
|
179
|
+
enumerable: true,
|
|
180
|
+
configurable: true,
|
|
181
|
+
writable: true,
|
|
182
|
+
value: 0
|
|
183
|
+
});
|
|
137
184
|
Object.defineProperty(this, "safetySettings", {
|
|
138
185
|
enumerable: true,
|
|
139
186
|
configurable: true,
|
|
@@ -199,7 +246,13 @@ class ChatGoogleBase extends chat_models_1.BaseChatModel {
|
|
|
199
246
|
return new auth_js_1.ApiKeyGoogleAuth(apiKey);
|
|
200
247
|
}
|
|
201
248
|
buildApiKey(fields) {
|
|
202
|
-
|
|
249
|
+
if (fields?.platformType !== "gcp") {
|
|
250
|
+
return fields?.apiKey ?? (0, env_1.getEnvironmentVariable)("GOOGLE_API_KEY");
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
// GCP doesn't support API Keys
|
|
254
|
+
return undefined;
|
|
255
|
+
}
|
|
203
256
|
}
|
|
204
257
|
buildClient(fields) {
|
|
205
258
|
const apiKey = this.buildApiKey(fields);
|
package/dist/chat_models.d.ts
CHANGED
|
@@ -7,15 +7,17 @@ import { BaseLanguageModelInput, StructuredOutputMethodOptions } from "@langchai
|
|
|
7
7
|
import type { z } from "zod";
|
|
8
8
|
import { Runnable } from "@langchain/core/runnables";
|
|
9
9
|
import { AsyncCaller } from "@langchain/core/utils/async_caller";
|
|
10
|
-
import { GoogleAIBaseLLMInput, GoogleAIModelParams, GoogleAISafetySetting, GoogleConnectionParams, GooglePlatformType, GoogleAIBaseLanguageModelCallOptions, GoogleAIAPI, GoogleAIAPIParams } from "./types.js";
|
|
10
|
+
import { GoogleAIBaseLLMInput, GoogleAIModelParams, GoogleAISafetySetting, GoogleConnectionParams, GooglePlatformType, GoogleAIBaseLanguageModelCallOptions, GoogleAIAPI, GoogleAIAPIParams, GoogleSearchToolSetting } from "./types.js";
|
|
11
11
|
import { AbstractGoogleLLMConnection } from "./connection.js";
|
|
12
12
|
import { GoogleAbstractedClient } from "./auth.js";
|
|
13
|
-
import type { GoogleBaseLLMInput, GoogleAISafetyHandler, GoogleAISafetyParams, GoogleAIToolType } from "./types.js";
|
|
13
|
+
import type { GoogleBaseLLMInput, GoogleAISafetyHandler, GoogleAISafetyParams, GoogleAIToolType, GeminiAPIConfig } from "./types.js";
|
|
14
14
|
export declare class ChatConnection<AuthOptions> extends AbstractGoogleLLMConnection<BaseMessage[], AuthOptions> {
|
|
15
15
|
convertSystemMessageToHumanContent: boolean | undefined;
|
|
16
16
|
constructor(fields: GoogleAIBaseLLMInput<AuthOptions> | undefined, caller: AsyncCaller, client: GoogleAbstractedClient, streaming: boolean);
|
|
17
17
|
get useSystemInstruction(): boolean;
|
|
18
18
|
get computeUseSystemInstruction(): boolean;
|
|
19
|
+
computeGoogleSearchToolAdjustmentFromModel(): Exclude<GoogleSearchToolSetting, boolean>;
|
|
20
|
+
computeGoogleSearchToolAdjustment(apiConfig: GeminiAPIConfig): Exclude<GoogleSearchToolSetting, true>;
|
|
19
21
|
buildGeminiAPI(): GoogleAIAPI;
|
|
20
22
|
get api(): GoogleAIAPI;
|
|
21
23
|
}
|
|
@@ -39,7 +41,11 @@ export declare abstract class ChatGoogleBase<AuthOptions> extends BaseChatModel<
|
|
|
39
41
|
maxOutputTokens: number;
|
|
40
42
|
topP: number;
|
|
41
43
|
topK: number;
|
|
44
|
+
presencePenalty: number;
|
|
45
|
+
frequencyPenalty: number;
|
|
42
46
|
stopSequences: string[];
|
|
47
|
+
logprobs: boolean;
|
|
48
|
+
topLogprobs: number;
|
|
43
49
|
safetySettings: GoogleAISafetySetting[];
|
|
44
50
|
convertSystemMessageToHumanContent: boolean | undefined;
|
|
45
51
|
safetyHandler: GoogleAISafetyHandler;
|
package/dist/chat_models.js
CHANGED
|
@@ -50,10 +50,33 @@ export class ChatConnection extends AbstractGoogleLLMConnection {
|
|
|
50
50
|
}
|
|
51
51
|
return true;
|
|
52
52
|
}
|
|
53
|
+
computeGoogleSearchToolAdjustmentFromModel() {
|
|
54
|
+
if (this.modelName.startsWith("gemini-1.0")) {
|
|
55
|
+
return "googleSearchRetrieval";
|
|
56
|
+
}
|
|
57
|
+
else if (this.modelName.startsWith("gemini-1.5")) {
|
|
58
|
+
return "googleSearchRetrieval";
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
return "googleSearch";
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
computeGoogleSearchToolAdjustment(apiConfig) {
|
|
65
|
+
const adj = apiConfig.googleSearchToolAdjustment;
|
|
66
|
+
if (adj === undefined || adj === true) {
|
|
67
|
+
return this.computeGoogleSearchToolAdjustmentFromModel();
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
return adj;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
53
73
|
buildGeminiAPI() {
|
|
74
|
+
const apiConfig = this.apiConfig ?? {};
|
|
75
|
+
const googleSearchToolAdjustment = this.computeGoogleSearchToolAdjustment(apiConfig);
|
|
54
76
|
const geminiConfig = {
|
|
55
77
|
useSystemInstruction: this.useSystemInstruction,
|
|
56
|
-
|
|
78
|
+
googleSearchToolAdjustment,
|
|
79
|
+
...apiConfig,
|
|
57
80
|
};
|
|
58
81
|
return getGeminiAPI(geminiConfig);
|
|
59
82
|
}
|
|
@@ -124,12 +147,36 @@ export class ChatGoogleBase extends BaseChatModel {
|
|
|
124
147
|
writable: true,
|
|
125
148
|
value: 40
|
|
126
149
|
});
|
|
150
|
+
Object.defineProperty(this, "presencePenalty", {
|
|
151
|
+
enumerable: true,
|
|
152
|
+
configurable: true,
|
|
153
|
+
writable: true,
|
|
154
|
+
value: void 0
|
|
155
|
+
});
|
|
156
|
+
Object.defineProperty(this, "frequencyPenalty", {
|
|
157
|
+
enumerable: true,
|
|
158
|
+
configurable: true,
|
|
159
|
+
writable: true,
|
|
160
|
+
value: void 0
|
|
161
|
+
});
|
|
127
162
|
Object.defineProperty(this, "stopSequences", {
|
|
128
163
|
enumerable: true,
|
|
129
164
|
configurable: true,
|
|
130
165
|
writable: true,
|
|
131
166
|
value: []
|
|
132
167
|
});
|
|
168
|
+
Object.defineProperty(this, "logprobs", {
|
|
169
|
+
enumerable: true,
|
|
170
|
+
configurable: true,
|
|
171
|
+
writable: true,
|
|
172
|
+
value: false
|
|
173
|
+
});
|
|
174
|
+
Object.defineProperty(this, "topLogprobs", {
|
|
175
|
+
enumerable: true,
|
|
176
|
+
configurable: true,
|
|
177
|
+
writable: true,
|
|
178
|
+
value: 0
|
|
179
|
+
});
|
|
133
180
|
Object.defineProperty(this, "safetySettings", {
|
|
134
181
|
enumerable: true,
|
|
135
182
|
configurable: true,
|
|
@@ -195,7 +242,13 @@ export class ChatGoogleBase extends BaseChatModel {
|
|
|
195
242
|
return new ApiKeyGoogleAuth(apiKey);
|
|
196
243
|
}
|
|
197
244
|
buildApiKey(fields) {
|
|
198
|
-
|
|
245
|
+
if (fields?.platformType !== "gcp") {
|
|
246
|
+
return fields?.apiKey ?? getEnvironmentVariable("GOOGLE_API_KEY");
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
// GCP doesn't support API Keys
|
|
250
|
+
return undefined;
|
|
251
|
+
}
|
|
199
252
|
}
|
|
200
253
|
buildClient(fields) {
|
|
201
254
|
const apiKey = this.buildApiKey(fields);
|
package/dist/index.cjs
CHANGED
|
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./chat_models.cjs"), exports);
|
|
18
18
|
__exportStar(require("./llms.cjs"), exports);
|
|
19
19
|
__exportStar(require("./embeddings.cjs"), exports);
|
|
20
|
+
__exportStar(require("./output_parsers.cjs"), exports);
|
|
20
21
|
__exportStar(require("./auth.cjs"), exports);
|
|
21
22
|
__exportStar(require("./connection.cjs"), exports);
|
|
22
23
|
__exportStar(require("./types.cjs"), exports);
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MarkdownGoogleSearchOutputParser = exports.SimpleGoogleSearchOutputParser = exports.BaseGoogleSearchOutputParser = void 0;
|
|
4
|
+
const output_parsers_1 = require("@langchain/core/output_parsers");
|
|
5
|
+
class BaseGoogleSearchOutputParser extends output_parsers_1.BaseLLMOutputParser {
|
|
6
|
+
constructor() {
|
|
7
|
+
super(...arguments);
|
|
8
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
configurable: true,
|
|
11
|
+
writable: true,
|
|
12
|
+
value: ["google_common", "output_parsers"]
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
generationToGroundingInfo(generation) {
|
|
16
|
+
if ("message" in generation) {
|
|
17
|
+
const responseMetadata = generation?.message?.response_metadata;
|
|
18
|
+
const metadata = responseMetadata?.groundingMetadata;
|
|
19
|
+
const supports = responseMetadata?.groundingSupport ?? metadata?.groundingSupports ?? [];
|
|
20
|
+
if (metadata) {
|
|
21
|
+
return {
|
|
22
|
+
metadata,
|
|
23
|
+
supports,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
generationsToGroundingInfo(generations) {
|
|
30
|
+
for (const generation of generations) {
|
|
31
|
+
const info = this.generationToGroundingInfo(generation);
|
|
32
|
+
if (info !== undefined) {
|
|
33
|
+
return info;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
generationToString(generation) {
|
|
39
|
+
if ("message" in generation) {
|
|
40
|
+
const content = generation?.message?.content;
|
|
41
|
+
if (typeof content === "string") {
|
|
42
|
+
return content;
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
return content
|
|
46
|
+
.map((c) => {
|
|
47
|
+
if (c?.type === "text") {
|
|
48
|
+
return c?.text ?? "";
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
return "";
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
.reduce((previousValue, currentValue) => `${previousValue}${currentValue}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return generation.text;
|
|
58
|
+
}
|
|
59
|
+
generationsToString(generations) {
|
|
60
|
+
return generations
|
|
61
|
+
.map((generation) => this.generationToString(generation))
|
|
62
|
+
.reduce((previousValue, currentValue) => `${previousValue}${currentValue}`);
|
|
63
|
+
}
|
|
64
|
+
annotateSegment(text, grounding, support, index) {
|
|
65
|
+
const start = support.segment.startIndex ?? 0;
|
|
66
|
+
const end = support.segment.endIndex;
|
|
67
|
+
const textBefore = text.substring(0, start);
|
|
68
|
+
const textSegment = text.substring(start, end);
|
|
69
|
+
const textAfter = text.substring(end);
|
|
70
|
+
const textPrefix = this.segmentPrefix(grounding, support, index) ?? "";
|
|
71
|
+
const textSuffix = this.segmentSuffix(grounding, support, index) ?? "";
|
|
72
|
+
return `${textBefore}${textPrefix}${textSegment}${textSuffix}${textAfter}`;
|
|
73
|
+
}
|
|
74
|
+
annotateTextSegments(text, grounding) {
|
|
75
|
+
// Go through each support info in reverse, since the segment info
|
|
76
|
+
// is sorted, and we won't need to adjust string indexes this way.
|
|
77
|
+
let ret = text;
|
|
78
|
+
for (let co = grounding.supports.length - 1; co >= 0; co -= 1) {
|
|
79
|
+
const support = grounding.supports[co];
|
|
80
|
+
ret = this.annotateSegment(ret, grounding, support, co);
|
|
81
|
+
}
|
|
82
|
+
return ret;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Google requires us to
|
|
86
|
+
* "Display the Search Suggestion exactly as provided without any modifications"
|
|
87
|
+
* So this will typically be called from the textSuffix() method to get
|
|
88
|
+
* a string that renders HTML.
|
|
89
|
+
* See https://ai.google.dev/gemini-api/docs/grounding/search-suggestions
|
|
90
|
+
* @param grounding
|
|
91
|
+
*/
|
|
92
|
+
searchSuggestion(grounding) {
|
|
93
|
+
return grounding?.metadata?.searchEntryPoint?.renderedContent ?? "";
|
|
94
|
+
}
|
|
95
|
+
annotateText(text, grounding) {
|
|
96
|
+
const prefix = this.textPrefix(text, grounding) ?? "";
|
|
97
|
+
const suffix = this.textSuffix(text, grounding) ?? "";
|
|
98
|
+
const body = this.annotateTextSegments(text, grounding);
|
|
99
|
+
return `${prefix}${body}${suffix}`;
|
|
100
|
+
}
|
|
101
|
+
async parseResult(generations, _callbacks) {
|
|
102
|
+
const text = this.generationsToString(generations);
|
|
103
|
+
const grounding = this.generationsToGroundingInfo(generations);
|
|
104
|
+
if (!grounding) {
|
|
105
|
+
return text;
|
|
106
|
+
}
|
|
107
|
+
return this.annotateText(text, grounding);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
exports.BaseGoogleSearchOutputParser = BaseGoogleSearchOutputParser;
|
|
111
|
+
class SimpleGoogleSearchOutputParser extends BaseGoogleSearchOutputParser {
|
|
112
|
+
segmentPrefix(_grounding, _support, _index) {
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
segmentSuffix(_grounding, support, _index) {
|
|
116
|
+
const indices = support.groundingChunkIndices.map((i) => i + 1);
|
|
117
|
+
return ` [${indices.join(", ")}]`;
|
|
118
|
+
}
|
|
119
|
+
textPrefix(_text, _grounding) {
|
|
120
|
+
return "Google Says:\n";
|
|
121
|
+
}
|
|
122
|
+
chunkToString(chunk, index) {
|
|
123
|
+
const info = chunk.retrievedContext ?? chunk.web;
|
|
124
|
+
return `${index + 1}. ${info.title} - ${info.uri}`;
|
|
125
|
+
}
|
|
126
|
+
textSuffix(_text, grounding) {
|
|
127
|
+
let ret = "\n";
|
|
128
|
+
const chunks = grounding?.metadata?.groundingChunks ?? [];
|
|
129
|
+
chunks.forEach((chunk, index) => {
|
|
130
|
+
ret = `${ret}${this.chunkToString(chunk, index)}\n`;
|
|
131
|
+
});
|
|
132
|
+
return ret;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
exports.SimpleGoogleSearchOutputParser = SimpleGoogleSearchOutputParser;
|
|
136
|
+
class MarkdownGoogleSearchOutputParser extends BaseGoogleSearchOutputParser {
|
|
137
|
+
segmentPrefix(_grounding, _support, _index) {
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
chunkLink(grounding, index) {
|
|
141
|
+
const chunk = grounding.metadata.groundingChunks[index];
|
|
142
|
+
const url = chunk.retrievedContext?.uri ?? chunk.web?.uri;
|
|
143
|
+
const num = index + 1;
|
|
144
|
+
return `[[${num}](${url})]`;
|
|
145
|
+
}
|
|
146
|
+
segmentSuffix(grounding, support, _index) {
|
|
147
|
+
let ret = "";
|
|
148
|
+
support.groundingChunkIndices.forEach((chunkIndex) => {
|
|
149
|
+
const link = this.chunkLink(grounding, chunkIndex);
|
|
150
|
+
ret = `${ret}${link}`;
|
|
151
|
+
});
|
|
152
|
+
return ret;
|
|
153
|
+
}
|
|
154
|
+
textPrefix(_text, _grounding) {
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
chunkSuffixLink(chunk, index) {
|
|
158
|
+
const num = index + 1;
|
|
159
|
+
const info = chunk.retrievedContext ?? chunk.web;
|
|
160
|
+
const url = info.uri;
|
|
161
|
+
const site = info.title;
|
|
162
|
+
return `${num}. [${site}](${url})`;
|
|
163
|
+
}
|
|
164
|
+
textSuffix(_text, grounding) {
|
|
165
|
+
let ret = "\n**Search Sources**\n";
|
|
166
|
+
const chunks = grounding.metadata.groundingChunks;
|
|
167
|
+
chunks.forEach((chunk, index) => {
|
|
168
|
+
ret = `${ret}${this.chunkSuffixLink(chunk, index)}\n`;
|
|
169
|
+
});
|
|
170
|
+
const search = this.searchSuggestion(grounding);
|
|
171
|
+
ret = `${ret}\n${search}`;
|
|
172
|
+
return ret;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
exports.MarkdownGoogleSearchOutputParser = MarkdownGoogleSearchOutputParser;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { BaseLLMOutputParser } from "@langchain/core/output_parsers";
|
|
2
|
+
import { Callbacks } from "@langchain/core/callbacks/manager";
|
|
3
|
+
import { ChatGeneration, Generation } from "@langchain/core/outputs";
|
|
4
|
+
import { GeminiGroundingChunk, GeminiGroundingMetadata, GeminiGroundingSupport } from "./types.js";
|
|
5
|
+
type Generations = Generation[] | ChatGeneration[];
|
|
6
|
+
type GroundingInfo = {
|
|
7
|
+
metadata: GeminiGroundingMetadata;
|
|
8
|
+
supports: GeminiGroundingSupport[];
|
|
9
|
+
};
|
|
10
|
+
export declare abstract class BaseGoogleSearchOutputParser extends BaseLLMOutputParser<string> {
|
|
11
|
+
lc_namespace: string[];
|
|
12
|
+
protected generationToGroundingInfo(generation: Generation | ChatGeneration): GroundingInfo | undefined;
|
|
13
|
+
protected generationsToGroundingInfo(generations: Generations): GroundingInfo | undefined;
|
|
14
|
+
protected generationToString(generation: Generation | ChatGeneration): string;
|
|
15
|
+
protected generationsToString(generations: Generations): string;
|
|
16
|
+
protected abstract segmentPrefix(grounding: GroundingInfo, support: GeminiGroundingSupport, index: number): string | undefined;
|
|
17
|
+
protected abstract segmentSuffix(grounding: GroundingInfo, support: GeminiGroundingSupport, index: number): string | undefined;
|
|
18
|
+
protected annotateSegment(text: string, grounding: GroundingInfo, support: GeminiGroundingSupport, index: number): string;
|
|
19
|
+
protected annotateTextSegments(text: string, grounding: GroundingInfo): string;
|
|
20
|
+
protected abstract textPrefix(text: string, grounding: GroundingInfo): string | undefined;
|
|
21
|
+
protected abstract textSuffix(text: string, grounding: GroundingInfo): string | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Google requires us to
|
|
24
|
+
* "Display the Search Suggestion exactly as provided without any modifications"
|
|
25
|
+
* So this will typically be called from the textSuffix() method to get
|
|
26
|
+
* a string that renders HTML.
|
|
27
|
+
* See https://ai.google.dev/gemini-api/docs/grounding/search-suggestions
|
|
28
|
+
* @param grounding
|
|
29
|
+
*/
|
|
30
|
+
protected searchSuggestion(grounding: GroundingInfo): string;
|
|
31
|
+
protected annotateText(text: string, grounding: GroundingInfo): string;
|
|
32
|
+
parseResult(generations: Generations, _callbacks?: Callbacks): Promise<string>;
|
|
33
|
+
}
|
|
34
|
+
export declare class SimpleGoogleSearchOutputParser extends BaseGoogleSearchOutputParser {
|
|
35
|
+
protected segmentPrefix(_grounding: GroundingInfo, _support: GeminiGroundingSupport, _index: number): string | undefined;
|
|
36
|
+
protected segmentSuffix(_grounding: GroundingInfo, support: GeminiGroundingSupport, _index: number): string | undefined;
|
|
37
|
+
protected textPrefix(_text: string, _grounding: GroundingInfo): string;
|
|
38
|
+
protected chunkToString(chunk: GeminiGroundingChunk, index: number): string;
|
|
39
|
+
protected textSuffix(_text: string, grounding: GroundingInfo): string;
|
|
40
|
+
}
|
|
41
|
+
export declare class MarkdownGoogleSearchOutputParser extends BaseGoogleSearchOutputParser {
|
|
42
|
+
protected segmentPrefix(_grounding: GroundingInfo, _support: GeminiGroundingSupport, _index: number): string | undefined;
|
|
43
|
+
protected chunkLink(grounding: GroundingInfo, index: number): string;
|
|
44
|
+
protected segmentSuffix(grounding: GroundingInfo, support: GeminiGroundingSupport, _index: number): string | undefined;
|
|
45
|
+
protected textPrefix(_text: string, _grounding: GroundingInfo): string | undefined;
|
|
46
|
+
protected chunkSuffixLink(chunk: GeminiGroundingChunk, index: number): string;
|
|
47
|
+
protected textSuffix(_text: string, grounding: GroundingInfo): string | undefined;
|
|
48
|
+
}
|
|
49
|
+
export {};
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { BaseLLMOutputParser } from "@langchain/core/output_parsers";
|
|
2
|
+
export class BaseGoogleSearchOutputParser extends BaseLLMOutputParser {
|
|
3
|
+
constructor() {
|
|
4
|
+
super(...arguments);
|
|
5
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
configurable: true,
|
|
8
|
+
writable: true,
|
|
9
|
+
value: ["google_common", "output_parsers"]
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
generationToGroundingInfo(generation) {
|
|
13
|
+
if ("message" in generation) {
|
|
14
|
+
const responseMetadata = generation?.message?.response_metadata;
|
|
15
|
+
const metadata = responseMetadata?.groundingMetadata;
|
|
16
|
+
const supports = responseMetadata?.groundingSupport ?? metadata?.groundingSupports ?? [];
|
|
17
|
+
if (metadata) {
|
|
18
|
+
return {
|
|
19
|
+
metadata,
|
|
20
|
+
supports,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
generationsToGroundingInfo(generations) {
|
|
27
|
+
for (const generation of generations) {
|
|
28
|
+
const info = this.generationToGroundingInfo(generation);
|
|
29
|
+
if (info !== undefined) {
|
|
30
|
+
return info;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
generationToString(generation) {
|
|
36
|
+
if ("message" in generation) {
|
|
37
|
+
const content = generation?.message?.content;
|
|
38
|
+
if (typeof content === "string") {
|
|
39
|
+
return content;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
return content
|
|
43
|
+
.map((c) => {
|
|
44
|
+
if (c?.type === "text") {
|
|
45
|
+
return c?.text ?? "";
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
return "";
|
|
49
|
+
}
|
|
50
|
+
})
|
|
51
|
+
.reduce((previousValue, currentValue) => `${previousValue}${currentValue}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return generation.text;
|
|
55
|
+
}
|
|
56
|
+
generationsToString(generations) {
|
|
57
|
+
return generations
|
|
58
|
+
.map((generation) => this.generationToString(generation))
|
|
59
|
+
.reduce((previousValue, currentValue) => `${previousValue}${currentValue}`);
|
|
60
|
+
}
|
|
61
|
+
annotateSegment(text, grounding, support, index) {
|
|
62
|
+
const start = support.segment.startIndex ?? 0;
|
|
63
|
+
const end = support.segment.endIndex;
|
|
64
|
+
const textBefore = text.substring(0, start);
|
|
65
|
+
const textSegment = text.substring(start, end);
|
|
66
|
+
const textAfter = text.substring(end);
|
|
67
|
+
const textPrefix = this.segmentPrefix(grounding, support, index) ?? "";
|
|
68
|
+
const textSuffix = this.segmentSuffix(grounding, support, index) ?? "";
|
|
69
|
+
return `${textBefore}${textPrefix}${textSegment}${textSuffix}${textAfter}`;
|
|
70
|
+
}
|
|
71
|
+
annotateTextSegments(text, grounding) {
|
|
72
|
+
// Go through each support info in reverse, since the segment info
|
|
73
|
+
// is sorted, and we won't need to adjust string indexes this way.
|
|
74
|
+
let ret = text;
|
|
75
|
+
for (let co = grounding.supports.length - 1; co >= 0; co -= 1) {
|
|
76
|
+
const support = grounding.supports[co];
|
|
77
|
+
ret = this.annotateSegment(ret, grounding, support, co);
|
|
78
|
+
}
|
|
79
|
+
return ret;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Google requires us to
|
|
83
|
+
* "Display the Search Suggestion exactly as provided without any modifications"
|
|
84
|
+
* So this will typically be called from the textSuffix() method to get
|
|
85
|
+
* a string that renders HTML.
|
|
86
|
+
* See https://ai.google.dev/gemini-api/docs/grounding/search-suggestions
|
|
87
|
+
* @param grounding
|
|
88
|
+
*/
|
|
89
|
+
searchSuggestion(grounding) {
|
|
90
|
+
return grounding?.metadata?.searchEntryPoint?.renderedContent ?? "";
|
|
91
|
+
}
|
|
92
|
+
annotateText(text, grounding) {
|
|
93
|
+
const prefix = this.textPrefix(text, grounding) ?? "";
|
|
94
|
+
const suffix = this.textSuffix(text, grounding) ?? "";
|
|
95
|
+
const body = this.annotateTextSegments(text, grounding);
|
|
96
|
+
return `${prefix}${body}${suffix}`;
|
|
97
|
+
}
|
|
98
|
+
async parseResult(generations, _callbacks) {
|
|
99
|
+
const text = this.generationsToString(generations);
|
|
100
|
+
const grounding = this.generationsToGroundingInfo(generations);
|
|
101
|
+
if (!grounding) {
|
|
102
|
+
return text;
|
|
103
|
+
}
|
|
104
|
+
return this.annotateText(text, grounding);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
export class SimpleGoogleSearchOutputParser extends BaseGoogleSearchOutputParser {
|
|
108
|
+
segmentPrefix(_grounding, _support, _index) {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
segmentSuffix(_grounding, support, _index) {
|
|
112
|
+
const indices = support.groundingChunkIndices.map((i) => i + 1);
|
|
113
|
+
return ` [${indices.join(", ")}]`;
|
|
114
|
+
}
|
|
115
|
+
textPrefix(_text, _grounding) {
|
|
116
|
+
return "Google Says:\n";
|
|
117
|
+
}
|
|
118
|
+
chunkToString(chunk, index) {
|
|
119
|
+
const info = chunk.retrievedContext ?? chunk.web;
|
|
120
|
+
return `${index + 1}. ${info.title} - ${info.uri}`;
|
|
121
|
+
}
|
|
122
|
+
textSuffix(_text, grounding) {
|
|
123
|
+
let ret = "\n";
|
|
124
|
+
const chunks = grounding?.metadata?.groundingChunks ?? [];
|
|
125
|
+
chunks.forEach((chunk, index) => {
|
|
126
|
+
ret = `${ret}${this.chunkToString(chunk, index)}\n`;
|
|
127
|
+
});
|
|
128
|
+
return ret;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
export class MarkdownGoogleSearchOutputParser extends BaseGoogleSearchOutputParser {
|
|
132
|
+
segmentPrefix(_grounding, _support, _index) {
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
chunkLink(grounding, index) {
|
|
136
|
+
const chunk = grounding.metadata.groundingChunks[index];
|
|
137
|
+
const url = chunk.retrievedContext?.uri ?? chunk.web?.uri;
|
|
138
|
+
const num = index + 1;
|
|
139
|
+
return `[[${num}](${url})]`;
|
|
140
|
+
}
|
|
141
|
+
segmentSuffix(grounding, support, _index) {
|
|
142
|
+
let ret = "";
|
|
143
|
+
support.groundingChunkIndices.forEach((chunkIndex) => {
|
|
144
|
+
const link = this.chunkLink(grounding, chunkIndex);
|
|
145
|
+
ret = `${ret}${link}`;
|
|
146
|
+
});
|
|
147
|
+
return ret;
|
|
148
|
+
}
|
|
149
|
+
textPrefix(_text, _grounding) {
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
chunkSuffixLink(chunk, index) {
|
|
153
|
+
const num = index + 1;
|
|
154
|
+
const info = chunk.retrievedContext ?? chunk.web;
|
|
155
|
+
const url = info.uri;
|
|
156
|
+
const site = info.title;
|
|
157
|
+
return `${num}. [${site}](${url})`;
|
|
158
|
+
}
|
|
159
|
+
textSuffix(_text, grounding) {
|
|
160
|
+
let ret = "\n**Search Sources**\n";
|
|
161
|
+
const chunks = grounding.metadata.groundingChunks;
|
|
162
|
+
chunks.forEach((chunk, index) => {
|
|
163
|
+
ret = `${ret}${this.chunkSuffixLink(chunk, index)}\n`;
|
|
164
|
+
});
|
|
165
|
+
const search = this.searchSuggestion(grounding);
|
|
166
|
+
ret = `${ret}\n${search}`;
|
|
167
|
+
return ret;
|
|
168
|
+
}
|
|
169
|
+
}
|
package/dist/types.cjs
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.GoogleAISafetyMethod = exports.GoogleAISafetyThreshold = exports.GoogleAISafetyCategory = void 0;
|
|
17
|
+
exports.GeminiToolAttributes = exports.GeminiSearchToolAttributes = exports.GoogleAISafetyMethod = exports.GoogleAISafetyThreshold = exports.GoogleAISafetyCategory = void 0;
|
|
18
18
|
__exportStar(require("./types-anthropic.cjs"), exports);
|
|
19
19
|
exports.GoogleAISafetyCategory = {
|
|
20
20
|
Harassment: "HARM_CATEGORY_HARASSMENT",
|
|
@@ -54,3 +54,12 @@ exports.GoogleAISafetyMethod = {
|
|
|
54
54
|
Severity: "SEVERITY",
|
|
55
55
|
Probability: "PROBABILITY",
|
|
56
56
|
};
|
|
57
|
+
exports.GeminiSearchToolAttributes = [
|
|
58
|
+
"googleSearchRetrieval",
|
|
59
|
+
"googleSearch",
|
|
60
|
+
];
|
|
61
|
+
exports.GeminiToolAttributes = [
|
|
62
|
+
"functionDeclaration",
|
|
63
|
+
"retrieval",
|
|
64
|
+
...exports.GeminiSearchToolAttributes,
|
|
65
|
+
];
|