Skip to content

Commit

Permalink
Add JavaScript example
Browse files Browse the repository at this point in the history
  • Loading branch information
toshihikoyanase authored and mitmul committed Dec 13, 2024
1 parent 57bbe59 commit 64646bf
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

- [Simple Examples](./examples/simaple_examples/)
- 最も単純に、最低限の設定でAPIを利用するサンプルです。初回API利用時等、正しくアクセスできていることを確認するためにご利用いただけます。
- `curl` および Pythonの `openai` ライブラリ、`langchain` ライブラリを用いたサンプルを記載しています。
- `curl` および Pythonの `openai` ライブラリと `langchain` ライブラリ、Javascriptの `openai` ライブラリを用いたサンプルを記載しています。
- [Conversation with Memory](./examples/conversation_with_memory/)
- 過去の会話履歴を参照しながらユーザーとの会話を行うサンプルです。
- 会話履歴全体が処理できるトークン数の限界に達した場合は、履歴のうち前半半分を要約して短くし、これと置き換えることで会話を継続できるようにしています。
Expand Down
50 changes: 50 additions & 0 deletions examples/simple_examples/openai-js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
openai javascript example
========================

JavaScriptの [`openai` ライブラリ](https://github.com/openai/openai-node)を用いて、最低限APIをリクエストするためのサンプル

## セットアップ

### パッケージのインストール

```
npm install openai
```

### 環境変数の設定

環境変数 `PLAMO_API_KEY` に、[PLaMo Webサイト](https://plamo.preferredai.jp/)で申し込みを行ない、入手したAPIキーをセットしてください。

```sh
export PLAMO_API_KEY=<YOUR_API_KEY>
```

## 実行

下記コマンドで `sample.js` を実行してください。

```
node sample.js
```

## 実行結果例

```javascript
{
role: 'assistant',
content: '二次方程式の解の公式は次の通りです。\n' +
'\n' +
'x = (-b ± √(b^2 - 4ac)) / 2a\n' +
'\n' +
'二次方程式が ax^2 + bx + c = 0 の形式であるとき、この公式を使って解を求めることができます。\n' +
'\n' +
'a、b、cはそれぞれ二次方程式の係数であり、+√(b^2 - 4ac)の部分は、判別式と呼ばれます。判別式の値によって、二次方程式の解の数が決まります。\n' +
'\n' +
'・判別式が0の場合、解は1つです。\n' +
'・判別式が正の場合、解は2つです。\n' +
'・判別式が負の場合、解はありません。(二次方程式に実数解がないということです。)\n' +
'\n' +
'以上のように、この公式は二次方程式を解くために非常に重要な公式となります。',
tool_calls: []
}
```
20 changes: 20 additions & 0 deletions examples/simple_examples/openai-js/sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { OpenAI } = require('openai')

const openai = new OpenAI({
baseURL: 'https://platform.preferredai.jp/api/completion/v1',
apiKey: process.env.PLAMO_API_KEY
// other params...
});

async function main() {
const response = await openai.chat.completions.create({
model: 'plamo-1.0-prime',
messages: [
{role: 'system', content:'あなたは学校の先生です'},
{ role: 'user', content: '二次方程式の解の公式を端的に教えてください' }
],
});
console.log(response.choices[0].message);
}

main();

0 comments on commit 64646bf

Please sign in to comment.