|
|
Colab 노트북 사용해 보기
|
GitHub에서 노트북 보기
|
API 키 가져오기
시작하려면 API 키를 가져와야 합니다. 환경 변수로 설정합니다.
import os
os.environ["API_KEY"] = "<YOUR API KEY>"
API 클라이언트 설치
새 디렉터리에서 npm을 사용하여 Node.js 프로젝트를 초기화하고
google-auth 라이브러리:
npm init -ynpm install google-auth-library
Wrote to /content/package.json:
{
"name": "content",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@google-ai/generativelanguage": "^1.0.1",
"google-auth-library": "^9.0.0"
},
"devDependencies": {},
"description": ""
}
+ google-auth-library@9.0.0
updated 1 package and audited 74 packages in 1.105s
3 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
npm WARN content@1.0.0 No description
npm WARN content@1.0.0 No repository field.
다음으로 생성형 언어 클라이언트 라이브러리를 설치해야 합니다.
npm install @google-ai/generativelanguage
+ @google-ai/generativelanguage@1.0.1 updated 1 package and audited 74 packages in 2.126s 3 packages are looking for funding run `npm fund` for details found 0 vulnerabilities npm WARN content@1.0.0 No description npm WARN content@1.0.0 No repository field.
메시지 생성
새 파일 index.js를 만들고 다음 코드를 추가하여 API 키를 제공합니다.
다음과 같이 API_KEY 환경 변수를 통해 수정합니다.
%%writefile index.js
const { TextServiceClient } =
require("@google-ai/generativelanguage").v1beta2;
const { GoogleAuth } = require("google-auth-library");
const MODEL_NAME = "models/text-bison-001";
const API_KEY = process.env.API_KEY;
const client = new TextServiceClient({
authClient: new GoogleAuth().fromAPIKey(API_KEY),
});
const prompt = "Repeat after me: one, two,";
client
.generateText({
model: MODEL_NAME,
prompt: {
text: prompt,
},
})
.then((result) => {
console.log(JSON.stringify(result, null, 2));
});
Overwriting index.js
그런 다음 스크립트를 실행합니다.
node index.js
[
{
"candidates": [
{
"safetyRatings": [
{
"category": "HARM_CATEGORY_DEROGATORY",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_TOXICITY",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_VIOLENCE",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_SEXUAL",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_MEDICAL",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_DANGEROUS",
"probability": "NEGLIGIBLE"
}
],
"output": "One, two, three, four."
}
],
"filters": [],
"safetyFeedback": []
},
null,
null
]
Colab 노트북 사용해 보기
GitHub에서 노트북 보기