[{"content":"OpenAIのAI、function_callのパラメータについて、もうちょっと調べてみました。\n試してみた結果、type以外の細かいことは、descriptionに書けということです。\nJSONスキーマっての見たら、もうちょっと細かい指定ができるんではないか？と思って、 https://json-schema.org/\nまずは、minLengthとmaxLengthを試してみた。 [https://json-schema.org/understanding-json-schema/reference/string.html#id5]\nimport openai import json def submit(): messages = [{\u0026#34;role\u0026#34;: \u0026#34;user\u0026#34;, \u0026#34;content\u0026#34;: \u0026#34;test the sample_function\u0026#34;}] functions = [ { \u0026#34;name\u0026#34;: \u0026#34;sample_function\u0026#34;, \u0026#34;description\u0026#34;: \u0026#34;test for parameters\u0026#34;, \u0026#34;parameters\u0026#34;: { \u0026#34;type\u0026#34;: \u0026#34;object\u0026#34;, \u0026#34;properties\u0026#34;: { \u0026#34;arg1\u0026#34;: { \u0026#34;type\u0026#34;: \u0026#34;string\u0026#34;, \u0026#34;minLength\u0026#34;: 1, \u0026#34;maxLength\u0026#34;: 2, \u0026#34;description\u0026#34;: \u0026#34;string data\u0026#34;, } }, \u0026#34;required\u0026#34;: [\u0026#34;arg1\u0026#34;], }, } ] try: response = openai.ChatCompletion.create( model=\u0026#34;gpt-3.5-turbo-0613\u0026#34;, messages=messages, functions=functions, function_call=\u0026#34;auto\u0026#34;, ) print( response.choices[0] ) except Exception as ex: return ex.args submit() 実行結果\n$ python3 test.py { \u0026#34;finish_reason\u0026#34;: \u0026#34;function_call\u0026#34;, \u0026#34;index\u0026#34;: 0, \u0026#34;message\u0026#34;: { \u0026#34;content\u0026#34;: null, \u0026#34;function_call\u0026#34;: { \u0026#34;arguments\u0026#34;: \u0026#34;{\\n \\\u0026#34;arg1\\\u0026#34;: \\\u0026#34;Hello World\\\u0026#34;\\n}\u0026#34;, \u0026#34;name\u0026#34;: \u0026#34;sample_function\u0026#34; }, \u0026#34;role\u0026#34;: \u0026#34;assistant\u0026#34; } } minLengthとmaxLengthはあっさり無視されるらしい。\n次は、[https://json-schema.org/understanding-json-schema/reference/string.html#id6](Regular Expressions)\nこれが使えたら、結構うれしいんじゃないか？\n\u0026#34;properties\u0026#34;: { \u0026#34;arg1\u0026#34;: { \u0026#34;type\u0026#34;: \u0026#34;string\u0026#34;, \u0026#34;pattern\u0026#34;: \u0026#34;^(\\\\([0-9]{3}\\\\))?[0-9]{3}-[0-9]{4}$\u0026#34;, \u0026#34;description\u0026#34;: \u0026#34;string data\u0026#34;, } }, 実行結果\n$ python3 test.py { \u0026#34;finish_reason\u0026#34;: \u0026#34;function_call\u0026#34;, \u0026#34;index\u0026#34;: 0, \u0026#34;message\u0026#34;: { \u0026#34;content\u0026#34;: null, \u0026#34;function_call\u0026#34;: { \u0026#34;arguments\u0026#34;: \u0026#34;{\\n \\\u0026#34;arg1\\\u0026#34;: \\\u0026#34;Hello World\\\u0026#34;\\n}\u0026#34;, \u0026#34;name\u0026#34;: \u0026#34;sample_function\u0026#34; }, \u0026#34;role\u0026#34;: \u0026#34;assistant\u0026#34; } } やっぱり無視されました。ですよね〜〜〜〜。\nもしかして type に numberを指定しても無視するんじゃないか？　と思って\nimport openai import json def submit(): messages = [{\u0026#34;role\u0026#34;: \u0026#34;user\u0026#34;, \u0026#34;content\u0026#34;: \u0026#34;generate test data for sample_function. and test it.\u0026#34;}] functions = [ { \u0026#34;name\u0026#34;: \u0026#34;sample_function\u0026#34;, \u0026#34;description\u0026#34;: \u0026#34;test for parameters\u0026#34;, \u0026#34;parameters\u0026#34;: { \u0026#34;type\u0026#34;: \u0026#34;object\u0026#34;, \u0026#34;properties\u0026#34;: { \u0026#34;arg1\u0026#34;: { \u0026#34;type\u0026#34;: \u0026#34;number\u0026#34;, \u0026#34;description\u0026#34;: \u0026#34;\u0026#34;, } }, \u0026#34;required\u0026#34;: [\u0026#34;arg1\u0026#34;], }, } ] try: response = openai.ChatCompletion.create( model=\u0026#34;gpt-3.5-turbo-0613\u0026#34;, messages=messages, functions=functions, function_call=\u0026#34;auto\u0026#34;, ) print( response.choices[0] ) except Exception as ex: return ex.args submit() 実行結果\n{ \u0026#34;finish_reason\u0026#34;: \u0026#34;stop\u0026#34;, \u0026#34;index\u0026#34;: 0, \u0026#34;message\u0026#34;: { \u0026#34;content\u0026#34;: \u0026#34;To generate test data for `sample_function`, I need to know the expected output or the desired behavior of the function. Please provide more information about the expected behavior, any constraints on the input, and the desired output format.\u0026#34;, \u0026#34;role\u0026#34;: \u0026#34;assistant\u0026#34; } } うーむ。functoinの仕様を明確にしろってか？？？ なら、これでどうだ？\n\u0026#34;name\u0026#34;: \u0026#34;sample_function\u0026#34;, \u0026#34;description\u0026#34;: \u0026#34;return tha input data, just sample\u0026#34;, \u0026#34;parameters\u0026#34;: { \u0026#34;type\u0026#34;: \u0026#34;object\u0026#34;, \u0026#34;properties\u0026#34;: { \u0026#34;arg1\u0026#34;: { \u0026#34;type\u0026#34;: \u0026#34;number\u0026#34;, \u0026#34;description\u0026#34;: \u0026#34;\u0026#34;, } }, \u0026#34;required\u0026#34;: [\u0026#34;arg1\u0026#34;], }, 実行結果\n{ \u0026#34;finish_reason\u0026#34;: \u0026#34;function_call\u0026#34;, \u0026#34;index\u0026#34;: 0, \u0026#34;message\u0026#34;: { \u0026#34;content\u0026#34;: null, \u0026#34;function_call\u0026#34;: { \u0026#34;arguments\u0026#34;: \u0026#34;{\\n \\\u0026#34;arg1\\\u0026#34;: 5\\n}\u0026#34;, \u0026#34;name\u0026#34;: \u0026#34;sample_function\u0026#34; }, \u0026#34;role\u0026#34;: \u0026#34;assistant\u0026#34; } } おお！ 数字ってのは守ってくれるらしい。\nじゃあ、範囲指定はどうか？\n\u0026#34;arg1\u0026#34;: { \u0026#34;type\u0026#34;: \u0026#34;number\u0026#34;, \u0026#34;minimum\u0026#34;: 100, \u0026#34;maximum\u0026#34;: 200, \u0026#34;description\u0026#34;: \u0026#34;\u0026#34;, } 実行結果\n{ \u0026#34;finish_reason\u0026#34;: \u0026#34;function_call\u0026#34;, \u0026#34;index\u0026#34;: 0, \u0026#34;message\u0026#34;: { \u0026#34;content\u0026#34;: null, \u0026#34;function_call\u0026#34;: { \u0026#34;arguments\u0026#34;: \u0026#34;{\\n \\\u0026#34;arg1\\\u0026#34;: 10\\n}\u0026#34;, \u0026#34;name\u0026#34;: \u0026#34;sample_function\u0026#34; }, \u0026#34;role\u0026#34;: \u0026#34;assistant\u0026#34; } } うむ、やっぱり範囲指定は無視するらしい。\nつまり以下のようにdescriptionに書けば・・・・\n\u0026#34;arg1\u0026#34;: { \u0026#34;type\u0026#34;: \u0026#34;number\u0026#34;, \u0026#34;description\u0026#34;: \u0026#34;range 100 to 200\u0026#34;, } 実行結果\n{ \u0026#34;finish_reason\u0026#34;: \u0026#34;function_call\u0026#34;, \u0026#34;index\u0026#34;: 0, \u0026#34;message\u0026#34;: { \u0026#34;content\u0026#34;: null, \u0026#34;function_call\u0026#34;: { \u0026#34;arguments\u0026#34;: \u0026#34;{\\n \\\u0026#34;arg1\\\u0026#34;: 150\\n}\u0026#34;, \u0026#34;name\u0026#34;: \u0026#34;sample_function\u0026#34; }, \u0026#34;role\u0026#34;: \u0026#34;assistant\u0026#34; } } ちゃんと守ってくれるらしい。\n","date":"2023-06-18T11:37:07+09:00","permalink":"https://route250.github.io/p/function_call-api%E3%81%AE%E3%83%91%E3%83%A9%E3%83%A1%E3%83%BC%E3%82%BF%E3%82%BF%E3%82%A4%E3%83%97%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6%E3%82%82%E3%81%A3%E3%81%A8%E8%AA%BF%E3%81%B9%E3%81%A6%E3%81%BF%E3%81%9F/","title":"function_call APIのパラメータタイプについて、もっと調べてみた"},{"content":"OpenAIのAPIで、fucntion_callが発表されました。 英語が苦手なのでドキュメントを読んでも手探り状態です。 一番悩んだのがfunctionの引数の指定の仕方です。 JSON Schemaで指定するらしいんですけど？ https://json-schema.org/\n実際にどうなるか、テストコードで調べて見ました。 結果から推測するには、typeに指定できるのは下記っぽい\nstring number integer boolean object うーん？ スキーマとしては配列もいけそうな気がしたのだが・・・・\nテスト用コードです\nimport openai import json def submit(data): messages = [{\u0026#34;role\u0026#34;: \u0026#34;user\u0026#34;, \u0026#34;content\u0026#34;: data[\u0026#34;mesg\u0026#34;]}] functions = [ { \u0026#34;name\u0026#34;: data[\u0026#34;func_name\u0026#34;], \u0026#34;description\u0026#34;: data[\u0026#34;func_description\u0026#34;], \u0026#34;parameters\u0026#34;: { \u0026#34;type\u0026#34;: \u0026#34;object\u0026#34;, \u0026#34;properties\u0026#34;: { data[\u0026#34;prop_name\u0026#34;]: { \u0026#34;type\u0026#34;: data[\u0026#34;prop_type\u0026#34;], \u0026#34;description\u0026#34;: data[\u0026#34;prop_descriptoin\u0026#34;], } }, \u0026#34;required\u0026#34;: [data[\u0026#34;required\u0026#34;]], }, } ] try: response = openai.ChatCompletion.create( model=\u0026#34;gpt-3.5-turbo-0613\u0026#34;, messages=messages, functions=functions, function_call=\u0026#34;auto\u0026#34;, ) #print( response ) return response.choices[0].message.content except Exception as ex: return ex.args data={} data[\u0026#34;mesg\u0026#34;] = \u0026#39;hello\u0026#39; data[\u0026#34;func_name\u0026#34;] = \u0026#39;b\u0026#39; data[\u0026#34;func_description\u0026#34;] = \u0026#34;c\u0026#34; data[\u0026#34;prop_name\u0026#34;] = \u0026#39;d\u0026#39; data[\u0026#34;prop_type\u0026#34;] = \u0026#39;string\u0026#39; data[\u0026#34;prop_descriptoin\u0026#34;] = \u0026#39;e\u0026#39; data[\u0026#34;required\u0026#34;] = \u0026#39;f\u0026#39; print( \u0026#34;{:16s} {}\u0026#34;.format(\u0026#34;type\u0026#34;,\u0026#34;output\u0026#34;)) for t in [\u0026#39;askdfja\u0026#39;,\u0026#39;string\u0026#39;,\u0026#39;String\u0026#39;,\u0026#39;integer\u0026#39;,\u0026#39;number\u0026#39;,\u0026#39;number[]\u0026#39;,\u0026#39;float\u0026#39;,\u0026#39;float{}\u0026#39;,\u0026#39;{}\u0026#39;,\u0026#39;[]\u0026#39;,\u0026#39;double\u0026#39;,\u0026#39;bool\u0026#39;,\u0026#39;boolean\u0026#39;,\u0026#39;dictionary\u0026#39;,\u0026#39;array\u0026#39;,\u0026#39;array of string\u0026#39;,\u0026#39;object\u0026#39;,\u0026#39;オブジェクト\u0026#39;,\u0026#39;文字列\u0026#39;,\u0026#39;文字\u0026#39;,\u0026#39;{\u0026#34;aaa\u0026#34;:\u0026#34;bbb\u0026#34;}\u0026#39;,\u0026#39;[string]\u0026#39;,\u0026#39;string,number,boolean\u0026#39;]: data[\u0026#34;prop_type\u0026#34;] = t print( \u0026#34;{:16s} {}\u0026#34;.format(t,submit(data))) 実行結果、こうなりましたよ\ntype output askdfja (\u0026#34;Invalid schema for function \u0026#39;b\u0026#39;: \u0026#39;askdfja\u0026#39; is not valid under any of the given schemas\u0026#34;,) string Hello! How can I assist you today? String (\u0026#34;Invalid schema for function \u0026#39;b\u0026#39;: \u0026#39;String\u0026#39; is not valid under any of the given schemas\u0026#34;,) integer Hi, how can I assist you today? number Hello! How can I assist you today? number[] (\u0026#34;Invalid schema for function \u0026#39;b\u0026#39;: \u0026#39;number[]\u0026#39; is not valid under any of the given schemas\u0026#34;,) float (\u0026#34;Invalid schema for function \u0026#39;b\u0026#39;: \u0026#39;float\u0026#39; is not valid under any of the given schemas\u0026#34;,) float{} (\u0026#34;Invalid schema for function \u0026#39;b\u0026#39;: \u0026#39;float{}\u0026#39; is not valid under any of the given schemas\u0026#34;,) {} (\u0026#34;Invalid schema for function \u0026#39;b\u0026#39;: \u0026#39;{}\u0026#39; is not valid under any of the given schemas\u0026#34;,) [] (\u0026#34;Invalid schema for function \u0026#39;b\u0026#39;: \u0026#39;[]\u0026#39; is not valid under any of the given schemas\u0026#34;,) double (\u0026#34;Invalid schema for function \u0026#39;b\u0026#39;: \u0026#39;double\u0026#39; is not valid under any of the given schemas\u0026#34;,) bool (\u0026#34;Invalid schema for function \u0026#39;b\u0026#39;: \u0026#39;bool\u0026#39; is not valid under any of the given schemas\u0026#34;,) boolean Hi there! How can I assist you today? dictionary (\u0026#34;Invalid schema for function \u0026#39;b\u0026#39;: \u0026#39;dictionary\u0026#39; is not valid under any of the given schemas\u0026#34;,) array ([\u0026#34;Invalid schema for function \u0026#39;b\u0026#39;: In context=(\u0026#39;properties\u0026#39;, \u0026#39;d\u0026#39;), array schema missing items\u0026#34;],) array of string (\u0026#34;Invalid schema for function \u0026#39;b\u0026#39;: \u0026#39;array of string\u0026#39; is not valid under any of the given schemas\u0026#34;,) object Hello! How can I assist you today? オブジェクト (\u0026#34;Invalid schema for function \u0026#39;b\u0026#39;: \u0026#39;オブジェクト\u0026#39; is not valid under any of the given schemas\u0026#34;,) 文字列 (\u0026#34;Invalid schema for function \u0026#39;b\u0026#39;: \u0026#39;文字列\u0026#39; is not valid under any of the given schemas\u0026#34;,) 文字 (\u0026#34;Invalid schema for function \u0026#39;b\u0026#39;: \u0026#39;文字\u0026#39; is not valid under any of the given schemas\u0026#34;,) {\u0026#34;aaa\u0026#34;:\u0026#34;bbb\u0026#34;} (\u0026#39;Invalid schema for function \\\u0026#39;b\\\u0026#39;: \\\u0026#39;{\u0026#34;aaa\u0026#34;:\u0026#34;bbb\u0026#34;}\\\u0026#39; is not valid under any of the given schemas\u0026#39;,) [string] (\u0026#34;Invalid schema for function \u0026#39;b\u0026#39;: \u0026#39;[string]\u0026#39; is not valid under any of the given schemas\u0026#34;,) string,number,boolean (\u0026#34;Invalid schema for function \u0026#39;b\u0026#39;: \u0026#39;string,number,boolean\u0026#39; is not valid under any of the given schemas\u0026#34;,) ","date":"2023-06-18T10:55:44+09:00","permalink":"https://route250.github.io/p/function_call-api%E3%81%AE%E3%83%91%E3%83%A9%E3%83%A1%E3%83%BC%E3%82%BF%E3%82%BF%E3%82%A4%E3%83%97%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6%E8%AA%BF%E3%81%B9%E3%81%A6%E3%81%BF%E3%81%9F/","title":"function_call APIのパラメータタイプについて調べてみた"},{"content":"JavaScriptで変数を宣言する方法には、letとvarがあります。これらはどちらも変数を宣言するためのキーワードですが、実は大きな違いがあります。この記事では、letとvarの違いについて解説します。\nまず、varは関数スコープであるのに対して、letはブロックスコープです。関数スコープとは、変数を宣言した関数内で有効であるということです。一方、ブロックスコープとは、変数を宣言したブロック（中括弧で囲まれた範囲）内でのみ有効であるということです。つまり、letで宣言した変数は、そのブロック内でしか使えないということです。\n以下の例を見てみましょう。\nfunction example() { var x = 1; if (true) { var x = 2; console.log(x); // 2が出力される } console.log(x); // 2が出力される } この場合、xはvarを使って宣言されています。ifブロックの内側で再度宣言されているため、console.log(x)の出力は2となります。そして、ifブロックの外側でもconsole.log(x)が呼ばれているため、再度2が出力されます。\n一方、letを使った場合はどうでしょうか。\nfunction example() { let x = 1; if (true) { let x = 2; console.log(x); // 2が出力される } console.log(x); // 1が出力される } この場合、ifブロック内で宣言されたxは別の変数として扱われます。そのため、最初のconsole.log(x)の出力は2となりますが、ifブロックの外側のxは異なる変数であり、値は1となります。\n以上のように、letとvarの違いはスコープの扱い方にあります。可能であればletを使い、varを使う必要がある場合は注意深くスコープを考えて使うようにしましょう。\n","date":"2023-04-21T02:12:20+09:00","permalink":"https://route250.github.io/p/let-%E3%81%A8-var-%E3%81%AF%E4%BD%95%E3%81%8C%E9%81%95%E3%81%86/","title":"let と var は何が違う？"},{"content":"JavaScriptにおいて、比較演算子には== と === の2つがあります。これらの演算子は、異なる型の値を比較する際に異なる振る舞いをします。\n== 比較演算子は、比較する値の型が異なる場合に、自動的に型変換を行ってから値を比較します。例えば、1 == \u0026lsquo;1\u0026rsquo; のように数値と文字列を比較する場合、文字列の \u0026lsquo;1\u0026rsquo; を数値 1 に変換してから比較します。そのため、この場合 true が返されます。\nしかし、== 比較演算子は型変換によって予期せぬ挙動が生じる可能性があるため、使用には注意が必要です。例えば、0 == false のような比較では、false が数値 0 に変換されてから比較されるため、true が返されます。これは予期せぬ結果であるため、=== 比較演算子を使用することが推奨されます。\n=== 比較演算子は、比較する値の型が異なる場合でも、型変換を行いません。そのため、1 === \u0026lsquo;1\u0026rsquo; のような比較では、型が異なるために false が返されます。=== 比較演算子は、型が一致する場合に限り値を比較するため、より厳密な比較が可能です。\n一般的には、可能な限り === 比較演算子を使用することが推奨されます。ただし、== 比較演算子を使用する必要がある場合は、その挙動を理解してから使用するようにしましょう。\n","date":"2023-04-21T02:04:28+09:00","permalink":"https://route250.github.io/p/%E6%AF%94%E8%BC%83%E6%BC%94%E7%AE%97%E5%AD%90%E3%81%AE-%E3%81%A8-%E3%81%AF%E4%BD%95%E3%81%8C%E9%81%95%E3%81%86/","title":"比較演算子の == と === は何が違う？"},{"content":"lsコマンド 説明 lsコマンドは、Linux/Unixシステムでファイルやディレクトリのリストを表示するために使用されるコマンドです。 コマンドラインからの使い方の例 カレントディレクトリのファイル一覧を表示する\nls ディレクトリ名を指定してそのディレクトリ内のファイル一覧を表示する\nls ディレクトリ名 ファイルの日時、所有者、権限などを含めて詳細なリストを表示する\nls -l 非表示ファイルも含めてすべてのファイルを表示する\nls -a ファイル名の先頭が\u0026rsquo;.\u0026rsquo;(小数点)のファイルは、通常非表示となりますが、\u0026rsquo;-a\u0026rsquo;オプションを使用することで表示されます。\nサブディレクトリ内のファイルも含めて表示する\nls -R ファイルの更新日時が新しい順に表示する\nls -t ファイルサイズを人間が読みやすい形式で表示する\nls -h ファイルサイズが大きい順に表示する\nls -S スクリプトからの使い方の例 ディレクトリ内のファイル名でループするスクリプト例 for file in $(ls ディレクトリ名); do echo \u0026#34;ファイル名:$file\u0026#34; done 注意点として、スペースや特殊文字が含まれるファイル名には対応していないため、そのような場合はls -Qを使ってエスケープされたファイル名を表示する必要があります。また、バッククオート(`)ではなく、$( )を使用した方がよいとされています。 ","date":"2023-04-14T23:30:14+09:00","permalink":"https://route250.github.io/p/ls%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89%E3%81%AE%E4%BD%BF%E3%81%84%E6%96%B9/","title":"lsコマンドの使い方"},{"content":"日本語でかいてみるけども どれくらい？ できるかな\n","date":"2023-04-11T06:49:54+09:00","permalink":"https://route250.github.io/p/%E3%83%86%E3%82%B9%E3%83%88%E6%8A%95%E7%A8%BF/","title":"テスト投稿"}]