Language/Nodejs

[nodejs] bard api 무료사용하기

멱군 2023. 7. 14. 06:37

챗지피티(chatGPT)를 너무 잘사용하고 있지만, 구글의 AI 모델인 바드(bard)도 잘 쓰고 있다.

초창기에는 한글을 인식못해서 사용하기 애매한것도 있었고,  답변도 썩 좋지는 않았는데, 한글패치가 되면서 답변 퀄리티도 괜찮고 원하는 바를 잘 알려준다. 뿐만아니라 최신정보도 알려주기 때문에 점점 괜찮아지고 있다.

그리고 bard API가 공개되었다고 한다.

그래서 한번 적용해보기로 했다.

 

Node.js로 Bard API를 사용하려면 다음 단계를 따르세요.

1. Google Cloud Platform 콘솔에서 Bard API를 활성화합니다.

  1. Google Cloud Platform 콘솔에 로그인합니다.
  2. [API 및 서비스] 페이지로 이동합니다.
  3. [라이브러리] 탭을 클릭합니다.
  4. [Bard API]를 검색합니다.
  5. [Bard API]를 클릭합니다.
  6. [활성화]를 클릭합니다.

 

검색창에 bard api검색어 넣은 이미지

 

2. Node.js 애플리케이션을 만듭니다.

npm init

 

3. Bard API를 호출하는 코드를 작성합니다. 

Bard API를 호출하는 코드를 작성합니다.

const https = require('https');

const options = {
  host: 'bard.googleapis.com',
  port: 443,
  path: '/v1/text/generate',
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${process.env.GOOGLE_APPLICATION_CREDENTIALS}`,
    'Content-Type': 'application/json',
  }
};

const body = {
  prompt: 'What is the meaning of life?',
  max_tokens: 100,
  temperature: 0.7,
  presence_penalty: 0.2,
  fluency_penalty: 0.1
};

const request = https.request(options, (response) => {
  response.on('data', (data) => {
    console.log(data.toString());
  });

  response.on('end', () => {
    console.log('Response ended');
  });
});

request.write(JSON.stringify(body));
request.end();

 

4. 애플리케이션을 실행합니다.

node app.js

 

결과

애플리케이션을 실행하면 다음과 같은 결과가 출력됩니다.

{"text":"The meaning of life is to find your purpose and live it to the fullest.}

 

bard api를 사용하는데 대기하는 방법에 대한 자세한 내용은 이 링크를 참고하면 된다.

 

Google Bard API

My request to join the Bard preview was recently accepted and I've begun evaluating it. So far it looks amazing and I'm anxious to begin working with it as a developer. I asked Bard if there was an API that I can start to use and it responded with the info

www.googlecloudcommunity.com