1
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?

ChatGPTにAPIで阿部寛のホームページについて訊く

Last updated at Posted at 2025-03-17

Mar-18-2025 07-37-33.gif

インストール

pip install openai
pip install bs4
export OPENAI_API_KEY="your_api_key"

アシスタント実行クラスを作る

import openai
import os
import time
import requests
from bs4 import BeautifulSoup

class Assistant:
    def __init__(self, assistant_id=None, instructions=None):
        api_key = os.environ.get('OPENAI_API_KEY')
        if api_key is None:
            raise ValueError("環境変数 'OPENAI_API_KEY' が設定されていません。");

        self.client = openai.OpenAI(api_key=api_key)
        self.instructions = instructions
        self.assistant_id = assistant_id

        if not self.assistant_id:
            self.assistant_id = self.create_assistant()

    def fetch_and_summarize_webpage(self, url):
        response = requests.get(url)
        soup = BeautifulSoup(response.text, 'html.parser')
        text = soup.get_text(separator=" ")
        summary_prompt = f"以下のテキストを200文字以内に要約してください。:\n\n{text[:4000]}"
        completion = self.client.chat.completions.create(
            model="gpt-4o",
            messages=[{"role": "user", "content": summary_prompt}],
            temperature=0.5
        )
        return completion.choices[0].message.content.strip()

    def create_assistant(self):
        instructions = self.instructions
        assistant = self.client.beta.assistants.create(
            name=f"紹介bot",
            instructions=instructions,
            model="gpt-4o",
            temperature=0.7
        )
        return assistant.id

    def create_description(self, prompt, web_url=None, stream=False):
        thread = self.client.beta.threads.create()
        if web_url:
            webpage_summary = self.fetch_and_summarize_webpage(web_url)
            prompt += f"\n\n【参考Webページ要約】\n{webpage_summary}"
        print(webpage_summary)
        user_message = prompt

        self.client.beta.threads.messages.create(
            thread_id=thread.id,
            role="user",
            content=user_message
        )

        run = self.client.beta.threads.runs.create(
            thread_id=thread.id,
            assistant_id=self.assistant_id,
            stream=stream
        )

        if stream:
            return run, thread.id

        while True:
            run_status = self.client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
            if run_status.status == "completed":
                break
            elif run_status.status in ["failed", "cancelled", "expired"]:
                raise Exception(f"実行エラー: {run_status.status}")
            time.sleep(1)

        messages = self.client.beta.threads.messages.list(thread_id=thread.id)

        for message in messages.data:
            if message.role == "assistant":
                return message.content[0].text.value.strip()

実行

instructions = """
    あなたは芸能人の紹介文を作るプロです。対象芸能人の特徴を分かりやすく具体的に伝え、
    読者の関心を引く魅力的な紹介文を400文字程度で作成してください。
"""

bot = Assistant(instructions=instructions)

prompt = "この人について教えて"
web_url = "http://abehiroshi.la.coocan.jp/prof/prof.htm"

run, thread_id = bot.create_description(prompt,web_url=web_url, stream=True)
for event in run:
    if event.event == 'thread.message.delta':
        content = event.data.delta.content
        if content:
            print(content[0].text.value, end='', flush=True)

WEB情報をまとめたもの

1964年生まれの日本の俳優であり、多くの映画やテレビドラマに出演している。彼は国内外で数々の賞を受賞しており、特にアジア映画界での功績が認められている。代表作には「メンズノンノ」や「テルマエ・ロマエ」などがあり、幅広いジャンルで活躍している。彼の演技は高く評価され、観客からも支持を受けている。

応答

1964年生まれの日本の俳優であるこの人物は、多くの映画やテレビドラマでその存在感を発揮してきました。国内外で数々の賞を受賞し、特にアジア映画界での功績が高く評価されています。彼の代表作として知られる「メンズノンノ」や「テルマエ・ロマエ」は、彼の多才な演技力を示す絶好の例です。これらの作品で披露する彼の演技は、観客の心を掴んで離さず、批評家からも高い評価を受けています。

幅広いジャンルで活躍する彼は、シリアスなドラマからコメディまで、さまざまな役柄を自在に演じ分けることができ、その多面性が魅力の一つです。彼の演技には、キャラクターの内面を深く掘り下げ、視聴者に感情移入させる力があります。このような演技スタイルは、彼が出演する作品において、物語に深みを与える重要な要素となっています。

また、彼の演技は単に技術的な面だけでなく、自然体の魅力を放つことでも知られています。これにより、彼のキャラクターは常に観客に親しみやすく、共感を呼び起こします。彼は日本国内のみならず、国際的にもその名を知られ、今後も多くの作品で新たな魅力を発揮していくことでしょう

1
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
1
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?