From f5fea21d46f9e836a84d1eb0174a00d351ae9893 Mon Sep 17 00:00:00 2001 From: Shunta Saito Date: Tue, 3 Dec 2024 18:55:16 +0900 Subject: [PATCH 1/3] Update function_calling_basic --- examples/function_calling_basic/README.md | 4 ++-- examples/function_calling_basic/main.py | 19 ++++++++----------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/examples/function_calling_basic/README.md b/examples/function_calling_basic/README.md index 6699dc6..a5e0e49 100644 --- a/examples/function_calling_basic/README.md +++ b/examples/function_calling_basic/README.md @@ -13,10 +13,10 @@ get_current_feather(location: string, unit: string) ## 使い方 -まず、環境変数 `OPENAI_API_KEY` に、[PLaMo β版 トライアル](https://plamo.preferredai.jp/)で申し込みを行なって入手したAPIキーをセットしてください。 +まず、環境変数 `PLAMO_API_KEY` に、[PLaMo Prime](https://plamo.preferredai.jp/)で申し込みを行なっていただき入手したAPIキーをセットしてください。 ```sh -export OPENAI_API_KEY= +export PLAMO_API_KEY= ``` 次に、必要なパッケージをインストールして、サンプルを実行します。 diff --git a/examples/function_calling_basic/main.py b/examples/function_calling_basic/main.py index 1ebe806..815718b 100644 --- a/examples/function_calling_basic/main.py +++ b/examples/function_calling_basic/main.py @@ -1,6 +1,10 @@ +import os + from openai import OpenAI + client = OpenAI( - base_url="https://platform.preferredai.jp/api/completion/v1" + base_url="https://platform.preferredai.jp/api/completion/v1", + api_key=os.environ["PLAMO_API_KEY"], ) function = dict( @@ -16,15 +20,13 @@ ), ) -tools = [ - dict(type="function", function=function) -] +tools = [dict(type="function", function=function)] user_input = "今日の東京の気温は?" result = client.chat.completions.create( - model="plamo-beta", + model="plamo-1.0-prime", messages=[ {"role": "user", "content": user_input}, ], @@ -33,12 +35,7 @@ top_p=0.9, n=1, tools=tools, - tool_choice={ - "type": "function", - "function": { - "name": "get_current_weather" - } - }, + tool_choice={"type": "function", "function": {"name": "get_current_weather"}}, ) print(f"User input: {user_input}") From 64646bf1d537bec191d0c658866ad59c917ad603 Mon Sep 17 00:00:00 2001 From: Toshihiko Yanase Date: Fri, 13 Dec 2024 16:18:16 +0900 Subject: [PATCH 2/3] Add JavaScript example --- README.md | 2 +- examples/simple_examples/openai-js/README.md | 50 ++++++++++++++++++++ examples/simple_examples/openai-js/sample.js | 20 ++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 examples/simple_examples/openai-js/README.md create mode 100644 examples/simple_examples/openai-js/sample.js diff --git a/README.md b/README.md index 1b48a10..97c347b 100644 --- a/README.md +++ b/README.md @@ -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/) - 過去の会話履歴を参照しながらユーザーとの会話を行うサンプルです。 - 会話履歴全体が処理できるトークン数の限界に達した場合は、履歴のうち前半半分を要約して短くし、これと置き換えることで会話を継続できるようにしています。 diff --git a/examples/simple_examples/openai-js/README.md b/examples/simple_examples/openai-js/README.md new file mode 100644 index 0000000..97f1dfd --- /dev/null +++ b/examples/simple_examples/openai-js/README.md @@ -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= +``` + +## 実行 + +下記コマンドで `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: [] +} +``` diff --git a/examples/simple_examples/openai-js/sample.js b/examples/simple_examples/openai-js/sample.js new file mode 100644 index 0000000..0941410 --- /dev/null +++ b/examples/simple_examples/openai-js/sample.js @@ -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(); \ No newline at end of file From ed6d6b52d290779e0c64beb6a01a755d89906b6f Mon Sep 17 00:00:00 2001 From: Toshihiko Yanase Date: Fri, 13 Dec 2024 16:40:20 +0900 Subject: [PATCH 3/3] Update examples/simple_examples/openai-js/README.md Co-authored-by: Shunta Saito --- examples/simple_examples/openai-js/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/simple_examples/openai-js/README.md b/examples/simple_examples/openai-js/README.md index 97f1dfd..8b4ade2 100644 --- a/examples/simple_examples/openai-js/README.md +++ b/examples/simple_examples/openai-js/README.md @@ -1,5 +1,5 @@ openai javascript example -======================== +========================= JavaScriptの [`openai` ライブラリ](https://github.com/openai/openai-node)を用いて、最低限APIをリクエストするためのサンプル