はじめに

本記事では、OuterportのPython SDKを使用して、OuterportのAPIを呼び出す方法を説明します。

インストール

OuterportのPython SDKはPyPI(Python Package Index)からインストールできます。

pip install outerport

クライアントの初期化

OuterportのPython SDKを使用するにaは、まずOuterportのAPIキーを取得する必要があります。

現時点ではOuterport APIはまだInvite Onlyとなっています。APIキーをリクエストするにはこちらからお問い合わせください。

APIキーを取得したら、次のようにクライアントを初期化します。

from outerport import OuterportClient

client = OuterportClient(api_key="your_api_key")

自己環境でOuterport APIを使用する場合

自己環境でホスティングされたOuterport APIを使用する場合は、base_urlをホスティングされたAPIのURLに設定します。

client = OuterportClient(
    api_key="your_api_key",
    base_url="http://localhost:8080"
)

基本的な使い方

ドキュメントのアップロード

OuterportのPython SDKを使用して、ドキュメントをアップロードする方法を説明します。

with open("document.pdf", "rb") as f:
    doc = client.documents.create(file=f)

client.documents.createメソッドは、Documentオブジェクトを返します。

Outerport Python SDKは、基本的に「リソース」を作ると、オブジェクトを返します。リターンされるオブジェクトはクライアントと繋がっているので、reload()メソッドを呼び出すことで最新のデータを取得したり、delete()メソッドを呼び出すことで削除することができます。

ドキュメントに対して質問をする

OuterportのPython SDKを使用して、ドキュメントに対して質問をする方法を説明します。

question = client.questions.create(
    document_ids=[doc.id],
    question="弊社のパスワードポリシーについて教えてください"
)

client.questions.createメソッドがリターンされると、質問の答えがquestion.final_answerに格納されます。

print(question.final_answer)

question.final_answerの他にも、質問を答える為の「プラニング」と「リサーチ」の過程がquestion.planquestion.evidencesに格納されます。例えば、question.evidencesは以下のような結果が得られます。

print(question.evidences)

ドキュメントの検索

関連性の高そうなドキュメントをクエリから検索する事も可能です。

documents = client.documents.search(query="Explain the password policy")

client.documents.searchメソッドは、Documentオブジェクトのリストを返します。

タグやフォルダーを指定して検索する事も可能です。

documents = client.documents.search(
    query="Explain the password policy", 
    tag="security"
)