Skip to content

Commit 3113191

Browse files
extend readme
1 parent 9cf9c10 commit 3113191

File tree

1 file changed

+280
-1
lines changed

1 file changed

+280
-1
lines changed

libs/langchain-google/README.md

Lines changed: 280 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,37 @@ through the Google Cloud Platform environment it is running on.
1212
$ yarn add @langchain/google
1313
```
1414

15+
## Features
16+
17+
This package provides:
18+
19+
### Chat Models
20+
21+
- **ChatGoogle** - Base Google chat model with automatic platform detection
22+
- **ChatVertexAI** - Google Vertex AI chat models (Gemini Pro, Gemini Pro Vision, etc.)
23+
- **ChatGoogleGenerativeAI** - Google AI Studio chat models (formerly @langchain/google-genai)
24+
25+
### Language Models (LLMs)
26+
27+
- **GoogleLLM** - Base Google LLM with automatic platform detection
28+
- **VertexAI** - Google Vertex AI language models
29+
30+
### Embeddings
31+
32+
- **GoogleEmbeddings** - Base Google embeddings with automatic platform detection
33+
- **VertexAIEmbeddings** - Google Vertex AI embeddings
34+
- **GoogleGenerativeAIEmbeddings** - Google AI Studio embeddings
35+
36+
### Multimodal Support
37+
38+
- Image inputs for vision models
39+
- Support for various image formats (base64, URLs, GCS paths)
40+
41+
### Tool/Function Calling
42+
43+
- Full support for Google's function calling capabilities
44+
- Tool binding and structured outputs
45+
1546
## Authorization
1647

1748
Authorization is depending on the platform your are using this package in.
@@ -36,7 +67,7 @@ credentials from the first of the following that apply:
3667
logged in using `gcloud auth application-default login`, then the
3768
default credentials.
3869

39-
## Browser
70+
### Browser
4071

4172
Authorization is done through a Google Cloud Service Account.
4273

@@ -52,3 +83,251 @@ credentials from the first of the following that apply:
5283
`GOOGLE_WEB_CREDENTIALS`
5384
5. The Service Account credentials that are saved directly into the
5485
`GOOGLE_VERTEX_AI_WEB_CREDENTIALS` (deprecated)
86+
87+
## Usage Examples
88+
89+
### Chat Models
90+
91+
#### Basic Chat (Node.js)
92+
93+
```typescript
94+
import { ChatGoogle } from "@langchain/google";
95+
96+
const model = new ChatGoogle({
97+
model: "gemini-1.5-pro",
98+
temperature: 0.7,
99+
apiKey: "your-api-key", // or set GOOGLE_API_KEY environment variable
100+
});
101+
102+
const response = await model.invoke("Tell me a joke");
103+
console.log(response.content);
104+
```
105+
106+
#### Vertex AI Chat (Node.js)
107+
108+
```typescript
109+
import { ChatVertexAI } from "@langchain/google";
110+
111+
const model = new ChatVertexAI({
112+
model: "gemini-1.5-pro",
113+
temperature: 0.7,
114+
location: "us-central1", // optional
115+
// Credentials will be automatically detected from environment
116+
});
117+
118+
const response = await model.invoke("Explain quantum computing");
119+
console.log(response.content);
120+
```
121+
122+
#### Multimodal Chat with Images
123+
124+
```typescript
125+
import fs from "node:fs";
126+
import { ChatGoogle } from "@langchain/google";
127+
import { HumanMessage } from "@langchain/core/messages";
128+
import fs from "fs";
129+
130+
const model = new ChatGoogle({
131+
model: "gemini-1.5-pro-vision",
132+
});
133+
134+
const image = fs.readFileSync("./image.jpg").toString("base64");
135+
const message = new HumanMessage({
136+
content: [
137+
{
138+
type: "text",
139+
text: "What's in this image?",
140+
},
141+
{
142+
type: "image_url",
143+
image_url: `data:image/jpeg;base64,${image}`,
144+
},
145+
],
146+
});
147+
148+
const response = await model.invoke([message]);
149+
console.log(response.content);
150+
```
151+
152+
#### Web/Browser Usage
153+
154+
```typescript
155+
import { ChatGoogle } from "@langchain/google/web";
156+
157+
const model = new ChatGoogle({
158+
model: "gemini-1.5-pro",
159+
authOptions: {
160+
credentials: JSON.parse(process.env.GOOGLE_WEB_CREDENTIALS),
161+
},
162+
});
163+
164+
const response = await model.invoke("Hello!");
165+
console.log(response.content);
166+
```
167+
168+
### Language Models (LLMs)
169+
170+
```typescript
171+
import { GoogleLLM } from "@langchain/google";
172+
173+
const llm = new GoogleLLM({
174+
model: "gemini-1.5-pro",
175+
temperature: 0.7,
176+
maxOutputTokens: 1000,
177+
});
178+
179+
const response = await llm.invoke("Write a short story about AI");
180+
console.log(response);
181+
```
182+
183+
### Embeddings
184+
185+
#### Basic Embeddings
186+
187+
```typescript
188+
import { GoogleEmbeddings } from "@langchain/google";
189+
190+
const embeddings = new GoogleEmbeddings({
191+
model: "text-embedding-004",
192+
});
193+
194+
const vectors = await embeddings.embedDocuments([
195+
"Hello world",
196+
"How are you?",
197+
"Goodbye!",
198+
]);
199+
200+
console.log(vectors[0].length); // Vector dimension
201+
```
202+
203+
#### Vertex AI Embeddings
204+
205+
```typescript
206+
import { VertexAIEmbeddings } from "@langchain/google";
207+
208+
const embeddings = new VertexAIEmbeddings({
209+
model: "text-embedding-004",
210+
location: "us-central1",
211+
});
212+
213+
const vector = await embeddings.embedQuery("What is machine learning?");
214+
console.log(vector);
215+
```
216+
217+
### Function/Tool Calling
218+
219+
```typescript
220+
import { ChatGoogle } from "@langchain/google";
221+
import { z } from "zod";
222+
223+
const model = new ChatGoogle({
224+
model: "gemini-1.5-pro",
225+
});
226+
227+
const weatherTool = {
228+
name: "get_weather",
229+
description: "Get the current weather for a location",
230+
parameters: z.object({
231+
location: z.string().describe("The city and state"),
232+
unit: z.enum(["celsius", "fahrenheit"]).optional(),
233+
}),
234+
};
235+
236+
const modelWithTools = model.bindTools([weatherTool]);
237+
const response = await modelWithTools.invoke(
238+
"What's the weather like in Paris?"
239+
);
240+
console.log(response.tool_calls);
241+
```
242+
243+
### Streaming
244+
245+
```typescript
246+
import { ChatGoogle } from "@langchain/google";
247+
248+
const model = new ChatGoogle({
249+
model: "gemini-1.5-pro",
250+
});
251+
252+
const stream = await model.stream("Tell me a long story");
253+
for await (const chunk of stream) {
254+
console.log(chunk.content);
255+
}
256+
```
257+
258+
## Advanced Usage
259+
260+
### Custom Authentication
261+
262+
```typescript
263+
import { ChatGoogle } from "@langchain/google";
264+
265+
const model = new ChatGoogle({
266+
model: "gemini-1.5-pro",
267+
authOptions: {
268+
scopes: ["https://www.googleapis.com/auth/cloud-platform"],
269+
credentials: {
270+
// Your custom credentials
271+
},
272+
},
273+
});
274+
```
275+
276+
### Batch Processing
277+
278+
```typescript
279+
import { ChatGoogle } from "@langchain/google";
280+
281+
const model = new ChatGoogle({
282+
model: "gemini-1.5-pro",
283+
});
284+
285+
const prompts = [
286+
"Summarize this article...",
287+
"Translate this text...",
288+
"Answer this question...",
289+
];
290+
291+
const responses = await model.batch(prompts);
292+
responses.forEach((response, index) => {
293+
console.log(`Response ${index + 1}:`, response.content);
294+
});
295+
```
296+
297+
## Troubleshooting
298+
299+
### Common Issues
300+
301+
1. **Authentication Errors**
302+
303+
- Ensure your API key or service account has the correct permissions
304+
- Check that environment variables are set correctly
305+
306+
2. **Rate Limiting**
307+
308+
- Implement exponential backoff
309+
- Use batch processing for multiple requests
310+
311+
3. **Model Not Found**
312+
313+
- Verify the model name is correct
314+
- Ensure the model is available in your region
315+
316+
4. **Browser CORS Issues**
317+
- Ensure your domain is authorized in the Google Cloud Console
318+
- Use appropriate CORS headers
319+
320+
### Getting Help
321+
322+
- [Google AI Documentation](https://ai.google.dev/docs)
323+
- [Vertex AI Documentation](https://cloud.google.com/vertex-ai/docs)
324+
- [LangChain.js Documentation](https://js.langchain.com/)
325+
- [GitHub Issues](https://github.com/langchain-ai/langchainjs/issues)
326+
327+
## Contributing
328+
329+
Contributions are welcome! Please see the [Contributing Guide](../../CONTRIBUTING.md) for details.
330+
331+
## License
332+
333+
This project is licensed under the MIT License.

0 commit comments

Comments
 (0)