Gemini
出た直後の情報はっつけ
公式、利用例 notebooks
画像に関するベスト プラクティス
がある、テキストの前に画像を置く / 複数置く場合はインデックスつける等
モデル情報
en のほう見るべき
pro は json schema 指定できるようになっていた
Colab でちょっと試す時のコピペ用
code:colab.py
import google
google.colab.auth.authenticate_user()
import vertexai
from vertexai.generative_models import (
GenerationConfig,
GenerativeModel,
)
project = "pokutuna-playground"
location = "us-central1"
vertexai.init(project=project, location=location)
model_id = "gemini-1.5-pro-001" # or "gemini-1.5-flash-001"
model = GenerativeModel(
model_id,
# system_instructions="",
)
generation_config = GenerationConfig(
candidate_count=1,
# ...
)
res = model.generate_content(
generation_config=generation_config,
)
print(res.text)
token 数見る
とある電子書籍1冊入れるとして
code:count_tokens.py
from pprint import pprint
import vertexai
from vertexai.generative_models import (
GenerativeModel,
)
project = "pokutuna-playground"
location = "asia-northeast1"
vertexai.init(project=project, location=location)
model_id = "gemini-1.5-pro-preview-0409"
model = GenerativeModel(model_id)
content = data0.page_content pprint(f"chars: {len(content)}")
# => chars: 240926
pprint(f"tokens: {model.count_tokens(content)}")
# => tokens: total_tokens: 117983\ntotal_billable_characters: 232291\n
node で readline 使いつつチャット
code:chat.ts
import { VertexAI } from "@google-cloud/vertexai";
import * as readline from "readline";
const project = "pokutuna-playground";
const location = "us-central1";
const model = "gemini-1.5-pro-preview-0409";
const vertexAI = new VertexAI({ project, location });
const genModel = vertexAI.getGenerativeModel({ model });
const chat = genModel.startChat();
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
async function* userMessageGenerator(rl: readline.Interface): AsyncGenerator<string> {
while (true) {
const input = await new Promise<string>((resolve) => {
rl.question("> ", (input) => resolve(input));
});
if (/^\s*$/.test(input)) break;
yield input;
}
}
for await (const input of userMessageGenerator(rl)) {
const res = await chat.sendMessageStream(input);
for await (const item of res.stream) {
process.stdout.write(item.candidates?.0.content.parts0.text || ""); }
}
rl.close();