LoginSignup
0
0

Assistants API v2の新機能紹介とコードサンプル

Posted at

背景

  • OpenAiからAssistantsAPIのVersion2の公開が発表されました
  • Version2で新しくなった点と、簡単な使い方を書いていきます
  • assistantにVector Storeを紐づける方法と、threadに紐づける方法の2通りあります
    • assistantに紐づける場合は、Vector Storeを作成し紐付けます
    • threadに紐づける場合は、Fileをuploadすると自動でVector Storeが作成され、紐付けが行われます
  • 今回はthreadに紐づけるサンプルコードを書きます

Version2で変わった点

What's new in v2?にまとまっていますが

  • file_search(旧 retrieval)
    • 10,000files/assistant
    • 並列クエリOK
  • vector_store
    • 自動でparse, chuck, embeddingされる
      • token制御、response_formatなど
  • response_formatが指定できるように
    • json指定が可能に

File Search(旧 retrieval)詳細

特徴

  • クエリを検索に最適化する
  • 複雑なクエリを複数に分割し並列で実行する
  • キーワード検索とセマンティック検索を両方する
  • 最終レスポンスの生成前に、検索結果を再度ランキングする

今後良くなっていくもの(現在は制限中)

  • chunkやembeddingやその他の検索系の設定
  • 検索前のフィルタ
  • 画像のパース
  • 構造データの検索
  • 要約機能

Vector Store

  • threadのvector storeにおいては、最大1分までfileのprocessを待ってくれる

実装

  • threadにfileを紐づける方法です
  • 自動的にvector storeが作成され、threadに紐付きgroundingされます
client = OpenAI()

# assistantの作成
assistant = client.beta.assistants.create(
  name="CTR Analyst",
  instructions="You are an expert ad CTR analyst. Use you knowledge base to answer questions about audited digital advetise statements.",
  model="gpt-4-turbo",
  tools=[{"type": "file_search"}],
)

# fileをupload
file_ids = []
for file in YOUR_FILES:
    file = client.files.create(file=file.name, purpose="assistants")
    file_ids.append(file.id)

# attachmentsの作成
attachements = []
for file_id in file_ids:
    attachements.append({
        "file_id": file_id,
        "tools": [{"type": "file_search"}]
    })

# このタイミングでvector storeも作成され、threadに紐づく
thread = client.beta.threads.create(
    messages=[{
        "role": "user",
        "content": "attachmentsのファイルは[related files]です",
        "attachments": attachements
    }]
)

# 実行
run = client.beta.threads.runs.create_and_poll(
    thread_id=thread_id,
    assistant_id=assistant.id,
    instructions="create CTR report from [related files]"
)

if run.status == 'completed': 
  messages = client.beta.threads.messages.list(
    thread_id=thread.id
  )
  print(messages)
else:
  print(run.status)

参照

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