Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Function callingによるSlackbotの機能拡張

iwamot
September 01, 2023

Function callingによるSlackbotの機能拡張

2023-09-01
ENECHANGE Tech Talk(社内勉強会)

iwamot

September 01, 2023
Tweet

More Decks by iwamot

Other Decks in Technology

Transcript

  1. まず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)
  2. そして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"], }, }, ]
  3. 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)
  4. 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"], }, }, ]
  5. 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)
  6. 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"], }, }, ]
  7. 入力を関数呼び出しに変換 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
  8. 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
  9. 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
  10. 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
  11. 変更を最小限にするのが重要 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