Slide 1

Slide 1 text

Function callingによるSlackbotの機能拡張 2023-09-01 ENECHANGE Tech Talk(社内勉強会) CTO室 岩本隆史

Slide 2

Slide 2 text

ChatGPT Botに画像生成機能を追加

Slide 3

Slide 3 text

まずcreate_image関数を実装 def create_image(prompt, size="256x256"): """Creates an image given a prompt""" openai.api_key = os.environ["FUNCTION_CALL_OPENAI_API_KEY"] openai.api_type = "open_ai" openai.api_base = "https://api.openai.com/v1" openai.api_version = None image = openai.Image.create(prompt=prompt, size=size) http = urllib3.PoolManager() r = http.request("GET", image["data"][0]["url"], preload_content=False) key = _generate_object_key("images", "png") _upload_to_s3(r, key, "image/png") r.release_conn() image_info = { "prompt": prompt, "size": size, "image_url": _build_url_for_object(key), } return json.dumps(image_info)

Slide 4

Slide 4 text

そしてfunctions変数を定義 functions = [ { "name": "create_image", "description": "Creates an image given a prompt", "parameters": { "type": "object", "properties": { "prompt": { "type": "string", "description": "A text description in English of the desired image", }, "size": { "type": "string", "description": "The size of the generated images", "enum": ["256x256", "512x512", "1024x1024"], }, }, "required": ["prompt"], }, }, ]

Slide 5

Slide 5 text

QRコード生成機能も追加

Slide 6

Slide 6 text

create_qrcode関数を実装 def create_qrcode(url): """Creates a QR code given a URL""" img = qrcode.make(url) key = _generate_object_key("images", "png") with tempfile.NamedTemporaryFile() as tf: img.save(tf.name) _upload_to_s3(tf.name, key, "image/png") qrcode_info = { "url": url, "qrcode_image_url": _build_url_for_object(key), } return json.dumps(qrcode_info)

Slide 7

Slide 7 text

functionsを更新 functions = [ { "name": "create_image", ... }, { "name": "create_qrcode", "description": "Creates a QR code given a URL", "parameters": { "type": "object", "properties": { "url": { "type": "string", "description": "The URL to encode in the QR code", }, }, "required": ["url"], }, }, ]

Slide 8

Slide 8 text

音声読み上げ機能も追加

Slide 9

Slide 9 text

synthesize_speech関数を実装 def synthesize_speech(text, gender="female"): """Synthesizes speech given text""" voice_id = {"female": "Tomoko", "male": "Takumi"}[gender] response = boto3.client("polly").synthesize_speech( VoiceId=voice_id, OutputFormat="mp3", Text=text, Engine="neural", ) key = _generate_object_key("audios", "mp3") _upload_to_s3(response["AudioStream"], key, "audio/mpeg") speech_info = { "text": text, "gender": gender, "voice_id": voice_id, "audio_url": _build_url_for_object(key), } return json.dumps(speech_info)

Slide 10

Slide 10 text

functionsを更新 functions = [ ... { "name": "synthesize_speech", "description": "Synthesizes speech given text", "parameters": { "type": "object", "properties": { "text": { "type": "string", "description": "The text to synthesize", }, "gender": { "type": "string", "description": "The gender of the voice", "enum": ["female", "male"], }, }, "required": ["text"], }, }, ]

Slide 11

Slide 11 text

簡単に拡張できる背景

Slide 12

Slide 12 text

Function callingが登場 (6/13) https://openai.com/blog/function-calling-and-other-api-updates

Slide 13

Slide 13 text

入力を関数呼び出しに変換 Convert queries such as “Email Anya to see if she wants to get coffee next Friday” to a function call like send_email(to: string, body: string) , or “What’s the weather like in Boston?” to get_current_weather(location: string, unit: 'celsius' | 'fahrenheit') . https://openai.com/blog/function-calling-and-other-api-updates

Slide 14

Slide 14 text

ChatGPT in SlackがFunction callingに対応 (8/21) # Experimental: You can try out the Function Calling feature (default: None) export OPENAI_FUNCTION_CALL_MODULE_NAME=tests.function_call_example https://github.com/seratch/ChatGPT-in-Slack#readme

Slide 15

Slide 15 text

誰でも拡張可能

Slide 16

Slide 16 text

enechange-configにPRを作成 例(QRコード生成分):https://github.com/enechange/enechange-config/pull/2254

Slide 17

Slide 17 text

関数とfunctions変数を実装 https://github.com/enechange/enechange-config/blob/develop/chatgpt-in- slack/environment/common/function_call.py

Slide 18

Slide 18 text

必要ならパッケージを追加 ベース:https://github.com/seratch/ChatGPT-in-Slack/blob/main/requirements.txt slack-bolt>=1.18.0,<2 openai>=0.27.9,<0.28 tiktoken>=0.4,<0.5 # https://github.com/Yelp/elastalert/issues/2306 urllib3<2 -- 追加分:https://github.com/enechange/enechange-config/blob/develop/chatgpt-in- slack/environment/common/requirements.txt(このファイルを編集) boto3 # Lambda関数も呼び出せる qrcode

Slide 19

Slide 19 text

ローカル環境構築は意外と簡単 ChatGPTとSlack上で会話するアプリを10分で構築しよう(前編:ローカル環境) https://qiita.com/rubita/items/f143f199e20b0310980a

Slide 20

Slide 20 text

余談:OSS貢献での学び

Slide 21

Slide 21 text

ChatGPT in SlackのFunction calling対応に貢献 @iwamot Thanks for the contribution! Your changes are now merged into the main branch: 626e492 https://github.com/seratch/ChatGPT-in-Slack/pull/52

Slide 22

Slide 22 text

issueで打診 To further enhance this software, I am proposing the addition of Function Calling support. (snip) May I proceed to create a PR for this proposed addition? https://github.com/seratch/ChatGPT-in-Slack/issues/50

Slide 23

Slide 23 text

変更を最小限にするのが重要 The addition of function calling is certainly something I've been looking forward to. (snip) When you submit a pull request, keeping the changes minimal would be very helpful. https://github.com/seratch/ChatGPT-in-Slack/issues/50

Slide 24

Slide 24 text

まとめ

Slide 25

Slide 25 text

機能拡張しよう ChatGPT Botの機能拡張PR、お待ちしてます! OSSの機能拡張も応援します!