âšGemini à ðŠLangChainå ¥é
ãŸãåãã«
GeminiãšLangChainã«é¢ããèšäºããŸã ããŸããªãã£ãã®ã§ãåå¿é²ãå ŒããŠæžããŠã¿ãŸãããAIç³»ã®ãµãŒãã¹ã䜿ãããªãããšäœåã®å¹ ã倧ããåºãããšæãã®ã§ããã²èªãã§ã¿ãŠãã ããã
ããšãããããã³ã¡ã³ãããã ãããšèšäºäœæã®å±ã¿ã«ãªããŸãð
ãã®èšäºã«å¯Ÿè±¡è
- Geminiã«èå³ã®ãã人
- LangChainã«èå³ã®ãã人
Geminiãšã¯
Geminiãšã¯ãGoogleãæ°ããå ¬éãã人工ç¥èœã¢ãã«ã§ããGeminiã¯3çš®é¡ã®ããŒãžã§ã³ãããããã®äžã§ãä»åæ±ãGemini Proã¯ç¡æã§1åããã60ãªã¯ãšã¹ããŸã§å©çšå¯èœã§ãã
LangChainãšã¯
LangChainãšã¯ãGPTãGeminiãªã©ã®LLM(倧èŠæš¡èšèªã¢ãã«)ãæ¡åŒµã§ããã©ã€ãã©ãªãšãªã£ãŠããŸããçŸåšã察å¿ããŠããèšèªã¯PythonãšTypeScript(JavaScript)ã§ãã
ä»å䜿ãããŒãž
LangChainããããèšäº
ç°å¢æ§ç¯
ä»åã¯Node.jsã䜿ã£ãŠããã¢ãè¡ããããšæããŸãã
Node.jsã®æºå
Node.jsããŸã å ¥ããŠããªããšãã人ã¯ãã¡ãã®ãµã€ãããããŠã³ããŒãããé¡ãããŸãã
ãŸããä»»æã®ååã®ãã©ã«ããäœããVScodeã§éããŸãããããã©ã«ãã®çŽäžã«index.js
ãã¡ã€ã«ãäœããäžèšã®ã³ãã³ããVScodeã®ã¿ãŒããã«ã«å
¥åããŸãããã
$ npm init -y
次ã«å¿ èŠãªã©ã€ãã©ãªãã€ã³ã¹ããŒã«ããŠããŸãã
$ npm install nodemon
import
ã䜿ããããã«ããããã«ãpackage.json
ãç·šéããŸãããã
{
"name": "gemini-langchain",
"version": "1.0.0",
"description": "",
"main": "index.js",
+ "type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2",
"nodemon": "^3.0.2"
}
}
index.js
ã®äžã«äžèšã®ã³ãŒããæžããŸãããã
console.log('Hello');
ã³ãã³ããå®è¡ããŸãããã
$ nodemon index.js
ã¿ãŒããã«ã«Helloãšåºåããããæåã§ãã
GeminiãšLangChainã®æºå
次ã«GeminiãšLangChainã®ã©ã€ãã©ãªãã€ã³ã¹ããŒã«ããŸãããã
$ npm install @google/generative-ai
$ npm install @langchain/google-genai
LangChainã®å
¬åŒããã¥ã¡ã³ãããåŒã£åŒµã£ãŠããé圢ãindex.js
ã«æžããŸããããå
ã»ã©æžããã³ãŒãã¯æ¶ããŠåé¡ãããŸããã
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
import { HarmBlockThreshold, HarmCategory } from "@google/generative-ai";
const model = new ChatGoogleGenerativeAI({
modelName: "gemini-pro",
maxOutputTokens: 2048,
safetySettings: [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
},
],
});
const res = await model.invoke([
[
"human",
"What would be a good company name for a company that makes colorful socks?",
],
]);
console.log(res);
APIKeyããªããšãšã©ãŒãåãããã®ã§ãAPIKeyãååŸããŸãã
APIKeyã®ååŸ
Google AI StudioããAPIKeyãååŸããŸãããã
æåã«å©çšèŠçŽãåºãŠããã®ã§ãåæããŠããµã€ãã¡ãã¥ãŒã®Get API keyãæŒããŸãããã
èµ€æ ãã¯ãªãã¯ããæ°ããAPI keyãçæããŠãã³ããŒããŠãããŸãããã
APIKeyãèšå®ãã
index.js
ã®modelã®éšåã«apiKeyãèšå®ããŸãããã
const model = new ChatGoogleGenerativeAI({
+ apiKey: '<ååŸããAPIKey>',
modelName: "gemini-pro",
maxOutputTokens: 2048,
safetySettings: [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
},
],
});
䜿ã£ãŠã¿ã
index.js
ã®resã®éšåãäžèšã®ããã«ç·šéããŠã¿ãŠãã ããã
const res = await model.invoke([
[
"human",
"ããã«ã¡ã¯!",
],
]);
nodemonã¯ãã©ã«ããç£èŠããå€æŽããªããããšãå床å®è¡ããããããã¿ãŒããã«ã«çµæãåºåãããŠãããšæããŸããcontent
ã®éšåã«ãAIããã®è¿çããããŸãã
AIMessage {
lc_serializable: true,
lc_kwargs: {
content: 'ããã«ã¡ã¯! ããªãã«ãäŒãã§ããŠãšãŠãå¬ããæããŸããç§ã¯ããªãã®ãæäŒããããããšæã£ãŠããã¢ã·ã¹ã¿ã³ãã§ããäœãã質åããäžæãªç¹ããŸãã¯ãäŸé Œã¯ããããŸãã?',
name: 'model',
additional_kwargs: { finishReason: 'STOP', index: 0, safetyRatings: [Array] }
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: 'ããã«ã¡ã¯! ããªãã«ãäŒãã§ããŠãšãŠãå¬ããæããŸããç§ã¯ããªãã®ãæäŒããããããšæã£ãŠããã¢ã·ã¹ã¿ã³ãã§ããäœãã質åããäžæãªç¹ããŸãã¯ãäŸé Œã¯ããããŸãã?',
name: 'model',
additional_kwargs: {
finishReason: 'STOP',
index: 0,
safetyRatings: [ [Object], [Object], [Object], [Object] ]
}
}
å¥ã®ãã¿ãŒã³ãè©Šã
ããã¥ã¡ã³ãã«ã¯ç»åãéã£ãŠãããã¹ãã§è¿ããImage to Textã®ãã¢ãã®ã£ãŠãããã§ãããè©ŠããŠã¿ãŸãããã
ãŸãã¯ãç»åã®ããŒã¿ã64é²æ°ã«å€æããããã«å¿ èŠãªã©ã€ãã©ãªãã€ã³ã¹ããŒã«ããŸãã
$ npm install fs
次ã«ã«ãŒããã£ã¬ã¯ããª(index.js
ãšåéå±€)ã«ãimages
ãã£ã¬ã¯ããªãäœããAIã«æãããç»åãå
¥ããŠãããŸãããããŸããæ¥æ¬èªã§è¿çããŠæ¬²ããã®ã§ãinput2ã®textã®éšåãæ¥æ¬èªã«å€æŽããŠãããŸãããã
ç¹ã«äœã§ããã人ã¯äžèšã®ç»åãhotdog.png
ãšããŠãä¿åããŸãããã
index.js
ã®äžèº«ãæ¶ããŠãäžèšã貌ãä»ããŠãã ãããå
ã»ã©åæ§ãapiKey
ãèšå®ããç»åã®ãã¹ãä¿®æ£ããŠãã ããã
import fs from "fs";
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
import { HumanMessage } from "@langchain/core/messages";
const vision = new ChatGoogleGenerativeAI({
+ apiKey: '<ååŸããAPIKey>',
modelName: "gemini-pro-vision",
maxOutputTokens: 2048,
});
+ const image = fs.readFileSync("images/hotdog.png").toString("base64");
const input2 = [
new HumanMessage({
content: [
{
type: "text",
+ text: "次ã®ç»åã«ã€ããŠã説æããŠãã ããã",
},
{
type: "image_url",
image_url: `data:image/png;base64,${image}`,
},
],
}),
];
const res2 = await vision.invoke(input2);
console.log(res2);
}
çæã«ã¯å°ãæéãããããŸãã
å
ã»ã©åæ§ãcontent
ã®äžã«ãAIããã®è¿çããããŸãããã£ãããšç»åããããããã°ã§ããããšãç解ããŠãããŠããŸãã
AIMessage {
lc_serializable: true,
lc_kwargs: {
content: ' ãããããã°ã®ç»åã§ãããããããã°ã¯ããœãŒã»ãŒãžã现é·ããã³ã«æãã æçã§ãããœãŒã»ãŒãžã¯ãè±èãçèãªã©ã®æœèãçŸè
žãè±è
žã«è©°ããŠå ç±ãããã®ã§ãããã³ã¯ãå°éºŠç²ãæ°Žãã€ãŒã¹ãèãç ç³ãå¡©ãªã©ã§äœã£ããã®ã§ãããããããã°ã«ã¯ãã±ãã£ããããã¹ã¿ãŒãããã¯ã«ã¹ããªããªã³ãããŒãºãªã©ã®ãããã³ã°ãããåããŸãã',
name: 'model',
additional_kwargs: { finishReason: 'STOP', index: 0, safetyRatings: [Array] }
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: ' ãããããã°ã®ç»åã§ãããããããã°ã¯ããœãŒã»ãŒãžã现é·ããã³ã«æãã æçã§ãããœãŒã»ãŒãžã¯ãè±èãçèãªã©ã®æœèãçŸè
žãè±è
žã«è©°ããŠå ç±ãããã®ã§ãããã³ã¯ãå°éºŠç²ãæ°Žãã€ãŒã¹ãèãç ç³ãå¡©ãªã©ã§äœã£ããã®ã§ãããããããã°ã«ã¯ãã±ãã£ããããã¹ã¿ãŒãããã¯ã«ã¹ããªããªã³ãããŒãºãªã©ã®ãããã³ã°ãããåããŸãã',
name: 'model',
additional_kwargs: {
finishReason: 'STOP',
index: 0,
safetyRatings: [ [Object], [Object], [Object], [Object] ]
}
}
ãŸãšã
ãããŸã§èªãã§ããã ãããããšãããããŸããã
ä»åã¯ãGeminiãšLangchainã«ã€ããŠå ¥éããŸãããLangchainã¯ãŸã ãŸã å€ãã®æ©èœãæäŸããŠããã®ã§ãGemini APIãç¡æã§äœ¿ãããã¡ã«è²ã è©ŠããŠãããŸãããã
æ®æ®µã¯Webå¶äœã«é¢ããæ å ±ãçåããããããããŸãšããŠèšäºãæžããŠããã®ã§ããã¡ããèªãã§ããã ãããå¬ããã§ãã
Discussion