ChatGPTの使用方法

World WideのLANSA技術フォーラムに投稿されたトピックと、日本のお客様からのお問い合わせへの回答の中から、皆様にお役に立つLANSAの開発テクニックやトラブルシュートに関する情報をご紹介します。

質問

LANSAでChatGPTは使えますか?

回答


下記は、ChatGPTで知られるOpenAIのChat APIを実行する、サーバーモジュールとサブルーチンのサンプルです。
#Callに質問を投げると#Responseに応答が返ってきます。
APIの実行はサーバーモジュールに限らず、RDMLXのプログラム(例:フォーム、再利用可能パーツ、ファンクション、WAM)から実行可能です。

APIキー

サンプルではAPIキーを外部リソースAPICFGから取得します。
サンプルの実行には、予め下記のJSONデータを持つ、外部リソースAPICFGを定義しておく必要があります。

{“key": “APIキー"}

サンプル

(1) サーバーモジュール

Begin_Com Role(*EXTENDS #PRIM_SRVM)

Define Field(#Call) Type(*NVARCHAR)
Define Field(#Response) Type(*NVARCHAR)

Define Field(#Header) Type(*NVARCHAR)
Define Field(#ErrorCode) Type(*NVARCHAR)
Define Field(#ErrorMessage) Type(*NVARCHAR)
Define Field(#Found) Type(*BOOLEAN)

Srvroutine Name(CallChat)
Field_Map For(*INPUT) Field(#Call) Parameter_Name(Call)
Field_Map For(*OUTPUT) Field(#Response) Parameter_Name(Response)

Field_Map For(*OUTPUT) Field(#ErrorCode) Parameter_Name(ErrorCode)
Field_Map For(*OUTPUT) Field(#ErrorMessage) Parameter_Name(ErrorMessage)

Define_Com Class(#XPRIM_HttpRequest) Name(#Request)
Define_Com Class(#XPRIM_JsonWriter) Name(#JsonWriter)
Define_Com Class(#XPRIM_RandomAccessJsonReader) Name(#JsonReader)
Define_Com Class(#XPRIM_ErrorInfo) Name(#ErrorInfo)

* Header
#JsonReader.SetSourceFile Path(#APICFG.FileName) Errorinfo(#ErrorInfo)
If Cond(#ErrorInfo.OK)
#Request.Options.AddHeader Name('Authorization’) Value('Bearer ' + #JsonReader.ReadStringWithPath( 'key’ ))
Endif

* Body
* {“model": “gpt-3.5-turbo", “messages": [{“role": “user", “content": “Hello!"}]}
#JsonWriter.SetOutputToHttpRequest Httprequest(#Request)
#JsonWriter.BeginObject
#JsonWriter.WriteString Name('model’) Value('gpt-3.5-turbo’)
#JsonWriter.BeginArray Name('messages’)
#JsonWriter.BeginObject
#JsonWriter.WriteString Name('role’) Value('user’)
#JsonWriter.WriteString Name('content’) Value(#Call)
#JsonWriter.EndObject
#JsonWriter.EndArray
#JsonWriter.EndObject

* Post
#request.DoPost Url('https://api.openai.com/v1/chat/completions’)
If (#request.Response.IsSuccessfulRequest)
If (#request.Response.IsSuccessHttpStatusCode)
* Response
#JsonReader.SetSourceHttpResponse Httpresponse(#Request.Response) Errorinfo(#ErrorInfo)
If Cond(#ErrorInfo.OK)
#JsonReader.BeginArrayWithName Name('choices’) Found(#Found)
Begin_Loop Using(#STD_NUM) To(#JsonReader.GetChildCount)
#JsonReader.BeginObjectAtIndex Index(#STD_NUM) Found(#Found)
If (#Found)
#JsonReader.BeginObjectWithName Name('message’) Found(#Found)
If Cond(#Found)
#Response := #JsonReader.ReadStringWithName( 'content’ )
#JsonReader.EndObject
Endif
#JsonReader.EndObject
Endif
End_Loop
#JsonReader.EndArray
Endif
Endif
Else
#ErrorCode := #Request.Response.ErrorCode
#ErrorMessage := #Request.Response.ErrorMessage
Endif
Endroutine

End_Com

(2) サブルーチン

Define Field(#Call) Type(*NVARCHAR)
Define Field(#Response) Type(*NVARCHAR)

Define Field(#Header) Type(*NVARCHAR)
Define Field(#ErrorCode) Type(*NVARCHAR)
Define Field(#ErrorMessage) Type(*NVARCHAR)
Define Field(#Found) Type(*BOOLEAN)

Define_Com Class(#XPRIM_HttpRequest) Name(#Request)
Define_Com Class(#XPRIM_JsonWriter) Name(#JsonWriter)
Define_Com Class(#XPRIM_RandomAccessJsonReader) Name(#JsonReader)
Define_Com Class(#XPRIM_ErrorInfo) Name(#ErrorInfo)

Subroutine Name(CallChat) Parms((#Call *RECEIVED) (#Response *RETURNED) (#ErrorCode *RETURNED) (#ErrorMessage *RETURNED))
* Header
#JsonReader.SetSourceFile Path(#APICFG.FileName) Errorinfo(#ErrorInfo)
If Cond(#ErrorInfo.OK)
#Request.Options.AddHeader Name('Authorization’) Value('Bearer ' + #JsonReader.ReadStringWithPath( 'key’ ))
Endif

* Body
* {“model": “gpt-3.5-turbo", “messages": [{“role": “user", “content": “Hello!"}]}
#JsonWriter.SetOutputToHttpRequest Httprequest(#Request)
#JsonWriter.BeginObject
#JsonWriter.WriteString Name('model’) Value('gpt-3.5-turbo’)
#JsonWriter.BeginArray Name('messages’)
#JsonWriter.BeginObject
#JsonWriter.WriteString Name('role’) Value('user’)
#JsonWriter.WriteString Name('content’) Value(#Call)
#JsonWriter.EndObject
#JsonWriter.EndArray
#JsonWriter.EndObject

* Post
#request.DoPost Url('https://api.openai.com/v1/chat/completions’)
If (#request.Response.IsSuccessfulRequest)
If (#request.Response.IsSuccessHttpStatusCode)
* Response
#JsonReader.SetSourceHttpResponse Httpresponse(#Request.Response) Errorinfo(#ErrorInfo)
If Cond(#ErrorInfo.OK)
#JsonReader.BeginArrayWithName Name('choices’) Found(#Found)
Begin_Loop Using(#STD_NUM) To(#JsonReader.GetChildCount)
#JsonReader.BeginObjectAtIndex Index(#STD_NUM) Found(#Found)
If (#Found)
#JsonReader.BeginObjectWithName Name('message’) Found(#Found)
If Cond(#Found)
#Response := #JsonReader.ReadStringWithName( 'content’ )
#JsonReader.EndObject
Endif
#JsonReader.EndObject
Endif
End_Loop
#JsonReader.EndArray
Endif
Endif
Else
#ErrorCode := #Request.Response.ErrorCode
#ErrorMessage := #Request.Response.ErrorMessage
Endif
Endroutine

関連サイト

OpenAI Chat API
https://platform.openai.com/docs/api-reference/chat

外部リソース
https://docs.lansa.com/15/ja/lansa015/index.htm#lansa/l4wtgu07_0255.htm

Webサービス
https://docs.lansa.com/15/ja/lansa018/index.htm

LANSA RDML 拡大ライブラリ
https://docs.lansa.com/15/ja/lansa015/index.htm#lansa/l4wtgu10_0095.htm

有償の「LANSAテクニカル・ホットライン・サービス」をご契約いただければ、データベースの検索(https://demo.lansa.jp/qadb/)」から、今回、ご紹介した以外の技術情報も探すことが出来ます。 「LANSAテクニカル・ホットライン・サービス」のご契約についてはお問い合わせフォーム(https://www.lansa.jp/inquiryforms/)よりお問い合わせください。