0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Azure OpenAIとPythonでレジュメ要約AIを作ってみた

Posted at

はじめに

最近、ChatGPTや生成AIが話題ですが、Azure OpenAIを使えば企業向けにも安全に大規模言語モデル(LLM)を活用できます。本記事では、Azure OpenAIのAPIをPythonから使って、履歴書(レジュメ)を100語以内で要約する簡単なアプリケーションを作成してみます。


使う技術

  • Azure OpenAI Service(GPT-3.5 Turbo)
  • Azure AD 認証
  • Python
  • Pandas

ステップ 1: 環境構築

pip install openai azure-identity pandas

Azure Portalで下記を漏れなく取得:

  • Azure OpenAI Endpoint
  • Deployment Name
  • Tenant ID
  • Client ID / Secret

※Qiitaの記事上ではこれらの情報を公開しないよう注意しましょう。os.environ.get() などで環境変数から読み込むのがポイントです。


ステップ 2: Azure OpenAIの設定

from azure.identity import ClientSecretCredential, get_bearer_token_provider
from openai import AzureOpenAI
import os

credential = ClientSecretCredential(
    tenant_id=os.environ.get('AZURE_TENANT_ID'),
    client_id=os.environ.get('AZURE_CLIENT_ID'),
    client_secret=os.environ.get('AZURE_CLIENT_SECRET')
)

scopes = "https://cognitiveservices.azure.com/.default"
azure_ad_token_provider = get_bearer_token_provider(credential, scopes)

client = AzureOpenAI(
    azure_endpoint=os.environ.get('AZURE_OPENAI_ENDPOINT'),
    api_version="2024-02-15-preview",
    azure_ad_token_provider=azure_ad_token_provider
)

ステップ 3: CSVからレジュメデータ読み込み

import pandas as pd

url = "https://raw.githubusercontent.com/CloudPak-Outcomes/Outcomes-Projects/main/watsonx-governance-l4/data/resume_summarization_test_data.csv"
df = pd.read_csv(url)
df.head()

ステップ 4: GPTで要約作成

PROMPT_TEMPLATE = """
You will be given a resume. Please summarize the resume in 100 words or less.

--- start of text ---
{text}
--- end of text ---
""".strip()

summaries = []
for index, row in df.iterrows():
    resume = row[0]  # 実際のレジュメ文章
    response = client.chat.completions.create(
        model=os.environ.get('AZURE_OPENAI_DEPLOYMENT_NAME'),
        messages=[{"role": "user", "content": PROMPT_TEMPLATE.format(text=resume)}],
        max_tokens=200
    )
    summary = response.choices[0].message.content
    print(f"{index}: {summary}")
    summaries.append(summary)

df['generated_text'] = summaries

ステップ 5: 結果を表示

df[['generated_text']].head()

必要に応じてCSV出力も可能です:

df.to_csv("resume_with_summary.csv", index=False)

おわりに

Azure OpenAIを利用すれば、安全に企業用LLMを活用して、レジュメのような自然言語データを短文で要約するサービスが作れます。

今後は一次分析や、職種ポジションとのマッチングへの応用も検討できますね。

ご覧いただきありがとうございました!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?