---
title: AzureChatにAPIで接続した話
tags:  #go #azure #python #jazug #azurechat #openaisdk  
author: [Ryuji Iwata](https://www.docswell.com/user/qt-luigi)
site: [Docswell](https://www.docswell.com/)
thumbnail: https://bcdn.docswell.com/page/3EK9QW4DED.jpg?width=480
description: 2026年3月23日(月)に東京とYouTubeでハイブリッド開催された「第59回 Tokyo Jazug Night」での私の登壇資料「AzureChatにAPIで接続した話」です。AzureChatにOpenAI SDKでアクセスする際のPythonとGoのコードを比較する形で紹介させて頂きました。
published: March 29, 26
canonical: https://www.docswell.com/s/qt-luigi/K1QJ6P-azurechat
---
# Page. 1

![Page Image](https://bcdn.docswell.com/page/3EK9QW4DED.jpg)

AzureChatにAPIで接続した話
第59回 Tokyo Jazug Night
23 March 2026
Ryuji Iwata
Okayama, Japan

# Page. 2

![Page Image](https://bcdn.docswell.com/page/L73WP1LP75.jpg)

Ryuji Iwata
X (Twitter) : @qt_luigi
Portfolio : sites.google.com/view/ryuji-iwata-portfolio
2

# Page. 3

![Page Image](https://bcdn.docswell.com/page/87DKLXN3JG.jpg)

AzureChat
Azure サブスクリプション内にプライベート チャット テナントをデプロイできるAzure
OpenAI Service搭載のオープンソースのソリューション アクセラレーター。
2023年7月に公開。最終コミット日：2025年3月26日
github.com/microsoft/azurechat
3

# Page. 4

![Page Image](https://bcdn.docswell.com/page/VJPK2P5PE8.jpg)

利用方法
通常は、Webブラウザーにてチャット画面を通じてやりとりするかと。
REST APIが用意されており、プログラミングでのアクセスも可能。
github.com/microsoft/azurechat/blob/main/docs/images/chat-history.png
4

# Page. 5

![Page Image](https://bcdn.docswell.com/page/2EVVD2PVEQ.jpg)

OpenAI SDK
• JavaScript / TypeScript : github.com/openai/openai-node
• Python : github.com/openai/openai-python
• .NET : github.com/openai/openai-dotnet
• Java : github.com/openai/openai-java
• Ruby : github.com/openai/openai-ruby
• Go : github.com/openai/openai-go
5

# Page. 6

![Page Image](https://bcdn.docswell.com/page/57GLYRP1EL.jpg)

ここからのコードについて
今回はPythonとGoのコードを併記しています。
説明用のコードのため
• エラー処理
• 値の定数化
などは行っておりません。
6

# Page. 7

![Page Image](https://bcdn.docswell.com/page/4EQYMV2NJP.jpg)

1. ライブラリーのインポート
Python
import os
from openai import AzureOpenAI
Go
package main
import (
&quot;context&quot;
&quot;fmt&quot;
&quot;os&quot;
openai &quot;github.com/openai/openai-go/v3&quot;
&quot;github.com/openai/openai-go/v3/azure&quot;
)
7

# Page. 8

![Page Image](https://bcdn.docswell.com/page/KJ4WVML371.jpg)

2. 環境変数からAPIキーを取得
Python
api_key = os.getenv(&#039;AZURE_OPENAI_KEY&#039;)
Go
apiKey := os.Getenv(&quot;AZURE_OPENAI_KEY&quot;)
※ Go : これ以降のコードはmain()関数内に記述されます。
8

# Page. 9

![Page Image](https://bcdn.docswell.com/page/LE1Y38LZ7G.jpg)

3. プロンプトの設定
Python
messages = [
{
&quot;role&quot;: &quot;system&quot;,
&quot;content&quot;: &quot;あなたは岡山県民です。 &quot;,
},
{
&quot;role&quot;: &quot;user&quot;,
&quot;content&quot;: &quot;岡山県の観光地を教えて。 &quot;,
},
]
Go
messages := []openai.ChatCompletionMessageParamUnion{
openai.SystemMessage(&quot;あなたは岡山県民です。 &quot;),
openai.UserMessage(&quot;岡山県の観光地を教えて。 &quot;),
}
9

# Page. 10

![Page Image](https://bcdn.docswell.com/page/GEWGPZV6J2.jpg)

4. クライアントの生成
Python
client = AzureOpenAI(
azure_endpoint=&quot;https://example.com&quot;,
api_version=&quot;YYYY-MM-DD&quot;,
api_key=api_key
)
Go
client := openai.NewClient(
azure.WithEndpoint(&quot;https://example.com&quot;, &quot;YYYY-MM-DD&quot;),
azure.WithAPIKey(apiKey),
)
10

# Page. 11

![Page Image](https://bcdn.docswell.com/page/47ZLK14RJ3.jpg)

5. 問い合わせの実行
Python
completion = client.chat.completions.create(
model=&quot;gpt-X.Y&quot;,
messages=messages,
)
Go
completion, err := client.Chat.Completions.New(context.TODO(), openai.ChatCompletionNewParams{
Model: openai.ChatModel(&quot;gpt-X.Y&quot;),
Messages: messages,
})
if err != nil {
os.Exit(1)
}
11

# Page. 12

![Page Image](https://bcdn.docswell.com/page/YJ6WQL31JV.jpg)

6. 結果を標準出力
Python
print(completion.choices[0].message.content)
Go
fmt.Println(completion.Choices[0].Message.Content)
12

# Page. 13

![Page Image](https://bcdn.docswell.com/page/GJ5MR1LLJ4.jpg)

このGo時世
最初、OpenAI SDKを使用したPythonのコードがありました。
Copilotにお願いしてGoのコードに書き換えてもらいました。
• 「このPythonのコードをGoに書き換えて」
この時に生成されたGoのコードはREST APIを生で叩くものでした。
次に、OpenAI SDKを使った書き方で修正をお願いしました。
• 「openai/openai-goを使ったコードに書き換えて」
生成されたコードを実行できるように手動で修正しました。
動作確認後、両者のコードをシンプルかつ併記の形に書き換えました。
生成AIにて、プログラミング言語も自然言語のように翻訳される時代に。
ですが、コードの読み書きはできることに越したことはないかと。
13

# Page. 14

![Page Image](https://bcdn.docswell.com/page/9E29D1L37R.jpg)

Thank you
Ryuji Iwata
Okayama, Japan
@qt_luigi

